> For the complete documentation index, see [llms.txt](https://devs.truebit.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devs.truebit.io/developing-truebit-tasks/dynamic-oracles/dynamic-js-execution.md).

# Dynamic Js Execution

###

{% stepper %}
{% step %}

### Inherit From BaseTBContract

Import `BaseTBContract.sol` contract and extend from it.
{% endstep %}

{% step %}

### 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`".
  {% endstep %}

{% step %}

### 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:

```solidity
function(uint256)
```

{% endstep %}

{% step %}

### **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(...)`.
{% endstep %}

{% step %}

### **Call \_requestExecution:**

Finally, you call \_requestExecution() with:

* The method signature (string).
* The encoded payload (bytes).
* The execution type: `DOTypes.ExecutionType.EMBEDDED_FUNCTION` .

```solidity
// 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
)
```

{% endstep %}

{% step %}

### Handling The Callback

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

```solidity
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**

<table><thead><tr><th width="194.9453125">Status</th><th width="95.2421875">Code</th><th>Description</th></tr></thead><tbody><tr><td><code>SUCCESS</code></td><td><code>0</code></td><td>The execution completed successfully.</td></tr><tr><td><code>FAILED</code></td><td><code>1</code></td><td>This execution is invalid. Check the <code>callbackMessageDetails</code> for more information.</td></tr><tr><td><code>ERROR</code></td><td><code>2</code></td><td>A verified error occurred during execution. The network <em>did</em> reach consensus, and the agreed-upon result is that the program failed.</td></tr><tr><td><code>EXCEEDED</code></td><td><code>3</code></td><td>Execution was halted for exceeding the computational resource limit. You Should optimize your program or reduce input size before retrying.</td></tr><tr><td><code>CODE_HASH_MISMATCH</code></td><td><code>4</code></td><td>The executed code's hash does not match the one committed in the original <code>ExecutionRequest</code> .</td></tr></tbody></table>

{% hint style="warning" %}
**Key considerations:**

* Always check `status` before decoding `resultData`&#x20;
* Treat all non-zero statuses as failures; the exact value is useful for logging but your control flow should not depend on it.
  {% endhint %}
  {% endstep %}
  {% endstepper %}
