-
-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial
callContract
implementation (#31)
* feat: callContract * chore: changeset * cleanup * cleanup * cleanup * format
- Loading branch information
Showing
56 changed files
with
2,803 additions
and
177 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"viem": patch | ||
--- | ||
|
||
Added initial `callContract` implementation |
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,28 @@ | ||
import { Contract } from 'ethers' | ||
import { bench, describe } from 'vitest' | ||
|
||
import { | ||
ethersProvider, | ||
publicClient, | ||
wagmiContractConfig, | ||
} from '../../../test' | ||
|
||
import { callContract } from './callContract' | ||
|
||
describe('Call Contract', () => { | ||
bench('viem: `callContract`', async () => { | ||
await callContract(publicClient, { | ||
...wagmiContractConfig, | ||
functionName: 'totalSupply', | ||
}) | ||
}) | ||
|
||
bench('ethers: `callStatic`', async () => { | ||
const wagmi = new Contract( | ||
wagmiContractConfig.address, | ||
wagmiContractConfig.abi, | ||
ethersProvider, | ||
) | ||
await wagmi.callStatic.mint(1) | ||
}) | ||
}) |
Large diffs are not rendered by default.
Oops, something went wrong.
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,80 @@ | ||
import { Abi } from 'abitype' | ||
|
||
import type { Chain, Formatter } from '../../chains' | ||
import type { PublicClient } from '../../clients' | ||
import type { | ||
Address, | ||
ExtractArgsFromAbi, | ||
ExtractResultFromAbi, | ||
ExtractFunctionNameFromAbi, | ||
GetValue, | ||
} from '../../types' | ||
import { | ||
EncodeFunctionDataArgs, | ||
decodeFunctionResult, | ||
encodeFunctionData, | ||
getContractError, | ||
} from '../../utils' | ||
import { call, CallArgs, FormattedCall } from './call' | ||
|
||
export type FormattedCallContract< | ||
TFormatter extends Formatter | undefined = Formatter, | ||
> = FormattedCall<TFormatter> | ||
|
||
export type CallContractArgs< | ||
TChain extends Chain = Chain, | ||
TAbi extends Abi | readonly unknown[] = Abi, | ||
TFunctionName extends string = any, | ||
> = Omit<CallArgs<TChain>, 'from' | 'to' | 'data' | 'value'> & { | ||
address: Address | ||
abi: TAbi | ||
from?: Address | ||
functionName: ExtractFunctionNameFromAbi<TAbi, TFunctionName> | ||
value?: GetValue<TAbi, TFunctionName, CallArgs<TChain>['value']> | ||
} & ExtractArgsFromAbi<TAbi, TFunctionName> | ||
|
||
export type CallContractResponse< | ||
TAbi extends Abi | readonly unknown[] = Abi, | ||
TFunctionName extends string = string, | ||
> = ExtractResultFromAbi<TAbi, TFunctionName> | ||
|
||
export async function callContract< | ||
TChain extends Chain, | ||
TAbi extends Abi = Abi, | ||
TFunctionName extends string = any, | ||
>( | ||
client: PublicClient, | ||
{ | ||
abi, | ||
address, | ||
args, | ||
functionName, | ||
...callRequest | ||
}: CallContractArgs<TChain, TAbi, TFunctionName>, | ||
): Promise<CallContractResponse<TAbi, TFunctionName>> { | ||
const calldata = encodeFunctionData({ | ||
abi, | ||
args, | ||
functionName, | ||
} as unknown as EncodeFunctionDataArgs<TAbi, TFunctionName>) | ||
try { | ||
const { data } = await call(client, { | ||
data: calldata, | ||
to: address, | ||
...callRequest, | ||
} as unknown as CallArgs<TChain>) | ||
return decodeFunctionResult({ | ||
abi, | ||
functionName, | ||
data: data || '0x', | ||
}) as CallContractResponse<TAbi, TFunctionName> | ||
} catch (err) { | ||
throw getContractError(err, { | ||
abi, | ||
address, | ||
args, | ||
functionName, | ||
sender: callRequest.from, | ||
}) | ||
} | ||
} |
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,125 @@ | ||
import { Abi } from 'abitype' | ||
import { Address } from '../types' | ||
import { BaseError } from './base' | ||
|
||
export class ContractMethodExecutionError extends BaseError { | ||
abi?: Abi | ||
args?: unknown[] | ||
contractAddress?: Address | ||
formattedArgs?: string | ||
functionName?: string | ||
reason?: string | ||
sender?: Address | ||
|
||
name = 'ContractMethodExecutionError' | ||
|
||
constructor( | ||
message?: string, | ||
{ | ||
abi, | ||
args, | ||
cause, | ||
contractAddress, | ||
formattedArgs, | ||
functionName, | ||
functionWithParams, | ||
sender, | ||
}: { | ||
abi?: Abi | ||
args?: any | ||
cause?: Error | ||
contractAddress?: Address | ||
formattedArgs?: string | ||
functionName?: string | ||
functionWithParams?: string | ||
sender?: Address | ||
} = {}, | ||
) { | ||
super( | ||
[ | ||
message, | ||
' ', | ||
sender && `Sender: ${sender}`, | ||
contractAddress && | ||
`Contract: ${ | ||
/* c8 ignore start */ | ||
process.env.TEST | ||
? '0x0000000000000000000000000000000000000000' | ||
: contractAddress | ||
/* c8 ignore end */ | ||
}`, | ||
functionWithParams && `Function: ${functionWithParams}`, | ||
formattedArgs && | ||
`Arguments: ${[...Array(functionName?.length ?? 0).keys()] | ||
.map(() => ' ') | ||
.join('')}${formattedArgs}`, | ||
] | ||
.filter(Boolean) | ||
.join('\n'), | ||
{ | ||
cause, | ||
}, | ||
) | ||
if (message) this.reason = message | ||
this.abi = abi | ||
this.args = args | ||
this.contractAddress = contractAddress | ||
this.functionName = functionName | ||
this.sender = sender | ||
} | ||
} | ||
|
||
export class ContractMethodZeroDataError extends BaseError { | ||
abi?: Abi | ||
args?: unknown[] | ||
contractAddress?: Address | ||
functionName?: string | ||
functionWithParams?: string | ||
|
||
name = 'ContractMethodZeroDataError' | ||
|
||
constructor({ | ||
abi, | ||
args, | ||
cause, | ||
contractAddress, | ||
functionName, | ||
functionWithParams, | ||
}: { | ||
abi?: Abi | ||
args?: any | ||
cause?: Error | ||
contractAddress?: Address | ||
functionName?: string | ||
functionWithParams?: string | ||
} = {}) { | ||
super( | ||
[ | ||
`The contract method "${functionName}" returned no data ("0x"). This could be due to any of the following:`, | ||
`- The contract does not have the function "${functionName}",`, | ||
'- The parameters passed to the contract function may be invalid, or', | ||
'- The address is not a contract.', | ||
' ', | ||
contractAddress && | ||
`Contract: ${ | ||
/* c8 ignore start */ | ||
process.env.TEST | ||
? '0x0000000000000000000000000000000000000000' | ||
: contractAddress | ||
/* c8 ignore end */ | ||
}`, | ||
functionWithParams && `Function: ${functionWithParams}`, | ||
functionWithParams && ` > "0x"`, | ||
] | ||
.filter(Boolean) | ||
.join('\n'), | ||
{ | ||
cause, | ||
}, | ||
) | ||
this.abi = abi | ||
this.args = args | ||
this.contractAddress = contractAddress | ||
this.functionName = functionName | ||
} | ||
} |
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
Oops, something went wrong.
1f65640
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.
Successfully deployed to the following URLs:
viem-benchmark – ./playgrounds/benchmark
viem-benchmark.vercel.app
viem-benchmark-git-main-wagmi-dev.vercel.app
viem-benchmark-wagmi-dev.vercel.app
1f65640
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.
Successfully deployed to the following URLs:
viem-playground – ./playgrounds/dev
viem-playground.vercel.app
viem-playground-wagmi-dev.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app
1f65640
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.
Successfully deployed to the following URLs:
viem-site – ./site
viem-site-git-main-wagmi-dev.vercel.app
viem-site.vercel.app
viem-site-wagmi-dev.vercel.app