From 69b95ed5991c712d11ff4de1bb873c9a2af152b3 Mon Sep 17 00:00:00 2001 From: awkweb Date: Thu, 20 Apr 2023 19:06:16 -0400 Subject: [PATCH] feat: make payable value required (#397) * feat: make payable value required * fix: types --- .changeset/clever-drinks-rule.md | 5 +++++ src/actions/getContract.test-d.ts | 11 +++++++---- src/actions/public/estimateContractGas.test.ts | 1 + src/actions/public/simulateContract.test.ts | 1 + src/types/contract.test-d.ts | 2 +- src/types/contract.ts | 6 +++--- 6 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 .changeset/clever-drinks-rule.md diff --git a/.changeset/clever-drinks-rule.md b/.changeset/clever-drinks-rule.md new file mode 100644 index 0000000000..ce39d93309 --- /dev/null +++ b/.changeset/clever-drinks-rule.md @@ -0,0 +1,5 @@ +--- +"viem": minor +--- + +Made `value` required for payable functions. diff --git a/src/actions/getContract.test-d.ts b/src/actions/getContract.test-d.ts index ac763b2132..bd03ab3a1b 100644 --- a/src/actions/getContract.test-d.ts +++ b/src/actions/getContract.test-d.ts @@ -704,8 +704,11 @@ test('argument permutations', async () => { // estimateGas contract.estimateGas.nonpayableWithoutArgs({ account: '0x' }) contract.estimateGas.nonpayableWithArgs(['foo', 69n], { account: '0x' }) - contract.estimateGas.payableWithoutArgs({ account: '0x' }) - contract.estimateGas.payableWithArgs(['foo', 69n], { account: '0x' }) + contract.estimateGas.payableWithoutArgs({ account: '0x', value: 1n }) + contract.estimateGas.payableWithArgs(['foo', 69n], { + account: '0x', + value: 1n, + }) contract.estimateGas.overloadedNonpayable({ account: '0x' }) contract.estimateGas.overloadedNonpayable(['foo'], { account: '0x' }) @@ -733,8 +736,8 @@ test('argument permutations', async () => { // simulate contract.simulate.nonpayableWithoutArgs({ account: '0x' }) contract.simulate.nonpayableWithArgs(['foo', 69n], { account: '0x' }) - contract.simulate.payableWithoutArgs({ account: '0x' }) - contract.simulate.payableWithArgs(['foo', 69n], { account: '0x' }) + contract.simulate.payableWithoutArgs({ account: '0x', value: 1n }) + contract.simulate.payableWithArgs(['foo', 69n], { account: '0x', value: 1n }) contract.simulate.overloadedNonpayable({ account: '0x' }) contract.simulate.overloadedNonpayable(['foo'], { account: '0x' }) diff --git a/src/actions/public/estimateContractGas.test.ts b/src/actions/public/estimateContractGas.test.ts index 956459038b..a84d4504b5 100644 --- a/src/actions/public/estimateContractGas.test.ts +++ b/src/actions/public/estimateContractGas.test.ts @@ -183,6 +183,7 @@ describe('BAYC', () => { functionName: 'mintApe', account: accounts[0].address, args: [1n], + value: 1n, }), ).rejects.toThrowErrorMatchingInlineSnapshot(` "The contract function \\"mintApe\\" reverted with the following reason: diff --git a/src/actions/public/simulateContract.test.ts b/src/actions/public/simulateContract.test.ts index 0024d47f93..ffdc331d55 100644 --- a/src/actions/public/simulateContract.test.ts +++ b/src/actions/public/simulateContract.test.ts @@ -216,6 +216,7 @@ describe('BAYC', () => { functionName: 'mintApe', account: accounts[0].address, args: [1n], + value: 1n, }), ).rejects.toThrowErrorMatchingInlineSnapshot(` "The contract function \\"mintApe\\" reverted with the following reason: diff --git a/src/types/contract.test-d.ts b/src/types/contract.test-d.ts index a5c960c0f4..e8a43aa44a 100644 --- a/src/types/contract.test-d.ts +++ b/src/types/contract.test-d.ts @@ -167,7 +167,7 @@ test('GetFunctionArgs', () => { test('GetValue', () => { // payable type Result = GetValue - expectTypeOf().toEqualTypeOf<{ value?: bigint | undefined }>() + expectTypeOf().toEqualTypeOf<{ value: bigint }>() // other expectTypeOf>().toEqualTypeOf<{ diff --git a/src/types/contract.ts b/src/types/contract.ts index beea05a628..0eed900a26 100644 --- a/src/types/contract.ts +++ b/src/types/contract.ts @@ -19,7 +19,7 @@ import type { } from 'abitype' import type { Hex, LogTopic } from './misc.js' import type { TransactionRequest } from './transaction.js' -import type { Filter, MaybeRequired } from './utils.js' +import type { Filter, MaybeRequired, NoUndefined } from './utils.js' export type AbiItem = Abi[number] @@ -64,9 +64,9 @@ export type GetValue< ? ExtractAbiFunction : AbiFunction, > = TAbiFunction['stateMutability'] extends 'payable' - ? { value?: TValueType } + ? { value: NoUndefined } : TAbiFunction['payable'] extends true - ? { value?: TValueType } + ? { value: NoUndefined } : { value?: never } export type MaybeAbiEventName =