Solidity Structs – Cannot Retrieve uint Array from a Struct in Solidity with Truffle Test

arrayssoliditystructtruffle-test

I have the following struct in my contract

struct Transaction {
    TransactionType transactionType;
    uint256 id;
    uint256 productId;
    uint256 timestamp;
    address from;
    address to;
    uint256[] source;
    uint256 test;
}

which I then initialize like this

uint256[] memory source = new uint256[](1);
source[0] = 55;
Transaction memory transaction = Transaction(*other params*, source, 999);

In a test where I let above code run and examine the transaction in the debugger it looks like the following:

enter image description here

The object is supposed to contain 8 elements, but only 7 are shown.
Following the address fields at indices 4/5 is the uint field test.

What happened to the array field source?

Best Answer

Reference types (arrays, mappings, struct) that are part of the structure are not being returned when you try to return structure data. You can notice that only value types are returned.

You should create a function that would act as a getter that will access the data you need and return it back.

For your example:

function getSources(uint transactionId) external returns (uint[] memory sources) {
      sources = transactions[transactionId].sources; // In case you have array of transactions
}