-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathuseSelectedToken.ts
187 lines (167 loc) · 4.9 KB
/
useSelectedToken.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
import { useCallback } from 'react'
import { utils } from 'ethers'
import useSWRImmutable from 'swr/immutable'
import { Provider } from '@ethersproject/providers'
import {
getChainIdFromProvider,
getProviderForChainId
} from '@/token-bridge-sdk/utils'
import { ERC20BridgeToken, TokenType } from './arbTokenBridge.types'
import {
getL2ERC20Address,
isTokenArbitrumOneNativeUSDC,
isTokenArbitrumSepoliaNativeUSDC,
isTokenMainnetUSDC,
isTokenNativeUSDC,
isTokenSepoliaUSDC
} from '../util/TokenUtils'
import { useNetworks } from './useNetworks'
import { isNetwork } from '../util/networks'
import { CommonAddress } from '../util/CommonAddressUtils'
import { useNetworksRelationship } from './useNetworksRelationship'
import {
useTokensFromLists,
useTokensFromUser
} from '../components/TransferPanel/TokenSearchUtils'
import { useArbQueryParams } from './useArbQueryParams'
const commonUSDC = {
name: 'USD Coin',
type: TokenType.ERC20,
symbol: 'USDC',
decimals: 6,
listIds: new Set<string>()
}
export const useSelectedToken = () => {
const [{ token: tokenFromSearchParams }, setQueryParams] = useArbQueryParams()
const [networks] = useNetworks()
const { childChain, parentChain } = useNetworksRelationship(networks)
const tokensFromLists = useTokensFromLists()
const tokensFromUser = useTokensFromUser()
const { data: usdcToken } = useSWRImmutable(
[
tokenFromSearchParams,
parentChain.id,
childChain.id,
'useSelectedToken_usdc'
],
async ([_tokenAddress, _parentChainId, _childChainId]) => {
if (!_tokenAddress) {
return null
}
if (!isTokenNativeUSDC(_tokenAddress)) {
return null
}
const parentProvider = getProviderForChainId(_parentChainId)
const childProvider = getProviderForChainId(_childChainId)
return getUsdcToken({
tokenAddress: _tokenAddress,
parentProvider,
childProvider
})
}
)
const setSelectedToken = useCallback(
(erc20ParentAddress: string | null) =>
setQueryParams({ token: sanitizeTokenAddress(erc20ParentAddress) }),
[setQueryParams]
)
if (!tokenFromSearchParams) {
return [null, setSelectedToken] as const
}
return [
tokensFromUser[tokenFromSearchParams] ||
tokensFromLists[tokenFromSearchParams] ||
usdcToken ||
null,
setSelectedToken
] as const
}
function sanitizeTokenAddress(tokenAddress: string | null): string | undefined {
if (!tokenAddress) {
return undefined
}
if (utils.isAddress(tokenAddress)) {
return tokenAddress
}
return undefined
}
export async function getUsdcToken({
tokenAddress,
parentProvider,
childProvider
}: {
tokenAddress: string
parentProvider: Provider
childProvider: Provider
}): Promise<ERC20BridgeToken | null> {
const parentChainId = await getChainIdFromProvider(parentProvider)
const childChainId = await getChainIdFromProvider(childProvider)
const {
isEthereumMainnet: isParentChainEthereumMainnet,
isSepolia: isParentChainSepolia,
isArbitrumOne: isParentChainArbitrumOne,
isArbitrumSepolia: isParentChainArbitrumSepolia
} = isNetwork(parentChainId)
// Ethereum Mainnet USDC
if (isTokenMainnetUSDC(tokenAddress) && isParentChainEthereumMainnet) {
return {
...commonUSDC,
address: CommonAddress.Ethereum.USDC,
l2Address: CommonAddress.ArbitrumOne['USDC.e']
}
}
// Ethereum Sepolia USDC
if (isTokenSepoliaUSDC(tokenAddress) && isParentChainSepolia) {
return {
...commonUSDC,
address: CommonAddress.Sepolia.USDC,
l2Address: CommonAddress.ArbitrumSepolia['USDC.e']
}
}
// Arbitrum One USDC when Ethereum is the parent chain
if (
isTokenArbitrumOneNativeUSDC(tokenAddress) &&
isParentChainEthereumMainnet
) {
return {
...commonUSDC,
address: CommonAddress.ArbitrumOne.USDC,
l2Address: CommonAddress.ArbitrumOne.USDC
}
}
// Arbitrum Sepolia USDC when Ethereum is the parent chain
if (isTokenArbitrumSepoliaNativeUSDC(tokenAddress) && isParentChainSepolia) {
return {
...commonUSDC,
address: CommonAddress.ArbitrumSepolia.USDC,
l2Address: CommonAddress.ArbitrumSepolia.USDC
}
}
// Arbitrum USDC with Orbit chains
if (
(isTokenArbitrumOneNativeUSDC(tokenAddress) && isParentChainArbitrumOne) ||
(isTokenArbitrumSepoliaNativeUSDC(tokenAddress) &&
isParentChainArbitrumSepolia)
) {
let childChainUsdcAddress
try {
childChainUsdcAddress = isNetwork(childChainId).isOrbitChain
? (
await getL2ERC20Address({
erc20L1Address: tokenAddress,
l1Provider: parentProvider,
l2Provider: childProvider
})
).toLowerCase()
: undefined
} catch {
// could be never bridged before
}
return {
...commonUSDC,
address: tokenAddress,
l2Address: childChainUsdcAddress
}
}
return null
}