# Algo/AI

## Integration Approach

* User creates a vault via rethink.finance
* User grants their script specific permissions
* User script executes trades through the vaults [Roles Modifier](https://docs.rethink.finance/rethink.finance/protocol/architecture#zodiac-roles-modifier) contract

## Technical Implementation:

* Route all transaction calls through the vault's Roles Modifier
* All transaction execution will use the `execTransactionWithRole` function on the role modifier contract
  * ABI: <https://raw.githubusercontent.com/rethink-finance/rethink-frontend/refs/heads/main/src/contracts/zodiac/RolesFull.json>
  * `execTransactionWithRole` def: <https://github.com/gnosisguild/zodiac-modifier-roles-v1/blob/main/packages/evm/contracts/Roles.sol#L364-L378>
* Your bot will only have permission to execute specific trading functions with predefined parameter limits

## Data Storage and Configuration

* Store vault contract address and role ID directly in the users AWS instance (env or parameter store, or in script directly)

## Psuecode of Intergration

```python
# 1. Build the transaction data using SDK
w3 = Web3()
sdk = GainsTradingSDK()
tx_data = sdk.build.openTrade(trade_params_here)

# 2. Create role modifier contract instance
role_modifier = w3.eth.contract(
    address=QIV_ROLE_MODIFIER,  # Obtained during the user onboarding process
    abi=ROLE_MODIFIER_ABI  # We just have this file
)

# 3. Execute through vault's role modifier
tx = role_modifier.functions.execTransactionWithRole(
    tx_data.to,  # G-Trade address
    0,  # No ETH value
    tx_data.data,  # Trading function data
    0,  # Call (not delegatecall)
    ROLE_ID,  # Obtained during the user onboarding process
    True
).build_transaction({
    'from': "some_address",
    'gas': 2000000,
    'nonce': "some_nonce"
})

# 4. Sign
signed_tx = w3.eth.account.sign_transaction(tx, "some_private_key")
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)

```


---

# 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://docs.rethink.finance/rethink.finance/execution/algo-ai.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.
