Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: contract.simulate does not accept feeCurrency field #11

Open
1 task done
arthurgousset opened this issue Feb 29, 2024 · 2 comments
Open
1 task done

bug: contract.simulate does not accept feeCurrency field #11

arthurgousset opened this issue Feb 29, 2024 · 2 comments
Assignees

Comments

@arthurgousset
Copy link

arthurgousset commented Feb 29, 2024

Is there an existing issue for this?

  • I have searched the existing issues

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

@arthurgousset arthurgousset changed the title bug: contract.publicWallet.simulate() does not accept feeCurrency field bug: contract.simulate does not accept feeCurrency field Feb 29, 2024
@arthurgousset
Copy link
Author

This issue could be done around the same time or shortly after this:

(while you have your head in viem)

@shazarre
Copy link

shazarre commented Apr 8, 2024

Apparently it works fine if you pass the chain explicitly to the simulate transaction params:

Screenshot 2024-04-08 at 10 22 33

and fails as described by @arthurgousset if you don't:

Screenshot 2024-04-08 at 10 22 43

For write transaction though it seems to just accept any arbitrary parameters with the chain explicitly provided:

Screenshot 2024-04-08 at 10 23 39

and without it as well:

Screenshot 2024-04-08 at 10 23 50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants