# Integrate YBridge Contract

## Integration Notice

Before starting the integration, please note the following points for your input parameters:

* The dstChainId must be one of the chains supported by yBridge. If your dstChainId is not supported by yBridge, the funds may be temporarily locked in the system. you could use [supported-blockchains](https://docs.xy.finance/single-bridge-integration/ybridge-api/supported-blockchains "mention") to get latest list
* Use `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE` as the address for the native token in your parameter.

## yBridge

## State-Changing Functions

Here are two state-changing functions provided by our YBridge Contract:

* **swapWithReferrer**: The function itself offers a service that combines bridge and destination chain swap. However, please note that the destination chain swap is intended for internal use. Nevertheless, **you can perform a bridge request through this function. For more details, you can refer to the** [legacy-bridge-example](https://docs.xy.finance/single-bridge-integration/ybridge-contract-integration/smart-contract/legacy-bridge-example "mention")
* **singleChainSwapWithReferrer**: This function is also intended for internal use. If you require a same chain swap function, you may consider using services such as 1inch or OpenOcean.

### swapWithReferrer

```solidity
function swapWithReferrer(
    address aggregatorAdaptor,
    IDexAggregatorAdaptor.SwapDescription memory swapDesc,
    bytes memory aggregatorData,
    PreviousDstChainDescription calldata dstChainDesc, ## Renamed after 2024/7/17
    address referrer
) 
```

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

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

```solidity
struct SwapDescription {
    IERC20 srcToken;
    IERC20 dstToken;
    address receiver;
    uint256 amount;
    uint256 minReturnAmount;
}
```

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

```solidity
struct PreviousDstChainDescription ## Renamed after 2024/7/17 {
    uint32 dstChainId;
    IERC20 dstChainToken;
    address dstAggregatorAdaptor;
    uint256 expectedDstChainTokenAmount;
    uint32 slippage;// denominator = 10**4
}
```

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

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

If you simply want to bridge, please follow the guidelines below to fill in the parameters for `aggregatorAdaptor` and `aggregatorData`

* `aggregatorAdaptor` should be `0x0000000000000000000000000000000000000000`
* `aggregatorData` should be `0x`

### singleChainSwapWithReferrer

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

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

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

The `swapDesc` contains the information of the 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;

## View Functions

### getEverClosed

```solidity
function getEverClosed(uint32 _srcChainId, uint256 _srcChainSwapId) returns (bool)
```

Check whether a swap request is closed (processed) on destination 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.

## Events

### System Setting Related Events

#### StartSwapIdSet

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

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

#### YBridgeVaultSet

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

This event will be emitted when a yBridge Vault (liquidity pool) is set.

#### AggregatorAdaptorSet

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

This event will be emitted when an aggregator adaptor is set.

#### AggregatorSet

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

This event will be emitted when an aggregator is set.

#### AcceptSwapRequestSet

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

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

***

### User Related Events

#### SwapRequested

```solidity
struct DstChainDescription {
    uint32 dstChainId;
    IERC20 dstChainToken;
    address dstAggregatorAdaptor;
    uint256 expectedDstChainTokenAmount;
    uint32 slippage; // denominator = 10**4
}
event SwapRequested(uint256 _swapId, address indexed _aggregatorAdaptor, DstChainDescription _dstChainDesc, IERC20 _srcToken, IERC20 indexed _vaultToken, uint256 _vaultTokenAmount, address _receiver, uint256 _srcTokenAmount, uint256 _expressFeeAmount, address indexed _referrer);
```

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

#### AggregatorSwapped

```solidity
event AggregatorSwapped(address indexed aggregator, address sender, IERC20 srcToken, IERC20 dstToken, address receiver, uint256 srcTokenAmount, uint256 dstTokenAmount, address indexed referrer);
```

This event will be emitted whenever a same-chain swap is completed.

#### CloseSwapCompleted

```solidity
event CloseSwapCompleted(CloseSwapResult _swapResult, uint32 _srcChainId, uint256 _srcChainSwapId);
```

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

#### SwappedForUser

```solidity
event SwappedForUser(address indexed _aggregatorAdaptor, IERC20 indexed _srcToken, uint256 _srcTokenAmount, IERC20 _dstToken, uint256 _dstTokenAmountOut, address _receiver);
```

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

#### SwapRefunded

```solidity
event SwapRefunded(uint256 _swapId, address _receiver, address _gasFeeReceiver, address _vaultToken, uint256 _refundAmount, uint256 _refundGasFee);
```

This event will be emitted when a request is refunded on the source chain.
