如何使用 HardHat 调用可支付的 Solidity 函数?

问题描述

我有一个称为领养狗的可靠函数,如下所示,它基本上是合同中的一个应付函数

// 这是失败的,因为我不知道如何在 HARDHAT/ETHER.JS 中传递 ETH

哈德哈特

 const Adopt = await ethers.getContractFactory("Adopt");
    const adopt = await Adopt.deploy();
    await adopt.deployed();
    await adopt.adopt("Hachiko"); 

合同

 function adopt(string calldata dog_breed) external payable {
             require(msg.value >= 1 ether,"Min 1 ether needs to be transfered");
            require(user_list[msg.sender].user_allowed_to_adopt,"User not 
            allowed to participate for adoption");
            require(!user_list[msg.sender].adopted,"User has already 
            adopted the dog");
            
        User memory user=user_list[msg.sender];
        user.adopted=true;
        user_list[msg.sender]=user;
    }

解决方法

您可以使用 overrides 参数。

await adopt.adopt("Hachiko",{
    value: ethers.utils.parseEther("1.0")
}); 

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...