Is there an existing issue for this?
Package Version
2.7.9
Current Behavior
This code snippet shows an error (red squiggly line) under feeCurrency
:
const transactionSimulation = await contract.simulate.transfer(
[cUSD_CONTRACT_ADDRESS, parseUnits("0.01", decimals)],
{ account: sender, feeCurrency: "0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1" }
);
The error message is:
Object literal may only specify known properties, and 'feeCurrency' does not exist in type 'Omit<SimulateContractParameters<readonly ...
But the same code as contract.write.transfer
accepts the feeCurrency
const transactionReceipt = await contract.write.transfer(
[cUSD_CONTRACT_ADDRESS, parseUnits("0.01", decimals)],
{ account: sender, feeCurrency: "0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1" }
);
Expected Behavior
simulate
and WalletClient
calls should accept the same fields.
Viem recommends that most contract calls or transactions are first simulated
Warning: The write internally sends a transaction โ it does not validate if the contract write will succeed (the contract may throw an error). It is highly recommended to simulate the contract write with `contract.simulate` before you execute it.
Steps To Reproduce
import {
createPublicClient,
createWalletClient,
http,
parseEther,
parseGwei,
Address,
getContract,
erc20Abi,
parseUnits,
formatUnits,
} from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { celoAlfajores } from "viem/chains";
import {
PRIVATE_KEY,
RECIPIENT,
cUSD_CONTRACT_ADDRESS,
USDC_CONTRACT_ADDRESS,
USDC_ADAPTER_ADDRESS,
} from "./constants";
/**
* Boilerplate to create a viem client and viem-compatible wallet
*/
const read = createPublicClient({
chain: celoAlfajores,
transport: http(),
});
const write = createWalletClient({
chain: celoAlfajores, // Celo testnet
transport: http(),
});
const sender = privateKeyToAccount(`0x${PRIVATE_KEY}`);
/**
* Set up ERC20 contract
*/
const contract = getContract({
address: cUSD_CONTRACT_ADDRESS,
abi: erc20Abi,
client: { public: read, wallet: write },
});
const [symbol, decimals, tokenBalance] = await Promise.all([
contract.read.symbol(),
contract.read.decimals(),
contract.read.balanceOf(["0xcEe284F754E854890e311e3280b767F80797180d"]),
]);
/**
* Makes a transaction to transfer ERC20 tokens using a fee currency
*/
async function erc20Transfer() {
console.log(`Initiating fee currency transaction...`);
console.log(`${symbol} balance of ${sender}: ${formatUnits(tokenBalance, decimals)}`);
const transactionSimulation = await contract.simulate.transfer(
[RECIPIENT, parseUnits("0.01", decimals)],
{ account: sender, feeCurrency: "0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1" }
);
const transactionReceipt = await contract.write.transfer(
[RECIPIENT, parseUnits("0.01", decimals)],
{ account: sender, feeCurrency: "0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1" }
);
console.log(transactionReceipt);
}
erc20Transfer().catch((err) => {
console.error("An error occurred:", err);
});
Link to Minimal Reproducible Example (StackBlitz, CodeSandbox, GitHub repo etc.)
https://github.com/celo-org/feecurrency/blob/main/viem.ts
Anything else?
No response
Related