Bruce Jayasinghe
Bruce Jayasinghe

Reputation: 317

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

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)'.

Upvotes: 12

Views: 16747

Answers (4)

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);

Upvotes: 6

CBK
CBK

Reputation: 190

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
}

Upvotes: 11

subhajit das
subhajit das

Reputation: 445

await contractInstance
  .connect(rpcProvider)
  .function({
    value: ethers.utils.parseUnits("1","ether")
  });

this should work

Upvotes: 5

jhonny
jhonny

Reputation: 858

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

Upvotes: -1

Related Questions