# Swap Process

The swap process is initiated by a user. They enter the parameters of a swap (such as tokens, networks, amounts, etc.) and send them to the decentralized query service. This service queries registered Intent Agents for suitable swap orders they can provide.

Each Intent Agent can provide its own rates and set faster execution times, securing orders by higher collateral amounts. Each Intent Agent also has a score calculated in a decentralized way, according to order execution success rate, total trading volume, etc. This score, along with the parameters mentioned above, is an important criterion for choosing the most suitable order.

Once the best suitable order is chosen, it is important to ensure that the Intent Agent has sufficient unlocked collateral to fulfill it. To achieve this, the `collateralUnlocked` field in the `Order` structure should contain an `unlockCounter` value from the `CollateralManager`. It will enable the `OrderReceiver` contract to verify that the collateral can cover the order.

After that, the user signs the order with the private key associated with the address they hold the assets to swap on. The required signature is of EIP-712 standard for the `Order` typed data structure.

Next, the signature is passed to the Intent Agent to execute the order via the contracts.

In general, the first stage of successful order execution looks like the following:

1. A **User** sets order parameters and sends them to the query service.
2. This service queries the registered **Intent Agents** and pick the best order according to the rate, execution time, and score.
3. If the **User** agrees to this order, they check the collateral state of the **Intent Agent**.
4. If the **Intent Agent** has enough collateral, the **User** can sign the order.
5. The **User** approves the transfer of the order asset to the **Intent Agent**.
6. The **Intent Agent** receives an order from the **User** in the initial network.
7. The order is received by the `OrderReceiver` smart contract that immediately transfers the User asset to the **Intent Agent**.

If receive is not performed by the **Intent Agent** (within the deadline specified in the order), the swap is considered canceled. Continuous cancellations can affect the **Intent Agent's** score and limit its ability to execute orders.

Once the **Intent Agent** calls the `OrderReceiver` contract, it computes the locked collateral for the order. The locked collateral is the sum of the order amount and the liquidation bounty. The liquidation bounty is an amount set aside to incentivize **Liquidators** to step in and complete the order if the **Intent Agent** fails to do so. The size of the collateral is always higher than one and is calculated depending on the assets and the execution time.

If the unlocked collateral (the **Intent Agent's** balance not currently involved in securing transactions) is smaller than the required collateral (collateral to be locked), a transaction revert will occur, and the **Intent Agent** will fail to confirm the order.

If the unlocked collateral is bigger than the required collateral amount, the `OrderReceiver` updates the state and emits an `AssetReceive` event, signaling that the order assets have been received successfully. This event may be used as part of the proof of order execution, checked by Swaps.io Light Clients, to unlock the collateral.

After successfully receiving the order asset in the initial blockchain, the **Intent Agent** fills the order in the destination blockchain:

1. The **Intent Agent** calls the `OrderSender` contract with the order structure and transfers funds to the contract.&#x20;
2. The order is checked by the `OrderSender` contract, and then the **Intent Agent’s** assets are transferred to the **User**.
3. After transferring the assets to the **User**, the `OrderSender` emits an `AssetSend` event, signaling that the order assets have been sent successfully. This event may be used as proof to unlock the collateral.


---

# 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.swaps.io/swaps.io-protocol/swap-process.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.
