Skip to content

Commit

Permalink
chore: ibc tokens script
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-moore committed Sep 26, 2023
1 parent 6590820 commit 28341c7
Show file tree
Hide file tree
Showing 6 changed files with 904 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/sdk-ts/src/client/wasm/nameservice/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const nameToNode = (name: string) => {
if (!name) {
return []
}

const hash = nameHash(name)

return Array.from(Buffer.from(hash.slice(2), 'hex'))
}

Expand Down
5 changes: 3 additions & 2 deletions packages/sdk-ui-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"scripts": {
"postinstall": "link-module-alias",
"build": "tsc --build tsconfig.build.json && tsc --build tsconfig.build.esm.json && yarn build:post && link-module-alias && yarn copy-files",
"build": "tsc --build tsconfig.build.json && tsc --build tsconfig.build.esm.json && yarn build:post && link-module-alias && yarn copy-files && yarn get-ibc-tokens",
"build:watch": "tsc --build -w tsconfig.build.json && tsc -w --build tsconfig.build.esm.json && yarn build:post && link-module-alias",
"build:post": "shx cp ../../etc/stub/package.json.stub dist/cjs/package.json && shx cp ../../etc/stub/package.esm.json.stub dist/esm/package.json",
"clean": "tsc --build tsconfig.build.json --clean && tsc --build tsconfig.build.esm.json --clean && shx rm -rf coverage *.log junit.xml dist && jest --clearCache && shx mkdir -p dist",
Expand All @@ -31,7 +31,8 @@
"dev": "ts-node-dev -r tsconfig-paths/register src/index.ts",
"start": "node dist/index.js",
"validators-logo": "ts-node-dev --ignore-watch=validators-logo ./src/validators-logo && yarn copy-files",
"validators-logo:update-all": "ts-node-dev --ignore-watch=validators-logo ./src/validators-logo --update:all && yarn copy-files"
"validators-logo:update-all": "ts-node-dev --ignore-watch=validators-logo ./src/validators-logo --update:all && yarn copy-files",
"get-ibc-tokens": "ts-node-dev ./src/services/ibc/tokens.ts"
},
"dependencies": {
"@injectivelabs/contracts": "^1.14.1-beta.0",
Expand Down
27 changes: 25 additions & 2 deletions packages/sdk-ui-ts/src/denom/DenomClientAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { getTokenFromInsuranceFund } from '../utils'
import { IbcApplicationsTransferV1Transfer } from '@injectivelabs/core-proto-ts'
import { ErrorType, GeneralException } from '@injectivelabs/exceptions'
import { awaitForAll } from '@injectivelabs/utils'
// @ts-ignore
import ibcTokenMetadata from '../services/ibc/ibcTokenMetadata.json'

const IGNORED_DENOMS = ['peggy0xB855dBC314C39BFa2583567E02a40CBB246CF82B']

Expand Down Expand Up @@ -57,6 +59,8 @@ export class DenomClientAsync {
IbcApplicationsTransferV1Transfer.DenomTrace
> = {}

private cachedIbcTokens: Token[] = []

constructor(
network: Network = Network.Mainnet,
options: { endpoints?: NetworkEndpoints; alchemyRpcUrl?: string },
Expand Down Expand Up @@ -253,10 +257,19 @@ export class DenomClientAsync {
await this.fetchAndCacheDenomTraces()
}

if (this.cachedIbcTokens.length === 0) {
await this.fetchAndCacheIbcTokens()
}

const cachedDenomTrace = this.cachedDenomTraces[hash]
const cachedIbcToken = this.cachedIbcTokens.find(
(token) => token?.denom === denom,
)

if (cachedDenomTrace) {
const token = this.denomClient.getDenomToken(cachedDenomTrace.baseDenom)
const token =
this.denomClient.getDenomToken(cachedDenomTrace.baseDenom) ||
cachedIbcToken

if (!token) {
return undefined
Expand All @@ -276,7 +289,8 @@ export class DenomClientAsync {
try {
const denomTrace = await this.chainIbcApi.fetchDenomTrace(hash)

const token = this.denomClient.getDenomToken(denomTrace.baseDenom)
const token =
this.denomClient.getDenomToken(denomTrace.baseDenom) || cachedIbcToken

if (!token) {
return undefined
Expand Down Expand Up @@ -322,9 +336,18 @@ export class DenomClientAsync {
)
}

private async fetchAndCacheIbcTokens() {
if (ibcTokenMetadata?.length === 0) {
return
}

this.cachedIbcTokens = ibcTokenMetadata as Token[]
}

public async preloadMetadata() {
await this.getFactoryDenomMetadata('')
await this.getInsuranceFund('')
await this.fetchAndCacheDenomTraces()
await this.fetchAndCacheIbcTokens()
}
}
Loading

0 comments on commit 28341c7

Please sign in to comment.