Ethereum 数组作为映射变量的函数参数

Ethereum 数组作为映射变量的函数参数,ethereum,solidity,smartcontracts,Ethereum,Solidity,Smartcontracts,假设一个函数接受一个地址数组,如下所示: function setVoters(address[] _inputAddresses) public ownerOnly { // [...] } 使用上述函数的同一契约有一个定义为映射的变量: mapping(address => bool) voter; 当涉及到气体消耗/费用时,在数组上循环并将其推送到映射被认为是最好的选择,还是如果函数接受一个地址并通过一些JavaScript功能从给定的UI进行迭代会更好 选项a: Ja

假设一个函数接受一个地址数组,如下所示:

function setVoters(address[] _inputAddresses) public ownerOnly {
    // [...]
}
使用上述函数的同一契约有一个定义为映射的变量:

mapping(address => bool) voter;
当涉及到气体消耗/费用时,在数组上循环并将其推送到映射被认为是最好的选择,还是如果函数接受一个地址并通过一些JavaScript功能从给定的UI进行迭代会更好

选项a

JavaScript看起来像这样


就气体效率而言,最好的选择是选项a,调用函数需要相当多的气体,因此,如果在一个大的tx中调用所有函数,而不是在多个小tx中调用,则所需的费用会更低。

因此,我在a中也被告知。虽然出于视觉上的原因,我希望得到一个每个选项的汽油成本的答案,但你的答案符合我的要求
function setVoters(address[] _inputAddresses) public ownerOnly {
    // [...]
    for (uint index = 0; index < _inputAddresses.length; index++) {
        voter[_inputAddresses[index]] = true;
    }
}
function setVoter(address _inputAddress) public ownerOnly {
    // [...]
    voter[_inputAddress] = true;
}
// loop condition starts here
    await task.methods.setVoter(address[key]).send({
        from: accounts[0]
    });
// loop condition ends here