This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdata.ts
225 lines (204 loc) · 7.84 KB
/
data.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
import useSWR, { responseInterface } from 'swr'
import { Token, TokenAmount, Pair, JSBI, ChainId } from '@uniswap/sdk'
import { useWeb3React } from '@web3-react/core'
import { Contract } from '@ethersproject/contracts'
import { parseBytes32String } from '@ethersproject/strings'
import { getAddress } from '@ethersproject/address'
import { Web3Provider } from '@ethersproject/providers'
import IERC20 from '@uniswap/v2-core/build/IERC20.json'
import IUniswapV2Pair from '@uniswap/v2-core/build/IUniswapV2Pair.json'
import { ZERO, ADDRESS_ZERO, ERC20_BYTES32 } from './constants'
import { useContract, useKeepSWRDataLiveAsBlocksArrive } from './hooks'
export enum DataType {
BlockNumber,
ETHBalance,
TokenBalance,
TokenAllowance,
Reserves,
Token,
RemoteTokens,
}
function getBlockNumber(library: Web3Provider): () => Promise<number> {
return async (): Promise<number> => {
return library.getBlockNumber()
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useBlockNumber(): responseInterface<number, any> {
const { library } = useWeb3React()
const shouldFetch = !!library
return useSWR(shouldFetch ? [DataType.BlockNumber] : null, getBlockNumber(library), {
refreshInterval: 10 * 1000,
})
}
function getETHBalance(library: Web3Provider): (chainId: number, address: string) => Promise<TokenAmount> {
return async (chainId: number, address: string): Promise<TokenAmount> => {
const ETH = new Token(chainId, ADDRESS_ZERO, 18)
return library
.getBalance(address)
.then((balance: { toString: () => string }) => new TokenAmount(ETH, balance.toString()))
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useETHBalance(address?: string | null, suspense = false): responseInterface<TokenAmount, any> {
const { chainId, library } = useWeb3React()
const shouldFetch = typeof chainId === 'number' && typeof address === 'string' && !!library
const result = useSWR(shouldFetch ? [chainId, address, DataType.ETHBalance] : null, getETHBalance(library), {
suspense,
})
useKeepSWRDataLiveAsBlocksArrive(result.mutate)
return result
}
function getTokenBalance(contract: Contract, token: Token): (address: string) => Promise<TokenAmount> {
return async (address: string): Promise<TokenAmount> =>
contract
.balanceOf(address)
.then((balance: { toString: () => string }) => new TokenAmount(token, balance.toString()))
}
export function useTokenBalance(
token?: Token,
address?: string | null,
suspense = false
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): responseInterface<TokenAmount, any> {
const contract = useContract(token?.address, IERC20.abi)
const result = useSWR(
typeof address === 'string' && token && contract
? [address, token.chainId, token.address, DataType.TokenBalance]
: null,
getTokenBalance(contract as Contract, token as Token),
{ suspense }
)
useKeepSWRDataLiveAsBlocksArrive(result.mutate)
return result
}
function getTokenAllowance(contract: Contract, token: Token): (owner: string, spender: string) => Promise<TokenAmount> {
return async (owner: string, spender: string): Promise<TokenAmount> =>
contract
.allowance(owner, spender)
.then((balance: { toString: () => string }) => new TokenAmount(token, balance.toString()))
}
export function useTokenAllowance(
token?: Token,
owner?: string | null,
spender?: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): responseInterface<TokenAmount, any> {
const contract = useContract(token?.address, IERC20.abi)
const result = useSWR(
typeof owner === 'string' && typeof spender === 'string' && token && contract
? [owner, spender, token.chainId, token.address, DataType.TokenAllowance]
: null,
getTokenAllowance(contract as Contract, token as Token)
)
useKeepSWRDataLiveAsBlocksArrive(result.mutate)
return result
}
function getReserves(contract: Contract, token0: Token, token1: Token): () => Promise<Pair | null> {
return async (): Promise<Pair | null> =>
contract
.getReserves()
.then(
({ reserve0, reserve1 }: { reserve0: { toString: () => string }; reserve1: { toString: () => string } }) => {
const pair = new Pair(
new TokenAmount(token0, reserve0.toString()),
new TokenAmount(token1, reserve1.toString())
)
return JSBI.equal(pair.reserve0.raw, ZERO) || JSBI.equal(pair.reserve1.raw, ZERO) ? null : pair
}
)
.catch(() => null)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useReserves(tokenA?: Token, tokenB?: Token): responseInterface<Pair | null, any> {
const invalid = !!tokenA && !!tokenB && tokenA.equals(tokenB)
const [token0, token1] =
!!tokenA && !!tokenB && !invalid ? (tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA]) : []
const pairAddress = !!token0 && !!token1 ? Pair.getAddress(token0, token1) : undefined
const contract = useContract(pairAddress, IUniswapV2Pair.abi)
const result = useSWR(
token0 && pairAddress && contract && token1 ? [token0.chainId, pairAddress, DataType.Reserves] : null,
getReserves(contract as Contract, token0 as Token, token1 as Token)
)
useKeepSWRDataLiveAsBlocksArrive(result.mutate)
return result
}
function getOnchainToken(
contract: Contract,
contractBytes32: Contract
): (chainId: number, address: string) => Promise<Token | null> {
return async (chainId: number, address: string): Promise<Token | null> => {
const [decimals, symbol, name] = await Promise.all([
contract.decimals().catch(() => null),
contract.symbol().catch(() =>
contractBytes32
.symbol()
.then(parseBytes32String)
.catch(() => 'UNKNOWN')
),
contract.name().catch(() =>
contractBytes32
.name()
.then(parseBytes32String)
.catch(() => 'Unknown')
),
])
return decimals === null ? null : new Token(chainId, address, decimals, symbol, name)
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useOnchainToken(address?: string, suspense = false): responseInterface<Token | null, any> {
const { chainId } = useWeb3React()
const contract = useContract(address, IERC20.abi)
const contractBytes32 = useContract(address, ERC20_BYTES32)
return useSWR(
typeof chainId === 'number' && typeof address === 'string' && contract && contractBytes32
? [chainId, address, DataType.Token]
: null,
getOnchainToken(contract as Contract, contractBytes32 as Contract),
{
dedupingInterval: 60 * 1000,
refreshInterval: 60 * 1000,
suspense,
}
)
}
interface RemoteToken {
address: string
symbol: string
name: string
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function getRemoteTokens(searchQuery: string): Promise<RemoteToken[]> {
const { request } = await import('graphql-request')
return request(
'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2',
`
query getRemoteTokens($searchQuery: String!) {
tokens(where: { symbol_contains: $searchQuery }) {
id
symbol
name
}
}`,
{ searchQuery }
).then((result) =>
result.tokens.map(
(token: { id: string; symbol: string; name: string }): RemoteToken => ({
address: getAddress(token.id),
symbol: token.symbol ?? 'UNKNOWN',
name: token.name ?? 'Unknown',
})
)
)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useRemoteTokens(query = '', suspense = false): responseInterface<RemoteToken[], any> {
const { chainId } = useWeb3React()
const shouldFetch = chainId === ChainId.MAINNET && query.length > 0
return useSWR(shouldFetch ? [query, DataType.RemoteTokens] : null, getRemoteTokens, {
dedupingInterval: 60 * 5 * 1000,
refreshInterval: 60 * 5 * 1000,
suspense,
})
}