How to send ETH to a contract function with ethers.js?
Asked Answered
R

4

12

I am trying to send ETH to a contract function from a web app via metamask and ethers.js. So far I have tried:

const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const splitterManager = new ethers.Contract(contract.address, contract.abi, signer);
var overrides = {value: 5}
const result = await splitterManager.newSplitter(addresses, shares, erc20, overrides);
console.log(result);

But I keep getting 'Error: resolver or addr is not configured for ENS name (argument="name", value="", code=INVALID_ARGUMENT, version=contracts/5.2.0)'.

Rhizo answered 10/3, 2022 at 10:11 Comment(0)
A
10

You can call the contracts function and pass it an object containing a value key.

contractInstance.testFunction(<any function args>, { value: ethers.utils.parseUnits("1", "ether") });

This would call your contract's function and send that amount of wei to the contract.

function testFunction() public payable {
    // contract code
}
Ayres answered 6/6, 2022 at 2:43 Comment(1)
Note can do ethers.utils.parseEther("1")Stercoraceous
K
6

If the contract has implemented the receive function, you can send ether to a contract same as sending ether to any other account. Here's a short example:

const accounts = await provider.listAccounts();
const signer = provider.getSigner(accounts[0]);
tx = {
    to: **CONTRACT_ADDRESS**,
    value: ethers.utils.parseEther('2', 'ether')
};
const transaction = await signer.sendTransaction(tx);

Kalmuck answered 1/5, 2022 at 23:1 Comment(0)
A
5
await contractInstance
  .connect(rpcProvider)
  .function({
    value: ethers.utils.parseUnits("1","ether")
  });

this should work

Astronaut answered 5/4, 2022 at 18:14 Comment(0)
L
-1

some address is an invalid address it could be the contract.address, the addresses, or some other address

Lazo answered 12/3, 2022 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.