Commit 9492e737 authored by Moody Salem's avatar Moody Salem

chore(multicall/migrate): move some v1 stuff around for migrate

parent 8a6a10be
import { ChainId, Token, WETH, JSBI, Percent } from '@uniswap/sdk' import { ChainId, JSBI, Percent, Token, WETH } from '@uniswap/sdk'
import { fortmatic, injected, portis, walletconnect, walletlink } from '../connectors' import { fortmatic, injected, portis, walletconnect, walletlink } from '../connectors'
export const V1_FACTORY_ADDRESS = '0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95'
export const ROUTER_ADDRESS = '0xf164fC0Ec4E93095b804a4795bBe1e041497b92a' export const ROUTER_ADDRESS = '0xf164fC0Ec4E93095b804a4795bBe1e041497b92a'
// used for display in the default list when adding liquidity // used for display in the default list when adding liquidity
......
import { Interface } from '@ethersproject/abi'
import V1_EXCHANGE_ABI from './v1_exchange.json'
import V1_FACTORY_ABI from './v1_factory.json'
const V1_FACTORY_ADDRESS = '0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95'
const V1_FACTORY_INTERFACE = new Interface(V1_FACTORY_ABI)
const V1_EXCHANGE_INTERFACE = new Interface(V1_EXCHANGE_ABI)
export { V1_FACTORY_ADDRESS, V1_FACTORY_INTERFACE, V1_FACTORY_ABI, V1_EXCHANGE_INTERFACE, V1_EXCHANGE_ABI }
This diff is collapsed.
...@@ -14,8 +14,7 @@ ...@@ -14,8 +14,7 @@
"inputs": [{ "type": "address", "name": "template" }], "inputs": [{ "type": "address", "name": "template" }],
"constant": false, "constant": false,
"payable": false, "payable": false,
"type": "function", "type": "function"
"gas": 35725
}, },
{ {
"name": "createExchange", "name": "createExchange",
...@@ -23,8 +22,7 @@ ...@@ -23,8 +22,7 @@
"inputs": [{ "type": "address", "name": "token" }], "inputs": [{ "type": "address", "name": "token" }],
"constant": false, "constant": false,
"payable": false, "payable": false,
"type": "function", "type": "function"
"gas": 187911
}, },
{ {
"name": "getExchange", "name": "getExchange",
...@@ -32,8 +30,7 @@ ...@@ -32,8 +30,7 @@
"inputs": [{ "type": "address", "name": "token" }], "inputs": [{ "type": "address", "name": "token" }],
"constant": true, "constant": true,
"payable": false, "payable": false,
"type": "function", "type": "function"
"gas": 715
}, },
{ {
"name": "getToken", "name": "getToken",
...@@ -41,8 +38,7 @@ ...@@ -41,8 +38,7 @@
"inputs": [{ "type": "address", "name": "exchange" }], "inputs": [{ "type": "address", "name": "exchange" }],
"constant": true, "constant": true,
"payable": false, "payable": false,
"type": "function", "type": "function"
"gas": 745
}, },
{ {
"name": "getTokenWithId", "name": "getTokenWithId",
...@@ -50,8 +46,7 @@ ...@@ -50,8 +46,7 @@
"inputs": [{ "type": "uint256", "name": "token_id" }], "inputs": [{ "type": "uint256", "name": "token_id" }],
"constant": true, "constant": true,
"payable": false, "payable": false,
"type": "function", "type": "function"
"gas": 736
}, },
{ {
"name": "exchangeTemplate", "name": "exchangeTemplate",
...@@ -59,8 +54,7 @@ ...@@ -59,8 +54,7 @@
"inputs": [], "inputs": [],
"constant": true, "constant": true,
"payable": false, "payable": false,
"type": "function", "type": "function"
"gas": 633
}, },
{ {
"name": "tokenCount", "name": "tokenCount",
...@@ -68,7 +62,6 @@ ...@@ -68,7 +62,6 @@
"inputs": [], "inputs": [],
"constant": true, "constant": true,
"payable": false, "payable": false,
"type": "function", "type": "function"
"gas": 663
} }
] ]
...@@ -2,7 +2,7 @@ import { ChainId, Pair, Percent, Route, Token, TokenAmount, Trade, TradeType, WE ...@@ -2,7 +2,7 @@ import { ChainId, Pair, Percent, Route, Token, TokenAmount, Trade, TradeType, WE
import { useMemo } from 'react' import { useMemo } from 'react'
import { useActiveWeb3React } from '../hooks' import { useActiveWeb3React } from '../hooks'
import { useV1FactoryContract } from '../hooks/useContract' import { useV1FactoryContract } from '../hooks/useContract'
import { useSingleCallResult } from '../state/multicall/hooks' import { NEVER_RELOAD, useSingleCallResult, useSingleContractMultipleData } from '../state/multicall/hooks'
import { useETHBalances, useTokenBalance } from '../state/wallet/hooks' import { useETHBalances, useTokenBalance } from '../state/wallet/hooks'
function useV1PairAddress(tokenAddress?: string): string | undefined { function useV1PairAddress(tokenAddress?: string): string | undefined {
...@@ -29,6 +29,20 @@ function useMockV1Pair(token?: Token): MockV1Pair | undefined { ...@@ -29,6 +29,20 @@ function useMockV1Pair(token?: Token): MockV1Pair | undefined {
: undefined : undefined
} }
export function useAllV1ExchangeAddresses(): string[] {
const factory = useV1FactoryContract()
const exchangeCount = useSingleCallResult(factory, 'tokenCount')?.result
const parsedCount = parseInt(exchangeCount?.toString() ?? '0')
const indices = [...Array(parsedCount).keys()].map(ix => [ix])
return (
useSingleContractMultipleData(factory, 'getTokenWithId', indices, NEVER_RELOAD)
?.map(({ result }) => result?.[0])
?.filter(x => x) ?? []
)
}
export function useV1TradeLinkIfBetter( export function useV1TradeLinkIfBetter(
isExactIn?: boolean, isExactIn?: boolean,
input?: Token, input?: Token,
......
...@@ -2,9 +2,8 @@ import { Contract } from '@ethersproject/contracts' ...@@ -2,9 +2,8 @@ import { Contract } from '@ethersproject/contracts'
import { ChainId } from '@uniswap/sdk' import { ChainId } from '@uniswap/sdk'
import { abi as IUniswapV2PairABI } from '@uniswap/v2-core/build/IUniswapV2Pair.json' import { abi as IUniswapV2PairABI } from '@uniswap/v2-core/build/IUniswapV2Pair.json'
import { useMemo } from 'react' import { useMemo } from 'react'
import { V1_FACTORY_ADDRESS } from '../constants'
import ERC20_ABI from '../constants/abis/erc20.json' import ERC20_ABI from '../constants/abis/erc20.json'
import IUniswapV1Factory from '../constants/abis/v1_factory.json' import { V1_EXCHANGE_ABI, V1_FACTORY_ABI, V1_FACTORY_ADDRESS } from '../constants/v1'
import { MULTICALL_ABI, MULTICALL_NETWORKS } from '../constants/multicall' import { MULTICALL_ABI, MULTICALL_NETWORKS } from '../constants/multicall'
import { getContract } from '../utils' import { getContract } from '../utils'
import { useActiveWeb3React } from './index' import { useActiveWeb3React } from './index'
...@@ -25,7 +24,12 @@ function useContract(address?: string, ABI?: any, withSignerIfPossible = true): ...@@ -25,7 +24,12 @@ function useContract(address?: string, ABI?: any, withSignerIfPossible = true):
} }
export function useV1FactoryContract(): Contract | null { export function useV1FactoryContract(): Contract | null {
return useContract(V1_FACTORY_ADDRESS, IUniswapV1Factory, false) const { chainId } = useActiveWeb3React()
return useContract(chainId === 1 ? V1_FACTORY_ADDRESS : undefined, V1_FACTORY_ABI, false)
}
export function useV1ExchangeContract(address: string): Contract | null {
return useContract(address, V1_EXCHANGE_ABI, false)
} }
export function useTokenContract(tokenAddress?: string, withSignerIfPossible = true): Contract | null { export function useTokenContract(tokenAddress?: string, withSignerIfPossible = true): Contract | null {
......
...@@ -30,9 +30,9 @@ export function parseCallKey(callKey: string): Call { ...@@ -30,9 +30,9 @@ export function parseCallKey(callKey: string): Call {
} }
} }
interface ListenerOptions { export interface ListenerOptions {
// how often this data should be fetched, by default 1 // how often this data should be fetched, by default 1
blocksPerFetch?: number readonly blocksPerFetch?: number
} }
export const addMulticallListeners = createAction<{ chainId: number; calls: Call[]; options?: ListenerOptions }>( export const addMulticallListeners = createAction<{ chainId: number; calls: Call[]; options?: ListenerOptions }>(
......
...@@ -6,7 +6,14 @@ import { useDispatch, useSelector } from 'react-redux' ...@@ -6,7 +6,14 @@ import { useDispatch, useSelector } from 'react-redux'
import { useActiveWeb3React } from '../../hooks' import { useActiveWeb3React } from '../../hooks'
import { useBlockNumber } from '../application/hooks' import { useBlockNumber } from '../application/hooks'
import { AppDispatch, AppState } from '../index' import { AppDispatch, AppState } from '../index'
import { addMulticallListeners, Call, removeMulticallListeners, parseCallKey, toCallKey } from './actions' import {
addMulticallListeners,
Call,
removeMulticallListeners,
parseCallKey,
toCallKey,
ListenerOptions
} from './actions'
export interface Result extends ReadonlyArray<any> { export interface Result extends ReadonlyArray<any> {
readonly [key: string]: any readonly [key: string]: any
...@@ -35,8 +42,13 @@ interface CallResult { ...@@ -35,8 +42,13 @@ interface CallResult {
const INVALID_RESULT: CallResult = { valid: false, blockNumber: undefined, data: undefined } const INVALID_RESULT: CallResult = { valid: false, blockNumber: undefined, data: undefined }
// use this options object
export const NEVER_RELOAD: ListenerOptions = {
blocksPerFetch: Infinity
}
// the lowest level call for subscribing to contract data // the lowest level call for subscribing to contract data
function useCallsData(calls: (Call | undefined)[]): CallResult[] { function useCallsData(calls: (Call | undefined)[], options?: ListenerOptions): CallResult[] {
const { chainId } = useActiveWeb3React() const { chainId } = useActiveWeb3React()
const callResults = useSelector<AppState, AppState['multicall']['callResults']>(state => state.multicall.callResults) const callResults = useSelector<AppState, AppState['multicall']['callResults']>(state => state.multicall.callResults)
const dispatch = useDispatch<AppDispatch>() const dispatch = useDispatch<AppDispatch>()
...@@ -60,7 +72,8 @@ function useCallsData(calls: (Call | undefined)[]): CallResult[] { ...@@ -60,7 +72,8 @@ function useCallsData(calls: (Call | undefined)[]): CallResult[] {
dispatch( dispatch(
addMulticallListeners({ addMulticallListeners({
chainId, chainId,
calls calls,
options
}) })
) )
...@@ -68,11 +81,12 @@ function useCallsData(calls: (Call | undefined)[]): CallResult[] { ...@@ -68,11 +81,12 @@ function useCallsData(calls: (Call | undefined)[]): CallResult[] {
dispatch( dispatch(
removeMulticallListeners({ removeMulticallListeners({
chainId, chainId,
calls calls,
options
}) })
) )
} }
}, [chainId, dispatch, serializedCallKeys]) }, [chainId, dispatch, options, serializedCallKeys])
return useMemo( return useMemo(
() => () =>
...@@ -130,7 +144,8 @@ function toCallState( ...@@ -130,7 +144,8 @@ function toCallState(
export function useSingleContractMultipleData( export function useSingleContractMultipleData(
contract: Contract | null | undefined, contract: Contract | null | undefined,
methodName: string, methodName: string,
callInputs: OptionalMethodInputs[] callInputs: OptionalMethodInputs[],
options?: ListenerOptions
): CallState[] { ): CallState[] {
const fragment = useMemo(() => contract?.interface?.getFunction(methodName), [contract, methodName]) const fragment = useMemo(() => contract?.interface?.getFunction(methodName), [contract, methodName])
...@@ -147,7 +162,7 @@ export function useSingleContractMultipleData( ...@@ -147,7 +162,7 @@ export function useSingleContractMultipleData(
[callInputs, contract, fragment] [callInputs, contract, fragment]
) )
const results = useCallsData(calls) const results = useCallsData(calls, options)
const latestBlockNumber = useBlockNumber() const latestBlockNumber = useBlockNumber()
...@@ -160,7 +175,8 @@ export function useMultipleContractSingleData( ...@@ -160,7 +175,8 @@ export function useMultipleContractSingleData(
addresses: (string | undefined)[], addresses: (string | undefined)[],
contractInterface: Interface, contractInterface: Interface,
methodName: string, methodName: string,
callInputs?: OptionalMethodInputs callInputs?: OptionalMethodInputs,
options?: ListenerOptions
): CallState[] { ): CallState[] {
const fragment = useMemo(() => contractInterface.getFunction(methodName), [contractInterface, methodName]) const fragment = useMemo(() => contractInterface.getFunction(methodName), [contractInterface, methodName])
const callData: string | undefined = useMemo( const callData: string | undefined = useMemo(
...@@ -186,7 +202,7 @@ export function useMultipleContractSingleData( ...@@ -186,7 +202,7 @@ export function useMultipleContractSingleData(
[addresses, callData, fragment] [addresses, callData, fragment]
) )
const results = useCallsData(calls) const results = useCallsData(calls, options)
const latestBlockNumber = useBlockNumber() const latestBlockNumber = useBlockNumber()
...@@ -198,7 +214,8 @@ export function useMultipleContractSingleData( ...@@ -198,7 +214,8 @@ export function useMultipleContractSingleData(
export function useSingleCallResult( export function useSingleCallResult(
contract: Contract | null | undefined, contract: Contract | null | undefined,
methodName: string, methodName: string,
inputs?: OptionalMethodInputs inputs?: OptionalMethodInputs,
options?: ListenerOptions
): CallState { ): CallState {
const fragment = useMemo(() => contract?.interface?.getFunction(methodName), [contract, methodName]) const fragment = useMemo(() => contract?.interface?.getFunction(methodName), [contract, methodName])
...@@ -213,7 +230,7 @@ export function useSingleCallResult( ...@@ -213,7 +230,7 @@ export function useSingleCallResult(
: [] : []
}, [contract, fragment, inputs]) }, [contract, fragment, inputs])
const result = useCallsData(calls)[0] const result = useCallsData(calls, options)[0]
const latestBlockNumber = useBlockNumber() const latestBlockNumber = useBlockNumber()
return useMemo(() => { return useMemo(() => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment