# Trading

**Note:** For sets that are using Delegated Maneger system should refer to the 'Batch Trading' section on how to execute trades manually via BscScan

## What is the TradeModule:

The Trade Module enables managers of SetTokens to perform atomic trades using aggregators such as 0x, and decentralized exchanges such as Pancakeswap and Uniswap. This rebalances the Set for all Set holders.

## Initializing the TradeModule:

All modules need to be added to the SetToken as the first step. Learn how to add a module to your SetToken by referring to the 'Adding a Module' guide.&#x20;

Once you have added the TradeModule to the SetToken, you must initialize the SetToken on the TradeModule:

```typescript
/**
 * Initializes this module to the SetToken. Only callable by the SetToken's manager.
 *
 * @param _setToken        Instance of the SetToken to initialize
 */
function initialize(ISetToken _setToken);
```

## Trading:

Only the manager will be able to trade. Call the `trade` function with inputs as follows:

**Important**: `sendQuantity` and `_minReceiveQuantity` are denominated in **position units** and not the notional quantity. For example, if 1 SetToken contains 1 WETH, and there are 10 supply of SetTokens (10 WETH locked in total), trading 100% of the ETH to DAI requires passing in 1 (more accurately 10\*\*18) into the `sendQuantity`.

```typescript
/**
 * Executes a trade on a supported DEX. Only callable by the SetToken's manager.
 * @dev Although the SetToken units are passed in for the send and receive quantities, the total quantity
 * sent and received is the quantity of SetToken units multiplied by the SetToken totalSupply.
 *
 * @param _setToken             Instance of the SetToken to trade
 * @param _exchangeName         Human readable name of the exchange in the integrations registry
 * @param _sendToken            Address of the token to be sent to the exchange
 * @param _sendQuantity         Units of token in SetToken sent to the exchange
 * @param _receiveToken         Address of the token that will be received from the exchange
 * @param _minReceiveQuantity   Min units of token in SetToken to be received from the exchange
 * @param _data                 Arbitrary bytes to be used to construct trade call data
 */
function trade(
    ISetToken _setToken,
    string memory _exchangeName,
    address _sendToken,
    uint256 _sendQuantity,
    address _receiveToken,
    uint256 _minReceiveQuantity,
    bytes memory _data
);
```

The exchange name is a string specifying which exchange adapter to execute the trade. The exchange name is registered in the IntegrationRegistry contract. Exchanges and aggregators currently supported by the TradeModule are:

{% tabs %}
{% tab title="BNB-Chain" %}

| Exchange               | Adaptor                      |
| ---------------------- | ---------------------------- |
| 0x                     | ZeroExApiAdapterV5           |
| Uniswap V3 (BNB-Chain) | UniswapV3ExchangeAdapter     |
| Pancakeswap V2         | PancakeswapV2ExchangeAdapter |
| Pancakeswap V3         | PancakeswapV3ExchangeAdapter |
| AMMSplitter            | AMMSplitterExchangeAdapter   |
| {% endtab %}           |                              |
| {% endtabs %}          |                              |

The `_data` parameter is optional on some exchanges and required for others. For example, you will need to input the trade `calldata` fetched from the 0x API or 1inch API to execute an 1Inch trade or custom path to route the trade through Uniswap or Sushiswap. For information on how to generate the custom `_data` parameter, check out the next few sections.

## Trading on BscScan using 0x

1. Note: For sets that are using Delegated Maneger system should refer to the 'Batch Trading'
2. Navigate to the TradeModule that you have already initialized on your Set. If you have not, refer to the 'Add a Module' section
3. Connect your web3 wallet
4. Fill in the trade() details and click submit
   1. **tradeData** is pulled from the **data** field from 0x API. Refer to 0x API sction to fetch a quote
   2. ```
      {
      ```

      ```
          "price": "108.2609065500801642",
      ```

      ```
          "guaranteedPrice": "102.8478612225761558",
      ```

      ```
          "to": "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
      ```

      ```
          "data": "0xd9627aa40000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000002386f26fc100000000000000000000000000000000000000000000000000000e45e3d944cda116000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000007fc66500c84a76ad7e9c93437bfc5ac33e2ddae9000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000954906da0bf32d5479e25f46056d22f08464cab869584cd000000000000000000000000100000000000000000000000000000000000001100000000000000000000000000000000000000000000003e67d7ea31600e8c94",
      ...
      ```

<figure><img src="https://1125707491-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJbSsfBtRgcYOTpcRm63M%2Fuploads%2Fgi7S220r81v577y4QsGV%2FScreen%20Shot%202023-01-19%20at%202.04.05%20PM.png?alt=media&#x26;token=a72c3555-47ac-4a5f-b891-8660db753b70" alt=""><figcaption></figcaption></figure>


---

# 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://vingt-io.gitbook.io/vingt.io/technical-guide/visp-technical-foundation/index-token-manger-guide/trading.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.
