-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathTokens.js
149 lines (130 loc) · 4.49 KB
/
Tokens.js
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
import React, { createContext, useContext, useReducer, useMemo, useCallback, useEffect } from 'react'
import { useWeb3React } from '../hooks'
import {
isAddress,
getTokenName,
getTokenSymbol,
getTokenDecimals,
getTokenExchangeAddressFromFactory,
safeAccess
} from '../utils'
import { stat } from 'fs'
const NAME = 'name'
const SYMBOL = 'symbol'
const DECIMALS = 'decimals'
const EXCHANGE_ADDRESS = 'exchangeAddress'
const UPDATE = 'UPDATE'
const ETH = {
ETH: {
[NAME]: 'Ethereum',
[SYMBOL]: 'ETH',
[DECIMALS]: 18,
[EXCHANGE_ADDRESS]: null
}
}
export const INITIAL_TOKENS_CONTEXT = {
8995: {
'0xf8c1459e7cf95811f6748a34c9e79ab1c978664a': {
[NAME]: 'DaiStable Coin',
[SYMBOL]: 'DAI',
[DECIMALS]: 8,
[EXCHANGE_ADDRESS]: '0x389212ea4899D5e03F18C50556288EAE978FA965'
},
'0xc1D19fB05Cd9CbFeF48985aBf074aFB02681Bf4a': {
[NAME]: 'Mana',
[SYMBOL]: 'MANA',
[DECIMALS]: 8,
[EXCHANGE_ADDRESS]: '0x9b22aeAA356fBa6bF2a782287DBeee426D2F077E'
},
// Using DAI EXCHANGE IN CDAI SINCE IT'S MANDATORY
'0xf190929858f5f6dC55A03B40C25545fE9c79CF8c': {
[NAME]: 'Cdai',
[SYMBOL]: 'CDAI',
[DECIMALS]: 8,
[EXCHANGE_ADDRESS]: '0x389212ea4899D5e03F18C50556288EAE978FA965'
},
// Using DAI EXCHANGE IN LAND SINCE IT'S MANDATORY THE EXCHANGE ADDRESS IS NEVER USED IN THIS CASE
'0xd1981d74abae2ec51e045242d8bb86a9daaa76b5': {
[NAME]: 'Land Token',
[SYMBOL]: 'LAND',
[DECIMALS]: 8,
[EXCHANGE_ADDRESS]: '0x389212ea4899D5e03F18C50556288EAE978FA965'
}
}
}
const TokensContext = createContext()
function useTokensContext() {
return useContext(TokensContext)
}
function reducer(state, { type, payload }) {
switch (type) {
case UPDATE: {
const { networkId, tokenAddress, name, symbol, decimals, exchangeAddress } = payload
return {
...state,
[networkId]: {
...(safeAccess(state, [networkId]) || {}),
[tokenAddress]: {
[NAME]: name,
[SYMBOL]: symbol,
[DECIMALS]: decimals,
[EXCHANGE_ADDRESS]: exchangeAddress
}
}
}
}
default: {
throw Error(`Unexpected action type in TokensContext reducer: '${type}'.`)
}
}
}
export default function Provider({ children }) {
const [state, dispatch] = useReducer(reducer, INITIAL_TOKENS_CONTEXT)
const update = useCallback((networkId, tokenAddress, name, symbol, decimals, exchangeAddress) => {
dispatch({ type: UPDATE, payload: { networkId, tokenAddress, name, symbol, decimals, exchangeAddress } })
}, [])
return (
<TokensContext.Provider value={useMemo(() => [state, { update }], [state, update])}>
{children}
</TokensContext.Provider>
)
}
export function useTokenDetails(tokenAddress) {
const { library, chainId } = useWeb3React()
const [state, { update }] = useTokensContext()
const allTokensInNetwork = { ...ETH, ...(safeAccess(state, [chainId]) || {}) }
const { [NAME]: name, [SYMBOL]: symbol, [DECIMALS]: decimals, [EXCHANGE_ADDRESS]: exchangeAddress } =
safeAccess(allTokensInNetwork, [tokenAddress]) || {}
useEffect(() => {
if (
isAddress(tokenAddress) &&
(name === undefined || symbol === undefined || decimals === undefined || exchangeAddress === undefined) &&
(chainId || chainId === 0) &&
library
) {
let stale = false
const namePromise = getTokenName(tokenAddress, library).catch(() => null)
const symbolPromise = getTokenSymbol(tokenAddress, library).catch(() => null)
const decimalsPromise = getTokenDecimals(tokenAddress, library).catch(() => null)
const exchangeAddressPromise = getTokenExchangeAddressFromFactory(tokenAddress, chainId, library).catch(
() => null
)
Promise.all([namePromise, symbolPromise, decimalsPromise, exchangeAddressPromise]).then(
([resolvedName, resolvedSymbol, resolvedDecimals, resolvedExchangeAddress]) => {
if (!stale) {
update(chainId, tokenAddress, resolvedName, resolvedSymbol, resolvedDecimals, resolvedExchangeAddress)
}
}
)
return () => {
stale = true
}
}
}, [tokenAddress, name, symbol, decimals, exchangeAddress, chainId, library, update])
return { name, symbol, decimals, exchangeAddress }
}
export function useAllTokenDetails() {
const { chainId } = useWeb3React()
const [state] = useTokensContext()
return useMemo(() => ({ ...ETH, ...(safeAccess(state, [chainId]) || {}) }), [state, chainId])
}