内存中的坚固性动态数组:溢出错误

发布于 2025-01-26 19:52:06 字数 1961 浏览 1 评论 0原文

我试图创建一个彩票智能合约,该合同存储每个机票(Ticket =地址),有人将其投入到阵列中,然后从中获得精选的获奖者。不幸的是,我偶然发现了第16行的溢出错误,我抬头看了看调试器,但我不明白为什么它会溢出。第一件代码是我看到许多人使用固定数组而不是动态阵列后提出的解决方案,但仍然不起作用。我只想了解为什么它会引发该错误以及如何解决。预先感谢您的帮助!

错误:

“错误”:“无法解码输出:错误:溢出(FARS =“溢出”,uperation =“ Tonumber”,value =“ 35408467139434345059221743318723196451964531694949494900783006255555555387966666666690599909999099999999999996666. Bignumber/5.5.0)”

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Lottery{

    address[] public s_players; //storage

    function enterLottery(uint _tickets) public payable returns (address[] memory) {
        uint lotteryCost = _tickets*250000000000000000;
        address sender = msg.sender;
        require(msg.value >= lotteryCost, 'Ticket cant be purchased');
        address[] memory _players = new address[](1000);
        _players = s_players;
        uint index = _players.length;
        for (uint x = 0; x < _tickets; x++){
            _players[index] = payable(sender); //.push() only storage arrays = use index
            index += 1;
        }
        s_players = _players;
        // Tickets as input, return players
        return s_players;
    }
}
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Lottery {

    address payable[] public s_players; //storage

    function enterLottery(uint _tickets) public payable returns (address payable[] memory) {
        uint lotteryCost = _tickets*500000000 gwei;
        require(msg.value >= lotteryCost, 'Ticket cant be purchased');
        address payable[] memory _players = s_players;
        uint length = _players.length;
        for (uint x = 0; x < _tickets; x = unsafe_inc(x)){
            _players[length] = payable(msg.sender); //.push() only storage arrays = use index
            length += 1;
        }
        s_players = _players;
        // Tickets as input, return players
        return s_players;
    }
}

I was trying to create a lottery smart contract that stores every tickets (ticket = address) that someone buys into into an array to later on select winners from it. Unfortunately I stumbled into an overflow error at line 16, I have looked up at the debugger but I can't understand why it goes into overflow. The first piece of code is a solution that I come up to after seeing many people using fixed arrays instead of dynamic ones but still it doesn't work. I just want to understand why it throws that error and how I can solve it. Thank you in advance for your help!

the error:

"error": "Failed to decode output: Error: overflow (fault="overflow", operation="toNumber", value="35408467139433450592217433187231851964531694900788300625387963629091585785856", code=NUMERIC_FAULT, version=bignumber/5.5.0)"

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Lottery{

    address[] public s_players; //storage

    function enterLottery(uint _tickets) public payable returns (address[] memory) {
        uint lotteryCost = _tickets*250000000000000000;
        address sender = msg.sender;
        require(msg.value >= lotteryCost, 'Ticket cant be purchased');
        address[] memory _players = new address[](1000);
        _players = s_players;
        uint index = _players.length;
        for (uint x = 0; x < _tickets; x++){
            _players[index] = payable(sender); //.push() only storage arrays = use index
            index += 1;
        }
        s_players = _players;
        // Tickets as input, return players
        return s_players;
    }
}
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Lottery {

    address payable[] public s_players; //storage

    function enterLottery(uint _tickets) public payable returns (address payable[] memory) {
        uint lotteryCost = _tickets*500000000 gwei;
        require(msg.value >= lotteryCost, 'Ticket cant be purchased');
        address payable[] memory _players = s_players;
        uint length = _players.length;
        for (uint x = 0; x < _tickets; x = unsafe_inc(x)){
            _players[length] = payable(msg.sender); //.push() only storage arrays = use index
            length += 1;
        }
        s_players = _players;
        // Tickets as input, return players
        return s_players;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烦人精 2025-02-02 19:52:06

这是代码。溢出来自以下事实:您的动态阵列设置为静态数组值,并带有77位数字的数字。在坚固的情况下,最大的数字是2^255-1,因为那是可以存储256位的信息。 (5.7896045E76)

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Lottery {

    // dynamic array that stores all players
    address[] public s_players;

    // use _tickets when its a private function, it is a convention
    function enterLottery(uint tickets) public payable returns (address[] memory) {

        // we can use ether to represent 10**18 to make code clearer
        uint lotteryCost = _tickets * 25 ether / 100;
        address sender = msg.sender;
        require(msg.value >= lotteryCost, 'Ticket cant be purchased');

        // we use the for loop to push to the s_players array each iteration        
        for (uint x = 0; x < _tickets; x++){
            s_players.push(payable(sender));
            /*
            instead of creating a new length variable in this code,
            we could have instead just used x, because each time it
            loops, it is incremented (x++) */
        }
        
        // we then return the storage array
        return s_players;
    }

}

Here is the code. The overflow came from the fact that your dynamic array was set to a static array value, and it lead to a number with 77 digits. In solidity the biggest possible number is 2^255 - 1, as that is how much 256 bits of information can store. (5.7896045e76)

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Lottery {

    // dynamic array that stores all players
    address[] public s_players;

    // use _tickets when its a private function, it is a convention
    function enterLottery(uint tickets) public payable returns (address[] memory) {

        // we can use ether to represent 10**18 to make code clearer
        uint lotteryCost = _tickets * 25 ether / 100;
        address sender = msg.sender;
        require(msg.value >= lotteryCost, 'Ticket cant be purchased');

        // we use the for loop to push to the s_players array each iteration        
        for (uint x = 0; x < _tickets; x++){
            s_players.push(payable(sender));
            /*
            instead of creating a new length variable in this code,
            we could have instead just used x, because each time it
            loops, it is incremented (x++) */
        }
        
        // we then return the storage array
        return s_players;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文