Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch token info #316

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/ethereum/ethEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ import {
type EthereumTxOtherParams,
type EthereumWalletOtherData,
type LastEstimatedGasLimit,
asEthereumFees
type MetaToken,
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)
Expand All @@ -60,6 +64,7 @@ export class EthereumEngine extends CurrencyEngine {
ethNetwork: EthereumNetwork
lastEstimatedGasLimit: LastEstimatedGasLimit
fetchCors: EdgeFetchFunction
otherMethods: Object

constructor(
currencyPlugin: EthereumPlugin,
Expand All @@ -86,6 +91,10 @@ export class EthereumEngine extends CurrencyEngine {
gasLimit: ''
}
this.fetchCors = fetchCors

this.otherMethods = {
getTokenInfo: this.getTokenInfo.bind(this)
}
}

updateBalance(tk: string, balance: string) {
Expand Down Expand Up @@ -191,6 +200,43 @@ 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) {
for (const contractAddress in jsonObj) {
const { name, symbol, decimals, iconUrl }: MetaToken =
jsonObj[contractAddress]

this.allTokens.push({
currencyCode: symbol,
currencyName: name,
denominations: [
{
name: symbol,
multiplier: Math.pow(10, decimals).toString()
}
],
symbolImage: iconUrl,
contractAddress
})
}
} 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
Expand Down Expand Up @@ -352,6 +398,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()

Expand Down
14 changes: 14 additions & 0 deletions src/ethereum/ethTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ export const asEthereumFees = asObject<EthereumFee>(asEthereumFee)

export type EthereumFees = $Call<typeof asEthereumFees>

export const asMetaToken = asObject({
name: asString,
symbol: asString,
decimals: asNumber,
tradable: asBoolean,
iconUrl: asString
})

export type MetaToken = $Call<typeof asMetaToken>

export const asMetaTokenMap = asObject<MetaToken>(asMetaToken)

export type MetaTokenMap = $Call<typeof asMetaTokenMap>

export type EthereumCalcedFees = {
gasPrice: string,
gasLimit: string,
Expand Down