Skip to content

Commit

Permalink
fix: pr review comments
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Maxwell <[email protected]>
  • Loading branch information
nicole-obrien and maxwellmattryan committed Sep 6, 2023
1 parent a4d95ad commit 25ab455
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
import { localize } from '@core/i18n'
import { canAccountMakeEvmTransaction, pollEvmChainGasPrice } from '@core/layer-2/actions'
import { marketCoinPrices } from '@core/market/stores'
import { getNetwork, isEvmChain } from '@core/network'
import { AccountTokens, BASE_TOKEN_ID, IToken, ITokenWithBalance, TokenStandard } from '@core/token'
import { NetworkId, getNetwork, isEvmChain } from '@core/network'
import {
AccountTokens,
BASE_TOKEN_ID,
IAccountTokensPerNetwork,
IToken,
ITokenWithBalance,
TokenStandard,
} from '@core/token'
import { getAccountTokensForSelectedAccount, getTokenBalance } from '@core/token/actions'
import { selectedAccountTokens } from '@core/token/stores'
import { SendFlowType, sendFlowParameters, setSendFlowParameters } from '@core/wallet'
Expand Down Expand Up @@ -42,7 +49,10 @@
let tokenList: ITokenWithBalance[]
function getTokenList(): ITokenWithBalance[] {
const list = []
for (const [networkId, tokensPerNetwork] of Object.entries(accountTokens)) {
for (const [networkId, tokensPerNetwork] of Object.entries(accountTokens) as [
NetworkId,
IAccountTokensPerNetwork,
][]) {
if (isEvmChain(networkId)) {
pollEvmChainGasPrice(networkId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NetworkId } from '@core/network/types'
import { getNetwork } from '@core/network/stores'
import { Converter } from '@core/utils'

export async function getGasPriceFromProvider(networkId: NetworkId): Promise<string | undefined> {
export async function getGasPriceForNetwork(networkId: NetworkId): Promise<string | undefined> {
const chain = getNetwork()?.getChain(networkId)

const provider = chain?.getProvider()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import { EvmChainId } from '@core/network/enums'
import { NetworkId } from '@core/network/types'
import { MILLISECONDS_PER_SECOND } from '@core/utils'
import { updateEvmChainGasPrice } from './updateEvmChainGasPrice'

const EVM_CHAIN_GAS_PRICE_POLLING_INTERVAL: number = 15 * MILLISECONDS_PER_SECOND
const EVM_CHAIN_GAS_PRICE_POLLING_INTERVAL: number = 60 * MILLISECONDS_PER_SECOND

const pollIntervalMap: { [key in EvmChainId]?: number } = {}
const pollIntervalMap: { [id in NetworkId]?: number } = {}

export function pollEvmChainGasPrice(chainId: NetworkId): void {
if (!chainId || typeof pollIntervalMap[chainId] === 'number') {
export function pollEvmChainGasPrice(networkId: NetworkId): void {
if (!networkId || isPollingEvmChainGasPrice(networkId)) {
return
}
void updateEvmChainGasPrice(chainId)
pollIntervalMap[chainId] = window.setInterval(() => {
void updateEvmChainGasPrice(chainId)
void updateEvmChainGasPrice(networkId)
pollIntervalMap[networkId] = window.setInterval(() => {
void updateEvmChainGasPrice(networkId)
}, EVM_CHAIN_GAS_PRICE_POLLING_INTERVAL)
}

export function pollEvmChainGasPrices(chainIds: NetworkId[]): void {
export function pollEvmChainGasPrices(networkIds: NetworkId[]): void {
stopPollingEvmChainGasPrices()
for (const chainId of chainIds) {
pollEvmChainGasPrice(chainId)
for (const networkId of networkIds) {
pollEvmChainGasPrice(networkId)
}
}

export function stopPollingEvmChainGasPrices(chainIdsToIgnore?: NetworkId[]): void {
for (const chainId of Object.keys(pollIntervalMap)) {
if (!chainIdsToIgnore?.includes(chainId as NetworkId)) {
clearInterval(pollIntervalMap[chainId])
export function stopPollingEvmChainGasPrices(networkIdsToIgnore?: NetworkId[]): void {
for (const networkId of Object.keys(pollIntervalMap) as NetworkId[]) {
if (!networkIdsToIgnore?.includes(networkId)) {
if (isPollingEvmChainGasPrice(networkId)) {
clearInterval(pollIntervalMap[networkId])
}
}
}
}

export function isPollingEvmChainGasPrice(networkId: NetworkId): boolean {
return typeof pollIntervalMap[networkId] === 'number'
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { handleError } from '@core/error/handlers'
import { NetworkId } from '@core/network/types'
import { setEvmChainGasPrice } from '../stores'
import { getGasPriceFromProvider } from './getGasPriceFromProvider'
import { getGasPriceForNetwork } from './getGasPriceFromProvider'

export async function updateEvmChainGasPrice(chainId: NetworkId): Promise<void> {
try {
const gasPrice = await getGasPriceFromProvider(chainId)
const gasPrice = await getGasPriceForNetwork(chainId)
if (gasPrice) {
setEvmChainGasPrice(BigInt(gasPrice), chainId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export function getEvmChainGasPrice(networkId: NetworkId): bigint | undefined {
}

export function setEvmChainGasPrice(price: bigint, networkId: NetworkId): void {
evmChainGasPrices.update((_evmChainGasPrices) => {
evmChainGasPrices.update((state) => {
if (typeof price === 'bigint' && isEvmChain(networkId)) {
return {
..._evmChainGasPrices,
...state,
[networkId]: price,
}
} else {
return _evmChainGasPrices
return state
}
})
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EvmChainId } from '@core/network'
import { NetworkId } from '@core/network/types'

export type EvmChainGasPrices = {
[key in EvmChainId]?: bigint
[id in NetworkId]?: bigint
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type SpitNetworkId =
| { namespace: NetworkNamespace.Evm; chainId: EvmChainId }

export function getSplitNetworkId(networkId: NetworkId): SpitNetworkId | undefined {
const parts = networkId?.split(':')
const parts = networkId.split(':')
if ((parts?.[0] as NetworkNamespace) === NetworkNamespace.Stardust && parts?.[1]) {
return {
namespace: NetworkNamespace.Stardust,
Expand Down

0 comments on commit 25ab455

Please sign in to comment.