I'm trying to get back all the struct
type records from my solidity contract
function please see the below solidity contract
code snippet
struct Record {
uint _id;
string _countryCode;
string _state;
string _postal;
string _location;
string _userId;
string _additionalDetails;
}
Record[] public records;
function getEntries() public view returns (Record[] memory) {
return records;
}
function setRecord(
string memory _countryCode,
string memory _state,
string memory _postal,
string memory _location,
string memory _userId,
string memory _additionalDetails
) public onlyOwner {
Record memory newStruct = Record({
_id: _counter + 1,
_countryCode: _countryCode,
_state: _state,
_postal: _postal,
_location: _location,
_userId: _userId,
_additionalDetails: _additionalDetails
});
records.push(newStruct);
_counter++;
}
I'm successfully able to add a new record using tronweb
in nodejs
below in my NodeJs
code snippet
const walletAddress = "";
const privateKey = "";
const contractAddress = "";
tronweb.setAddress(walletAddress);
tronweb.setPrivateKey(privateKey);
const init = async () => {
let contract = await tronweb.contract().at(contractAddress);
const getRecords = async () => await contract.getEntries().call();
const setRecord = async () => await contract.setRecord(
"USA",
"LA",
"12345",
"Street 1",
"1324-12345-12456-45642",
"Testing notes"
).send({
"from": walletAddress
});
await setRecord();
getRecords().then(result => {
console.log(result); // getting empty array like [ [], [] ]
});
};
init();
Here is the console image of my nodejs script results which is always returning an empty array of arrays
Here is the Shasta Network transaction details with status CONFIRMED
Is there anyone who can help me?
In ABI, any return value of a struct type will be interpreted as tuple
. In your case, its type is tuple[]
.
The library has no clue what's your tuple
is, how long it is, what's the type.
You should parse the return data by hand.
{
"constant_result": [
"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000005800000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000003555341000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024c4100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000053132333435000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853747265657420310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016313332342d31323334352d31323435362d343536343200000000000000000000000000000000000000000000000000000000000000000000000000000000000d54657374696e67206e6f74657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000003555341000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024c4100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000053132333435000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853747265657420310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016313332342d31323334352d31323435362d343536343200000000000000000000000000000000000000000000000000000000000000000000000000000000000d54657374696e67206e6f74657300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000003555341000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024c4100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000053132333435000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853747265657420310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016313332342d31323334352d31323435362d343536343200000000000000000000000000000000000000000000000000000000000000000000000000000000001254657374696e672077697468204a616d65730000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000003555341000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024c4100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000053132333435000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853747265657420310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016313332342d31323334352d31323435362d343536343200000000000000000000000000000000000000000000000000000000000000000000000000000000001e54657374696e672077697468204a616d6573206e65772076657273696f6e0000"
],
"result": {
"code": "SUCCESS",
"message": "",
"result": true
}
}
To parse the constant_result:
0000000000000000000000000000000000000000000000000000000000000020 offset of first parm
0000000000000000000000000000000000000000000000000000000000000004
0000000000000000000000000000000000000000000000000000000000000080
0000000000000000000000000000000000000000000000000000000000000300
0000000000000000000000000000000000000000000000000000000000000580
0000000000000000000000000000000000000000000000000000000000000800
0000000000000000000000000000000000000000000000000000000000000001 #first
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000140
0000000000000000000000000000000000000000000000000000000000000180
00000000000000000000000000000000000000000000000000000000000001c0
0000000000000000000000000000000000000000000000000000000000000200
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000240
0000000000000000000000000000000000000000000000000000000000000003 a string with 3 chars
5553410000000000000000000000000000000000000000000000000000000000 "USA"
0000000000000000000000000000000000000000000000000000000000000002 a string with 2 chars
4c41000000000000000000000000000000000000000000000000000000000000 "LA"
0000000000000000000000000000000000000000000000000000000000000005
3132333435000000000000000000000000000000000000000000000000000000 '12345'
0000000000000000000000000000000000000000000000000000000000000008
5374726565742031000000000000000000000000000000000000000000000000 'Street 1'
0000000000000000000000000000000000000000000000000000000000000016
313332342d31323334352d31323435362d343536343200000000000000000000 '1324-12345-12456-45642'
000000000000000000000000000000000000000000000000000000000000000d
54657374696e67206e6f74657300000000000000000000000000000000000000 'Testing notes'
0000000000000000000000000000000000000000000000000000000000000002 #second
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000140
0000000000000000000000000000000000000000000000000000000000000180
00000000000000000000000000000000000000000000000000000000000001c0
0000000000000000000000000000000000000000000000000000000000000200
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000240
0000000000000000000000000000000000000000000000000000000000000003
5553410000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000002
4c41000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000005
3132333435000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000008
5374726565742031000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000016
313332342d31323334352d31323435362d343536343200000000000000000000
000000000000000000000000000000000000000000000000000000000000000d
54657374696e67206e6f74657300000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000003 #third
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000140
0000000000000000000000000000000000000000000000000000000000000180
00000000000000000000000000000000000000000000000000000000000001c0
0000000000000000000000000000000000000000000000000000000000000200
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000240
0000000000000000000000000000000000000000000000000000000000000003
5553410000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000002
4c41000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000005
3132333435000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000008
5374726565742031000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000016
313332342d31323334352d31323435362d343536343200000000000000000000
0000000000000000000000000000000000000000000000000000000000000012
54657374696e672077697468204a616d65730000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000004 #fourth
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000140
0000000000000000000000000000000000000000000000000000000000000180
00000000000000000000000000000000000000000000000000000000000001c0
0000000000000000000000000000000000000000000000000000000000000200
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000240
0000000000000000000000000000000000000000000000000000000000000003
5553410000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000002
4c41000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000005
3132333435000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000008
5374726565742031000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000016
313332342d31323334352d31323435362d343536343200000000000000000000
000000000000000000000000000000000000000000000000000000000000001e
54657374696e672077697468204a616d6573206e65772076657273696f6e0000