如何使用 ethers.js 将 ETH 发送到合约函数?

Bru*_*ghe 12 ethereum solidity smartcontracts ethers.js

我正在尝试通过元掩码和 ethers.js 从网络应用程序将 ETH 发送到合约函数。到目前为止我已经尝试过:

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);
Run Code Online (Sandbox Code Playgroud)

但我不断收到“错误:未为 ENS 名称配置解析器或地址(参数=“名称”,值=“”,代码= INVALID_ARGUMENT,版本=合同/ 5.2.0)”。

CBK*_*CBK 10

您可以调用合约函数并向其传递一个包含value密钥的对象。

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

这将调用您的合约函数并将该数量的 wei 发送到合约。

function testFunction() public payable {
    // contract code
}
Run Code Online (Sandbox Code Playgroud)


Jor*_* P. 5

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

Run Code Online (Sandbox Code Playgroud)


小智 -1

某些地址是无效地址,它可能是合约地址、地址或其他地址