如何在 solidity 0.5.2 或更高版本中分配或重置 address[] payable 变量?
How to assign or reset the address[] payable variable in solidity 0.5.2 or above?
我使用的版本是0.5.2
我正在 Remix IDE
中执行以下代码
pragma solidity ^0.5.2;
contract Lottery {
address public manager;
address payable[] public players;
constructor () public {
manager = msg.sender;
}
function enter() public payable {
require(msg.value > 0.01 ether);
players.push(msg.sender);
}
// function getPlayers() public view returns(address[] memory) {
// return players;
// }
function random() public view returns(uint) {
return uint(keccak256(abi.encodePacked(block.difficulty, now, players)));
}
function pickWinner() public {
uint index = random() % players.length;
players[index].transfer(address(this).balance);
players = new address[](0); // This line of code giving an error
}
}
我得到的错误是:
Type address[] memory is not implicitly convertible to expected type address payable[] storage ref.
在函数 pickWinner() 中:
function pickWinner() public {
uint index = random() % players.length;
players[index].transfer(address(this).balance);
players = new address[](0); // This line of code giving an error
}
我正在尝试将我的玩家数组全部重置为 0 以重置我的彩票合同
可能 best/easiest 要做的事情是 players.length = 0
。
请注意,这将使用与数组中元素数量成比例的气体(因为它会删除所有元素)。如果这是一个问题,您可能需要考虑使用映射来代替单独存储的长度。例如
mapping(uint256 => address payable) players;
uint256 playersLength;
然后playersLength = 0
到"reset."
编辑
根据评论,您似乎没有看到基于数组大小的 gas 使用情况。这是在 Remix 中测试的简单方法:
pragma solidity 0.5.2;
contract Test {
uint256[] foo;
uint256[] bar;
constructor() public {
for (uint256 i = 0; i < 5; i++) {
foo.push(i);
}
for (uint256 i = 0; i < 100; i++) {
bar.push(i);
}
}
function deleteFoo() external {
foo.length = 0;
}
function deleteBar() external {
bar.length = 0;
}
}
在我的测试中,使用 JavaScript VM,deleteFoo
消耗 26,070 gas,deleteBar
消耗 266,267 gas。
function pickWinner() public {
uint index = random() % players.length;
players[index].transfer(address(this).balance);
players = new address payable [](0); //add payable to this line
}
我使用的版本是0.5.2
我正在 Remix IDE
中执行以下代码pragma solidity ^0.5.2;
contract Lottery {
address public manager;
address payable[] public players;
constructor () public {
manager = msg.sender;
}
function enter() public payable {
require(msg.value > 0.01 ether);
players.push(msg.sender);
}
// function getPlayers() public view returns(address[] memory) {
// return players;
// }
function random() public view returns(uint) {
return uint(keccak256(abi.encodePacked(block.difficulty, now, players)));
}
function pickWinner() public {
uint index = random() % players.length;
players[index].transfer(address(this).balance);
players = new address[](0); // This line of code giving an error
}
}
我得到的错误是:
Type address[] memory is not implicitly convertible to expected type address payable[] storage ref.
在函数 pickWinner() 中:
function pickWinner() public {
uint index = random() % players.length;
players[index].transfer(address(this).balance);
players = new address[](0); // This line of code giving an error
}
我正在尝试将我的玩家数组全部重置为 0 以重置我的彩票合同
可能 best/easiest 要做的事情是 players.length = 0
。
请注意,这将使用与数组中元素数量成比例的气体(因为它会删除所有元素)。如果这是一个问题,您可能需要考虑使用映射来代替单独存储的长度。例如
mapping(uint256 => address payable) players;
uint256 playersLength;
然后playersLength = 0
到"reset."
编辑
根据评论,您似乎没有看到基于数组大小的 gas 使用情况。这是在 Remix 中测试的简单方法:
pragma solidity 0.5.2;
contract Test {
uint256[] foo;
uint256[] bar;
constructor() public {
for (uint256 i = 0; i < 5; i++) {
foo.push(i);
}
for (uint256 i = 0; i < 100; i++) {
bar.push(i);
}
}
function deleteFoo() external {
foo.length = 0;
}
function deleteBar() external {
bar.length = 0;
}
}
在我的测试中,使用 JavaScript VM,deleteFoo
消耗 26,070 gas,deleteBar
消耗 266,267 gas。
function pickWinner() public {
uint index = random() % players.length;
players[index].transfer(address(this).balance);
players = new address payable [](0); //add payable to this line
}