Skip to content

Commit

Permalink
Extract keyAssetsByAddress as a shared util
Browse files Browse the repository at this point in the history
  • Loading branch information
hyphenized committed Feb 14, 2025
1 parent d108f08 commit d931b06
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
6 changes: 6 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 Down Expand Up @@ -226,6 +227,11 @@ export function isSmartContractFungibleAsset<T extends AnyAsset>(
return "homeNetwork" in asset && isFungibleAsset(asset)
}

export function keyAssetsByAddress(assets: SmartContractFungibleAsset[]) {
const key: keyof SmartContractFungibleAsset = "contractAddress"
return keyBy(assets, key)
}

/**
* Type guard to check if an AnyAssetAmount is actually a FungibleAssetAmount.
*
Expand Down
27 changes: 4 additions & 23 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,21 +96,6 @@ interface Events extends ServiceLifecycleEvents {
removeAssetData: SmartContractFungibleAsset
}

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

return activeAssetsByAddress
}

function allowVerifyAssetByManualImport(
asset: SmartContractFungibleAsset,
verified?: boolean,
Expand Down Expand Up @@ -540,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 @@ -872,11 +853,11 @@ export default class IndexingService extends BaseService<Events> {
// FIXME networks; instead, the first listed network produces the
// FIXME final price in those cases.

const allActiveAssetsByAddress = indexAssetsByAddress(activeAssetsToTrack)
const allActiveAssetsByAddress = keyAssetsByAddress(activeAssetsToTrack)

const activeAssetsByNetwork = trackedNetworks
.map((network) => ({
activeAssetsByAddress: indexAssetsByAddress(
activeAssetsByAddress: keyAssetsByAddress(
activeAssetsToTrack.filter(({ homeNetwork }) =>
sameNetwork(homeNetwork, network),
),
Expand Down

0 comments on commit d931b06

Please sign in to comment.