-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: type multicall usage #380
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { Contract } from '@ethersproject/contracts' | ||
import { CallState as CallStateBase } from '@uniswap/redux-multicall' | ||
import { useWeb3React } from '@web3-react/core' | ||
import { Interface } from 'ethers/lib/utils' | ||
import useBlockNumber from 'hooks/useBlockNumber' | ||
import multicall from 'state/multicall' | ||
|
||
|
@@ -7,33 +10,53 @@ export { NEVER_RELOAD } from '@uniswap/redux-multicall' // re-export for conveni | |
|
||
// Create wrappers for hooks so consumers don't need to get latest block themselves | ||
|
||
type MulticallParams<T extends (chainId: number | undefined, latestBlock: number | undefined, ...args: any) => any> = | ||
Parameters<T> extends [any, any, ...infer P] ? P : never | ||
interface CallState<C extends Contract, M extends keyof C['functions']> extends Omit<CallStateBase, 'result'> { | ||
result: C['functions'][M] extends (...args: any) => Promise<infer T> ? T | undefined : never | ||
} | ||
|
||
type MultipleContractParams< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that |
||
M extends string, | ||
T extends ( | ||
chainId: number | undefined, | ||
latestBlock: number | undefined, | ||
addresses: (string | undefined)[], | ||
contractInterface: Interface, | ||
methodName: M, | ||
...args: any | ||
) => any | ||
> = Parameters<T> extends [any, any, ...infer P] ? P : never | ||
|
||
export function useMultipleContractSingleData( | ||
...args: MulticallParams<typeof multicall.hooks.useMultipleContractSingleData> | ||
export function useMultipleContractSingleData<C extends Contract, M extends string>( | ||
...args: MultipleContractParams<M, typeof multicall.hooks.useMultipleContractSingleData> | ||
) { | ||
const { chainId, latestBlock } = useCallContext() | ||
return multicall.hooks.useMultipleContractSingleData(chainId, latestBlock, ...args) | ||
return multicall.hooks.useMultipleContractSingleData(chainId, latestBlock, ...args) as CallState<C, M>[] | ||
} | ||
|
||
export function useSingleCallResult(...args: MulticallParams<typeof multicall.hooks.useSingleCallResult>) { | ||
const { chainId, latestBlock } = useCallContext() | ||
return multicall.hooks.useSingleCallResult(chainId, latestBlock, ...args) | ||
} | ||
type SingleContractParams< | ||
C extends Contract, | ||
M extends string, | ||
T extends ( | ||
chainId: number | undefined, | ||
latestBlock: number | undefined, | ||
contract: C | null | undefined, | ||
methodName: M, | ||
...args: any | ||
) => any | ||
> = Parameters<T> extends [any, any, ...infer P] ? P : never | ||
|
||
export function useSingleContractMultipleData( | ||
...args: MulticallParams<typeof multicall.hooks.useSingleContractMultipleData> | ||
export function useSingleCallResult<C extends Contract, M extends string>( | ||
...args: SingleContractParams<C, M, typeof multicall.hooks.useSingleCallResult> | ||
) { | ||
const { chainId, latestBlock } = useCallContext() | ||
return multicall.hooks.useSingleContractMultipleData(chainId, latestBlock, ...args) | ||
return multicall.hooks.useSingleCallResult(chainId, latestBlock, ...args) as CallState<C, M> | ||
} | ||
|
||
export function useSingleContractWithCallData( | ||
...args: MulticallParams<typeof multicall.hooks.useSingleContractWithCallData> | ||
export function useSingleContractMultipleData<C extends Contract, M extends string>( | ||
...args: SingleContractParams<C, M, typeof multicall.hooks.useSingleContractMultipleData> | ||
) { | ||
const { chainId, latestBlock } = useCallContext() | ||
return multicall.hooks.useSingleContractWithCallData(chainId, latestBlock, ...args) | ||
return multicall.hooks.useSingleContractMultipleData(chainId, latestBlock, ...args) as CallState<C, M>[] | ||
} | ||
|
||
function useCallContext() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,9 @@ import useIsArgentWallet from './useIsArgentWallet' | |
export function useArgentWalletContract(): ArgentWalletContract | null { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type is now implied, and can be removed from the function definition. |
||
const { account } = useWeb3React() | ||
const isArgentWallet = useIsArgentWallet() | ||
return useContract( | ||
return useContract<ArgentWalletContract>( | ||
isArgentWallet ? account ?? undefined : undefined, | ||
ArgentWalletContractABI, | ||
true | ||
) as ArgentWalletContract | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
@ethersproject/contract-interfaces
or something similar, not from theethers
umbrella package.