Builder API Methods

Builder API Methods

This reference outlines the methods that are exposed to contract authors for building a GatewayRequest to read data from other chains.

Internally these methods utilize OP codes (opens in a new tab) that our Virtual Machine can interpret and execute.

Targets

These methods modifies the target contract for operations that follow.
Sets the target contract to the specified address.
JavascriptSolidity
setTarget(x: HexString)setTarget(address a)
Sets the target contract to the top item on the stack.
JavascriptSolidity
target()target()

Slot Manipulation

These methods manipulate the slot from which data will be read when a read command is executed.
Sets the current slot to x.
JavascriptSolidity
setSlot(x: BigNumberish)setSlot(uint256 x)
Sets the current slot to the value at the top of the stack.
JavascriptSolidity
slot()slot()
Increments the current slot by x.
JavascriptSolidity
offset(x: BigNumberish)offset(uint256 dx)
Increments the current slot by the value at the top of the stack.
JavascriptSolidity
addSlot()addSlot()
Updates the slot based on a key defined at the top of the stack.
JavascriptSolidity
follow()follow()
JavascriptSolidity
followIndex()followIndex()

Reading Values

These commands read data from the target contract.
Reads the value of n slots starting from the current VM slot.
JavascriptSolidity
read(n = 1)read() / read(uint256 n)
Reads the bytes value at the current storage slot.
JavascriptSolidity
readBytes()readBytes()
JavascriptSolidity
readHashedBytes()readHashedBytes()

Reads an array value at the current storage slot.
The step represents the array value length (in bytes).

JavascriptSolidity
readArray(step: number)readArray(uint256 step)

Outputs

These commands set outputs that will be returned to the callback in the implementing contract.
Sets the output at index i to the item at the HEAD of the stack.
JavascriptSolidity
setOutput(i: number)setOutput(uint8 i)
Sets the output at the index defined at the HEAD of the stack to the value found at HEAD-1
JavascriptSolidity
output()output()

Nested Requests

These commands allow you to evaluate nested requests.

!! Advanced functionality - more documentation coming.

Will evaluate an encoded request that has been pushed onto the HEAD of the stack.
JavascriptSolidity
eval()eval()
Will evaluate an encoded request that has been pushed onto the HEAD of the stack IF the previous operation resolves to true.
JavascriptSolidity
evalIf()evalIf()
Will evaluate an encoded request that has been pushed onto the HEAD of the stack against the specified number of items on the stack.

Execution results are conditional on passed flags which in Javascript are passed as booleans on object properties, whilst in Solidity they are passed as a uint8.
JavascriptSolidity
evalLoop(opts: {success?: boolean; failure?: boolean; acquire?: boolean; keep?: boolean; count?: number;} = {})evalLoop(uint8 flags)/evalLoop(uint8 flags,uint256 count)

The available evaluation flags are enumerated here: Typescript (opens in a new tab) and Solidity (opens in a new tab). They are:

  • STOP_ON_SUCCESS
  • STOP_ON_FAILURE
  • ACQUIRE_STATE
  • KEEP_ARGS

Exiting

These commands handle assertions, requirements, and graceful request exits.
Exit with the specified exit code.
JavascriptSolidity
exit(exitCode: number)exit(uint8 exitCode)
Exit with the specified exit code if the item at the top of the stack is not zero.
JavascriptSolidity
assertNonzero(exitCode: number)assertNonzero(uint8 exitCode)
Exit with the specified exit code if the current target is not a contract.
JavascriptSolidity
requireContract(exitCode = 1)requireContract(uint8 exitCode)
Exit with the specified exit code if the item at the top of the stack is not zero. Leaves the item on the stack
JavascriptSolidity
requireNonzero(exitCode = 1)requireNonzero(uint8 exitCode)

Stack Manipulation

