Skip to content

Commit

Permalink
feat: make payable value required (#397)
Browse files Browse the repository at this point in the history
* feat: make payable value required

* fix: types
  • Loading branch information
tmm authored Apr 20, 2023
1 parent fe3326f commit 69b95ed
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
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

2 comments on commit 69b95ed

@vercel
Copy link

@vercel vercel bot commented on 69b95ed Apr 20, 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:

viem-playground – ./playgrounds/browser

viem-playground-wagmi-dev.vercel.app
viem-playground.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 69b95ed Apr 20, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.