Skip to content

Commit

Permalink
Reduce price lookups to tracked assets (#3779)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowfiend authored Feb 14, 2025
2 parents 0635316 + 0b82a5b commit 989b7b9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 61 deletions.
2 changes: 1 addition & 1 deletion background/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnyAssetAmount, SmartContractFungibleAsset } from "./assets"
import type { AnyAssetAmount, SmartContractFungibleAsset } from "./assets"
import { EVMNetwork, NetworkBaseAsset } from "./networks"
import { HexString } from "./types"

Expand Down
8 changes: 8 additions & 0 deletions background/assets.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { keyBy } from "lodash"
import { TokenList } from "@uniswap/token-lists"
import { UNIXTime, HexString } from "./types"
import {
Expand All @@ -7,6 +8,7 @@ import {
NetworkBaseAsset,
} from "./networks"
import { fromFixedPoint } from "./lib/fixed-point"
import { normalizeEVMAddress } from "./lib/utils"

/**
* A reference to a token list, with the name, URL, and potentially logo of the
Expand Down Expand Up @@ -226,6 +228,12 @@ export function isSmartContractFungibleAsset<T extends AnyAsset>(
return "homeNetwork" in asset && isFungibleAsset(asset)
}

export function keyAssetsByAddress(assets: SmartContractFungibleAsset[]) {
return keyBy(assets, (asset: SmartContractFungibleAsset) =>
normalizeEVMAddress(asset.contractAddress),
)
}

/**
* Type guard to check if an AnyAssetAmount is actually a FungibleAssetAmount.
*
Expand Down
16 changes: 0 additions & 16 deletions background/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { BigNumber, ethers, utils } from "ethers"
import { normalizeHexAddress, toChecksumAddress } from "@tallyho/hd-keyring"
import { NormalizedEVMAddress, UNIXTime } from "../../types"
import { EVMNetwork } from "../../networks"
import { ETHEREUM, SEPOLIA } from "../../constants"
import { AddressOnNetwork } from "../../accounts"

export function isValidChecksumAddress(
Expand Down Expand Up @@ -151,20 +149,6 @@ export function decodeJSON(input: string): unknown {
)
}

/**
* Determine which Ethereum network should be used based on the .env file
*/
export function getEthereumNetwork(): EVMNetwork {
const ethereumNetwork = process.env.ETHEREUM_NETWORK?.toUpperCase()

if (ethereumNetwork === "SEPOLIA") {
return SEPOLIA
}

// Default to mainnet
return ETHEREUM
}

export function isProbablyEVMAddress(str: string): boolean {
if (normalizeHexAddress(str).startsWith("0x") && str.length === 42) {
return true
Expand Down
60 changes: 18 additions & 42 deletions background/services/indexing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
AnyAssetMetadata,
FungibleAsset,
isSmartContractFungibleAsset,
keyAssetsByAddress,
PricePoint,
SmartContractAmount,
SmartContractFungibleAsset,
Expand Down Expand Up @@ -95,32 +96,6 @@ interface Events extends ServiceLifecycleEvents {
removeAssetData: SmartContractFungibleAsset
}

const getAssetsByAddress = (assets: SmartContractFungibleAsset[]) => {
const activeAssetsByAddress = assets.reduce(
(agg, t) => {
const newAgg = {
...agg,
}
newAgg[t.contractAddress.toLowerCase()] = t
return newAgg
},
{} as { [address: string]: SmartContractFungibleAsset },
)

return activeAssetsByAddress
}

const getActiveAssetsByAddressForNetwork = (
network: EVMNetwork,
activeAssetsToTrack: SmartContractFungibleAsset[],
) => {
const networkActiveAssets = activeAssetsToTrack.filter(
(asset) => asset.homeNetwork.chainID === network.chainID,
)

return getAssetsByAddress(networkActiveAssets)
}

function allowVerifyAssetByManualImport(
asset: SmartContractFungibleAsset,
verified?: boolean,
Expand Down Expand Up @@ -551,12 +526,7 @@ export default class IndexingService extends BaseService<Events> {
smartContractAssets?.map(({ contractAddress }) => contractAddress),
)

const listedAssetByAddress = (smartContractAssets ?? []).reduce<{
[contractAddress: string]: SmartContractFungibleAsset
}>((acc, asset) => {
acc[normalizeEVMAddress(asset.contractAddress)] = asset
return acc
}, {})
const listedAssetByAddress = keyAssetsByAddress(smartContractAssets ?? [])

const removedCustomAssets = await this.db.getRemovedCustomAssetsByNetworks([
addressNetwork.network,
Expand Down Expand Up @@ -863,28 +833,34 @@ export default class IndexingService extends BaseService<Events> {
* Loads prices for all tracked assets except untrusted/custom network assets
*/
private async getTrackedAssetsPrices() {
logger.debug("Fetching tracked asset prices...")
// get the prices of all assets to track and save them
const assetsToTrack = await this.db.getAssetsToTrack()
const trackedNetworks = await this.chainService.getTrackedNetworks()
const assetsToTrack = trackedNetworks.flatMap((network) =>
this.getCachedAssets(network),
)

// Filter all assets based on supported networks
const activeAssetsToTrack = assetsToTrack
.filter(isSmartContractFungibleAsset)
.filter(isTrustedAsset)
const activeAssetsToTrack = assetsToTrack.filter(
(asset) =>
isTrustedAsset(asset) &&
trackedNetworks.some((network) =>
sameNetwork(network, asset.homeNetwork),
),
)

try {
// TODO only uses USD
// FIXME None of the below handles assets that exist on multiple
// FIXME networks; instead, the first listed network produces the
// FIXME final price in those cases.

const allActiveAssetsByAddress = getAssetsByAddress(activeAssetsToTrack)
const allActiveAssetsByAddress = keyAssetsByAddress(activeAssetsToTrack)

const activeAssetsByNetwork = trackedNetworks
.map((network) => ({
activeAssetsByAddress: getActiveAssetsByAddressForNetwork(
network,
activeAssetsToTrack,
activeAssetsByAddress: keyAssetsByAddress(
activeAssetsToTrack.filter(({ homeNetwork }) =>
sameNetwork(homeNetwork, network),
),
),
network,
}))
Expand Down
3 changes: 1 addition & 2 deletions background/services/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { PermissionRequest } from "@tallyho/provider-bridge-shared"
import { utils } from "ethers"

import {
getEthereumNetwork,
isProbablyEVMAddress,
normalizeEVMAddress,
sameEVMAddress,
Expand Down Expand Up @@ -1273,7 +1272,7 @@ export default class ReduxService extends BaseService<never> {
"setClaimReferrer",
async (referral: string) => {
const isAddress = isProbablyEVMAddress(referral)
const network = getEthereumNetwork()
const network = ETHEREUM
const ensName = isAddress
? (
await this.nameService.lookUpName({
Expand Down

0 comments on commit 989b7b9

Please sign in to comment.