These commands manipulate the data on the Virtual Machine's internal stack.
JavascriptSolidity
pop()pop()
JavascriptSolidity
dup(back = 0)dup() / dup(uint256 back)
JavascriptSolidity
swap(back = 1)swap() / swap(uint256 back)
JavascriptSolidity
pushOutput(i: number)pushOutput(uint256 i)
JavascriptSolidity
pushStack(i: number)pushStack(uint256 i)
JavascriptSolidity
push(x: BigNumberish | boolean)N/A
JavascriptSolidity
pushStr(s: string)N/A
JavascriptSolidity
pushBytes(v: BytesLike)N/A
JavascriptSolidity
pushProgram(program: GatewayProgram)N/A
Pushes the current slot onto the stack.
JavascriptSolidity
getSlot()getSlot()
Pushes the current target onto the stack.
JavascriptSolidity
getTarget()getTarget()
Pushes the current stack size onto the stack.
JavascriptSolidity
stackSize()stackCount()
Pushes onto the stack if the current target is a contract.
JavascriptSolidity
isContract()isContract()

Data Manipulation

These commands manipulate data on the stack.
Replaces the top two items on the stack with their concatentation.
JavascriptSolidity
concat()concat()
Replaces the top item on the stack with its keccak256 representation.
JavascriptSolidity
keccak()keccak()
Slices n/len bytes from the value at the HEAD of the stack starting at position x/pos
JavascriptSolidity
slice(x: number, n: number)slice(uint256 pos, uint256 len)
Replaces the value at the head of the stack with its length.
JavascriptSolidity
length()length()
Replaces the two items at the top of the stack with their summation.
JavascriptSolidity
plus()plus()
Replaces the two items at the head of the stack with the result of subtracting the value at HEAD from the value at HEAD - 1.
JavascriptSolidity
subtract()subtract()
Replaces the value at the top of the stack with its twos complement.
JavascriptSolidity
twosComplement()twosComplement()
Multiplies the two values at the head of the stack and replaces them with the result.
JavascriptSolidity
times()times()
Divides the value at HEAD-1 of the stack by the value at HEAD and replaces them with the result.
JavascriptSolidity
divide()divide()
Calculates the value at HEAD-1 modulo the value at HEAD and replaces them with the result.
JavascriptSolidity
mod()mod()
Replaces the two items at the head of the stack with the result of the value at HEAD-1 being raised to the power of the value at HEAD.
JavascriptSolidity
pow()pow()
Replaces the two values at the top of the stack with their Bitwise AND
JavascriptSolidity
and()and()
Replaces the two values at the top of the stack with their Bitwise OR
JavascriptSolidity
or()or()
Replaces the two values at the top of the stack with their Bitwise XOR
JavascriptSolidity
xor()xor()
Replaces the value at the top of the stack with its Bitwise NOT
JavascriptSolidity
not()not()
Bitwise shifts the value at the top of the stack by `shift` bits to the left.
JavascriptSolidity
shl(shift: BigNumberish)shl(uint8 shift)
Bitwise shifts the value at the top of the stack by `shift` bits to the right.
JavascriptSolidity
shr(shift: BigNumberish)shr(uint8 shift)

Comparison Operations

These commands perform comparison operations on stack data.
Replaces the value at the top of the stack with a boolean indicating if it is zero.
JavascriptSolidity
isZero()isZero()
Replaces the top two items on the stack with a boolean indicating if they are equal.
JavascriptSolidity
eq()eq()
Replaces the top two items on the stack with a boolean indicating if they are not equal.
JavascriptSolidity
neq()neq()
Replaces the top two items on the stack with a boolean indicating of the value at HEAD-1 is greater than the value at the HEAD.
JavascriptSolidity
gt()gt()
Replaces the top two items on the stack with a boolean indicating of the value at HEAD-1 is less than the value at the HEAD.
JavascriptSolidity
lt()lt()
Replaces the top two items on the stack with a boolean indicating of the value at HEAD-1 is less than or equal the value at the HEAD.
JavascriptSolidity
lte()lte()
Replaces the top two items on the stack with a boolean indicating of the value at HEAD-1 is greater than or equal to the value at the HEAD.
JavascriptSolidity
gte()gte()
Duplicate the top two items on the stack.
JavascriptSolidity
dup2()dup2()
Puts the minimum value out of the last two values on the stack at the HEAD.
JavascriptSolidity
min()min()
Puts the maximum value out of the last two values on the stack at the HEAD.
JavascriptSolidity
max()max()

Debug

These commands aid in debugging your requests.
Used for debugging. Outputs the machine state to the console. Available only in JavaScript.
JavascriptSolidity
debug(label = '')N/A