1

I'm trying to create an array contains addresses which belongs to particular transaction ID.

Array structure:

transactions[0x12345][0] = 123456
transactions[0x12345][1] = 789101
transactions[0x12345][2] = 865436

Expected output when calling getAccountTransactions():

[0] = 123456
[1] = 789101
[2] = 865436

Code:

mapping (address => mapping(uint => uint)) public transactions;

function getAccountTransactions() public returns (uint[] tx) {

    return transactions[msg.sender];

}

Compiler returns error:

TypeError: Return argument type mapping(uint256 => uint256) is not implicitly convertible to expected type (type of first return variable) uint256[] memory.

1 Answer 1

1

The main issue with your code is that you are trying to return a uint[] which is an array, however you defined your variable as a mapping of mappings, and mapping and array are different types.

You could get your code to compile like this:

pragma solidity ^0.4.24;

contract test {

    mapping (address => uint[]) public transactions;

    constructor() public {
        transactions[msg.sender].push(123456);
        transactions[msg.sender].push(789101);
        transactions[msg.sender].push(865436);
    }

    function getTransactions() public view returns (uint[]) {

        return transactions[msg.sender];

    }
}

However in general, I would not recommend a single function which returns all the values. While this may be useable in a web front-end, it will not scale as a normal contract function, since eventually you will reach a number of objects in the array where the gas cost to return them all will be too large for the evm.

Instead, I suggest you break it into two functions:

pragma solidity ^0.4.24;

contract test {

    mapping (address => uint[]) public transactions;

    constructor() public {
        transactions[msg.sender].push(123456);
        transactions[msg.sender].push(789101);
        transactions[msg.sender].push(865436);
    }

    function getTransactionsLength() public view returns (uint) {

        return transactions[msg.sender].length;

    }

    function getTransactionsValue(uint index) public view returns (uint) {

        return transactions[msg.sender][index];

    }
}

One is getTransactionsLength(), and the other is getTransactionsValue(). Both of these functions return just a single uint, which means you will have predictable output size and gas costs, which means it can be used in other parts of your contract.

Furthermore, you will also create better user experiences by controlling the number of values you are showing. If you need to show all the values, you could first get the length, and then loop the value call from 0 to length-1.

But even better, you may only want to show the last 10 transactions in the UI, where then you can loop from length-1 to length-11. These may seem like complexities at first, but in reality will be simplifications to your dApp is it grows; things you will really come to appreciate from immutable programs :)

3
  • I cannot vote but thank you very much for the quick answer. Your example bring me some new ideas that I'm going to implement to my contract :)
    – Wojtek B
    Oct 30, 2018 at 11:27
  • No problem. If this answers your question, should be able to mark the answer as correct, which will the forum know that the problem is resolved! Good luck :) Oct 30, 2018 at 11:33
  • Sure, message marked :)
    – Wojtek B
    Oct 30, 2018 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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