> For the complete documentation index, see [llms.txt](https://tilepmoney.gitbook.io/tilepmoney-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tilepmoney.gitbook.io/tilepmoney-docs/technical/smart-contract.md).

# Smart Contract

TilepMoney's smart contract architecture relies on a central orchestration pattern to ensure atomic, secure, and modular workflow execution.

### Architecture

The system is centered around the **MainController**, which acts as the single point of entry for all user operations. It routes funds to specialized "Routers" for specific DeFi actions (Swapping, Yield, Bridging).

<figure><img src="/files/XD4DkOL4ZfgZvdAOceTo" alt="" width="563"><figcaption></figcaption></figure>

### Core Components

#### 1. MainController.sol

The brain of the operation. It receives a list of encoded execution steps ("Actions") and processes them sequentially in a single transaction.

* **Atomic Execution**: If any step fails (e.g., high slippage), the entire transaction reverts.
* **Funds Management**: Pulls funds from the user only at the start; intermediate funds stay in the contract until the final payout.

#### 2. Action Routers

Specialized contracts that handle logic for specific domains:

* **SwapAggregator:** Finds the best price across multiple DEXs (FusionX, MerchantMoe).
* **YieldRouter:** Manages deposits/withdrawals into lending protocols (InitCapital, MethLab) via a consistent interface.
* **BridgeRouter:** Routes assets to cross-chain protocols (Hyperlane, LayerZero).

#### 3. Adapters

To support new protocols without upgrading the core system, we use an Adapter pattern. Each external protocol (like Aave or Uniswap) has a dedicated adapter that normalizes its interface.

### Key Workflows

#### Workflow Execution

1. **Frontend** encodes a sequence of actions (e.g., `[SWAP, YIELD, TRANSFER]`).
2. **User** calls `MainController.executeWorkflow(actions)`.
3. **MainController** iterates through the list:
   * Decodes parameters (Token In, Amount, Slippage).
   * Approves the specific Router to spend tokens.
   * Calls the Router to execute the logic.
   * Updates the "Input Token" for the next step dynamically.

#### Dynamic Token Resolution

The controller supports "Dynamic Inputs" (address `0x0`). If Step 2 needs the output from Step 1 but doesn't know the exact amount or token address beforehand, it asks the MainController to "use the output from the previous step."

### Security

* **Non-Custodial**: The MainController never holds user funds long-term. Assets are moved, processed, and returned to the user (or their specific target) in the same transaction.
* **Reentrancy Protection**: All external calls are guarded to prevent reentrancy attacks.
* **Access Control**: Only the Owner can add or update Router/Adapter whitelists.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://tilepmoney.gitbook.io/tilepmoney-docs/technical/smart-contract.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.
