Skip to content

Commit

Permalink
chore: add tokenByAddress map on TokenFactoryStatic
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRalee committed Dec 9, 2024
1 parent ffea9e6 commit 66b592a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/sdk-ts/src/service/TokenFactoryStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class TokenFactoryStatic {
public registry: TokenStatic[]
public tokensByDenom: Record<string, TokenStatic>
public tokensBySymbol: Record<string, TokenStatic[]>
public tokensByAddress: Record<string, TokenStatic[]>

constructor(registry: TokenStatic[]) {
this.registry = registry
Expand All @@ -27,6 +28,16 @@ export class TokenFactoryStatic {

return { ...list, [symbol]: [...(list[symbol] || []), token] }
}, {} as Record<string, TokenStatic[]>)

this.tokensByAddress = registry.reduce((list, token) => {
const address = token.address.toLowerCase()

if (!address) {
return list
}

return { ...list, [address]: [...(list[address] || []), token] }
}, {} as Record<string, TokenStatic[]>)
}

toToken(denom: string): TokenStatic | undefined {
Expand Down Expand Up @@ -70,9 +81,11 @@ export class TokenFactoryStatic {
return token || sortedTokens[0]
}

getMetaByDenomOrAddress(denom: string): TokenStatic | undefined {
const formattedDenom = denom.toLowerCase()
getMetaByDenomOrAddress(denomOrAddress: string): TokenStatic | undefined {
const formattedDenom = denomOrAddress.toLowerCase()

return this.tokensByDenom[formattedDenom]
return (
this.tokensByDenom[formattedDenom] || this.tokensByAddress[formattedDenom]
)
}
}

0 comments on commit 66b592a

Please sign in to comment.