Skip to content

Commit

Permalink
fixes wevm#2560 value type on function overload
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuripetusko committed Jul 30, 2024
1 parent 410ff2b commit 0e3b207
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 17 deletions.
22 changes: 15 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/actions/public/estimateContractGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type EstimateContractGasParameters<
UnionOmit<EstimateGasParameters<chain>, 'data' | 'to' | 'value'> &
GetValue<
abi,
'nonpayable' | 'payable',
functionName,
EstimateGasParameters<chain> extends EstimateGasParameters
? EstimateGasParameters<chain>['value']
Expand Down
1 change: 1 addition & 0 deletions src/actions/public/simulateContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type SimulateContractParameters<
> &
GetValue<
abi,
'nonpayable' | 'payable',
functionName,
CallParameters<derivedChain> extends CallParameters
? CallParameters<derivedChain>['value']
Expand Down
12 changes: 12 additions & 0 deletions src/actions/wallet/writeContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ test('overloaded function', async () => {
).toBeDefined()
})

test('payable overloaded function', async () => {
expect(
await writeContract(client, {
...wagmiContractConfig,
account: accounts[0].address,
functionName: 'mint',
args: [69420n, 1n],
value: 1n,
}),
).toBeDefined()
})

test('w/ simulateContract', async () => {
const { request } = await simulateContract(client, {
...wagmiContractConfig,
Expand Down
4 changes: 3 additions & 1 deletion src/actions/wallet/writeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ export type WriteContractParameters<
GetAccountParameter<account> &
GetValue<
abi,
'nonpayable' | 'payable',
functionName,
FormattedTransactionRequest<derivedChain>['value']
FormattedTransactionRequest<derivedChain>['value'],
args
> & {
/** Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f). */
dataSuffix?: Hex | undefined
Expand Down
2 changes: 1 addition & 1 deletion src/experimental/eip5792/actions/writeContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,4 @@ export type WriteContractFunctionParameters<
| (functionName extends allFunctionNames ? functionName : never) // infer value
args?: (abi extends Abi ? UnionWiden<args> : never) | allArgs | undefined
} & (readonly [] extends allArgs ? {} : { args: Widen<args> }) &
GetValue<abi, functionName>
GetValue<abi, mutability, functionName>
1 change: 1 addition & 0 deletions src/op-stack/actions/estimateContractL1Fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type EstimateContractL1FeeParameters<
> &
GetValue<
abi,
'nonpayable' | 'payable',
functionName,
EstimateL1FeeParameters<
chain,
Expand Down
1 change: 1 addition & 0 deletions src/op-stack/actions/estimateContractL1Gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type EstimateContractL1GasParameters<
> &
GetValue<
abi,
'nonpayable' | 'payable',
functionName,
EstimateL1GasParameters<
chain,
Expand Down
1 change: 1 addition & 0 deletions src/op-stack/actions/estimateContractTotalFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type EstimateContractTotalFeeParameters<
> &
GetValue<
abi,
'nonpayable' | 'payable',
functionName,
EstimateTotalFeeParameters<
chain,
Expand Down
1 change: 1 addition & 0 deletions src/op-stack/actions/estimateContractTotalGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type EstimateContractTotalGasParameters<
> &
GetValue<
abi,
'nonpayable' | 'payable',
functionName,
EstimateTotalGasParameters<
chain,
Expand Down
16 changes: 11 additions & 5 deletions src/types/contract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,23 @@ test('GetEventArgs', () => {

test('GetValue', () => {
// payable
type Result = GetValue<typeof seaportAbi, 'fulfillAdvancedOrder'>
type Result = GetValue<typeof seaportAbi, 'payable', 'fulfillAdvancedOrder'>
expectTypeOf<Result>().toEqualTypeOf<{ value?: bigint }>()

// other
expectTypeOf<GetValue<typeof seaportAbi, 'getOrderStatus'>>().toEqualTypeOf<{
expectTypeOf<
GetValue<typeof seaportAbi, 'view', 'getOrderStatus'>
>().toEqualTypeOf<{
value?: never
}>()
expectTypeOf<GetValue<typeof seaportAbi, 'cancel'>>().toEqualTypeOf<{
expectTypeOf<
GetValue<typeof seaportAbi, 'nonpayable', 'cancel'>
>().toEqualTypeOf<{
value?: never
}>()

// unknown abi
expectTypeOf<GetValue<Abi, 'foo'>>().toEqualTypeOf<{
expectTypeOf<GetValue<Abi, 'nonpayable' | 'payable', 'foo'>>().toEqualTypeOf<{
value?: bigint | undefined
}>()
const abi = [
Expand All @@ -244,7 +248,9 @@ test('GetValue', () => {
outputs: [{ type: 'uint256' }],
},
]
expectTypeOf<GetValue<typeof abi, 'foo'>>().toEqualTypeOf<{
expectTypeOf<
GetValue<typeof abi, 'nonpayable' | 'payable', 'foo'>
>().toEqualTypeOf<{
value?: bigint | undefined
}>()
})
Expand Down
13 changes: 11 additions & 2 deletions src/types/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,19 @@ export type EventDefinition = `${string}(${string})`

export type GetValue<
abi extends Abi | readonly unknown[],
functionName extends string,
mutability extends AbiStateMutability = AbiStateMutability,
functionName extends ContractFunctionName<
abi,
mutability
> = ContractFunctionName<abi, mutability>,
valueType = TransactionRequest['value'],
args extends ContractFunctionArgs<
abi,
mutability,
functionName
> = ContractFunctionArgs<abi, mutability, functionName>,
abiFunction extends AbiFunction = abi extends Abi
? ExtractAbiFunction<abi, functionName>
? ExtractAbiFunctionForArgs<abi, mutability, functionName, args>
: AbiFunction,
_Narrowable extends boolean = IsNarrowable<abi, Abi>,
> = _Narrowable extends true
Expand Down
2 changes: 1 addition & 1 deletion src/utils/abi/decodeFunctionData.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test('default', () => {
functionName: 'isApprovedForAll'
}
| {
args: readonly [] | readonly [bigint]
args: readonly [] | readonly [bigint] | readonly [bigint, bigint]
functionName: 'mint'
}
| {
Expand Down
10 changes: 10 additions & 0 deletions test/src/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,16 @@ export const wagmiContractConfig = {
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{ name: 'tokenId', type: 'uint256' },
{ name: 'quantity', type: 'uint256' },
],
name: 'mint',
outputs: [],
stateMutability: 'payable',
type: 'function',
},
{
inputs: [],
name: 'name',
Expand Down

0 comments on commit 0e3b207

Please sign in to comment.