0

This is a function that returns an array of addresses that confirmed for a transactionId.

    mapping (uint => Transaction) public transactions;
    mapping (uint => mapping (address => bool)) public confirmations; 
    address[] public ownersArr;

    /// @dev Returns array with owner addresses, which confirmed transaction.
    /// @param transactionId Transaction ID.
    /// @return Returns array of owner addresses.
    function getConfirmations(uint transactionId)
        public
        view
        returns (address[] _confirmations)
    {
        address[] memory confirmationsTemp = new address[](ownersArr.length);
        uint count = 0;
        uint i;
        for (i=0; i<ownersArr.length; i++)
            if (confirmations[transactionId][ownersArr[i]]) {
                confirmationsTemp[count] = ownersArr[i];
                count += 1;
            }
        _confirmations = new address[](count);
        for (i=0; i<count; i++)
            _confirmations[i] = confirmationsTemp[i];
    }

I could understand whats the point of running two loops inside this function when only one loop is sufficient to get all the addresses that confirmed for a transactioId.


    function getConfirmations(uint _transactionId) public view returns (address[] _confirmations)
    {
        for (uint i=0; i < ownersArr.length; i++){
            if (confirmations[_transactionId][ownersArr[i]]){
                _confirmations.push(ownersArr[i]);
            }

        }

    }
1

Gnosis Multisig Wallet was implemented under solc v0.4.x.

If you tried to compile your code, then you would know why this is not possible:

_confirmations.push(ownersArr[i]);

Because the compiler would to you:

TypeError: Member "push" is not available in address[] memory outside of storage.
            _confirmations.push(ownersArr[i]);
            ^-----------------^

In other words, dynamic-length arrays can only be declared in global scope (i.e., as state-variables of the contract), while _confirmations is a local array in the function, hence not a dynamic-length array, hence push is not available for it, hence its length needs to be computed before it is allocated, and that length is calculated by the first loop which you couldn't understand the need for.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.