首页 > 解决方案 > 我如何将 eth 值发送到在 ethers.js 中支付的特定智能合约功能?

问题描述

我试图在只接受一个参数的智能合约上调用应付函数。

我如何将 eth 值与函数调用一起发送到 ethers.js 中的这个函数?文档似乎没有提供太多关于最佳方式的示例。

我的函数调用

const reciept = await contract.buyPunk(1001);

所有其他读写函数调用都按预期工作,但它调用了我尚未解决的应付函数。

标签: node.jsethereumsolidityethers.js

解决方案


const options = {value: ethers.utils.parseEther("1.0")}
const reciept = await contract.buyPunk(1001, options);

通过 ethers.js 调用合约函数时,您可以在参数末尾传递一个选项对象。该对象可以设置value与交易一起发送的。

此处的文档:https ://docs.ethers.io/v5/api/contract/contract/#Contract-functionsCall


推荐阅读