简体   繁体   English

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

[英]How to invoke a payable solidity function using HardHat?

I have a solidity function called adopt a dog as below which is bascically a payable function in the contract.我有一个称为领养狗的可靠功能,如下所示,它基本上是合同中的一个应付功能。

// THIS IS FAILING AS I DONT KNOW HOW TO PASS ETHERS IN HARDHAT/ETHER.JS // 这是失败的,因为我不知道如何在 HARDHAT/ETHER.JS 中传递 ETH

Hardhart哈德哈特

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

Contract合同

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

You can use the overrides param.您可以使用overrides参数。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 solidity中payable function如何通过msg.value方式收款? - How payable function in solidity receives money by msg.value method? 如何测试 Solidity 应付合同 - How to test a Solidity payable contract 如何检查功能是否应付? - How to check function is payable or not? 如何在solidity 0.5.2或更高版本中分配或重置address[]payable变量? - How to assign or reset the address[] payable variable in solidity 0.5.2 or above? 可靠性:调用另一个合约的函数时出错。 错误:发送值时应向构造函数付款 - Solidity: Error when calling a function of another contract. Error: The constructor should be payable when you send value solidity 交易错误,如果您发送 value 应该支付调用的 function 并且您发送的 value 应该小于您当前的余额 - solidity transaction error, The called function should be payable if you send value and the value you send should be less than your current balance Solidity:“如果您发送价值,则应支付称为 function 并且您发送的价值应小于当前余额” - Solidity: "The called function should be payable if you send value and the value you send should be less than your current balance" 安全帽测试:<functionname> 不是 function</functionname> - hardhat test: <functionName> is not a function 如何在solidity中使用传递函数 - How to use transfer function in solidity 叫function突然需要应付 - Called function suddenly needs to be payable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM