Reading structs
The solidity storage layout documentation (opens in a new tab) outlines that "The elements of structs and arrays are stored after each other, just as if they were given as individual values."
As such, once the slot pointer points to the root of a struct, we can view contiguous items by simply incrementing the slot pointer. The Builder API exposes methods to do this (addSlot
).
Contract definition
Example.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
contract Example {
struct Item {
uint256 index;
string name;
}
Item root;
constructor() {
root.index = 0;
root.name = "root";
}
}
Reading values
Can have the name
value of its constructed struct accessed as follows:
example.js
const OUTPUT_COUNT = 1;
const CONTRACT_ADDRESS = "0x123..;
new GatewayRequest(OUTPUT_COUNT) //Specify the number of outputs
.setTarget(CONTRACT_ADDRESS) //Specify the contract address
.setSlot(0) //Specify the base slot number
.push(1) //Pass the value 1 to the Virtual Machine
.addSlot() //This increments the slot ID by the last passed values
.readBytes() //Read the bytes
.setOutput(0); //Set it at output index 0