2020-01-09 04:34:58 +01:00
|
|
|
import ObservableStore from 'obs-store'
|
|
|
|
import log from 'loglevel'
|
|
|
|
import BN from 'bn.js'
|
|
|
|
import createId from '../lib/random-id'
|
|
|
|
import { bnToHex } from '../lib/util'
|
2019-09-04 22:00:11 +02:00
|
|
|
import fetchWithTimeout from '../lib/fetch-with-timeout'
|
2020-01-09 04:34:58 +01:00
|
|
|
|
|
|
|
import {
|
2019-08-16 20:54:10 +02:00
|
|
|
ROPSTEN,
|
|
|
|
RINKEBY,
|
|
|
|
KOVAN,
|
2019-09-18 18:30:29 +02:00
|
|
|
GOERLI,
|
2019-08-16 20:54:10 +02:00
|
|
|
MAINNET,
|
2020-05-20 17:57:45 +02:00
|
|
|
NETWORK_TYPE_TO_ID_MAP,
|
2020-01-09 04:34:58 +01:00
|
|
|
} from './network/enums'
|
|
|
|
|
2019-08-20 20:52:00 +02:00
|
|
|
const fetch = fetchWithTimeout({
|
|
|
|
timeout: 30000,
|
|
|
|
})
|
2019-08-16 20:54:10 +02:00
|
|
|
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class IncomingTransactionsController {
|
2019-08-16 20:54:10 +02:00
|
|
|
|
|
|
|
constructor (opts = {}) {
|
|
|
|
const {
|
|
|
|
blockTracker,
|
|
|
|
networkController,
|
|
|
|
preferencesController,
|
|
|
|
} = opts
|
|
|
|
this.blockTracker = blockTracker
|
|
|
|
this.networkController = networkController
|
|
|
|
this.preferencesController = preferencesController
|
|
|
|
this.getCurrentNetwork = () => networkController.getProviderConfig().type
|
|
|
|
|
2019-08-20 20:52:00 +02:00
|
|
|
this._onLatestBlock = async (newBlockNumberHex) => {
|
|
|
|
const selectedAddress = this.preferencesController.getSelectedAddress()
|
|
|
|
const newBlockNumberDec = parseInt(newBlockNumberHex, 16)
|
|
|
|
await this._update({
|
|
|
|
address: selectedAddress,
|
|
|
|
newBlockNumberDec,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-16 20:54:10 +02:00
|
|
|
const initState = Object.assign({
|
|
|
|
incomingTransactions: {},
|
|
|
|
incomingTxLastFetchedBlocksByNetwork: {
|
|
|
|
[ROPSTEN]: null,
|
|
|
|
[RINKEBY]: null,
|
|
|
|
[KOVAN]: null,
|
2019-09-18 18:30:29 +02:00
|
|
|
[GOERLI]: null,
|
2019-08-16 20:54:10 +02:00
|
|
|
[MAINNET]: null,
|
|
|
|
},
|
|
|
|
}, opts.initState)
|
|
|
|
this.store = new ObservableStore(initState)
|
|
|
|
|
2019-08-21 20:42:14 +02:00
|
|
|
this.preferencesController.store.subscribe(pairwise((prevState, currState) => {
|
|
|
|
const { featureFlags: { showIncomingTransactions: prevShowIncomingTransactions } = {} } = prevState
|
|
|
|
const { featureFlags: { showIncomingTransactions: currShowIncomingTransactions } = {} } = currState
|
|
|
|
|
|
|
|
if (currShowIncomingTransactions === prevShowIncomingTransactions) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prevShowIncomingTransactions && !currShowIncomingTransactions) {
|
|
|
|
this.stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.start()
|
|
|
|
}))
|
|
|
|
|
|
|
|
this.preferencesController.store.subscribe(pairwise(async (prevState, currState) => {
|
|
|
|
const { selectedAddress: prevSelectedAddress } = prevState
|
|
|
|
const { selectedAddress: currSelectedAddress } = currState
|
|
|
|
|
|
|
|
if (currSelectedAddress === prevSelectedAddress) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await this._update({
|
|
|
|
address: currSelectedAddress,
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
2019-08-16 20:54:10 +02:00
|
|
|
this.networkController.on('networkDidChange', async (newType) => {
|
|
|
|
const address = this.preferencesController.getSelectedAddress()
|
|
|
|
await this._update({
|
|
|
|
address,
|
|
|
|
networkType: newType,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-20 20:52:00 +02:00
|
|
|
start () {
|
2019-08-21 20:42:14 +02:00
|
|
|
const { featureFlags = {} } = this.preferencesController.store.getState()
|
|
|
|
const { showIncomingTransactions } = featureFlags
|
|
|
|
|
|
|
|
if (!showIncomingTransactions) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-20 20:52:00 +02:00
|
|
|
this.blockTracker.removeListener('latest', this._onLatestBlock)
|
|
|
|
this.blockTracker.addListener('latest', this._onLatestBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
stop () {
|
|
|
|
this.blockTracker.removeListener('latest', this._onLatestBlock)
|
|
|
|
}
|
|
|
|
|
2019-08-16 20:54:10 +02:00
|
|
|
async _update ({ address, newBlockNumberDec, networkType } = {}) {
|
|
|
|
try {
|
|
|
|
const dataForUpdate = await this._getDataForUpdate({ address, newBlockNumberDec, networkType })
|
|
|
|
await this._updateStateWithNewTxData(dataForUpdate)
|
|
|
|
} catch (err) {
|
|
|
|
log.error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _getDataForUpdate ({ address, newBlockNumberDec, networkType } = {}) {
|
|
|
|
const {
|
|
|
|
incomingTransactions: currentIncomingTxs,
|
|
|
|
incomingTxLastFetchedBlocksByNetwork: currentBlocksByNetwork,
|
|
|
|
} = this.store.getState()
|
|
|
|
|
|
|
|
const network = networkType || this.getCurrentNetwork()
|
|
|
|
const lastFetchBlockByCurrentNetwork = currentBlocksByNetwork[network]
|
|
|
|
let blockToFetchFrom = lastFetchBlockByCurrentNetwork || newBlockNumberDec
|
|
|
|
if (blockToFetchFrom === undefined) {
|
|
|
|
blockToFetchFrom = parseInt(this.blockTracker.getCurrentBlock(), 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
const { latestIncomingTxBlockNumber, txs: newTxs } = await this._fetchAll(address, blockToFetchFrom, network)
|
|
|
|
|
|
|
|
return {
|
|
|
|
latestIncomingTxBlockNumber,
|
|
|
|
newTxs,
|
|
|
|
currentIncomingTxs,
|
|
|
|
currentBlocksByNetwork,
|
|
|
|
fetchedBlockNumber: blockToFetchFrom,
|
|
|
|
network,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _updateStateWithNewTxData ({
|
|
|
|
latestIncomingTxBlockNumber,
|
|
|
|
newTxs,
|
|
|
|
currentIncomingTxs,
|
|
|
|
currentBlocksByNetwork,
|
|
|
|
fetchedBlockNumber,
|
|
|
|
network,
|
|
|
|
}) {
|
|
|
|
const newLatestBlockHashByNetwork = latestIncomingTxBlockNumber
|
|
|
|
? parseInt(latestIncomingTxBlockNumber, 10) + 1
|
|
|
|
: fetchedBlockNumber + 1
|
|
|
|
const newIncomingTransactions = {
|
|
|
|
...currentIncomingTxs,
|
|
|
|
}
|
2020-02-15 21:34:12 +01:00
|
|
|
newTxs.forEach((tx) => {
|
2019-11-20 01:03:20 +01:00
|
|
|
newIncomingTransactions[tx.hash] = tx
|
|
|
|
})
|
2019-08-16 20:54:10 +02:00
|
|
|
|
|
|
|
this.store.updateState({
|
|
|
|
incomingTxLastFetchedBlocksByNetwork: {
|
|
|
|
...currentBlocksByNetwork,
|
|
|
|
[network]: newLatestBlockHashByNetwork,
|
|
|
|
},
|
|
|
|
incomingTransactions: newIncomingTransactions,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async _fetchAll (address, fromBlock, networkType) {
|
2019-09-12 22:00:35 +02:00
|
|
|
const fetchedTxResponse = await this._fetchTxs(address, fromBlock, networkType)
|
|
|
|
return this._processTxFetchResponse(fetchedTxResponse)
|
2019-08-16 20:54:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async _fetchTxs (address, fromBlock, networkType) {
|
|
|
|
let etherscanSubdomain = 'api'
|
2020-05-20 17:57:45 +02:00
|
|
|
const currentNetworkID = NETWORK_TYPE_TO_ID_MAP[networkType]?.networkId
|
2019-08-16 20:54:10 +02:00
|
|
|
|
2020-05-20 17:57:45 +02:00
|
|
|
if (!currentNetworkID) {
|
2019-08-16 20:54:10 +02:00
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
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`
|
|
|
|
|
|
|
|
if (fromBlock) {
|
|
|
|
url += `&startBlock=${parseInt(fromBlock, 10)}`
|
|
|
|
}
|
|
|
|
const response = await fetch(url)
|
|
|
|
const parsedResponse = await response.json()
|
|
|
|
|
|
|
|
return {
|
|
|
|
...parsedResponse,
|
|
|
|
address,
|
|
|
|
currentNetworkID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:41:18 +02:00
|
|
|
_processTxFetchResponse ({ status, result = [], address, currentNetworkID }) {
|
2019-11-15 18:17:42 +01:00
|
|
|
if (status === '1' && Array.isArray(result) && result.length > 0) {
|
2019-08-16 20:54:10 +02:00
|
|
|
const remoteTxList = {}
|
|
|
|
const remoteTxs = []
|
|
|
|
result.forEach((tx) => {
|
|
|
|
if (!remoteTxList[tx.hash]) {
|
|
|
|
remoteTxs.push(this._normalizeTxFromEtherscan(tx, currentNetworkID))
|
|
|
|
remoteTxList[tx.hash] = 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const incomingTxs = remoteTxs.filter((tx) => tx.txParams.to && tx.txParams.to.toLowerCase() === address.toLowerCase())
|
2019-08-16 20:54:10 +02:00
|
|
|
incomingTxs.sort((a, b) => (a.time < b.time ? -1 : 1))
|
|
|
|
|
|
|
|
let latestIncomingTxBlockNumber = null
|
|
|
|
incomingTxs.forEach((tx) => {
|
|
|
|
if (
|
|
|
|
tx.blockNumber &&
|
|
|
|
(!latestIncomingTxBlockNumber ||
|
|
|
|
parseInt(latestIncomingTxBlockNumber, 10) < parseInt(tx.blockNumber, 10))
|
|
|
|
) {
|
|
|
|
latestIncomingTxBlockNumber = tx.blockNumber
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
latestIncomingTxBlockNumber,
|
|
|
|
txs: incomingTxs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
latestIncomingTxBlockNumber: null,
|
|
|
|
txs: [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_normalizeTxFromEtherscan (txMeta, currentNetworkID) {
|
|
|
|
const time = parseInt(txMeta.timeStamp, 10) * 1000
|
|
|
|
const status = txMeta.isError === '0' ? 'confirmed' : 'failed'
|
|
|
|
return {
|
|
|
|
blockNumber: txMeta.blockNumber,
|
|
|
|
id: createId(),
|
2020-07-08 23:05:09 +02:00
|
|
|
metamaskNetworkId: currentNetworkID,
|
2019-08-16 20:54:10 +02:00
|
|
|
status,
|
|
|
|
time,
|
|
|
|
txParams: {
|
|
|
|
from: txMeta.from,
|
|
|
|
gas: bnToHex(new BN(txMeta.gas)),
|
|
|
|
gasPrice: bnToHex(new BN(txMeta.gasPrice)),
|
|
|
|
nonce: bnToHex(new BN(txMeta.nonce)),
|
|
|
|
to: txMeta.to,
|
|
|
|
value: bnToHex(new BN(txMeta.value)),
|
|
|
|
},
|
|
|
|
hash: txMeta.hash,
|
|
|
|
transactionCategory: 'incoming',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:42:14 +02:00
|
|
|
function pairwise (fn) {
|
|
|
|
let first = true
|
|
|
|
let cache
|
|
|
|
return (value) => {
|
|
|
|
try {
|
|
|
|
if (first) {
|
|
|
|
first = false
|
|
|
|
return fn(value, value)
|
|
|
|
} else {
|
|
|
|
return fn(cache, value)
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
cache = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|