传递 uint256 数组参数

huangapple go评论61阅读模式
英文:

Passing argument of uint256 array

问题

在Solidity中,将uint256数组传递给函数时,应该使用以下格式:

- ["500000000000000000000000000", "300000000000000000000000000"]
- ["0x1dcd6500", "0x11e1a300"]
- ['0x1dcd6500', '0x11e1a300']
- [0x1dcd6500, 0x1dcd6500]
- "[500000000000000000000000000, 300000000000000000000000000]"
- "[0x1dcd6500, 0x1dcd6500]"
英文:

In Solidity what should be the correct format when passing an array of uint256 in etherscan to a function?
Function

    function sendAmount(address[] memory recipients, uint256[] memory amount) external onlyOwner {
        require(recipients.length > 0, "No recipients specified.");
        require(amount.length > 0, "Amount should be greater than zero.");

        for (uint256 i = 0; i < recipients.length; i++) {
            _transfer(msg.sender, recipients[i], amount[i]);
        }
    }  

I tried to pass, samples below, but all are getting an invalid Big Number error.

  • ["500000000000000000000000000", "300000000000000000000000000"]
  • ["0x1dcd6500", "0x11e1a300"]
  • ['0x1dcd6500', '0x11e1a300']
  • [0x1dcd6500, 0x1dcd6500]
  • "[500000000000000000000000000, 300000000000000000000000000]"
  • "[0x1dcd6500, 0x1dcd6500]"

Screenshot of function call

答案1

得分: 1

[1,2,3] 这应该是正确的。您可以使用 Remix 上的以下示例代码进行测试。

contract ContractB {
    address contractAddress = 0x9D7f74d0C41E726EC95884E0e97Fa6129e3b5E99;
    function total(uint[] memory numbers) public view returns (uint) {
        uint tot = 0;
        for (uint i = 0; i < numbers.length; i++){
            tot += numbers[i];
        }
        return tot;
    }
}

当您调用 total 时,只需传入 [1,2,3],您将得到 6。

英文:

[1,2,3] this should be correct. you can test it with this sample code on Remix.

contract ContractB  {
    address contractAddress = 0x9D7f74d0C41E726EC95884E0e97Fa6129e3b5E99;
    function total(uint[] memory numbers) public view returns (uint) {
        uint tot=0;
        for (uint i=0;i&lt;numbers.length;i++){
            tot+=numbers[i];
        }
        return tot;
    }
}

when you call total, just pass [1,2,3] you will get 6.

答案2

得分: 0

如果您有一个uint256[]数组,想要为其分配值,您需要提供与之相同类型的uint256值。您提供的值不应该用引号括起来,因为那样会使它们被视为字符串而不是数值。

不要使用["500000000000000000000000000", "300000000000000000000000000"],尝试将数组写成[500000000000000000000000000, 300000000000000000000000000]。

英文:

If you have a uint256[] array and you want to assign values to it, you need to provide values that are of the same i.e. type uint256. The values you provide should not be enclosed in quotation marks, as that would make them treated as strings instead of numerical values.

Instead of ["500000000000000000000000000", "300000000000000000000000000"], try taking the array as [500000000000000000000000000, 300000000000000000000000000]

huangapple
  • 本文由 发表于 2023年5月28日 21:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定