Replies: 1 comment
-
Made the simplest example I could, this first one works and compiles happily (very similar to the docs): import { erc20Abi, type Abi, type Account, type Address, type Chain, type ContractFunctionArgs, type ContractFunctionName, type SimulateContractParameters } from "viem"
import { getWeb3 } from "./Web3"
export const { publicClient, walletClient, account } = await getWeb3()
const address = "0x"
const simulateContractParams = {
abi: erc20Abi,
address,
functionName: "approve",
args: ["0x", 0n]
} as const;
const { request } = await publicClient.simulateContract(simulateContractParams)
publicClient.estimateGas(request) // OKAY
walletClient.writeContract(request) // OKAY I would like to be able to write a function like: async function managedWrite(args){
const { request } = await publicClient.simulateContract(args)
publicClient.estimateGas(request) // OKAY
const someOtherLogic = true // dummy logic
if (someOtherLogic) {
walletClient.writeContract(request) // COMPILE ERROR
}
} When I copy the async function managedWrite<
const abi extends Abi | readonly unknown[],
functionName extends ContractFunctionName<abi, "nonpayable" | "payable">,
args extends ContractFunctionArgs<
abi,
"nonpayable" | "payable",
functionName
>,
chainOverride extends Chain | undefined,
accountOverride extends Account | Address | undefined = undefined,
>(
args: SimulateContractParameters<
abi,
functionName,
args,
Chain | undefined, // was TChain, changed to Chain | undefined
chainOverride,
accountOverride
>,
) {
const { request } = await publicClient.simulateContract(args);
const gasEstimate = publicClient.estimateGas(request); // OKAY
const someOtherLogic = true; // dummy logic
if (someOtherLogic) {
walletClient.writeContract(request); // COMPILE ERROR
}
/*
Argument of type '{ [K in keyof (UnionEvaluate<UnionOmit<WriteContractParameters<readonly [ExtractAbiFunctionForArgs<
abi extends Abi ? abi : Abi, "nonpayable" | "payable", functionName, args>], ... 4 more ...,
chainOverride>, "chain" | ... 3 more ... | "functionName">> & { ...; } & (readonly [] extends ContractFunctionArgs<...> ? {} ...'
is not assignable to parameter of type
'WriteContractParameters<(UnionEvaluate<UnionOmit<WriteContractParameters<readonly [ExtractAbiFunctionForArgs<
abi extends Abi ? abi : Abi, "nonpayable" | "payable", functionName, args>], ... 4 more ...,
chainOverride>, "chain" | ... 3 more ... | "functionName">> & { ...; } & (readonly [] extends ContractFunctionArgs<...'.
Type '{ [K in keyof (UnionEvaluate<UnionOmit<WriteContractParameters<readonly [ExtractAbiFunctionForArgs<
abi extends Abi ? abi : Abi, "nonpayable" | "payable", functionName, args>], ... 4 more ...,
chainOverride>, "chain" | ... 3 more ... | "functionName">> & { ...; } & (readonly [] extends ContractFunctionArgs<...> ? {} ...'
is not assignable to type
'readonly [] extends ContractFunctionArgs<(UnionEvaluate<UnionOmit<WriteContractParameters<readonly [ExtractAbiFunctionForArgs<
abi extends Abi ? abi : Abi, "nonpayable" | "payable", functionName, args>], ... 4 more ...,
chainOverride>, "chain" | ... 3 more ... | "functionName">> & { ...; } & (readonly [] extends Cont...'
*/
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm porting a script from
ethers
toviem
🎉In my existing flow, I follow these steps:
I repeat these 3 steps across multiple contracts so have a set of utility functions to avoid repeating the code.
So far, I have things working without pulling the pieces into their own functions.
When I try to split the code and reuse, I am getting type errors. For example, passing the result of
simulateContract
into a "check if it's worth it" function:Am I making the wrong choice to try to pass
SimulateContractReturnType
data to a function, if so is there a better way to look at the problem to avoid repeating code?Thanks for an awesome library, and for taking the time to engage.
Cheers,
Peter
Beta Was this translation helpful? Give feedback.
All reactions