Skip to content

Commit

Permalink
refactor: token metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Jan 12, 2024
1 parent 0b3ac4e commit 1e1418d
Show file tree
Hide file tree
Showing 18 changed files with 1,155 additions and 1,819 deletions.
12 changes: 6 additions & 6 deletions packages/token-metadata/src/TokenFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getTokensBySymbolForDevnet2,
getTokensBySymbolForTestnet,
} from './tokens/network'
import { Token, TokenMeta } from './types'
import { Token, TokenMetaBase } from './types'
import tokensBySymbol from './tokens/tokens'
import {
getTokenFromMeta,
Expand All @@ -25,7 +25,7 @@ export class TokenFactory {

static make(
network: Network = Network.Mainnet,
registry: Record<string, TokenMeta> = {},
registry: Record<string, TokenMetaBase> = {},
): TokenFactory {
if (isTestnet(network)) {
return new TokenFactory(
Expand Down Expand Up @@ -118,7 +118,7 @@ export class TokenFactory {
}
}

getPeggyDenomTokenMeta(denom: string): TokenMeta | undefined {
getPeggyDenomTokenMeta(denom: string): TokenMetaBase | undefined {
const address = denom.startsWith('peggy')
? denom.replace('peggy', '')
: denom
Expand All @@ -138,7 +138,7 @@ export class TokenFactory {
return this.tokenMetaUtils.getMetaByAddress(address)
}

getCw20DenomTokenMeta(address: string): TokenMeta | undefined {
getCw20DenomTokenMeta(address: string): TokenMetaBase | undefined {
if (!isCw20ContractAddress(address)) {
throw new GeneralException(
new Error(`The address ${address} is not a valid CW20 address`),
Expand All @@ -148,11 +148,11 @@ export class TokenFactory {
return this.tokenMetaUtils.getMetaByAddress(address)
}

getIbcDenomTokenMeta(hash: string): TokenMeta | undefined {
getIbcDenomTokenMeta(hash: string): TokenMetaBase | undefined {
return this.tokenMetaUtils.getMetaByHash(hash)
}

getFactoryDenomTokenMeta(denom: string): TokenMeta | undefined {
getFactoryDenomTokenMeta(denom: string): TokenMetaBase | undefined {
const [address] = denom.split('/').reverse()

if (!address) {
Expand Down
48 changes: 21 additions & 27 deletions packages/token-metadata/src/TokenMetaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import {
import { getMappedTokensByName } from './tokens/mappings/mapByName'
import { getMappedTokensByHash } from './tokens/mappings/mapByHash'
import { getMappedTokensBySymbol } from './tokens/mappings/mapBySymbol'
import { TokenMeta, TokenVerification, TokenType } from './types'
import { TokenMetaBase, TokenVerification, TokenType } from './types'

export class TokenMetaUtils {
protected tokens: Record<string, TokenMeta>
protected tokens: Record<string, TokenMetaBase>

protected tokensByErc20Address: Record<string, TokenMeta>
protected tokensByErc20Address: Record<string, TokenMetaBase>

protected tokensByCw20Address: Record<string, TokenMeta>
protected tokensByCw20Address: Record<string, TokenMetaBase>

protected tokensByHash: Record<string, TokenMeta>
protected tokensByHash: Record<string, TokenMetaBase>

protected tokensByName: Record<string, TokenMeta>
protected tokensByName: Record<string, TokenMetaBase>

constructor(tokens: Record<string, TokenMeta>) {
constructor(tokens: Record<string, TokenMetaBase>) {
this.tokens = getMappedTokensBySymbol(tokens)
this.tokensByErc20Address = getMappedTokensByErc20Address(this.tokens)
this.tokensByCw20Address = getMappedTokensByCw20Address(this.tokens)
Expand All @@ -32,7 +32,7 @@ export class TokenMetaUtils {
* - BaseDenom based on the ibc hash
* - Variation of a symbol for multiple versions of the same token (ex: USDC, USDCet, USDCso)
*/
getMetaBySymbol(symbol: string): TokenMeta | undefined {
getMetaBySymbol(symbol: string): TokenMetaBase | undefined {
const { tokens: tokensBySymbol } = this
const tokenSymbol = symbol.toUpperCase() as keyof typeof tokensBySymbol

Expand All @@ -48,7 +48,7 @@ export class TokenMetaUtils {
}
}

getMetaByFactory(denom: string): TokenMeta | undefined {
getMetaByFactory(denom: string): TokenMetaBase | undefined {
const [symbolOrName, creatorAddress] = denom.split('/').reverse()
const tokenMeta =
this.getMetaByName(symbolOrName) || this.getMetaBySymbol(symbolOrName)
Expand All @@ -57,21 +57,15 @@ export class TokenMetaUtils {
return
}

if (tokenMeta.tokenFactories) {
const tokenFactory = tokenMeta.tokenFactories.find(
(tokenFactory) => tokenFactory.creator === creatorAddress,
)

if (tokenFactory) {
return {
...tokenMeta,
tokenType: TokenType.TokenFactory,
tokenVerification: TokenVerification.Verified,
}
}
if (!tokenMeta.tokenFactories) {
return
}

if (tokenMeta.tokenFactory?.creator !== creatorAddress) {
const tokenFactory = tokenMeta.tokenFactories.find(
(tokenFactory) => tokenFactory.creator === creatorAddress,
)

if (!tokenFactory) {
return
}

Expand All @@ -82,13 +76,13 @@ export class TokenMetaUtils {
}
}

getMetaByAddress(address: string): TokenMeta | undefined {
getMetaByAddress(address: string): TokenMetaBase | undefined {
return address.startsWith('0x')
? this.getMetaByErc20Address(address)
: this.getMetaByCw20Address(address)
}

getMetaByCw20Address(address: string): TokenMeta | undefined {
getMetaByCw20Address(address: string): TokenMetaBase | undefined {
const { tokensByCw20Address } = this
const contractAddress =
address.toLowerCase() as keyof typeof tokensByCw20Address
Expand All @@ -112,7 +106,7 @@ export class TokenMetaUtils {
: undefined
}

getMetaByErc20Address(address: string): TokenMeta | undefined {
getMetaByErc20Address(address: string): TokenMetaBase | undefined {
const { tokensByErc20Address } = this
const contractAddress =
address.toLowerCase() as keyof typeof tokensByErc20Address
Expand Down Expand Up @@ -170,7 +164,7 @@ export class TokenMetaUtils {
}
}

getMetaByHash(hash: string): TokenMeta | undefined {
getMetaByHash(hash: string): TokenMetaBase | undefined {
const { tokensByHash } = this
const ibcHash = hash
.toUpperCase()
Expand All @@ -191,7 +185,7 @@ export class TokenMetaUtils {
: undefined
}

getMetaByName(name: string): TokenMeta | undefined {
getMetaByName(name: string): TokenMetaBase | undefined {
const { tokensByName } = this
const tokenName = name.toLowerCase() as keyof typeof tokensByName

Expand Down
1 change: 0 additions & 1 deletion packages/token-metadata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { TokenMetaUtilsFactory } from './TokenMetaUtilsFactory'
export * from './ibc'
export * from './types'
export * from './utils'
export * from './tokens/canonical'

export const tokenMetaUtils = TokenMetaUtilsFactory.make()
export const tokenFactory = TokenFactory.make()
Expand Down
23 changes: 8 additions & 15 deletions packages/token-metadata/src/tokens/mappings/mapByAddress.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TokenMeta } from '../../types'
import { TokenMetaBase } from '../../types'

export const getMappedTokensByErc20Address = (
tokens: Record<string, TokenMeta>,
tokens: Record<string, TokenMetaBase>,
) =>
(Object.keys(tokens) as Array<keyof typeof tokens>)
.filter((token) => !!(tokens[token].erc20 || tokens[token].evm))
Expand All @@ -25,34 +25,27 @@ export const getMappedTokensByErc20Address = (
}

return result
}, {}) as Record<string, TokenMeta>
}, {}) as Record<string, TokenMetaBase>

export const getMappedTokensByCw20Address = (
tokens: Record<string, TokenMeta>,
tokens: Record<string, TokenMetaBase>,
) =>
(Object.keys(tokens) as Array<keyof typeof tokens>)
.filter((token) => tokens[token].cw20 || tokens[token].cw20s)
.filter((token) => tokens[token].cw20s)
.reduce((result, token) => {
if (!tokens[token].cw20 && !tokens[token].cw20s) {
if (!tokens[token].cw20s) {
return result
}

const tokenMeta = tokens[token]

if (tokenMeta.cw20) {
return {
...result,
[tokenMeta.cw20.address]: tokens[token],
}
}

if (tokenMeta.cw20s) {
const cw20Maps = tokenMeta.cw20s.reduce(
(result, cw20) => ({
...result,
[cw20.address]: tokens[token],
}),
{} as Record<string, TokenMeta>,
{} as Record<string, TokenMetaBase>,
)

return {
Expand All @@ -62,4 +55,4 @@ export const getMappedTokensByCw20Address = (
}

return result
}, {}) as Record<string, TokenMeta>
}, {}) as Record<string, TokenMetaBase>
6 changes: 3 additions & 3 deletions packages/token-metadata/src/tokens/mappings/mapByHash.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TokenMeta } from '../../types'
import { TokenMetaBase } from '../../types'

export const getMappedTokensByHash = (tokens: Record<string, TokenMeta>) =>
export const getMappedTokensByHash = (tokens: Record<string, TokenMetaBase>) =>
(Object.keys(tokens) as Array<keyof typeof tokens>).reduce(
(result, token) => {
if (!tokens[token].ibc) {
Expand All @@ -13,4 +13,4 @@ export const getMappedTokensByHash = (tokens: Record<string, TokenMeta>) =>
}
},
{},
) as Record<string, TokenMeta>
) as Record<string, TokenMetaBase>
6 changes: 3 additions & 3 deletions packages/token-metadata/src/tokens/mappings/mapByName.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TokenMeta } from '../../types'
import { TokenMetaBase } from '../../types'

export const getMappedTokensByName = (tokens: Record<string, TokenMeta>) =>
export const getMappedTokensByName = (tokens: Record<string, TokenMetaBase>) =>
(Object.keys(tokens) as Array<keyof typeof tokens>).reduce(
(result, token) => ({
...result,
[tokens[token].name!.toLowerCase()]: tokens[token],
}),
{},
) as Record<string, TokenMeta>
) as Record<string, TokenMetaBase>
17 changes: 10 additions & 7 deletions packages/token-metadata/src/tokens/mappings/mapBySymbol.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { TokenMeta } from '../../types'
import { TokenMetaBase } from '../../types'

export const getMappedTokensBySymbol = (tokens: Record<string, TokenMeta>) =>
export const getMappedTokensBySymbol = (
tokens: Record<string, TokenMetaBase>,
) =>
(Object.keys(tokens) as Array<keyof typeof tokens>).reduce(
(result, token) => {
const tokenMeta = tokens[token]
const symbolKey = token.toUpperCase()
const symbol = tokenMeta.symbol.toUpperCase()
const symbol = tokenMeta.symbol?.toUpperCase()
const symbolDiffs = symbol !== symbolKey

let ibcResults = {}
Expand All @@ -30,12 +32,11 @@ export const getMappedTokensBySymbol = (tokens: Record<string, TokenMeta>) =>
...result,
[cw20.symbol.toUpperCase()]: tokenMeta,
}),
{} as Record<string, TokenMeta>,
{} as Record<string, TokenMetaBase>,
)

cw20sResults = {
...cw20Maps,
[symbol.toUpperCase()]: tokenMeta,
}
}

Expand All @@ -59,11 +60,13 @@ export const getMappedTokensBySymbol = (tokens: Record<string, TokenMeta>) =>
...cw20Results,
...cw20sResults,
...erc20Results,
[symbol.toUpperCase()]: tokenMeta,
...(symbol && {
[symbol.toUpperCase()]: tokenMeta,
}),
...(symbolDiffs && {
[symbolKey.toUpperCase()]: tokenMeta,
}),
}
},
{},
) as Record<string, TokenMeta>
) as Record<string, TokenMetaBase>
Loading

0 comments on commit 1e1418d

Please sign in to comment.