2

I'm a beginner in develop solidity and using Foundry.
I've created a contract which is include some state value as below

    address[] public owners;
    mapping(address => bool) public isOwner;
    mapping(uint256 => mapping(address => bool)) public approved;
    uint256 public requireNum;
    Transaction[] public transactions;

I use cast call $CONTRACT_ADDRESS "requireNum()(uint)" is able to get requireNum, everything is fine.

But when I tried to use cast call $CONTRACT_ADDRESS "owners()(address[])" to get owners, I got some error

Error: 
(code: 3, message: execution reverted, data: Some(String("0x")))

I'm not sure how to get the state value if it is a array or struct of address or even if it is a mapping. Hope anyone can help, thx

2 Answers 2

1

Now I can use command below to get values

get owner in index 0 and return as type address
cast call $CONTRACT_ADDRESS "owners(uint)(address)" 0

check is owner

cast call $CONTRACT_ADDRESS "isOwner(address)(bool)" 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

But still not find a way to get entire array or mapping

0

Solidity is not intended to return large data sets, especially when the size is not known as in an mapping.

The thing is, what do you need the data for? If its for the command line, maybe you want a list of addresses separated by new lines. You could implement something like this:

function getOwners() public returns(string memory) {
    string memory s_owners;
    for (uint i = 0; i < owners.length; i++) {
        s_owners = string.concat(s_owners, "\n", addressToString(owners[i]));
    }
    return s_owners;
}

function addressToString(address _address) public pure returns(string memory) {
    bytes memory data = abi.encodePacked(_address);
    bytes memory str = new bytes(data.length * 2);
    bytes memory alphabet = "0123456789abcdef";
    for (uint i = 0; i < data.length; i++) {
        str[i*2] = alphabet[uint(uint8(data[i] >> 4))];
        str[i*2+1] = alphabet[uint(uint8(data[i] & 0x0f))];
    }
    return string.concat("0x", string(str));
}
1
  • 1
    Thanks for your explain, this make sense. And yes as you said I just wanna check for the command line, but seems this is not a good practice to see entire array or mapping. Anyway thanks for your answer again.
    – Jung
    Jul 13, 2023 at 8:50

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.