Skip to content

Commit

Permalink
feat: asset data encoding reconciling against full dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
woodenfurniture committed Dec 19, 2024
1 parent aa5227f commit 4fbd84f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 48 deletions.
31 changes: 31 additions & 0 deletions packages/utils/src/assetData/assetData.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { AssetsById } from '@shapeshiftoss/types'
import fs from 'fs'
import path from 'path'
import { describe, expect, it } from 'vitest'

import { decodeAssetData } from './decodeAssetData'
Expand Down Expand Up @@ -122,4 +124,33 @@ describe('assetData', () => {
expect(assetData).toEqual(mockGeneratedAssetData)
expect(sortedAssetIds).toEqual(mockSortedAssetIds)
})

it('reconciles against full dataset', () => {
const generatedAssetsPath = path.join(
__dirname,
'../../../../src/lib/asset-service/service/generatedAssetData.json',
)

const removeUndefined = (obj: object) => JSON.parse(JSON.stringify(obj))

const generatedAssetData = JSON.parse(fs.readFileSync(generatedAssetsPath, 'utf8'))
const assetIds = Object.keys(generatedAssetData).sort()
const encodedAssetData = encodeAssetData(assetIds, generatedAssetData)

fs.writeFileSync(
path.join(
__dirname,
'../../../../src/lib/asset-service/service/generatedAssetData_smol.json',
),
JSON.stringify(encodedAssetData),
)
console.time('decodeAssetData')
const { assetData, sortedAssetIds } = decodeAssetData(encodedAssetData)
console.timeEnd('decodeAssetData')
Object.entries(generatedAssetData).forEach(([assetId, expectedAsset]) => {
const actualAsset = assetData[assetId]
expect(removeUndefined(actualAsset)).toEqual(expectedAsset)
})
expect(sortedAssetIds).toEqual(assetIds)
})
})
2 changes: 1 addition & 1 deletion packages/utils/src/assetData/baseAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export const base: Readonly<Asset> = Object.freeze({
explorer: 'https://basescan.org',
explorerAddressLink: 'https://basescan.org/address/',
explorerTxLink: 'https://basescan.org/tx/',
relatedAssetKey: null,
relatedAssetKey: 'eip155:1/slip44:60',
})

export const solana: Readonly<Asset> = Object.freeze({
Expand Down
17 changes: 10 additions & 7 deletions packages/utils/src/assetData/decodeAssetData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AssetId } from '@shapeshiftoss/caip'
import { fromAssetId } from '@shapeshiftoss/caip'
import type { Asset } from '@shapeshiftoss/types'
import { clone, pick } from 'lodash'
import { pick } from 'lodash'

import { assertUnreachable } from '../assertUnreachable'
import { FIELDS } from './constants'
Expand All @@ -17,11 +17,6 @@ export const decodeAssetData = (encodedAssetData: EncodedAssetData) => {

const baseAsset = getBaseAsset(chainId)

if (encodedAsset === null) {
acc[assetId] = clone(baseAsset)
return acc
}

// Initialize with default values for the chain
const asset: Asset = Object.assign(
{
Expand All @@ -40,6 +35,12 @@ export const decodeAssetData = (encodedAssetData: EncodedAssetData) => {
pick(baseAsset, ['explorer', 'explorerAddressLink', 'explorerTxLink']),
)

if (assetId === baseAsset.assetId) {
asset.networkName = baseAsset.networkName
asset.networkIcon = baseAsset.networkIcon
asset.networkColor = baseAsset.networkColor
}

// Apply each field's decoded value to the asset
FIELDS.forEach((field, fieldIdx) => {
const value = encodedAsset[fieldIdx]
Expand All @@ -56,7 +57,9 @@ export const decodeAssetData = (encodedAssetData: EncodedAssetData) => {
}
case 'relatedAssetKey': {
const typedValue = value as FieldToType[typeof field]
asset.relatedAssetKey = typedValue === null ? null : sortedAssetIds[typedValue]
const relatedAssetId = typedValue === null ? null : sortedAssetIds[typedValue]
// if (relatedAssetId === undefined) throw Error()
asset.relatedAssetKey = relatedAssetId
break
}
case 'name':
Expand Down
10 changes: 0 additions & 10 deletions packages/utils/src/assetData/encodeAssetData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { Asset, AssetsById } from '@shapeshiftoss/types'

import { assertUnreachable } from '../assertUnreachable'
import { FIELDS } from './constants'
import { getBaseAsset } from './getBaseAsset'
import type { EncodedAsset, EncodedAssetData, Field, FieldToType } from './types'

const encodeField = <F extends Field>(
Expand Down Expand Up @@ -48,15 +47,6 @@ export const encodeAssetData = (
for (let i = 0; i < sortedAssetIds.length; i++) {
const assetId = sortedAssetIds[i]
const asset = assetsById[assetId]

// If this is a base asset, don't encode it - native assets have additional fields we don't want
// to encode. Instead, we can use the hardcoded base asset data we already have in the codebase.
const baseAsset = getBaseAsset(asset.chainId)
if (baseAsset?.assetId === assetId) {
encodedAssets.push(null)
continue
}

const record = FIELDS.map(field => encodeField(field, assetIdToAssetIdx, asset)) as EncodedAsset
encodedAssets.push(record)
}
Expand Down
22 changes: 10 additions & 12 deletions packages/utils/src/assetData/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ export type FieldToType = {
isPool: 0 | 1 // stored as 0|1 to minimize JSON storage size
}

export type EncodedAsset =
| [
FieldToType['assetIdx'],
FieldToType['name'],
FieldToType['precision'],
FieldToType['color'],
FieldToType['icon'],
FieldToType['symbol'],
FieldToType['relatedAssetKey'],
FieldToType['isPool'],
]
| null // Base assets aren't encoded because we have them hardcoded already
export type EncodedAsset = [
FieldToType['assetIdx'],
FieldToType['name'],
FieldToType['precision'],
FieldToType['color'],
FieldToType['icon'],
FieldToType['symbol'],
FieldToType['relatedAssetKey'],
FieldToType['isPool'],
]

export type EncodedAssetData = {
sortedAssetIds: AssetId[]
Expand Down
36 changes: 18 additions & 18 deletions src/lib/asset-service/service/generatedAssetData.json
Original file line number Diff line number Diff line change
Expand Up @@ -10072,7 +10072,7 @@
"explorer": "https://etherscan.io",
"explorerAddressLink": "https://etherscan.io/address/",
"explorerTxLink": "https://etherscan.io/tx/",
"relatedAssetKey": "eip155:56/bep20:0x16594930d16f3970e1a4317c6016555cb2e7b7fc"
"relatedAssetKey": null
},
"eip155:1/erc20:0x167478921b907422f8e88b43c4af2b8bea278d3a": {
"explorer": "https://etherscan.io",
Expand Down Expand Up @@ -23915,7 +23915,7 @@
"explorer": "https://etherscan.io",
"explorerAddressLink": "https://etherscan.io/address/",
"explorerTxLink": "https://etherscan.io/tx/",
"relatedAssetKey": "eip155:137/erc20:0x7d4d68f18d1be3410ab8d827fb7ebc690f938d2d"
"relatedAssetKey": null
},
"eip155:1/erc20:0x37a2f8701856a78de92dbe35df2200c355eae090": {
"assetId": "eip155:1/erc20:0x37a2f8701856a78de92dbe35df2200c355eae090",
Expand Down Expand Up @@ -46839,7 +46839,7 @@
"explorer": "https://etherscan.io",
"explorerAddressLink": "https://etherscan.io/address/",
"explorerTxLink": "https://etherscan.io/tx/",
"relatedAssetKey": "eip155:42161/erc20:0x6c249b6f6492864d914361308601a7abb32e68f8"
"relatedAssetKey": null
},
"eip155:1/erc20:0x6c280db098db673d30d5b34ec04b6387185d3620": {
"explorer": "https://etherscan.io",
Expand Down Expand Up @@ -95385,7 +95385,7 @@
"explorer": "https://etherscan.io",
"explorerAddressLink": "https://etherscan.io/address/",
"explorerTxLink": "https://etherscan.io/tx/",
"relatedAssetKey": "eip155:42161/erc20:0xde5ed76e7c05ec5e4572cfc88d1acea165109e44"
"relatedAssetKey": null
},
"eip155:1/erc20:0xde654f497a563dd7a121c176a125dd2f11f13a83": {
"assetId": "eip155:1/erc20:0xde654f497a563dd7a121c176a125dd2f11f13a83",
Expand Down Expand Up @@ -125453,7 +125453,7 @@
"explorer": "https://polygonscan.com/",
"explorerAddressLink": "https://polygonscan.com/address/",
"explorerTxLink": "https://polygonscan.com/tx/",
"relatedAssetKey": "eip155:1/erc20:0x9d0b65a76274645b29e4cc41b8f23081fa09f4a3"
"relatedAssetKey": null
},
"eip155:137/erc20:0x7f792db54b0e580cdc755178443f0430cf799aca": {
"assetId": "eip155:137/erc20:0x7f792db54b0e580cdc755178443f0430cf799aca",
Expand Down Expand Up @@ -131787,7 +131787,7 @@
"explorer": "https://polygonscan.com/",
"explorerAddressLink": "https://polygonscan.com/address/",
"explorerTxLink": "https://polygonscan.com/tx/",
"relatedAssetKey": "eip155:42161/erc20:0xde5ed76e7c05ec5e4572cfc88d1acea165109e44"
"relatedAssetKey": null
},
"eip155:137/erc20:0xde990994309bc08e57aca82b1a19170ad84323e8": {
"explorer": "https://polygonscan.com/",
Expand Down Expand Up @@ -148401,7 +148401,7 @@
"explorer": "https://arbiscan.io",
"explorerAddressLink": "https://arbiscan.io/address/",
"explorerTxLink": "https://arbiscan.io/tx/",
"relatedAssetKey": "eip155:1/erc20:0xb4371da53140417cbb3362055374b10d97e420bb"
"relatedAssetKey": null
},
"eip155:42161/erc20:0xf7693c6fd9a7172d537fa75d133d309501cbd657": {
"assetId": "eip155:42161/erc20:0xf7693c6fd9a7172d537fa75d133d309501cbd657",
Expand Down Expand Up @@ -148466,7 +148466,7 @@
"explorer": "https://arbiscan.io",
"explorerAddressLink": "https://arbiscan.io/address/",
"explorerTxLink": "https://arbiscan.io/tx/",
"relatedAssetKey": "eip155:1/erc20:0xdd50c053c096cb04a3e3362e2b622529ec5f2e8a"
"relatedAssetKey": null
},
"eip155:42161/erc20:0xf890360473c12d8015da8dbf7af11da87337a065": {
"explorer": "https://arbiscan.io",
Expand Down Expand Up @@ -154652,7 +154652,7 @@
"explorer": "https://snowtrace.dev",
"explorerAddressLink": "https://snowtrace.dev/address/",
"explorerTxLink": "https://snowtrace.dev/tx/",
"relatedAssetKey": "eip155:42161/erc20:0x6c249b6f6492864d914361308601a7abb32e68f8"
"relatedAssetKey": null
},
"eip155:43114/erc20:0xb44a9b6905af7c801311e8f4e76932ee959c663c": {
"assetId": "eip155:43114/erc20:0xb44a9b6905af7c801311e8f4e76932ee959c663c",
Expand Down Expand Up @@ -168448,7 +168448,7 @@
"explorer": "https://bscscan.com",
"explorerAddressLink": "https://bscscan.com/address/",
"explorerTxLink": "https://bscscan.com/tx/",
"relatedAssetKey": "eip155:1/erc20:0xcc1a8bd438bebc4b2a885a34475bb974f2124317"
"relatedAssetKey": null
},
"eip155:56/bep20:0x366d71ab095735b7dae83ce2b82d5262ef655f10": {
"assetId": "eip155:56/bep20:0x366d71ab095735b7dae83ce2b82d5262ef655f10",
Expand Down Expand Up @@ -169048,7 +169048,7 @@
"explorer": "https://bscscan.com",
"explorerAddressLink": "https://bscscan.com/address/",
"explorerTxLink": "https://bscscan.com/tx/",
"relatedAssetKey": "eip155:1/erc20:0xd9ec3ff1f8be459bb9369b4e79e9ebcf7141c093"
"relatedAssetKey": null
},
"eip155:56/bep20:0x39bff8613defd221b0410ed3d4e5c07512d55f2d": {
"assetId": "eip155:56/bep20:0x39bff8613defd221b0410ed3d4e5c07512d55f2d",
Expand Down Expand Up @@ -183140,7 +183140,7 @@
"explorer": "https://bscscan.com",
"explorerAddressLink": "https://bscscan.com/address/",
"explorerTxLink": "https://bscscan.com/tx/",
"relatedAssetKey": "eip155:1/erc20:0x9d0b65a76274645b29e4cc41b8f23081fa09f4a3"
"relatedAssetKey": null
},
"eip155:56/bep20:0x7bd6fabd64813c48545c9c0e312a0099d9be2540": {
"assetId": "eip155:56/bep20:0x7bd6fabd64813c48545c9c0e312a0099d9be2540",
Expand Down Expand Up @@ -194913,7 +194913,7 @@
"explorer": "https://bscscan.com",
"explorerAddressLink": "https://bscscan.com/address/",
"explorerTxLink": "https://bscscan.com/tx/",
"relatedAssetKey": "eip155:1/erc20:0x9fc8f0ca1668e87294941b7f627e9c15ea06b459"
"relatedAssetKey": null
},
"eip155:56/bep20:0xb350aebaedb1ed3269b0e25d5e593a9bb4b9f9d5": {
"assetId": "eip155:56/bep20:0xb350aebaedb1ed3269b0e25d5e593a9bb4b9f9d5",
Expand Down Expand Up @@ -197920,7 +197920,7 @@
"explorer": "https://bscscan.com",
"explorerAddressLink": "https://bscscan.com/address/",
"explorerTxLink": "https://bscscan.com/tx/",
"relatedAssetKey": "eip155:1/erc20:0xb4371da53140417cbb3362055374b10d97e420bb"
"relatedAssetKey": null
},
"eip155:56/bep20:0xc0eff7749b125444953ef89682201fb8c6a917cd": {
"assetId": "eip155:56/bep20:0xc0eff7749b125444953ef89682201fb8c6a917cd",
Expand Down Expand Up @@ -203560,7 +203560,7 @@
"explorer": "https://bscscan.com",
"explorerAddressLink": "https://bscscan.com/address/",
"explorerTxLink": "https://bscscan.com/tx/",
"relatedAssetKey": "eip155:42161/erc20:0xde5ed76e7c05ec5e4572cfc88d1acea165109e44"
"relatedAssetKey": null
},
"eip155:56/bep20:0xde619a9e0eeeaa9f8cd39522ed788234837f3b26": {
"assetId": "eip155:56/bep20:0xde619a9e0eeeaa9f8cd39522ed788234837f3b26",
Expand Down Expand Up @@ -226086,7 +226086,7 @@
"explorer": "https://basescan.org",
"explorerAddressLink": "https://basescan.org/address/",
"explorerTxLink": "https://basescan.org/tx/",
"relatedAssetKey": "eip155:1/erc20:0xacdf0dba4b9839b96221a8487e9ca660a48212be"
"relatedAssetKey": null
},
"eip155:8453/erc20:0xcd2f22236dd9dfe2356d7c543161d4d260fd9bcb": {
"assetId": "eip155:8453/erc20:0xcd2f22236dd9dfe2356d7c543161d4d260fd9bcb",
Expand Down Expand Up @@ -227220,7 +227220,7 @@
"explorer": "https://basescan.org",
"explorerAddressLink": "https://basescan.org/address/",
"explorerTxLink": "https://basescan.org/tx/",
"relatedAssetKey": "eip155:42161/erc20:0xde5ed76e7c05ec5e4572cfc88d1acea165109e44"
"relatedAssetKey": null
},
"eip155:8453/erc20:0xde66c35e01ed8e619bf092352338ef94f2327337": {
"explorer": "https://basescan.org",
Expand Down Expand Up @@ -234322,7 +234322,7 @@
"explorer": "https://explorer.solana.com",
"explorerAddressLink": "https://explorer.solana.com/address/",
"explorerTxLink": "https://explorer.solana.com/tx/",
"relatedAssetKey": "eip155:1/erc20:0xdd50c053c096cb04a3e3362e2b622529ec5f2e8a"
"relatedAssetKey": null
},
"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:4MshgHvWGvxDs8mtFqPGKC8kX6kuhniWSYPguBb1p1bh": {
"assetId": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/token:4MshgHvWGvxDs8mtFqPGKC8kX6kuhniWSYPguBb1p1bh",
Expand Down

0 comments on commit 4fbd84f

Please sign in to comment.