Skip to content

Commit

Permalink
fix: writeContract parameter compat with prepareWriteContract (#2589
Browse files Browse the repository at this point in the history
)

fix: writeContract parameters
  • Loading branch information
jxom committed Jun 21, 2023
1 parent 9ecad6d commit 9680c34
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/tidy-dodos-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@wagmi/core": patch
"wagmi": patch
---

Fixed `writeContract` parameters to be compatible with `prepareWriteContract`.
4 changes: 2 additions & 2 deletions packages/core/src/actions/contracts/writeContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ describe('writeContract', () => {

it('prepared', async () => {
await connect({ connector })
const { request } = await prepareWriteContract({
const config = await prepareWriteContract({
...wagmiContractConfig,
functionName: 'mint',
args: [getRandomTokenId()],
account,
})
const { hash } = await writeContract(request)
const { hash } = await writeContract(config)
const { from } = await getPublicClient().getTransaction({ hash })
expect(from).toEqual(account)
})
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/actions/contracts/writeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export type WriteContractMode = 'prepared' | undefined
export type WriteContractPreparedArgs<
TAbi extends Abi | readonly unknown[] = readonly unknown[],
TFunctionName extends string = string,
> = WriteContractParameters<TAbi, TFunctionName, Chain, Account> & {
mode: 'prepared'
> = {
/** Chain id. */
chainId?: number
mode: 'prepared'
request: WriteContractParameters<TAbi, TFunctionName, Chain, Account>
}

export type WriteContractUnpreparedArgs<
Expand All @@ -30,9 +31,9 @@ export type WriteContractUnpreparedArgs<
WriteContractParameters<TAbi, TFunctionName, Chain, Account>,
'chain'
> & {
mode?: never
/** Chain id. */
chainId?: number
mode?: never
}

export type WriteContractArgs<
Expand Down Expand Up @@ -74,7 +75,7 @@ export async function writeContract<

let request: WriteContractParameters<TAbi, TFunctionName, Chain, Account>
if (config.mode === 'prepared') {
request = config
request = config.request
} else {
const { chainId: _, mode: __, ...args } = config
const res = await prepareWriteContract(args as PrepareWriteContractConfig)
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/contracts/useContractWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function mutationFn(
if (!config.request) throw new Error('request is required')
return writeContract({
mode: 'prepared',
...config.request,
request: config.request,
})
}

Expand Down

0 comments on commit 9680c34

Please sign in to comment.