forked from Uniswap/interface
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(ethereum): reduce number of calls by batching all polling node c…
…alls (Uniswap#840) * initial refactoring * rebase lint error * start implementing reducer * multicall reducer * working multicall! * clean up performance, re-fix annoying error * use multicall everywhere * use multicall for balances * fix lint warning * Use checksummed address * Fix strict warning * get it to a working state with the more generic form * convert useETHBalances * Remove the eth-scan contract completely * Remove the eth-scan contract completely more * Default export * Put the encoding/decoding in the methods that can do it most efficiently * Avoid duplicate fetches via debounce * Reduce delay to something less noticeable * Return null if pair reserves are undefined to indicate it does not exist
- Loading branch information
1 parent
3c10b57
commit 17b4ff4
Showing
48 changed files
with
881 additions
and
735 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Interface } from '@ethersproject/abi' | ||
import ERC20_ABI from './erc20.json' | ||
|
||
const ERC20_INTERFACE = new Interface(ERC20_ABI) | ||
|
||
export default ERC20_INTERFACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
[ | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "getCurrentBlockTimestamp", | ||
"outputs": [ | ||
{ | ||
"name": "timestamp", | ||
"type": "uint256" | ||
} | ||
], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [ | ||
{ | ||
"components": [ | ||
{ | ||
"name": "target", | ||
"type": "address" | ||
}, | ||
{ | ||
"name": "callData", | ||
"type": "bytes" | ||
} | ||
], | ||
"name": "calls", | ||
"type": "tuple[]" | ||
} | ||
], | ||
"name": "aggregate", | ||
"outputs": [ | ||
{ | ||
"name": "blockNumber", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"name": "returnData", | ||
"type": "bytes[]" | ||
} | ||
], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "getLastBlockHash", | ||
"outputs": [ | ||
{ | ||
"name": "blockHash", | ||
"type": "bytes32" | ||
} | ||
], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [ | ||
{ | ||
"name": "addr", | ||
"type": "address" | ||
} | ||
], | ||
"name": "getEthBalance", | ||
"outputs": [ | ||
{ | ||
"name": "balance", | ||
"type": "uint256" | ||
} | ||
], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "getCurrentBlockDifficulty", | ||
"outputs": [ | ||
{ | ||
"name": "difficulty", | ||
"type": "uint256" | ||
} | ||
], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "getCurrentBlockGasLimit", | ||
"outputs": [ | ||
{ | ||
"name": "gaslimit", | ||
"type": "uint256" | ||
} | ||
], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "getCurrentBlockCoinbase", | ||
"outputs": [ | ||
{ | ||
"name": "coinbase", | ||
"type": "address" | ||
} | ||
], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [ | ||
{ | ||
"name": "blockNumber", | ||
"type": "uint256" | ||
} | ||
], | ||
"name": "getBlockHash", | ||
"outputs": [ | ||
{ | ||
"name": "blockHash", | ||
"type": "bytes32" | ||
} | ||
], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ChainId } from '@uniswap/sdk' | ||
import MULTICALL_ABI from './abi.json' | ||
|
||
const MULTICALL_NETWORKS: { [chainId in ChainId]: string } = { | ||
[ChainId.MAINNET]: '0xeefBa1e63905eF1D7ACbA5a8513c70307C1cE441', | ||
[ChainId.ROPSTEN]: '0x53C43764255c17BD724F74c4eF150724AC50a3ed', | ||
[ChainId.KOVAN]: '0x2cc8688C5f75E365aaEEb4ea8D6a480405A48D2A', | ||
[ChainId.RINKEBY]: '0x42Ad527de7d4e9d9d011aC45B31D8551f8Fe9821', | ||
[ChainId.GÖRLI]: '0x77dCa2C955b15e9dE4dbBCf1246B4B85b651e50e' | ||
} | ||
|
||
export { MULTICALL_ABI, MULTICALL_NETWORKS } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,17 @@ | ||
import { Contract } from '@ethersproject/contracts' | ||
import { Token, TokenAmount } from '@uniswap/sdk' | ||
import useSWR from 'swr' | ||
import { useMemo } from 'react' | ||
|
||
import { SWRKeys, useKeepSWRDataLiveAsBlocksArrive } from '.' | ||
import { useTokenContract } from '../hooks' | ||
import { useTokenContract } from '../hooks/useContract' | ||
import { useSingleCallResult } from '../state/multicall/hooks' | ||
|
||
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, spender?: string): TokenAmount { | ||
export function useTokenAllowance(token?: Token, owner?: string, spender?: string): TokenAmount | undefined { | ||
const contract = useTokenContract(token?.address, false) | ||
|
||
const shouldFetch = !!contract && typeof owner === 'string' && typeof spender === 'string' | ||
const { data, mutate } = useSWR( | ||
shouldFetch ? [owner, spender, token.address, token.chainId, SWRKeys.Allowances] : null, | ||
getTokenAllowance(contract, token) | ||
) | ||
useKeepSWRDataLiveAsBlocksArrive(mutate) | ||
const inputs = useMemo(() => [owner, spender], [owner, spender]) | ||
const allowance = useSingleCallResult(contract, 'allowance', inputs) | ||
|
||
return data | ||
return useMemo(() => (token && allowance ? new TokenAmount(token, allowance.toString()) : undefined), [ | ||
token, | ||
allowance | ||
]) | ||
} |
Oops, something went wrong.