# Swap

Taya is an automated market maker (AMM), a decentralized exchange (DEX) that allows users to trade assets directly without the need for a traditional order book to match buyers and sellers.

**SwapCraft Function**

At the heart of SwapCraft lies the "swapTokens" function, a powerful mechanism that facilitates token exchanges with ease. Let's delve into its core components:

```solidity
function swapTokens(
    TokenSwap tokenSwapRequest,
    AssetManagement assetRouting,
    uint256 tradeThreshold,
    uint256 swapExpiry
) returns (uint256 tradeResults[In/Out])
```

* `poolIdentifier`: Identifies the pool designated for the swap.
* `tradeType`: Precisely defines the type of trade, allowing for either "Out Given Exact In" or "In Given Exact Out" swaps.
* `inputAsset`: Refers to the asset intended to be swapped into the pool.
* `outputAsset`: Signals the asset to be acquired in exchange.
* `tradeAmount`: Its meaning seamlessly adjusts according to the selected "tradeType":
  1. `GIVEN_IN`: Signifies the amount of assets being dispatched.
  2. `GIVEN_OUT`: Designates the target amount of assets to be received.
* `userSpecificData`: This field serves as a conduit for any supplementary information necessary for the pool to execute the swap, thus facilitating future enhancements in the swapping protocol.

#### **TokenSwap Structure**

A vital cog in the SwapCraft machinery is the "TokenSwap" structure, meticulously outlined as follows:

```solidity
enum ExchangeType { GIVEN_IN, GIVEN_OUT }

struct TokenSwap {
   bytes32 poolIdentifier;
   ExchangeType tradeType;
   IAsset inputAsset;
   IAsset outputAsset;
   uint256 tradeAmount;
   bytes userSpecificData;
}
```

**AssetManagement Structure**

The "AssetManagement" structure plays a pivotal role in determining the flow of input and output assets. It takes the following form:

```solidity
struct AssetManagement {
    address senderAddress;
    bool sourceFromInternalStorage;
    address payable receiverAddress;
    bool sendToInternalStorage;
}
```

* `senderAddress`: Specifies the address from which assets will be sourced for the swap.
* `sourceFromInternalStorage`: Dictates whether the assets should be drawn from the sender's pre-existing holdings within the Vault.
* Taya's bespoke swap function tailored to our unique pool types, fortified with the robust technology inherited from the Balancer swap module.

{% hint style="info" %}
Tayaswap's bespoke swap function tailored to our unique pool types, fortified with the robust technology inherited from the [Balancer](https://docs.balancer.fi/concepts/vault/swaps.html) swap module.
{% endhint %}


---

# 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.taya.fi/about-taya/swap.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.
