![](/img/trans.png)
[英]Solidity error message when executing smart contract: “the constructor should be payable if you send value”
[英]Error message while executing solidity contract
我正在学习坚固性。 我编写的代码编译正确,并且在问题的解决方案中给出了相同的解决方案,但它在没有任何 output 的情况下不断恢复。 请检查代码一次,让我知道错误。
问题是将给定的数量从数量数组转移到相同的position,即从数量的第i个position到to的第i个position。
它显示错误消息:“交易已恢复为初始 state。注意:如果您发送价值,则调用的 function 应该是应付的,并且您发送的价值应该小于您当前的余额。”
//SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.5.0 <0.9.0;
contract Day4 {
address owner;
constructor() {
owner = msg.sender;
}
function send(address payable[] memory to, uint256[] memory amount)
public
payable
ownerOnly
{
require(to.length == amount.length, "to must be same length as amount");
for (uint256 i = 0; i < to.length; i++) {
to[i].transfer(amount[i]); //to array - 0x00 0x01 0x02
//amount array - 10 20 30
}
}
modifier ownerOnly() {
require(msg.sender == owner,"You are not the owner");
_;
}
}
您的问题取决于transfer()
function。 详细地说,这个 function 允许您将特定金额从智能合约余额转移到另一个地址。
在您的情况下,当您直接调用send()
function 时,智能合约余额为 0,因为我认为(因为我没有看到存款方法)您没有向它存入金额。
因此,当您调用send()
function 时,它会恢复并且不起作用。
要在调用send()
方法之前解决此问题,请尝试将特定数量的以太币存入智能合约,然后调用send()
方法。
记住:数组金额的总和必须小于智能合约余额,否则它会恢复。
我以这种方式调整了你的智能合约:
//SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.5.0 <0.9.0;
contract Day4 {
address owner;
constructor() {
owner = msg.sender;
}
function send(address payable[] memory to, uint256[] memory amount)
public
payable
ownerOnly
{
require(to.length == amount.length, "to must be same length as amount");
for (uint256 i = 0; i < to.length; i++) {
to[i].transfer(amount[i]);
}
}
modifier ownerOnly() {
require(msg.sender == owner,"You are not the owner");
_;
}
// NOTE: Call this function for first, and before this operation set the values inside msg.value textbox in
// Remix IDE (if you're using it)
function deposit() public payable {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.