Skip to content

Commit

Permalink
refactor: multicall types
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Aug 18, 2023
1 parent 9e059c4 commit 58060d1
Show file tree
Hide file tree
Showing 35 changed files with 435 additions and 264 deletions.
6 changes: 5 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default withTwoslash(
'script',
{
src: 'https://cdn.usefathom.com/script.js',
['data-site']: 'QWAXSUPT',
'data-site': 'QWAXSUPT',
defer: '',
},
],
Expand Down Expand Up @@ -239,6 +239,10 @@ function getSidebar() {
link: '/react/hooks',
items: [
{ text: 'useAccount', link: '/react/hooks/useAccount' },
{
text: 'useAccountEffect 🚧',
link: '/react/hooks/useAccountEffect',
},
{ text: 'useBalance', link: '/react/hooks/useBalance' },
{
text: 'useBlockNumber',
Expand Down
4 changes: 2 additions & 2 deletions docs/core/actions/getToken.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ Number of decimals for token.

### name

`string`
`string | undefined`

Name of token.

### symbol

`string`
`string | undefined`

Symbol of token.

Expand Down
2 changes: 1 addition & 1 deletion docs/react/hooks/useToken.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const packageName = 'wagmi'
const actionName = 'getToken'
const typeName = 'GetToken'
const TData = '{ address: Address; decimals: number; name: string; symbol: string; totalSupply: { formatted: string; value: bigint; }; }'
const TData = '{ address: Address; decimals: number; name: string | undefined; symbol: string | undefined; totalSupply: { formatted: string; value: bigint; }; }'
const TError = 'GetTokenError'
</script>

Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
"msw": "^1.2.1",
"publint": "^0.2.0",
"rimraf": "^4.4.1",
"rome": "^12.1.0",
"rome": "^12.1.3",
"simple-git-hooks": "^2.8.1",
"typescript": "5.0.4",
"viem": "0.0.0-w-20230802141753",
"viem": "0.0.0-w-20230818192605",
"vite": "^4.3.9",
"vitest": "^0.34.1"
},
Expand All @@ -60,10 +60,9 @@
"overrides": {
"@wagmi/connectors": "workspace:*",
"@wagmi/core": "workspace:*",
"viem": "0.0.0-w-20230802141753"
"viem": "0.0.0-w-20230818192605"
},
"patchedDependencies": {
"@coinbase/wallet-sdk@3.8.0-beta.0": "patches/@coinbase__wallet-sdk@3.8.0-beta.0.patch",
"vitepress@1.0.0-rc.4": "patches/vitepress@1.0.0-rc.4.patch"
},
"peerDependencyRules": {
Expand Down
2 changes: 1 addition & 1 deletion packages/connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
},
"dependencies": {
"@coinbase/wallet-sdk": "3.8.0-beta.0",
"@coinbase/wallet-sdk": "3.8.0-beta.2",
"@ledgerhq/connect-kit-loader": "^1.1.2",
"@safe-global/safe-apps-provider": "^0.18.0",
"@safe-global/safe-apps-sdk": "^8.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('behavior: user rejected request', async () => {
[UserRejectedRequestError: User rejected the request.
Details: Failed to connect.
Version: viem@1.5.1]
Version: viem@1.5.4]
`)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/getBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async function getTokenBalance(
{ ...contract, functionName: 'balanceOf', args: [balanceAddress] },
{ ...contract, functionName: 'decimals' },
{ ...contract, functionName: 'symbol' },
],
] as const,
})
const formatted = formatUnits(value ?? '0', getUnit(unit ?? decimals))
return { decimals, formatted, symbol, value }
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/getToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ test('behavior: bogus token', async () => {
function: decimals()
Docs: https://viem.sh/docs/contract/multicall.html
Version: viem@1.5.1"
Version: viem@1.5.4"
`)
})
67 changes: 57 additions & 10 deletions packages/core/src/actions/getToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from 'viem'

