diff --git a/src/ethereum/ethEngine.js b/src/ethereum/ethEngine.js index 41cb8ba94..919d45823 100644 --- a/src/ethereum/ethEngine.js +++ b/src/ethereum/ethEngine.js @@ -46,10 +46,13 @@ import { type EthereumTxOtherParams, type EthereumWalletOtherData, type LastEstimatedGasLimit, - asEthereumFees + type MetaTokenMap, + asEthereumFees, + asMetaTokenMap } from './ethTypes.js' const NETWORKFEES_POLL_MILLISECONDS = 60 * 10 * 1000 // 10 minutes +const METATOKENS_UPDATE_MILLISECONDS = 24 * 60 * 60 * 1000 // 1 day const ETH_GAS_STATION_WEI_MULTIPLIER = 100000000 // 100 million is the multiplier for ethgassstation because it uses 10x gwei const WEI_MULTIPLIER = 1000000000 const GAS_PRICE_SANITY_CHECK = 30000 // 3000 Gwei (ethgasstation api reports gas prices with additional decimal place) @@ -60,6 +63,7 @@ export class EthereumEngine extends CurrencyEngine { ethNetwork: EthereumNetwork lastEstimatedGasLimit: LastEstimatedGasLimit fetchCors: EdgeFetchFunction + otherMethods: Object constructor( currencyPlugin: EthereumPlugin, @@ -86,6 +90,10 @@ export class EthereumEngine extends CurrencyEngine { gasLimit: '' } this.fetchCors = fetchCors + + this.otherMethods = { + getTokenInfo: this.getTokenInfo.bind(this) + } } updateBalance(tk: string, balance: string) { @@ -191,6 +199,27 @@ export class EthereumEngine extends CurrencyEngine { this.updateNetworkFeesFromBaseFeePerGas(hexToDecimal(baseFeePerGas)) } + async checkUpdateCustomMetaTokens() { + // Get the meta tokens from the info server + try { + const infoServer = getEdgeInfoServer() + const url = `${infoServer}/v1/knownContract/allContracts` + const jsonObj: MetaTokenMap = await this.ethNetwork.fetchGet(url) + const valid = asMaybe(asMetaTokenMap)(jsonObj) != null + + if (valid) { + this.allTokens = jsonObj + } else { + this.log.error( + `Error: Fetched invalid metaTokens ${JSON.stringify(jsonObj)}` + ) + } + } catch (err) { + this.log.error(`Error fetching metaTokens from Edge info server`) + this.log.error(err) + } + } + async updateNetworkFeesFromBaseFeePerGas(baseFeePerGas: string) { /* This algorithm calculates fee amounts using the base multiplier from the @@ -198,13 +227,13 @@ export class EthereumEngine extends CurrencyEngine { Formula: fee = baseMultiplier * baseFee + minPriorityFee - + Where: minPriorityFee = baseFee = baseMultiplier = - Reference analysis for choosing 2 gwei minimum priority fee: + Reference analysis for choosing 2 gwei minimum priority fee: https://hackmd.io/@q8X_WM2nTfu6nuvAzqXiTQ/1559-wallets#:~:text=2%20gwei%20is%20probably%20a%20very%20good%20default */ @@ -352,6 +381,10 @@ export class EthereumEngine extends CurrencyEngine { async startEngine() { this.engineOn = true this.addToLoop('checkUpdateNetworkFees', NETWORKFEES_POLL_MILLISECONDS) + this.addToLoop( + 'checkUpdateCustomMetaTokens', + METATOKENS_UPDATE_MILLISECONDS + ) this.ethNetwork.needsLoop() diff --git a/src/ethereum/ethTypes.js b/src/ethereum/ethTypes.js index 78990f57b..04b4dcc3c 100644 --- a/src/ethereum/ethTypes.js +++ b/src/ethereum/ethTypes.js @@ -77,6 +77,25 @@ export const asEthereumFees = asObject(asEthereumFee) export type EthereumFees = $Call +export const asMetaToken = asObject({ + currencyCode: asString, + currencyName: asString, + denominations: asArray( + asObject({ + name: asString, + multiplier: asString + }) + ), + symbolImage: asOptional(asString), + contractAddress: asOptional(asString) +}) + +export type MetaToken = $Call + +export const asMetaTokenMap = asArray(asMetaToken) + +export type MetaTokenMap = $Call + export type EthereumCalcedFees = { gasPrice: string, gasLimit: string,