# (Legacy) Integrate X Swap Contract

{% hint style="warning" %}
yBridge has been updated to version v3, and this v2 documentation is currently only for reference purposes. If you need to integrate with our new contracts, please refer to [integrate-ybridge-contract](https://docs.xy.finance/single-bridge-integration/ybridge-contract-integration/integrate-ybridge-contract "mention") to get the latest information.
{% endhint %}

## XSwapper

### Code

To enable ease of access to the content of the smart contracts (its code, data, status, etc.) and ascertain how they work and how they are programmed on the network, here we give you (users and institutions alike) the APIs and custom-fit UIs to suit your needs and provide you with a sense of security.\
\
The link `XSwapper.sol` below offers a collection of our code and functions. The smart contract accounts, deployed to the network, will interact with you when you submit transactions that execute functions that are defined on the smart contracts per se. Just like a regular, typical contract, our smart contracts define rules and will automatically implement them *via the code*.&#x20;

[`XSwapper`](https://etherscan.io/address/0x47f704c91c7edaac125ba451c0f98cbd64c78340#code)

## Events

### System Setting Related Events

#### StartSwapIdSet

```solidity
event StartSwapIdSet(uint256 _swapId);
```

This will be emitted when the starting swap ID is set.

#### FeeStructureSet

```solidity
event FeeStructureSet(uint32 _toChainId, address _YPoolToken, uint256 _gas, uint256 _min, uint256 _max, uint256 _rate, uint256 _decimals);
```

This will be emitted when the fee structure of a certain liquidity pool token on a certain chain is set

#### YPoolVaultSet

```solidity
event YPoolVaultSet(address _supportedToken, address _vault, bool _isSet);
```

This will be emitted when a Y Pool (liquidity pool) is set.

#### AggregatorAdaptorSet

```solidity
event AggregatorAdaptorSet(address _aggregator, bool _isSet);
```

This will be emitted when an aggregator adaptor is set.

#### SwapValidatorXYChainSet

```solidity
event SwapValidatorXYChainSet(address _swapValidatorXYChain);
```

This will be emitted when the validator address (on settlement chain) is set.

#### AcceptSwapRequestSet

```solidity
event AcceptSwapRequestSet(bool _isSet);
```

This will be emitted when the acceptSwapRequest is set. If true, XSwapper accepts swap request.

### X Swap Related Events

#### SwapRequested

```solidity
event SwapRequested(uint256 _swapId, address indexed _aggregatorAdaptor, ToChainDescription _toChainDesc, IERC20 _fromToken, IERC20 indexed _YPoolToken, uint256 _YPoolTokenAmount, address _receiver, uint256 _xyFee, uint256 _gasFee);
```

This will be emitted whenever a cross-chain swap request is initiated.

#### CloseSwapCompleted

```solidity
event CloseSwapCompleted(CloseSwapResult _swapResult, uint32 _fromChainId, uint256 _fromSwapId);
```

This will be emitted by the time all a cross-chain swap request is processed at the target chain so as to keep a record of the request. &#x20;

#### SwappedForUser

```solidity
event SwappedForUser(address indexed _aggregatorAdaptor, IERC20 indexed _fromToken, uint256 _fromTokenAmount, IERC20 _toToken, uint256 _toTokenAmountOut, address _receiver);
```

This will be emitted also by the time all a cross-chain swap request is processed at the target chain. However, this event records more information of the swap details than the `CloseSwapCompleted` event.

#### SwapCompleted

```solidity
event SwapCompleted(CompleteSwapType _closeType, SwapRequest _swapRequest);
```

This will be emitted when a cross-chain swap request is completed at the source chain. That is, the asset is settled and claimed back to the liquidity pool.

## State-Changing Functions

### swap

```solidity
function swap(
    address aggregatorAdaptor,
    IDexAggregatorAdaptor.SwapDescription memory swapDesc,
    bytes memory aggregatorData,
    ToChainDescription calldata toChainDesc
)
```

Initiate a cross-chain request and execute source chain side swap.

The `aggregatorAdaptor` is to adapt to the DEX aggregator involved in the `aggregatorData`.

The `swapDesc` contains the information of the source chain side swap.

```solidity
struct SwapDescription {
    IERC20 fromToken;
    IERC20 toToken;
    address receiver;
    uint256 amount;
    uint256 minReturnAmount;
}
```

The `aggregatorData` is generated by server containing source chain part of best routing path.&#x20;

The `toChainDesc` would be emitted in event to record target chain swap path and slippage. Note that the slippage would be seen as BP (1223 slippage equals to 12.23%).&#x20;

```solidity
struct ToChainDescription {
    uint32 toChainId;
    IERC20 toChainToken;
    uint256 expectedToChainTokenAmount;
    uint32 slippage;
}
```

### swapWithReferrer

```solidity
function swapWithReferrer(
    address aggregatorAdaptor,
    IDexAggregatorAdaptor.SwapDescription memory swapDesc,
    bytes memory aggregatorData,
    ToChainDescription calldata toChainDesc,
    address referrer
)
```

Initiate a cross-chain request and execute source chain side swap and leave a record of the referrer. Please contact us for more information about referral program.

### singleChainSwap

```solidity
function singleChainSwap(
    address aggregator,
    IDexAggregatorAdaptor.SwapDescription memory swapDesc,
    bytes memory aggregatorData
)
```

Execute a single-chain swap.

The `aggregator` is the DEX aggregator involved in the `aggregatorData`.

The `swapDesc` contains the information of the source chain side swap.

```solidity
struct SwapDescription {
    IERC20 fromToken;
    IERC20 toToken;
    address receiver;
    uint256 amount;
    uint256 minReturnAmount;
}
```

The `aggregatorData` is generated by server containing source chain part of best routing path.&#x20;

### singleChainSwapWithReferrer

```solidity
function singleChainSwapWithReferrer(
    address aggregator,
    IDexAggregatorAdaptor.SwapDescription memory swapDesc,
    bytes memory aggregatorData,
    address referrer
)
```

Execute a single-chain swap and leave a record of the referrer. Please contact us for more information about referral program.

## View Functions

### getSwapRequest

```solidity
function getSwapRequest(uint256 _swapId) returns (SwapRequest)
```

Get a certain swap request information by swap ID

```solidity
struct SwapRequest {
    uint32 toChainId;
    uint256 swapId;
    address receiver;
    address sender;
    uint256 YPoolTokenAmount;
    uint256 xyFee;
    uint256 gasFee;
    IERC20 YPoolToken;
    RequestStatus status;
}

enum RequestStatus { Open, Closed } 
```

### getFeeStructure

```solidity
function getFeeStructure(uint32 _chainId, address _token) returns (FeeStructure)
```

Get the fee setting of a certain liquidity pool token of a certain chain.

For example, if you are using `Ethereum`, you can get the fee setting of `USDT` pool of `Ethereum` by invoking\
`getFeeStructure(1, "0xdAC17F958D2ee523a2206206994597C13D831ec7")`; \
you can also get the fee setting of `USDT` pool of `Polygon` by invoking\
`getFeeStructure(137, "0xdAC17F958D2ee523a2206206994597C13D831ec7")`.\
This way you can figure out the fee setting when you try to bridge from `Ethereum` to `Polygon` via `USDT` pool.

```solidity
struct FeeStructure {
    bool isSet;
    uint256 gas;
    uint256 min;
    uint256 max;
    uint256 rate;
    uint256 decimals;
}

// isSet: Has been set or not
// gas: gas amount
// min: minimum XY Fee amount
// max: maximum XY Fee amount
// rate: XY Fee rate
// decimals: decimals of `rate`

// Example:
// (True, 25000000, 40000000, 2000000000, 35, 5)
// Means the fee strucute is set with the following:
// The minimum XY Fee of the given chain and token is `40000000`
// The maximum XY Fee of the given chain and token is `2000000000`
// The part of gas amount (included in the XY Fee) is `25000000`
// The XY Fee rate is 35 / (10**5) == `0.035%`
```

### getEverClosed

```solidity
function getEverClosed(uint32 _chainId, uint256 _swapId) returns (bool)
```

Check whether a swap request is closed (processed) on target chain by the swap ID on the source chain and the source chain ID.

### getTokenBalance

```solidity
function getTokenBalance(IERC20 _token, address _account) returns (uint256)
```

Get balance of an account of a token.
