Skip to content

Commit

Permalink
Adds validateBeforeWrite param to createReadWriteContract (#69)
Browse files Browse the repository at this point in the history
* Adds validate before write

Co-authored-by: Danny Delott <[email protected]>

* adds changeset

* updates variable names per PR comments

---------

Co-authored-by: Danny Delott <[email protected]>
  • Loading branch information
jackburrus and DannyDelott authored May 13, 2024
1 parent 93531e6 commit b224f38
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rare-tools-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/evm-client-viem": minor
---

This added a validateBeforeWrite param to createReadWriteContract.
18 changes: 17 additions & 1 deletion packages/evm-client-viem/src/contract/createReadWriteContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import {
CreateReadContractOptions,
} from 'src/contract/createReadContract';
import { createSimulateContractParameters } from 'src/contract/utils/createSimulateContractParameters';
import { Abi, WalletClient } from 'viem';
import { Abi, Address, WalletClient } from 'viem';

export interface ReadWriteContractOptions<TAbi extends Abi = Abi>
extends CreateReadContractOptions<TAbi> {
walletClient: WalletClient;
readContract?: ReadContract<TAbi>;
/**
* Turn this off for debugging
*/
simulateBeforeWrite?: boolean;
}

/**
Expand All @@ -26,6 +30,7 @@ export function createReadWriteContract<TAbi extends Abi = Abi>({
publicClient,
walletClient,
readContract = createReadContract({ abi, address, publicClient }),
simulateBeforeWrite: simulateBeforeWrite = true,
}: ReadWriteContractOptions<TAbi>): ReadWriteContract<TAbi> {
return {
...readContract,
Expand Down Expand Up @@ -58,6 +63,17 @@ export function createReadWriteContract<TAbi extends Abi = Abi>({
value: args,
});

if (!simulateBeforeWrite) {
return walletClient.writeContract({
address,
abi: abi as Abi,
functionName,
args: arrayArgs,
account: account as Address,
chain: undefined,
});
}

const { request } = await publicClient.simulateContract({
abi: abi as Abi,
address: address,
Expand Down

0 comments on commit b224f38

Please sign in to comment.