Skip to content

Commit 51fde8a

Browse files
authoredMay 24, 2024··
fix(react,vue): useWriteContract value (#3984)
* fix(react,vue): useWriteContract value * chore: tweaks * chore: up * chore: changeset
1 parent 6be1c00 commit 51fde8a

File tree

8 files changed

+420
-222
lines changed

8 files changed

+420
-222
lines changed
 

‎.changeset/olive-squids-retire.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@wagmi/core": patch
3+
---
4+
5+
Fixed `writeContract` query types for `value` property.

‎.github/workflows/verify.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
timeout-minutes: 5
6363
strategy:
6464
matrix:
65-
typescript-version: ['5.0.4', '5.1.6', '5.2.2', 'latest']
65+
typescript-version: ['5.0.4', '5.1.6', '5.2.2', '5.3.3', '5.4.5']
6666
viem-version: ['2.9.31', 'latest']
6767

6868
steps:

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"publint": "^0.2.7",
5858
"sherif": "^0.8.4",
5959
"simple-git-hooks": "^2.11.1",
60-
"typescript": "5.4.2",
60+
"typescript": "5.4.5",
6161
"viem": "2.9.31",
6262
"vitest": "^1.6.0"
6363
},

‎packages/core/src/actions/writeContract.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
type ChainIdParameter,
2020
type ConnectorParameter,
2121
} from '../types/properties.js'
22-
import type { Evaluate, UnionEvaluate, UnionOmit } from '../types/utils.js'
22+
import type { Evaluate, UnionEvaluate } from '../types/utils.js'
2323
import { getAction } from '../utils/getAction.js'
2424
import { getAccount } from './getAccount.js'
2525
import {
@@ -49,17 +49,16 @@ export type WriteContractParameters<
4949
chains extends readonly Chain[] = SelectChains<config, chainId>,
5050
> = UnionEvaluate<
5151
{
52-
[key in keyof chains]: UnionOmit<
53-
viem_WriteContractParameters<
54-
abi,
55-
functionName,
56-
args,
57-
chains[key],
58-
Account,
59-
chains[key],
60-
allFunctionNames
61-
>,
62-
'chain'
52+
// TODO: Should use `UnionOmit<..., 'chain'>` on `viem_WriteContractParameters` result instead
53+
// temp workaround that doesn't affect runtime behavior for for https://github.com/wevm/wagmi/issues/3981
54+
[key in keyof chains]: viem_WriteContractParameters<
55+
abi,
56+
functionName,
57+
args,
58+
chains[key],
59+
Account,
60+
chains[key],
61+
allFunctionNames
6362
>
6463
}[number] &
6564
Evaluate<ChainIdParameter<config, chainId>> &
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import { http } from 'viem'
2+
import { writeContract } from 'viem/actions'
3+
import { base } from 'viem/chains'
4+
import { test } from 'vitest'
5+
6+
import { createConfig } from '../createConfig.js'
7+
import type { WriteContractMutate } from './writeContract.js'
8+
9+
// https://github.com/wevm/wagmi/issues/3981
10+
test('gh#3981', () => {
11+
const config = createConfig({
12+
chains: [base],
13+
transports: {
14+
[base.id]: http(),
15+
},
16+
})
17+
18+
const abi = [
19+
{
20+
type: 'function',
21+
name: 'example1',
22+
inputs: [
23+
{ name: 'exampleName', type: 'address', internalType: 'address' },
24+
],
25+
outputs: [],
26+
stateMutability: 'payable',
27+
},
28+
{
29+
type: 'function',
30+
name: 'example2',
31+
inputs: [
32+
{ name: 'exampleName', type: 'address', internalType: 'address' },
33+
],
34+
outputs: [],
35+
stateMutability: 'nonpayable',
36+
},
37+
] as const
38+
39+
const write: WriteContractMutate<typeof config> = () => {}
40+
write({
41+
abi,
42+
address: '0x...',
43+
functionName: 'example1',
44+
args: ['0x...'],
45+
value: 123n,
46+
})
47+
write({
48+
abi: [
49+
{
50+
type: 'function',
51+
name: 'example1',
52+
inputs: [
53+
{ name: 'exampleName', type: 'address', internalType: 'address' },
54+
],
55+
outputs: [],
56+
stateMutability: 'payable',
57+
},
58+
{
59+
type: 'function',
60+
name: 'example2',
61+
inputs: [
62+
{ name: 'exampleName', type: 'address', internalType: 'address' },
63+
],
64+
outputs: [],
65+
stateMutability: 'nonpayable',
66+
},
67+
] as const,
68+
address: '0x...',
69+
functionName: 'example1',
70+
args: ['0x...'],
71+
value: 123n,
72+
})
73+
74+
write({
75+
abi,
76+
address: '0x...',
77+
functionName: 'example2',
78+
args: ['0x...'],
79+
// @ts-expect-error
80+
value: 123n,
81+
})
82+
write({
83+
abi: [
84+
{
85+
type: 'function',
86+
name: 'example1',
87+
inputs: [
88+
{ name: 'exampleName', type: 'address', internalType: 'address' },
89+
],
90+
outputs: [],
91+
stateMutability: 'payable',
92+
},
93+
{
94+
type: 'function',
95+
name: 'example2',
96+
inputs: [
97+
{ name: 'exampleName', type: 'address', internalType: 'address' },
98+
],
99+
outputs: [],
100+
stateMutability: 'nonpayable',
101+
},
102+
],
103+
address: '0x...',
104+
functionName: 'example1',
105+
args: ['0x...'],
106+
value: 123n,
107+
})
108+
109+
const client = config.getClient({ chainId: base.id })
110+
writeContract(client, {
111+
abi,
112+
address: '0x...',
113+
account: '0x...',
114+
functionName: 'example1',
115+
args: ['0x...'],
116+
value: 123n,
117+
})
118+
writeContract(client, {
119+
abi: [
120+
{
121+
type: 'function',
122+
name: 'example1',
123+
inputs: [
124+
{ name: 'exampleName', type: 'address', internalType: 'address' },
125+
],
126+
outputs: [],
127+
stateMutability: 'payable',
128+
},
129+
{
130+
type: 'function',
131+
name: 'example2',
132+
inputs: [
133+
{ name: 'exampleName', type: 'address', internalType: 'address' },
134+
],
135+
outputs: [],
136+
stateMutability: 'nonpayable',
137+
},
138+
] as const,
139+
address: '0x...',
140+
account: '0x...',
141+
functionName: 'example1',
142+
args: ['0x...'],
143+
value: 123n,
144+
})
145+
})

‎packages/react/src/hooks/useWriteContract.test-d.ts

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { type WriteContractErrorType } from '@wagmi/core'
1+
import { http, type WriteContractErrorType, createConfig } from '@wagmi/core'
2+
import { base } from '@wagmi/core/chains'
23
import { abi } from '@wagmi/test'
34
import { type Abi, type Address, type Hash } from 'viem'
45
import { expectTypeOf, test } from 'vitest'
@@ -141,3 +142,51 @@ test('useSimulateContract', () => {
141142
const request = data?.request
142143
if (request) writeContract(request)
143144
})
145+
146+
// https://github.com/wevm/wagmi/issues/3981
147+
test('gh#3981', () => {
148+
const config = createConfig({
149+
chains: [base],
150+
transports: {
151+
[base.id]: http(),
152+
},
153+
})
154+
155+
const abi = [
156+
{
157+
type: 'function',
158+
name: 'example1',
159+
inputs: [
160+
{ name: 'exampleName', type: 'address', internalType: 'address' },
161+
],
162+
outputs: [],
163+
stateMutability: 'payable',
164+
},
165+
{
166+
type: 'function',
167+
name: 'example2',
168+
inputs: [
169+
{ name: 'exampleName', type: 'address', internalType: 'address' },
170+
],
171+
outputs: [],
172+
stateMutability: 'nonpayable',
173+
},
174+
] as const
175+
176+
const { writeContract } = useWriteContract({ config })
177+
writeContract({
178+
abi,
179+
address: '0x...',
180+
functionName: 'example1',
181+
args: ['0x...'],
182+
value: 123n,
183+
})
184+
writeContract({
185+
abi,
186+
address: '0x...',
187+
functionName: 'example2',
188+
args: ['0x...'],
189+
// @ts-expect-error
190+
value: 123n,
191+
})
192+
})

‎packages/vue/src/composables/useWriteContract.test-d.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { abi } from '@wagmi/test'
33
import { type Abi, type Address, type Hash } from 'viem'
44
import { expectTypeOf, test } from 'vitest'
55

6+
import { useSimulateContract } from './useSimulateContract.js'
67
import { useWriteContract } from './useWriteContract.js'
78

89
const contextValue = { foo: 'bar' } as const
@@ -127,17 +128,16 @@ test('context', () => {
127128
)
128129
})
129130

130-
// TODO: Simulate response passthrough
131-
// test('useSimulateContract', () => {
132-
// const { data } = useSimulateContract({
133-
// address: '0x',
134-
// abi: abi.erc20,
135-
// functionName: 'transferFrom',
136-
// args: ['0x', '0x', 123n],
137-
// chainId: 1,
138-
// })
139-
// const { writeContract } = useWriteContract()
140-
//
141-
// const request = data?.request
142-
// if (request) writeContract(request)
143-
// })
131+
test('useSimulateContract', () => {
132+
const { data } = useSimulateContract({
133+
address: '0x',
134+
abi: abi.erc20,
135+
functionName: 'transferFrom',
136+
args: ['0x', '0x', 123n],
137+
chainId: 1,
138+
})
139+
const { writeContract } = useWriteContract()
140+
141+
const request = data?.value?.request
142+
if (request) writeContract(request)
143+
})

‎pnpm-lock.yaml

+193-193
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.