Created
September 17, 2021 20:52
-
-
Save weserickson/340166c8198a7b76bcaf72f36008a1ed to your computer and use it in GitHub Desktop.
Demo of getter to grab data from array of structs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma soliditypp ^0.4.3; | |
// modification of answer from | |
// https://stackoverflow.com/questions/48877910/how-can-i-return-an-array-of-struct-in-solidity | |
// for soliditypp | |
contract Project | |
{ | |
struct Person { | |
address addr; | |
uint bal; | |
} | |
Person[] people; | |
onMessage addPerson(address addr, uint bal){ | |
people.push(Person(addr, bal)); | |
} | |
// returns selected people | |
getter getPeople(uint[] calldata indexes) returns (address[] memory addr, uint[] memory bal) { | |
address[] memory addrs = new address[](indexes.length); | |
uint[] memory bals = new uint[](indexes.length); | |
for (uint i = 0; i < indexes.length; i++) { | |
Person storage person = people[indexes[i]]; | |
addrs[i] = person.addr; | |
bals[i] = person.bal; | |
} | |
return (addrs, bals); | |
} | |
// returns all people | |
getter getAllPeople() returns (address[] memory addr, uint[] memory bal) { | |
address[] memory addrs = new address[](people.length); | |
uint[] memory bals = new uint[](people.length); | |
for (uint i = 0; i < people.length; i++) { | |
Person storage person = people[i]; | |
addrs[i] = person.addr; | |
bals[i] = person.bal; | |
} | |
return (addrs, bals); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment