![](/img/trans.png)
[英]How to send fixed amount of ether from contract using call function in solidity?
[英]cannot send entire contract amount to account in solidity
我创建了一个彩票合约,其中我将所有参与的玩家存储在一个地址数组中我在将我的合约资金转移给获胜者时遇到错误,即在 function 获胜者中另一个错误是将 hash 值转换为 uint
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.11;
contract lottery
{
address manager;
address[] public players;
function setManager() public{
manager = msg.sender;
}
function enterLottery () public payable{
require(msg.value > 0.9 ether);
players.push(msg.sender);
}
function random() private view returns(uint){
return uint(keccak256(block.difficulty,block.timestamp,players));
}
function winner() public payable{
uint index = random() % players.length;
players[index].send(address(this).balance);
players = new address[](0);
}
}
我在 remix 上运行您的代码并在线注释了错误。 现在编译成功:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.11;
contract lottery
{
address manager;
// send and transfer are only available for objects of type "address payable"
// address[] public players;
address payable[] public players;
function setManager() public{
manager = msg.sender;
}
function enterLottery () public payable{
require(msg.value > 0.9 ether);
// msg.sender was payable before version 8. we have to explicitly set it as payable
players.push(payable(msg.sender));
}
function random() private view returns(uint){
// Wrong argument count for function call:3 arguments given but expected 1. this function requires a single byte argument
// return uint(keccak256(block.difficulty,block.timestamp,players));
return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp)));
}
function winner() public payable{
uint index = random() % players.length;
// Failurer condition of 'send' is ignored. Consider using 'transfer' instead
// players[index].send(address(this).balance);
players[index].transfer(address(this).balance);
// type address[] memory is not implicitly convertible to expected type address payable[] storage ref
// players = new address[](0);
players = new address payable[](0);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.