forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.ts
122 lines (111 loc) · 3.74 KB
/
assets.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { fromBech32 } from '@cosmjs/encoding'
import { assets } from 'chain-registry'
import { GenericToken, TokenType } from '@dao-dao/types'
import { getChainForChainId } from './chain'
import { concatAddressStartEnd } from './conversion'
import { getFallbackImage } from './getFallbackImage'
// Cache once loaded.
const chainAssetsMap: Record<
string,
| (GenericToken & {
id: string
description?: string
denomAliases?: string[]
})[]
| undefined
> = {}
export const getChainAssets = (chainId: string) => {
if (!chainAssetsMap[chainId]) {
chainAssetsMap[chainId] =
assets
.find(
({ chain_name }) =>
chain_name === getChainForChainId(chainId).chain_name
)
?.assets.map(
({
base,
symbol,
logo_URIs: { png, svg, jpeg } = {},
name,
display,
denom_units,
}) => ({
chainId,
id: display,
type: TokenType.Native,
denomOrAddress: base,
denomAliases:
denom_units.find(({ denom }) => denom === base)?.aliases ?? [],
symbol,
decimals:
denom_units.find(({ denom }) => denom === display)?.exponent ??
denom_units.find(({ exponent }) => exponent > 0)?.exponent ??
denom_units[0]?.exponent ??
0,
imageUrl: svg || png || jpeg || getFallbackImage(base),
description: symbol === name ? undefined : name,
// This will be wrong when this is an IBC asset.
source: {
chainId,
type: TokenType.Native,
denomOrAddress: base,
},
})
)
.sort((a, b) => a.symbol.localeCompare(b.symbol)) ?? []
}
return chainAssetsMap[chainId]!
}
/**
* Valid native denom if it follows cosmos SDK validation logic. Specifically,
* the regex string `[a-zA-Z][a-zA-Z0-9/:._-]{2,127}`.
*
* <https://github.com/cosmos/cosmos-sdk/blob/7728516abfab950dc7a9120caad4870f1f962df5/types/coin.go#L865-L867>
*/
export const isValidNativeTokenDenom = (denom: string) =>
/^[a-zA-Z][a-zA-Z0-9/:._-]{2,127}$/.test(denom)
export const getNativeIbcUsdc = (chainId: string) =>
getChainAssets(chainId).find(
({ id, symbol }) => id === 'usdc' && symbol === 'USDC'
) || getChainAssets(chainId).find(({ id }) => id === 'usdc')
// Returns true if this denom is the IBC denom for USDC on the current chain.
export const isNativeIbcUsdc = (chainId: string, ibcDenom: string): boolean =>
ibcDenom === getNativeIbcUsdc(chainId)?.denomOrAddress
// Processes token symbol and converts into readable format (cut out middle and
// add ellipses) if long IBC or token factory string. Used in `TokenCard`,
// `TokenLine`, and `TokenDepositModal`.
export const shortenTokenSymbol = (
symbol: string
): {
isShortened: boolean
tokenSymbol: string
originalTokenSymbol: string
} => {
const isIbc = symbol.toLowerCase().startsWith('ibc')
const isFactory = symbol.toLowerCase().startsWith('factory')
// Get the bech32 prefix length of the factory token's creator address.
let factoryCreatorAddressPrefixLength = 5
if (isFactory) {
try {
factoryCreatorAddressPrefixLength = fromBech32(symbol.split('/')[1])
.prefix.length
} catch {}
}
// Truncate denominations to prevent overflow.
const tokenSymbol = isIbc
? concatAddressStartEnd(symbol, 7, 3)
: isFactory
? // Truncate address in middle.
`factory/${concatAddressStartEnd(
symbol.split('/')[1],
factoryCreatorAddressPrefixLength + 3,
3
)}/${symbol.split('/')[2]}`
: symbol
return {
isShortened: isIbc || isFactory,
tokenSymbol,
originalTokenSymbol: symbol,
}
}