2021-02-04 19:15:23 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
|
|
|
import log from 'loglevel';
|
|
|
|
import BN from 'bn.js';
|
2021-03-18 19:23:46 +01:00
|
|
|
import createId from '../../../shared/modules/random-id';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { bnToHex } from '../lib/util';
|
|
|
|
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2020-11-07 08:38:12 +01:00
|
|
|
import {
|
2021-03-10 21:16:44 +01:00
|
|
|
TRANSACTION_TYPES,
|
2020-11-07 08:38:12 +01:00
|
|
|
TRANSACTION_STATUSES,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../shared/constants/transaction';
|
2020-01-09 04:34:58 +01:00
|
|
|
import {
|
2020-10-31 01:58:12 +01:00
|
|
|
CHAIN_ID_TO_NETWORK_ID_MAP,
|
|
|
|
CHAIN_ID_TO_TYPE_MAP,
|
|
|
|
GOERLI_CHAIN_ID,
|
|
|
|
KOVAN_CHAIN_ID,
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
RINKEBY_CHAIN_ID,
|
|
|
|
ROPSTEN_CHAIN_ID,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../shared/constants/network';
|
2021-06-10 21:27:03 +02:00
|
|
|
import { SECOND } from '../../../shared/constants/time';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2021-06-10 21:27:03 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout(SECOND * 30);
|
2019-08-16 20:54:10 +02:00
|
|
|
|
2021-03-19 22:54:30 +01:00
|
|
|
/**
|
|
|
|
* @typedef {import('../../../shared/constants/transaction').TransactionMeta} TransactionMeta
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A transaction object in the format returned by the Etherscan API.
|
|
|
|
*
|
|
|
|
* Note that this is not an exhaustive type definiton; only the properties we use are defined
|
|
|
|
*
|
|
|
|
* @typedef {Object} EtherscanTransaction
|
|
|
|
* @property {string} blockNumber - The number of the block this transaction was found in, in decimal
|
|
|
|
* @property {string} from - The hex-prefixed address of the sender
|
2021-06-24 21:52:14 +02:00
|
|
|
* @property {string} gas - The gas limit, in decimal GWEI
|
|
|
|
* @property {string} [gasPrice] - The gas price, in decimal WEI
|
|
|
|
* @property {string} [maxFeePerGas] - The maximum fee per gas, inclusive of tip, in decimal WEI
|
|
|
|
* @property {string} [maxPriorityFeePerGas] - The maximum tip per gas in decimal WEI
|
2021-03-19 22:54:30 +01:00
|
|
|
* @property {string} hash - The hex-prefixed transaction hash
|
|
|
|
* @property {string} isError - Whether the transaction was confirmed or failed (0 for confirmed, 1 for failed)
|
|
|
|
* @property {string} nonce - The transaction nonce, in decimal
|
|
|
|
* @property {string} timeStamp - The timestamp for the transaction, in seconds
|
|
|
|
* @property {string} to - The hex-prefixed address of the recipient
|
|
|
|
* @property {string} value - The amount of ETH sent in this transaction, in decimal WEI
|
|
|
|
*/
|
|
|
|
|
2020-10-30 15:03:35 +01:00
|
|
|
/**
|
|
|
|
* This controller is responsible for retrieving incoming transactions. Etherscan is polled once every block to check
|
|
|
|
* for new incoming transactions for the current selected account on the current network
|
|
|
|
*
|
|
|
|
* Note that only the built-in Infura networks are supported (i.e. anything in `INFURA_PROVIDER_TYPES`). We will not
|
|
|
|
* attempt to retrieve incoming transactions on any custom RPC endpoints.
|
|
|
|
*/
|
2020-10-31 01:58:12 +01:00
|
|
|
const etherscanSupportedNetworks = [
|
|
|
|
GOERLI_CHAIN_ID,
|
|
|
|
KOVAN_CHAIN_ID,
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
RINKEBY_CHAIN_ID,
|
|
|
|
ROPSTEN_CHAIN_ID,
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-10-31 01:58:12 +01:00
|
|
|
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class IncomingTransactionsController {
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(opts = {}) {
|
2021-03-19 22:54:30 +01:00
|
|
|
const {
|
|
|
|
blockTracker,
|
|
|
|
onNetworkDidChange,
|
|
|
|
getCurrentChainId,
|
|
|
|
preferencesController,
|
|
|
|
} = opts;
|
2021-02-04 19:15:23 +01:00
|
|
|
this.blockTracker = blockTracker;
|
2021-03-19 22:54:30 +01:00
|
|
|
this.getCurrentChainId = getCurrentChainId;
|
2021-02-04 19:15:23 +01:00
|
|
|
this.preferencesController = preferencesController;
|
2019-08-16 20:54:10 +02:00
|
|
|
|
2019-08-20 20:52:00 +02:00
|
|
|
this._onLatestBlock = async (newBlockNumberHex) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const selectedAddress = this.preferencesController.getSelectedAddress();
|
|
|
|
const newBlockNumberDec = parseInt(newBlockNumberHex, 16);
|
2021-03-19 22:54:30 +01:00
|
|
|
await this._update(selectedAddress, newBlockNumberDec);
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-08-20 20:52:00 +02:00
|
|
|
|
2020-08-19 18:27:05 +02:00
|
|
|
const initState = {
|
2019-08-16 20:54:10 +02:00
|
|
|
incomingTransactions: {},
|
2021-03-19 22:54:30 +01:00
|
|
|
incomingTxLastFetchedBlockByChainId: {
|
|
|
|
[GOERLI_CHAIN_ID]: null,
|
|
|
|
[KOVAN_CHAIN_ID]: null,
|
|
|
|
[MAINNET_CHAIN_ID]: null,
|
|
|
|
[RINKEBY_CHAIN_ID]: null,
|
|
|
|
[ROPSTEN_CHAIN_ID]: null,
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
...opts.initState,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
this.store = new ObservableStore(initState);
|
2019-08-16 20:54:10 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
this.preferencesController.store.subscribe(
|
2021-03-19 22:54:30 +01:00
|
|
|
previousValueComparator((prevState, currState) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
featureFlags: {
|
|
|
|
showIncomingTransactions: prevShowIncomingTransactions,
|
|
|
|
} = {},
|
2021-02-04 19:15:23 +01:00
|
|
|
} = prevState;
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
featureFlags: {
|
|
|
|
showIncomingTransactions: currShowIncomingTransactions,
|
|
|
|
} = {},
|
2021-02-04 19:15:23 +01:00
|
|
|
} = currState;
|
2020-11-03 00:41:28 +01:00
|
|
|
|
|
|
|
if (currShowIncomingTransactions === prevShowIncomingTransactions) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2019-08-21 20:42:14 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (prevShowIncomingTransactions && !currShowIncomingTransactions) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.stop();
|
|
|
|
return;
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2019-08-21 20:42:14 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.start();
|
2021-03-19 22:54:30 +01:00
|
|
|
}, this.preferencesController.store.getState()),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-08-21 20:42:14 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
this.preferencesController.store.subscribe(
|
2021-03-19 22:54:30 +01:00
|
|
|
previousValueComparator(async (prevState, currState) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { selectedAddress: prevSelectedAddress } = prevState;
|
|
|
|
const { selectedAddress: currSelectedAddress } = currState;
|
2019-08-21 20:42:14 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (currSelectedAddress === prevSelectedAddress) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2021-03-19 22:54:30 +01:00
|
|
|
await this._update(currSelectedAddress);
|
|
|
|
}, this.preferencesController.store.getState()),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-08-21 20:42:14 +02:00
|
|
|
|
2021-03-19 22:54:30 +01:00
|
|
|
onNetworkDidChange(async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const address = this.preferencesController.getSelectedAddress();
|
2021-03-19 22:54:30 +01:00
|
|
|
await this._update(address);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-08-16 20:54:10 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
start() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { featureFlags = {} } = this.preferencesController.store.getState();
|
|
|
|
const { showIncomingTransactions } = featureFlags;
|
2019-08-21 20:42:14 +02:00
|
|
|
|
|
|
|
if (!showIncomingTransactions) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-08-21 20:42:14 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.blockTracker.removeListener('latest', this._onLatestBlock);
|
|
|
|
this.blockTracker.addListener('latest', this._onLatestBlock);
|
2019-08-20 20:52:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
stop() {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.blockTracker.removeListener('latest', this._onLatestBlock);
|
2019-08-20 20:52:00 +02:00
|
|
|
}
|
|
|
|
|
2021-03-19 22:54:30 +01:00
|
|
|
/**
|
|
|
|
* Determines the correct block number to begin looking for new transactions
|
|
|
|
* from, fetches the transactions and then saves them and the next block
|
|
|
|
* number to begin fetching from in state. Block numbers and transactions are
|
|
|
|
* stored per chainId.
|
|
|
|
* @private
|
|
|
|
* @param {string} address - address to lookup transactions for
|
|
|
|
* @param {number} [newBlockNumberDec] - block number to begin fetching from
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
async _update(address, newBlockNumberDec) {
|
|
|
|
const chainId = this.getCurrentChainId();
|
|
|
|
if (!etherscanSupportedNetworks.includes(chainId) || !address) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2020-10-31 01:58:12 +01:00
|
|
|
}
|
2019-08-16 20:54:10 +02:00
|
|
|
try {
|
2021-03-19 22:54:30 +01:00
|
|
|
const currentState = this.store.getState();
|
|
|
|
const currentBlock = parseInt(this.blockTracker.getCurrentBlock(), 16);
|
|
|
|
|
|
|
|
const mostRecentlyFetchedBlock =
|
|
|
|
currentState.incomingTxLastFetchedBlockByChainId[chainId];
|
|
|
|
const blockToFetchFrom =
|
|
|
|
mostRecentlyFetchedBlock ?? newBlockNumberDec ?? currentBlock;
|
|
|
|
|
|
|
|
const newIncomingTxs = await this._getNewIncomingTransactions(
|
2020-11-03 00:41:28 +01:00
|
|
|
address,
|
2021-03-19 22:54:30 +01:00
|
|
|
blockToFetchFrom,
|
2020-11-03 00:41:28 +01:00
|
|
|
chainId,
|
2021-03-19 22:54:30 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let newMostRecentlyFetchedBlock = blockToFetchFrom;
|
|
|
|
|
|
|
|
newIncomingTxs.forEach((tx) => {
|
|
|
|
if (
|
|
|
|
tx.blockNumber &&
|
|
|
|
parseInt(newMostRecentlyFetchedBlock, 10) <
|
|
|
|
parseInt(tx.blockNumber, 10)
|
|
|
|
) {
|
|
|
|
newMostRecentlyFetchedBlock = parseInt(tx.blockNumber, 10);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.store.updateState({
|
|
|
|
incomingTxLastFetchedBlockByChainId: {
|
|
|
|
...currentState.incomingTxLastFetchedBlockByChainId,
|
|
|
|
[chainId]: newMostRecentlyFetchedBlock + 1,
|
|
|
|
},
|
|
|
|
incomingTransactions: newIncomingTxs.reduce(
|
|
|
|
(transactions, tx) => {
|
|
|
|
transactions[tx.hash] = tx;
|
|
|
|
return transactions;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...currentState.incomingTransactions,
|
|
|
|
},
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-08-16 20:54:10 +02:00
|
|
|
} catch (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error(err);
|
2019-08-16 20:54:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 22:54:30 +01:00
|
|
|
/**
|
|
|
|
* fetches transactions for the given address and chain, via etherscan, then
|
|
|
|
* processes the data into the necessary shape for usage in this controller.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} [address] - Address to fetch transactions for
|
|
|
|
* @param {number} [fromBlock] - Block to look for transactions at
|
|
|
|
* @param {string} [chainId] - The chainId for the current network
|
|
|
|
* @returns {TransactionMeta[]}
|
|
|
|
*/
|
|
|
|
async _getNewIncomingTransactions(address, fromBlock, chainId) {
|
2020-11-03 00:41:28 +01:00
|
|
|
const etherscanSubdomain =
|
|
|
|
chainId === MAINNET_CHAIN_ID
|
|
|
|
? 'api'
|
2021-02-04 19:15:23 +01:00
|
|
|
: `api-${CHAIN_ID_TO_TYPE_MAP[chainId]}`;
|
2019-08-16 20:54:10 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const apiUrl = `https://${etherscanSubdomain}.etherscan.io`;
|
|
|
|
let url = `${apiUrl}/api?module=account&action=txlist&address=${address}&tag=latest&page=1`;
|
2019-08-16 20:54:10 +02:00
|
|
|
|
|
|
|
if (fromBlock) {
|
2021-02-04 19:15:23 +01:00
|
|
|
url += `&startBlock=${parseInt(fromBlock, 10)}`;
|
2019-08-16 20:54:10 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const response = await fetchWithTimeout(url);
|
2021-03-19 22:54:30 +01:00
|
|
|
const { status, result } = await response.json();
|
|
|
|
let newIncomingTxs = [];
|
2019-11-15 18:17:42 +01:00
|
|
|
if (status === '1' && Array.isArray(result) && result.length > 0) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const remoteTxList = {};
|
|
|
|
const remoteTxs = [];
|
2019-08-16 20:54:10 +02:00
|
|
|
result.forEach((tx) => {
|
|
|
|
if (!remoteTxList[tx.hash]) {
|
2021-02-04 19:15:23 +01:00
|
|
|
remoteTxs.push(this._normalizeTxFromEtherscan(tx, chainId));
|
|
|
|
remoteTxList[tx.hash] = 1;
|
2019-08-16 20:54:10 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-08-16 20:54:10 +02:00
|
|
|
|
2021-03-19 22:54:30 +01:00
|
|
|
newIncomingTxs = remoteTxs.filter(
|
2020-12-14 18:03:59 +01:00
|
|
|
(tx) => tx.txParams?.to?.toLowerCase() === address.toLowerCase(),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-03-19 22:54:30 +01:00
|
|
|
newIncomingTxs.sort((a, b) => (a.time < b.time ? -1 : 1));
|
2019-08-16 20:54:10 +02:00
|
|
|
}
|
2021-03-19 22:54:30 +01:00
|
|
|
return newIncomingTxs;
|
2019-08-16 20:54:10 +02:00
|
|
|
}
|
|
|
|
|
2021-03-19 22:54:30 +01:00
|
|
|
/**
|
|
|
|
* Transmutes a EtherscanTransaction into a TransactionMeta
|
|
|
|
* @param {EtherscanTransaction} etherscanTransaction - the transaction to normalize
|
|
|
|
* @param {string} chainId - The chainId of the current network
|
|
|
|
* @returns {TransactionMeta}
|
|
|
|
*/
|
|
|
|
_normalizeTxFromEtherscan(etherscanTransaction, chainId) {
|
|
|
|
const time = parseInt(etherscanTransaction.timeStamp, 10) * 1000;
|
2020-11-07 08:38:12 +01:00
|
|
|
const status =
|
2021-03-19 22:54:30 +01:00
|
|
|
etherscanTransaction.isError === '0'
|
2020-11-07 08:38:12 +01:00
|
|
|
? TRANSACTION_STATUSES.CONFIRMED
|
2021-02-04 19:15:23 +01:00
|
|
|
: TRANSACTION_STATUSES.FAILED;
|
2021-06-24 21:52:14 +02:00
|
|
|
const txParams = {
|
|
|
|
from: etherscanTransaction.from,
|
|
|
|
gas: bnToHex(new BN(etherscanTransaction.gas)),
|
|
|
|
nonce: bnToHex(new BN(etherscanTransaction.nonce)),
|
|
|
|
to: etherscanTransaction.to,
|
|
|
|
value: bnToHex(new BN(etherscanTransaction.value)),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (etherscanTransaction.gasPrice) {
|
|
|
|
txParams.gasPrice = bnToHex(new BN(etherscanTransaction.gasPrice));
|
|
|
|
} else if (etherscanTransaction.maxFeePerGas) {
|
|
|
|
txParams.maxFeePerGas = bnToHex(
|
|
|
|
new BN(etherscanTransaction.maxFeePerGas),
|
|
|
|
);
|
|
|
|
txParams.maxPriorityFeePerGas = bnToHex(
|
|
|
|
new BN(etherscanTransaction.maxPriorityFeePerGas),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-16 20:54:10 +02:00
|
|
|
return {
|
2021-03-19 22:54:30 +01:00
|
|
|
blockNumber: etherscanTransaction.blockNumber,
|
2019-08-16 20:54:10 +02:00
|
|
|
id: createId(),
|
2021-03-01 16:15:42 +01:00
|
|
|
chainId,
|
2020-10-31 01:58:12 +01:00
|
|
|
metamaskNetworkId: CHAIN_ID_TO_NETWORK_ID_MAP[chainId],
|
2019-08-16 20:54:10 +02:00
|
|
|
status,
|
|
|
|
time,
|
2021-06-24 21:52:14 +02:00
|
|
|
txParams,
|
2021-03-19 22:54:30 +01:00
|
|
|
hash: etherscanTransaction.hash,
|
2021-03-10 21:16:44 +01:00
|
|
|
type: TRANSACTION_TYPES.INCOMING,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-08-16 20:54:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 22:54:30 +01:00
|
|
|
/**
|
|
|
|
* Returns a function with arity 1 that caches the argument that the function
|
|
|
|
* is called with and invokes the comparator with both the cached, previous,
|
|
|
|
* value and the current value. If specified, the initialValue will be passed
|
|
|
|
* in as the previous value on the first invocation of the returned method.
|
|
|
|
* @template A
|
|
|
|
* @params {A=} type of compared value
|
|
|
|
* @param {(prevValue: A, nextValue: A) => void} comparator - method to compare
|
|
|
|
* previous and next values.
|
|
|
|
* @param {A} [initialValue] - initial value to supply to prevValue
|
|
|
|
* on first call of the method.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function previousValueComparator(comparator, initialValue) {
|
2021-02-04 19:15:23 +01:00
|
|
|
let first = true;
|
|
|
|
let cache;
|
2019-08-21 20:42:14 +02:00
|
|
|
return (value) => {
|
|
|
|
try {
|
|
|
|
if (first) {
|
2021-02-04 19:15:23 +01:00
|
|
|
first = false;
|
2021-03-19 22:54:30 +01:00
|
|
|
return comparator(initialValue ?? value, value);
|
2019-08-21 20:42:14 +02:00
|
|
|
}
|
2021-03-19 22:54:30 +01:00
|
|
|
return comparator(cache, value);
|
2019-08-21 20:42:14 +02:00
|
|
|
} finally {
|
2021-02-04 19:15:23 +01:00
|
|
|
cache = value;
|
2019-08-21 20:42:14 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-08-21 20:42:14 +02:00
|
|
|
}
|