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

Denis/fetch token info #317

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Add loop fetch meta tokens from info server
Denis Boldyrev authored and Lex Peterson committed Aug 13, 2021
commit a07bb6103ec79f4b19a70536153534aaae0182fa
34 changes: 31 additions & 3 deletions src/ethereum/ethEngine.js
Original file line number Diff line number Diff line change
@@ -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)
@@ -191,20 +194,41 @@ 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
info server.

Formula:
fee = baseMultiplier * baseFee + minPriorityFee

Where:
minPriorityFee = <minimum priority fee from info server>
baseFee = <latest block's base fee>
baseMultiplier = <multiplier from info server for low, standard, high, etc>

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 +376,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()

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

export type EthereumFees = $Call<typeof asEthereumFees>

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<typeof asMetaToken>

export const asMetaTokenMap = asArray<MetaToken>(asMetaToken)

export type MetaTokenMap = $Call<typeof asMetaTokenMap>

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