无法将整个合同金额可靠地发送到账户
cannot send entire contract amount to account in solidity
我创建了一个彩票合约,我将所有参与的玩家存储在一个地址数组中
我在将我的合同资金转移给赢家时遇到错误,即功能赢家
另一个错误是将哈希值转换为 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);
}
}
我创建了一个彩票合约,我将所有参与的玩家存储在一个地址数组中 我在将我的合同资金转移给赢家时遇到错误,即功能赢家 另一个错误是将哈希值转换为 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);
}
}