# Dynamic Js Execution

### 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`.
* Ethereum Mainnet Address: `0x48C101d60F6f8a9589fa74e10da62460219B53e6`.
* **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:

```solidity
function(uint256)
```

#### 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(...)`.

#### 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
)
```

### 4. 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devs.truebit.io/developing-truebit-tasks/dynamic-oracles/dynamic-js-execution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
