Skip to content
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

feat: make payable value required #397

Merged
merged 2 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-drinks-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": minor
---

Made `value` required for payable functions.
11 changes: 7 additions & 4 deletions src/actions/getContract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down Expand Up @@ -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' })
Expand Down
1 change: 1 addition & 0 deletions src/actions/public/estimateContractGas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/actions/public/simulateContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/types/contract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ test('GetFunctionArgs', () => {
test('GetValue', () => {
// payable
type Result = GetValue<typeof seaportAbi, 'fulfillAdvancedOrder'>
expectTypeOf<Result>().toEqualTypeOf<{ value?: bigint | undefined }>()
expectTypeOf<Result>().toEqualTypeOf<{ value: bigint }>()

// other
expectTypeOf<GetValue<typeof seaportAbi, 'getOrderStatus'>>().toEqualTypeOf<{
Expand Down
6 changes: 3 additions & 3 deletions src/types/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -64,9 +64,9 @@ export type GetValue<
? ExtractAbiFunction<TAbi, TFunctionName>
: AbiFunction,
> = TAbiFunction['stateMutability'] extends 'payable'
? { value?: TValueType }
? { value: NoUndefined<TValueType> }
: TAbiFunction['payable'] extends true
? { value?: TValueType }
? { value: NoUndefined<TValueType> }
: { value?: never }

export type MaybeAbiEventName<TAbiEvent extends AbiEvent | undefined> =
Expand Down