forked from IndexCoop/index-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.ts
227 lines (210 loc) · 6.23 KB
/
tokens.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { IndexCoopInverseEthereumIndex } from '@indexcoop/flash-mint-sdk'
import { ARBITRUM, MAINNET, OPTIMISM, POLYGON } from '@/constants/chains'
import {
currencies,
indicesTokenList,
indicesTokenListArbitrum,
} from '@/constants/tokenlists'
import {
BedIndex,
Bitcoin2xFlexibleLeverageIndex,
CoinDeskEthTrendIndex,
DAI,
DiversifiedStakedETHIndex,
ETH,
Ethereum2xFlexibleLeverageIndex,
GitcoinStakedETHIndex,
GUSD,
HighYieldETHIndex,
ic21,
icETHIndex,
IndexCoopBitcoin2xIndex,
IndexCoopBitcoin3xIndex,
IndexCoopEthereum2xIndex,
IndexCoopEthereum3xIndex,
IndexCoopInverseBitcoinIndex,
IndexToken,
LeveragedRethStakingYield,
MATIC,
RealWorldAssetIndex,
RETH,
SETH2,
STETH,
Token,
USDC,
USDT,
WBTC,
WETH,
WSTETH,
} from '@/constants/tokens'
import { isSameAddress } from '.'
export function getAddressForToken(
token: Token,
chainId: number | undefined,
): string | undefined {
if (token.symbol === IndexToken.symbol) return token.address
switch (chainId) {
case ARBITRUM.chainId:
return token.arbitrumAddress
case MAINNET.chainId:
return token.address
case OPTIMISM.chainId:
return token.optimismAddress
case POLYGON.chainId:
return token.polygonAddress
default:
return undefined
}
}
/**
* Gets the list of currency tokens for the selected chain.
* @returns Token[] list of tokens
*/
export function getCurrencyTokens(chainId: number | undefined): Token[] {
switch (chainId) {
case MAINNET.chainId:
return currencies
default:
return []
}
}
/**
* Gets the supported currency tokens for the given index.
* @returns Token[] list of supported currency tokens
*/
export function getCurrencyTokensForIndex(
index: Token,
chainId: number,
): Token[] {
if (chainId === ARBITRUM.chainId) {
return [ETH, WETH, WBTC, USDC, USDT]
}
if (index.symbol === CoinDeskEthTrendIndex.symbol)
return [ETH, WETH, USDC, DAI, GUSD]
if (index.symbol === ic21.symbol) return [WETH]
if (index.symbol === icETHIndex.symbol) return [ETH, WETH, STETH]
if (
index.symbol === DiversifiedStakedETHIndex.symbol ||
index.symbol === GitcoinStakedETHIndex.symbol
)
return [ETH, WETH, USDC, GUSD, RETH, STETH, SETH2, WSTETH]
if (index.symbol === LeveragedRethStakingYield.symbol)
return [ETH, WETH, USDC, GUSD, RETH]
const currencyTokens = getCurrencyTokens(chainId)
if (index.symbol === Bitcoin2xFlexibleLeverageIndex.symbol) {
return [IndexCoopBitcoin2xIndex, ...currencyTokens]
}
if (index.symbol === Ethereum2xFlexibleLeverageIndex.symbol) {
return [IndexCoopEthereum2xIndex, ...currencyTokens]
}
return currencyTokens
}
export function getDefaultIndex(chainId: number = 1): Token {
if (chainId === ARBITRUM.chainId) return indicesTokenListArbitrum[0]
return indicesTokenList[0]
}
export function getNativeToken(chainId: number | undefined): Token | null {
switch (chainId) {
case ARBITRUM.chainId:
return ETH
case MAINNET.chainId:
return ETH
case OPTIMISM.chainId:
return ETH
case POLYGON.chainId:
return MATIC
default:
return null
}
}
export function getTokenBySymbol(symbol: string): Token | null {
const indexToken = indicesTokenList.find(
(index) => index.symbol.toLowerCase() === symbol.toLowerCase(),
)
if (indexToken) {
return indexToken
}
const currencyToken = currencies.find(
(token) => token.symbol.toLowerCase() === symbol.toLowerCase(),
)
return currencyToken ?? null
}
export function isAvailableForFlashMint(token: Token): boolean {
switch (token.symbol) {
case ic21.symbol:
case IndexToken.symbol:
return false
default:
return true
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function isAvailableForIssuance(
inputToken: Token,
outputToken: Token,
): boolean {
return (
inputToken.symbol === BedIndex.symbol ||
inputToken.symbol === GitcoinStakedETHIndex.symbol ||
inputToken.symbol === LeveragedRethStakingYield.symbol ||
inputToken.symbol === RealWorldAssetIndex.symbol ||
outputToken.symbol === RealWorldAssetIndex.symbol
)
}
export function isAvailableForRedemption(
inputToken: Token,
outputToken: Token,
): boolean {
return (
(inputToken.symbol === Bitcoin2xFlexibleLeverageIndex.symbol &&
outputToken.symbol === IndexCoopBitcoin2xIndex.symbol) ||
(inputToken.symbol === Ethereum2xFlexibleLeverageIndex.symbol &&
outputToken.symbol === IndexCoopEthereum2xIndex.symbol)
)
}
export function isAvailableForSwap(token: Token): boolean {
switch (token.symbol) {
case CoinDeskEthTrendIndex.symbol:
case IndexCoopBitcoin2xIndex.symbol:
case LeveragedRethStakingYield.symbol:
return false
default:
return true
}
}
export function isIndexToken(token: Token): boolean {
if (token.symbol === IndexToken.symbol) return false
if (token.symbol === HighYieldETHIndex.symbol) return true
if (token.symbol === IndexCoopBitcoin2xIndex.symbol) return true
if (token.symbol === IndexCoopBitcoin3xIndex.symbol) return true
if (token.symbol === IndexCoopEthereum2xIndex.symbol) return true
if (token.symbol === IndexCoopEthereum3xIndex.symbol) return true
if (token.symbol === IndexCoopInverseBitcoinIndex.symbol) return true
if (token.symbol === IndexCoopInverseEthereumIndex.symbol) return true
if (token.symbol === RealWorldAssetIndex.symbol) return true
return indicesTokenList.some((index) =>
isSameAddress(index.address!, token.address!),
)
}
export function isLeveragedToken(token: Token): boolean {
if (token === Bitcoin2xFlexibleLeverageIndex) return true
if (token === Ethereum2xFlexibleLeverageIndex) return true
if (token === icETHIndex) return true
return false
}
export const isNativeCurrency = (token: Token, chainId: number): boolean => {
const nativeCurrency = getNativeToken(chainId)
if (!nativeCurrency) return false
return token.symbol === nativeCurrency.symbol
}
export function isPerpToken(token: Token): boolean {
return token.isPerp ? true : false
}
export function isTokenPairTradable(
requiresProtection: boolean,
inputToken: Token,
outputToken: Token,
): boolean {
if (!requiresProtection) return true
return !inputToken.isDangerous && !outputToken.isDangerous
}