Mappings

Reading a mapping

Contract definition

Example.sol
    contract X {
        mapping(uint256=>string) exampleMapping;          
 
        constructor() {
            exampleMapping[42] = "string";
        }
    }

Reading values

The value at index 42 can be read as follows:

example.js
 
    const OUTPUT_COUNT = 1;
    const CONTRACT_ADDRESS = "0x123..;
    const MAPPING_KEY = 42;
 
    new GatewayRequest(OUTPUT_COUNT)        //Specify the number of outputs
        .setTarget(CONTRACT_ADDRESS)            //Specify the contract address
        .setSlot(0)                     //Specify the slot number
        .push(MAPPING_KEY)              //Push the key onto the stack
        .follow()                       //Follow that key 
        .readBytes()                    //Read the bytes
        .setOutput(0)                   //Set it at output index 0