For the complete documentation index, see llms.txt. This page is also available as Markdown.

Dynamic Js Execution

This Dynamic Oracle implementation allows your smart contract to execute a predefined JavaScript function on the Truebit network, providing off-chain computation with on-chain guarantees.

1

Inherit From BaseTBContract

Import BaseTBContract.sol contract and extend from it.

2

Constructor

When deploying the contract, ensure you set the correct values for watchTowerAddress, sourceCode within your contract:

  • watchTowerAddress: 0xEcd0EeD24Ed6a54d05a07125c4ccb809b3A09868.

  • sourceCode: "Place your JS code here inside quotes".

3

Submitting a Request

In your contract, to execute JS code on Truebit, call the _requestExecution function. This will trigger the task execution on the Truebit platform.

To call the _requestExecution , you must:

  1. Define the method signature string.

  2. Encode all parameters using abi.encode.

  3. Specify the execution type as: DOTypes.ExecutionType.EMBEDDED_FUNCTION.

Method Signature:

The method signature must follow a strict format. It begins with the method name (in this case, "function"), followed by parentheses containing the parameter definitions, separated by commas.

Example:

function(uint256)
4

Filling The Payload:

Once the method signature is defined, you must fill the payload with the values for the parameter defined on the signature, and encode it by using abi.encode(...).

5

Call _requestExecution:

Finally, you call _requestExecution() with:

  • The method signature (string).

  • The encoded payload (bytes).

  • The execution type: DOTypes.ExecutionType.EMBEDDED_FUNCTION .

// Prepare the payload for the function execution
bytes memory payload = abi.encode(
    input     // The input parameter for fibonacci calculation
);
// Submit the execution request to the Truebit network
uint256 wtExecutionId = _requestExecution(
    "function(uint256)",
    payload,
    DOTypes.ExecutionType.EMBEDDED_FUNCTION
)
6

Handling The Callback

When the execution is finished, your will receive the result within the callbackTask.

function callbackTask(
        bytes calldata resultData, 
        string[] memory transcripts, 
        uint256 wtExecutionId, 
        uint8 status,
        string memory callbackMessageDetails
    ) external override onlywatchTower {
  • resultData: The encoded result data from the execution.

  • transcripts: Array of execution transcripts for verification.

  • wtExecutionId: The execution identifier.

  • status: Execution status (0 = success, >0 = error).

  • callbackMessageDetails: Additional details about the execution.

Status types

Status
Code
Description

SUCCESS

0

The execution completed successfully.

FAILED

1

This execution is invalid. Check the callbackMessageDetails for more information.

ERROR

2

A verified error occurred during execution. The network did reach consensus, and the agreed-upon result is that the program failed.

EXCEEDED

3

Execution was halted for exceeding the computational resource limit. You Should optimize your program or reduce input size before retrying.

CODE_HASH_MISMATCH

4

The executed code's hash does not match the one committed in the original ExecutionRequest .

Last updated

Was this helpful?