import type { Config } from '../config.js'
import { erc20Abi, erc20Abi_bytes32 } from '../constants/abis.js'
import type { ChainIdParameter } from '../types/properties.js'
import type { Unit } from '../types/unit.js'
import type { Evaluate } from '../types/utils.js'
Expand All @@ -24,8 +23,8 @@ export type GetTokenParameters<config extends Config = Config> = Evaluate<
export type GetTokenReturnType = {
address: Address
decimals: number
name: string
symbol: string
name: string | undefined
symbol: string | undefined
totalSupply: {
formatted: string
value: bigint
Expand All @@ -40,27 +39,75 @@ export async function getToken<config extends Config>(
): Promise<GetTokenReturnType> {
const { address, chainId } = parameters

const abi = [
{
type: 'function',
name: 'decimals',
stateMutability: 'view',
inputs: [],
outputs: [{ type: 'uint8' }],
},
{
type: 'function',
name: 'name',
stateMutability: 'view',
inputs: [],
outputs: [{ type: 'string' }],
},
{
type: 'function',
name: 'totalSupply',
stateMutability: 'view',
inputs: [],
outputs: [{ type: 'uint256' }],
},
] as const

const erc20Abi = [
...abi,
{
type: 'function',
name: 'symbol',
stateMutability: 'view',
inputs: [],
outputs: [{ type: 'string' }],
},
] as const

const erc20Abi_bytes32 = [
...abi,
{
type: 'function',
name: 'symbol',
stateMutability: 'view',
inputs: [],
outputs: [{ type: 'bytes32' }],
},
] as const

async function get(args: { abi: typeof erc20Abi | typeof erc20Abi_bytes32 }) {
const { formatUnits: unit = 18 } = parameters
const erc20Config = { address, chainId, ...args } as const
const [decimals, name, symbol, totalSupply] = await readContracts(config, {
allowFailure: false,
allowFailure: true,
contracts: [
{ ...erc20Config, functionName: 'decimals' },
{ ...erc20Config, functionName: 'name' },
{ ...erc20Config, functionName: 'symbol' },
{ ...erc20Config, functionName: 'totalSupply' },
],
] as const,
})
if (decimals.error) throw decimals.error
if (totalSupply.error) throw totalSupply.error

return {
address,
decimals,
name: name,
symbol: symbol,
decimals: decimals.result!,
name: name.result,
symbol: symbol.result,
totalSupply: {
formatted: formatUnits(totalSupply, getUnit(unit)),
value: totalSupply,
formatted: formatUnits(totalSupply.result!, getUnit(unit)),
value: totalSupply.result!,
},
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/actions/multicall.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { multicall } from './multicall.js'

test('default', async () => {
const result = await multicall(config, {
chainId: 123,
contracts: [
{
address: '0x',
Expand Down
16 changes: 9 additions & 7 deletions packages/core/src/actions/multicall.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
ContractFunctionConfig,
ContractParameters,
MulticallContract,
MulticallParameters as viem_MulticallParameters,
MulticallReturnType as viem_MulticallReturnType,
} from 'viem'
Expand All @@ -10,27 +11,28 @@ import type { ChainIdParameter } from '../types/properties.js'

export type MulticallParameters<
config extends Config = Config,
contracts extends ContractFunctionConfig[] = ContractFunctionConfig[],
contracts extends readonly unknown[] = readonly MulticallContract[],
allowFailure extends boolean = true,
> = viem_MulticallParameters<contracts, allowFailure> & ChainIdParameter<config>

export type MulticallReturnType<
contracts extends ContractFunctionConfig[] = ContractFunctionConfig[],
contracts extends readonly unknown[] = readonly MulticallContract[],
allowFailure extends boolean = true,
> = viem_MulticallReturnType<contracts, allowFailure>

export async function multicall<
config extends Config,
const contracts extends ContractFunctionConfig[],
const contracts extends readonly ContractParameters[],
allowFailure extends boolean = true,
>(
config: config,
parameters: MulticallParameters<config, contracts, allowFailure>,
): Promise<MulticallReturnType<contracts, allowFailure>> {
const { chainId, ...rest } = parameters
const { allowFailure = true, chainId, contracts, ...rest } = parameters
const client = config.getClient({ chainId })
return viem_multicall(client, {
allowFailure: rest.allowFailure ?? true,
allowFailure,
contracts,
...rest,
} as viem_MulticallParameters<contracts, allowFailure>)
}) as Promise<MulticallReturnType<contracts, allowFailure>>
}
4 changes: 2 additions & 2 deletions packages/core/src/actions/prepareSendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type PrepareSendTransactionParameters<
> = Evaluate<
{
[key in keyof chains]: Omit<
SendTransactionParameters<chains[key], Account>,
SendTransactionParameters<chains[key], Account, chains[key]>,
'chain'
>
}[number]
Expand All @@ -41,7 +41,7 @@ export type PrepareSendTransactionReturnType<
> = Evaluate<
{
[key in keyof chains]: Omit<
SendTransactionParameters<chains[key], Account>,
SendTransactionParameters<chains[key], Account, chains[key]>,
'account' | 'chain'
> &
PartialBy<
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/actions/readContract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Abi } from 'viem'
import { type Abi, type ExtractAbiFunctionNames } from 'viem'
import {
type ReadContractParameters as viem_ReadContractParameters,
type ReadContractReturnType as viem_ReadContractReturnType,
Expand All @@ -11,7 +11,10 @@ import type { ChainIdParameter } from '../types/properties.js'
export type ReadContractParameters<
config extends Config = Config,
abi extends Abi | readonly unknown[] = Abi,
functionName extends string = string,
functionName extends ExtractAbiFunctionNames<
abi extends Abi ? abi : Abi,
'view' | 'pure'
> = string,
> = viem_ReadContractParameters<abi, functionName> & ChainIdParameter<config>

export type ReadContractReturnType<
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/actions/readContracts.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test('default', async () => {
abi: abi.erc20,
functionName: 'balanceOf',
args: ['0x'],
chainId: 123,
},
{
address: '0x',
Expand Down
Loading

1 comment on commit 58060d1

@vercel
Copy link

@vercel vercel bot commented on 58060d1 Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wagmi-v2 – ./docs

wagmi-v2.vercel.app
wagmi-v2-wagmi-dev.vercel.app
wagmi-v2-git-alpha-wagmi-dev.vercel.app
alpha.wagmi.sh

Please sign in to comment.