> For the complete documentation index, see [llms.txt](https://flipwidget.gitbook.io/ctm-widget/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://flipwidget.gitbook.io/ctm-widget/developer-docs/internals/maya-aggregators.md).

# MAYA Aggregators

This explains how the MAYA aggregator contract links ChainFlip and MAYA and can be sued to swap tokens on our widget&#x20;

This diagram shows the basic working of the Swap&#x20;

<figure><img src="/files/9GpEIO3f7sJsTM654l1V" alt=""><figcaption></figcaption></figure>

#### Solidity Code: `TokenSwapperAggregator`

<pre class="language-solidity"><code class="lang-solidity"><strong>address: 0x97e5889979f62669B3923b106151AF71A8cd900d
</strong></code></pre>

This Solidity smart contract, `TokenSwapperAggregator`, facilitates token swaps across different blockchain networks using the Maya Protocol and Chainflip.

**Key Components**

1. **Imports and Contract Declaration**
   * The contract imports `IERC20` for ERC-20 token interactions and `IMAYACHAIN_ROUTER` for interactions with the Maya Protocol router.
   * It is designed for the Arbitrum mainnet, with a specific address for the Maya router contract.
2. **State Variables**
   * `routercontract`: An instance of the Maya Chain Router contract, used for performing cross-chain token swaps.
   * `CFVAULT`: A constant address for a Cross-Chain Vault contract.
3. **Events**
   * `CCMReceive`: Emitted when a cross-chain message is received, including details about the source chain, message, token, and amount.
4. **Constructor**
   * Initializes the contract with the Maya Chain Router address.
5. **Functions**
   * `performSwap`: Initiates a token swap using the Maya Router contract. It requires a vault address, the asset to swap, a memo, an expiration time, and the amount to swap.
   * `cfReceive`: Handles incoming cross-chain messages. It checks if the message is from the expected Cross-Chain Vault, decodes the message, performs a token transfer, and then calls `performSwap` to complete the swap.

#### JavaScript Code

This JavaScript code interacts with the `TokenSwapperAggregator` contract using the Chainflip SDK and Maya Protocol for token swaps. It is intended to be run in a Node.js environment and handles the process of quoting and executing swaps.

**Key Components**

1. **Imports and Setup**
   * Imports the Chainflip SDK and Ethers.js library for interacting with Ethereum and Arbitrum networks.
   * Sets up the Chainflip SDK and an Ethers.js provider for the Sepolia testnet.
2. **Main Function**
   * **Gas Calculation**: Retrieves gas fee data from the provider and calculates the gas budget for the transaction.
   * **Quote Request**: Creates a quote request to get an exchange rate from Ethereum to Arbitrum using the Chainflip SDK.
   * **Maya Protocol Quote**: Sends a GET request to Maya Protocol to get the quote for swapping ARB.ETH to ETH.USDC, including inbound address, memo, and expiry time.
   * **Wallet Setup**: Configures a wallet with a private key to execute the swap.
   * **Execute Swap**: Uses the Chainflip SDK to execute the swap. This involves specifying the source and destination chains and assets, the amount to swap, and metadata for the cross-chain message (ccmMetadata). It then sends the transaction using the wallet.

#### How They Are Interlinked

1. **Cross-Chain Token Swap**
   * The Solidity contract (`TokenSwapperAggregator`) uses the Maya Protocol to facilitate cross-chain swaps by interacting with the Maya router. The JavaScript code provides the necessary information and performs the swap operation using the Chainflip SDK.
2. **Token Transfer and Swap Execution**
   * In the Solidity contract, the `cfReceive` function receives cross-chain messages, performs token transfers, and initiates swaps by calling `performSwap`.
   * In the JavaScript code, after receiving quotes and setting up the transaction details, the `executeSwap` function of the Chainflip SDK is called to perform the actual swap on the Ethereum and Arbitrum networks.
3. **Integration Points**
   * The `performSwap` function in Solidity is called after receiving and decoding cross-chain messages. It utilizes the Maya Protocol for executing the swap.
   * The JavaScript code calculates gas fees, requests quotes from Chainflip and Maya Protocol, and executes the swap by sending the transaction to the `TokenSwapperAggregator` contract.

In summary, the Solidity contract handles the smart contract side of the swap, including receiving cross-chain messages and executing swaps via Maya Protocol, while the JavaScript code interacts with the Chainflip SDK to fetch quotes and perform the actual swap transactions.
