2

In solidity do i return an array or structs in bytes.

This is not a duplicate of this question because that solution always returns the error: new BigNumber() not a number: [object Object]

pragma solidity ^0.4.2;
contract Something {
    mapping (address => Ton[]) allTons;

    struct Ton { 
        uint id;
        string name;
        bool access;
    }

    function Something() public {
        allTons[msg.sender].push(Ton({
            id: 1,
            name: "CoolDude",
            access: true
        }));
        allTons[msg.sender].push(Ton({
            id: 2,
            name: "NotCoolDude",
            access: false
        }));
    }

    function GiveBytes() public constant returns(bytes){
        // unsure

    }


}
1

I fiddled around with it until this works.

There is possibly a more elegant solution but you should be warned that returning arrays and structs is very new. Also, it will not scale as well as this pattern.

This will have a fixed gas cost at any scale by relying on the client to iterate over the function if wants the whole table.

pragma solidity ^0.4.18;

contract Something {

    mapping (address => Ton[]) allTons;

    struct Ton { 
        uint id;
        string name;
        bool access;
    }

    function Something() public {

        allTons[msg.sender].push(Ton({
            id: 1,
            name: "CoolDude",
            access: true
        }));

        allTons[msg.sender].push(Ton({
            id: 2,
            name: "NotCoolDude",
            access: false
        }));
    }

    // fish them out one row at a time
    // and return a simpler response.

    function getTonAtRow(address user, uint row) public constant returns(uint, string, bool) {
        return(allTons[user][row].id, allTons[user][row].name, allTons[user][row].access);

    }

}

More elaboration over here: Are there well-solved and simple storage patterns for Solidity?

Hope it helps.

  • Well this doesnt really help for the array of structs, – Alain Goldman Dec 21 '17 at 15:40
0

Just declare your mapping AllTons as public and you will get a default getter function that takes an address and the index of the array. Then define a function to return the number of items in the array, given an address. Iterate the getter function to get all your items.

pragma solidity ^0.4.23;

contract Something {

    mapping (address => Ton[]) public allTons;

    struct Ton { 
        uint id;
        string name;
        bool access;
    }

    constructor() public {

        allTons[msg.sender].push(Ton({
            id: 1,
            name: "CoolDude",
            access: true
        }));

        allTons[msg.sender].push(Ton({
            id: 2,
            name: "NotCoolDude",
            access: false
        }));
    }

    // Get the items count first,
    // then use the default allTons getter to get each item.

    function getTonCount(address _address) public view returns (uint _count) {
        _count = allTons[_address].length;        
    }

}

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.