Skip to content

Commit

Permalink
Remove Alethio (MetaMask#351)
Browse files Browse the repository at this point in the history
* Remove Alethio

* Get tests passing

* Add util tests

* Add some more ignores to get 100

* Update mock to use _not_ alethio

* update tests

* Fix mocks

* Fix lint nit

* Remove expect

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
  • Loading branch information
rickycodes and Gudahtt committed Feb 18, 2021
1 parent af700d8 commit fae916a
Show file tree
Hide file tree
Showing 4 changed files with 370 additions and 259 deletions.
58 changes: 12 additions & 46 deletions src/transaction/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export interface Result {
* @type Fetch All Options
*
* @property fromBlock - String containing a specific block decimal number
* @property alethioApiKey - API key to be used to fetch token transactions
* @property etherscanApiKey - API key to be used to fetch token transactions
*/
export interface FetchAllOptions {
fromBlock?: string;
alethioApiKey?: string;
etherscanApiKey?: string;
}

/**
Expand Down Expand Up @@ -278,43 +278,6 @@ export class TransactionController extends BaseController<TransactionConfig, Tra
};
}

/**
* Normalizes the transaction information from alethio
* to be compatible with the TransactionMeta interface
*
* @param txMeta - Object containing the transaction information
* @param currentNetworkID - string representing the current network id
* @returns - TransactionMeta
*/
normalizeTxFromAlehio = (txMeta: AlethioTransactionMeta, currentNetworkID: string): TransactionMeta => {
const {
attributes: { symbol, blockCreationTime, decimals, transactionGasLimit, transactionGasPrice, value },
relationships: { to, from, transaction, token },
} = txMeta;
const time = parseInt(blockCreationTime, 10) * 1000;
return {
id: random({ msecs: time }),
isTransfer: true,
networkID: currentNetworkID,
status: 'confirmed',
time: parseInt(blockCreationTime, 10) * 1000,
transaction: {
chainId: 1,
from: from.data.id,
gas: transactionGasLimit,
gasPrice: transactionGasPrice,
to: to.data.id,
value,
},
transactionHash: transaction.data.id,
transferInformation: {
contractAddress: token.data.id,
decimals,
symbol,
},
};
};

/**
* EventEmitter instance used to listen to specific transactional events
*/
Expand Down Expand Up @@ -749,23 +712,26 @@ export class TransactionController extends BaseController<TransactionConfig, Tra
return;
}

const [etherscanResponse, alethioResponse] = await handleTransactionFetch(networkType, address, opt);
const [etherscanTxResponse, etherscanTokenResponse] = await handleTransactionFetch(networkType, address, opt);
const remoteTxList: { [key: string]: number } = {};
const remoteTxs: TransactionMeta[] = [];

etherscanResponse.result.forEach((tx: EtherscanTransactionMeta) => {
etherscanTxResponse.result.forEach((tx: EtherscanTransactionMeta) => {
/* istanbul ignore next */
if (!remoteTxList[tx.hash]) {
remoteTxs.push(this.normalizeTxFromEtherscan(tx, currentNetworkID));
const cleanTx = this.normalizeTxFromEtherscan(tx, currentNetworkID);
remoteTxs.push(cleanTx);
remoteTxList[tx.hash] = 1;
}
});

alethioResponse.data.forEach((tx: AlethioTransactionMeta) => {
const cleanTx = this.normalizeTxFromAlehio(tx, currentNetworkID);
remoteTxs.push(cleanTx);
etherscanTokenResponse.result.forEach((tx: EtherscanTransactionMeta) => {
/* istanbul ignore next */
remoteTxList[cleanTx.transactionHash || ''] = 1;
if (!remoteTxList[tx.hash]) {
const cleanTx = this.normalizeTxFromEtherscan(tx, currentNetworkID);
remoteTxs.push(cleanTx);
remoteTxList[cleanTx.transactionHash || ''] = 1;
}
});

const localTxs = this.state.transactions.filter(
Expand Down
89 changes: 45 additions & 44 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,74 +79,75 @@ export function getBuyURL(networkCode = '1', address?: string, amount = 5) {
* @param fromBlock? - Block from which transactions are needed
* @returns - URL to fetch the transactions from
*/
export function getEtherscanApiUrl(networkType: string, address: string, fromBlock?: string): string {
export function getEtherscanApiUrl(
networkType: string,
address: string,
action: string,
fromBlock?: string,
etherscanApiKey?: string,
): string {
let etherscanSubdomain = 'api';
/* istanbul ignore next */
if (networkType !== 'mainnet') {
etherscanSubdomain = `api-${networkType}`;
}
const apiUrl = `https://${etherscanSubdomain}.etherscan.io`;
let url = `${apiUrl}/api?module=account&action=txlist&address=${address}&tag=latest&page=1`;
let url = `${apiUrl}/api?module=account&action=${action}&address=${address}&tag=latest&page=1`;
if (fromBlock) {
url += `&startBlock=${fromBlock}`;
}
return url;
}

/**
* Return a URL that can be used to fetch ERC20 token transactions
*
* @param networkType - Network type of desired network
* @param address - Address to get the transactions from
* @param opt? - Object that can contain fromBlock and Alethio service API key
* @returns - URL to fetch the transactions from
*/
export function getAlethioApiUrl(networkType: string, address: string, opt?: FetchAllOptions) {
if (networkType !== 'mainnet') {
return { url: '', headers: {} };
}
let url = `https://api.aleth.io/v1/token-transfers?filter[to]=${address}`;
// From alethio implementation
// cursor = hardcoded prefix `0x00` + fromBlock in hex format + max possible tx index `ffff`
let fromBlock = opt && opt.fromBlock;
if (fromBlock) {
fromBlock = parseInt(fromBlock).toString(16);
let prev = `0x00${fromBlock}ffff`;
while (prev.length < 34) {
prev += '0';
}
url += `&page[prev]=${prev}`;
if (etherscanApiKey) {
url += `&apikey=${etherscanApiKey}`;
}
/* istanbul ignore next */
const headers = opt && opt.alethioApiKey ? { Authorization: `Bearer ${opt.alethioApiKey}` } : undefined;
return { url, headers };
return url;
}

/**
* Handles the fetch of incoming transactions
*
* @param networkType - Network type of desired network
* @param address - Address to get the transactions from
* @param opt? - Object that can contain fromBlock and Alethio service API key
* @param opt? - Object that can contain fromBlock and Etherscan service API key
* @returns - Responses for both ETH and ERC20 token transactions
*/
export async function handleTransactionFetch(
networkType: string,
address: string,
opt?: FetchAllOptions,
): Promise<[{ [result: string]: [] }, { [data: string]: [] }]> {
const url = getEtherscanApiUrl(networkType, address, opt && opt.fromBlock);
const etherscanResponsePromise = handleFetch(url);
const alethioUrl = getAlethioApiUrl(networkType, address, opt);
const alethioResponsePromise = alethioUrl.url !== '' && handleFetch(alethioUrl.url, { headers: alethioUrl.headers });
let [etherscanResponse, alethioResponse] = await Promise.all([etherscanResponsePromise, alethioResponsePromise]);
if (etherscanResponse.status === '0' || etherscanResponse.result.length <= 0) {
etherscanResponse = { result: [] };
): Promise<[{ [result: string]: [] }, { [result: string]: [] }]> {
// transactions
const etherscanTxUrl = getEtherscanApiUrl(
networkType,
address,
'txlist',
opt && opt.fromBlock,
opt && opt.etherscanApiKey,
);
const etherscanTxResponsePromise = handleFetch(etherscanTxUrl);

// tokens
const etherscanTokenUrl = getEtherscanApiUrl(
networkType,
address,
'tokentx',
opt && opt.fromBlock,
opt && opt.etherscanApiKey,
);
const etherscanTokenResponsePromise = handleFetch(etherscanTokenUrl);

let [etherscanTxResponse, etherscanTokenResponse] = await Promise.all([
etherscanTxResponsePromise,
etherscanTokenResponsePromise,
]);

if (etherscanTxResponse.status === '0' || etherscanTxResponse.result.length <= 0) {
etherscanTxResponse = { result: [] };
}
if (!alethioUrl.url || !alethioResponse || !alethioResponse.data) {
alethioResponse = { data: [] };

if (etherscanTokenResponse.status === '0' || etherscanTokenResponse.result.length <= 0) {
etherscanTokenResponse = { result: [] };
}
return [etherscanResponse, alethioResponse];

return [etherscanTxResponse, etherscanTokenResponse];
}

/**
Expand Down
Loading

0 comments on commit fae916a

Please sign in to comment.