I have this solidity struct

struct Prices {
    uint sell_price;        // wei
    uint target_price;      // primary
    uint penalty_price;     // wei
Prices public prices

In truffle when I do contract.prices() I retrieve an array [sell_price, target_price, penalty_price] , the order which they are listed in the struct. I wanted to know if this order is guaranteed?



The getter function that Solidity generates uses the order listed in the struct. Solidity would generate a function of the following form:

function prices() constant returns (uint sell_price, uint target_price, uint penalty_price) {
  sell_price = prices.sell_price;
  target_price = prices.target_price;
  penalty_price = prices.penalty_price;

Here's the ABI and see that the outputs have a specific order:

    "constant": true,
    "inputs": [],
    "name": "prices",
    "outputs": [
        "name": "sell_price",
        "type": "uint256"
        "name": "target_price",
        "type": "uint256"
        "name": "penalty_price",
        "type": "uint256"
    "payable": false,
    "type": "function"

Additionally, a more complex example of a getter from http://solidity.readthedocs.io/en/develop/contracts.html#getter-functions

contract Complex {
    struct Data {
        uint a;
        bytes3 b;
        mapping (uint => uint) map;
    mapping (uint => mapping(bool => Data[])) public data;

It will generate a function of the following form:

function data(uint arg1, bool arg2, uint arg3) returns (uint a, bytes3 b) {
    a = data[arg1][arg2][arg3].a;
    b = data[arg1][arg2][arg3].b;

Note that the mapping in the struct is omitted because there is no good way to provide the key for the mapping.



The ABI is a collection of fixed-length arguments in a specific order. The elements will be reliably/consistently ordered.

Hope it helps.

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.