Send ETH along with ERC20 token transfer

126 views Asked by At

How to specify ETH value for a transaction that's made by using "useContractWrite" hook? I want to send ETH + ERC20 tokens.

Struggling with nesting stuff, async calls... reallydon't know hoe to manage this in wagmi.

1

There are 1 answers

0
user130657 On

Wagmi useContractWrite accepts value: as parameter to send ETH along with write contract transaction.

const {write: transferFunction} = useContractWrite({
  ...myContract,
  functionName: "transferFunction",
  args: [erc20_amount],
  value: eth_amount
});

In the example above, "myContract" has a function called "transferFunction" that manages to spend user's erc20 tokens, so by calling the function, user sends erc20 tokens. Along with that, the "value" paramenter lets user send ETH along with the write tx. Mind to use BigInt(amount) to format the amounts before sending.