> For the complete documentation index, see [llms.txt](https://wiki.zenlink.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.zenlink.pro/zenlink-dex-protocol/interface.md).

# Interface

## Module Assets

Assets is the fundamental module of ZenLink Dex Protocol. Assets looks like ERC20. Users can manage liquidity and token with it.

### **Issue**

Issue a new ERC20 token

```
fn issue(origin, #[compact] total: T::TokenBalance, asset_info: AssetInfo)
```

Parameters:

* \`total\`: initial total supply.
* \`asset\_info\`: the asset info contains \`name\`, \`symbol\`, \`decimals\`.&#x20;

### **Transfer**

Transfer token from owner to receiver.

```
fn transfer(origin, #[compact] id: T::AssetId, receiver: <T::Lookup as
StaticLookup>::Source, #[compact] amount: T::TokenBalance)
```

Parameters:

* \`target\`: the receiver of the asset.
* \`amount\`: the amount of the asset to transfer.&#x20;

### **Approve**

Set `amount` as the allowance of `spender` over the caller’s tokens with token `id`. Returns a boolean value indicating whether the operation succeeded.

```
fn approve(origin, #[compact] id: T::AssetId, spender: <T::Lookup as
StaticLookup>::Source,  #[compact] amount: T::TokenBalance)
```

Parameters:

* \`spender\`: the spender account.
* \`amount\`: the amount of allowance.&#x20;

### **Transfer From**

Moves amount tokens from sender to recipient using the allowance mechanism. the amount is then deducted from the caller’s allowance. Returns a boolean value indicating whether the operation succeeded.

```
fn transfer_from(origin, #[compact] id: T::AssetId, from: <T::Lookup as StaticLookup>::Source, target: <T::Lookup as StaticLookup>::Source, # [compact] amount: T::TokenBalance)
```

Parameters:

* \`id\`: the asset id.
* \`from\`: the source of the asset to be transferred.
* \`target\`: the receiver of the asset to be transferred.&#x20;
* \`amount\`: the amount of asset to be transferred.&#x20;

## Module DEX

Dex is the core module of ZenLink Dex Protocol. It implements the following functions:

* Initializing token trading pair.
* Token swap.
* Adding/extracting liquidity.
* Defining the liquidity constant function used throughout the protocol.

### **Create Pair**

initializing trading pair.&#x20;

```
pub fn create_pair(
    origin,
    token_0: AssetId,
    token_1: AssetId,
)-> DispatchResult  
```

Parameters:

* origin: Trading account
* token\_0: asset ID
* token\_1: asset ID

Description:

* Token\_0 and Token\_1 represent the two assets that make up the trading pair (also known as the liquidity pool).
* (token\_0, token\_1) and (token\_1, token\_0) is the same trading pair.

### **Add Liquidity**

```
pub fn add_liquidity(
   origin,
   token_0: AssetId,
   token_1: AssetId,
   amount_0_desired : T::TokenBalance,
   amount_1_desired : T::TokenBalance,
   amount_0_min : T::TokenBalance,
   amount_1_min : T::TokenBalance,
   target_parachain: ParaId,
   deadline: T::BlockNumber,
)->DispatchResult
```

Parameters:

* origin: Trading account
* token\_0: The asset ID that makes up the trading pair
* token\_1: The asset ID that makes up the trading pair
* amount\_0\_desired: The token\_0 amount you want to deposit to the liquidity pool.
* amount\_1\_desired: The token\_1 amount you want to deposit to the liquidity pool.
* amount\_0\_min: The minimum token\_0 amount you expect to deposit to the liquidity pool.
* amount\_1\_min: The minimum token\_1 amount you expect to deposit to the liquidity pool.
* target\_parachain: The parachain ID of the liquidity pool.
* deadline: The block deadline of this transaction.

Description:

Suppose in the following scenario:

* Alice has ABC(the native asset of Parachain200) and XYZ(the native asset of Parachain300).
* Alice wants to deposit liquidity to the ABC/XYZ liquidity pool.
* ABC is represented on Parachain300 as ABC'.
* The liquidity pool (trade pair ABC'/XYZ) is located on Parachain300.

Exception:

* If an add-liquidity transaction is made on Parachain200, please ensure enough Parachain300 assets(XYZ). Otherwise, the transaction must fail, and the Parachain200 assets(ABC) exist on Parachain300 in the form of ABC'.
* If an add-liquidity transaction is made on Parachain300, please ensure enough Parachain200 assets on Parachain300(ABC'). If you don't have ABC', even if you have enough ABC on Parachain200, it will be a failed transaction.

### **Remove Liquidity**

```
pub fn remove_liquidity(
   origin,
   token_0: AssetId,
   token_1: AssetId,
   liquidity: T::TokenBalance,
   amount_token_0_min : T::TokenBalance,
   amount_token_1_min : T::TokenBalance,
   to: <T::Lookup as StaticLookup>::Source,
   deadline: T::BlockNumber,
)->DispatchResult
```

Parameters:

* origin: Trading account.
* token\_0: The asset ID that makes up the trading pair.
* token\_1: The asset ID that makes up the trading pair.
* liquidity: The amount of liquidity you can hold up.
* amount\_token\_0\_min: The minimum token\_0 amount you expect to be obtained after liquidity extraction.
* amount\_token\_0\_min: The minimum token\_1 amount you expect to be obtained after liquidity extraction.
* to: The recipient account.
* deadline: The block deadline of this transaction.

### **Swap token\_0 with the exact amount for token\_1**

```
pub fn swap_exact_tokens_for_tokens(
   origin,
   amount_in: T::TokenBalance,
   amount_out_min: T::TokenBalance,
   path: Vec<AssetId>,
   to: <T::Lookup as StaticLookup>::Source,
   target_parachain: ParaId,
   deadline: T::BlockNumber,
)->DispatchResult
```

Parameters:

* origin: Trading account.
* amount\_in: The exact token\_0 amount you want to send.
* amount\_out\_min: The minimum token\_1 amount you expect to get.
* path: The transaction path.
* to: Recipient address.
* target\_parachain: The parachain id of the liquidity pool.
* deadline: The block deadline of this transaction.

Description:

* The path is represented as an array of asset ids.
* The first element represents the asset being sent, and the last element represents the target asset.
* \[A, B]: Exchange A for B in the A/B liquidity pool.
* \[B, A]: Exchange B for A in the A/B liquidity pool.
* \[A, B, C]: Exchange A for B in the A/B liquidity pool, then exchange B for C in the B/C liquidity pool.

Additionally,&#x20;

* There is no limit to the length of the path.
* The path is obtained from the API server. Since we currently have only one pool, the path length is fixed at 2.

### **Swap token\_0 for token\_1 with the exact amount**

```
pub fn swap_tokens_for_exact_tokens(
   origin,
   amount_out: T::TokenBalance,
   amount_in_max: T::TokenBalance,
   path: Vec<AssetId>,
   to: <T::Lookup as StaticLookup>::Source,
   deadline: T::BlockNumber,
   target_parachain: ParaId,
)->DispatchResult
```

Parameters:

* origin: Trading account.
* amount\_out: The exact token\_1 amount you want to get.
* amount\_in\_min: The minimum token\_0 amount you expect to send.
* path: The transaction path.
* to: Recipient address.
* target\_parachain: The parachain id of the liquidity pool.
* deadline: The block deadline of this transaction.

Description:

* The path is represented as an array of asset ids.
* The first element represents the asset being sent, and the last element represents the target asset.
* \[A, B]: Exchange A for B in the A/B liquidity pool.
* \[B, A]: Exchange B for A in the A/B liquidity pool.
* \[A, B, C]: Exchange A for B in the A/B liquidity pool, then exchange B for C in the B/C liquidity pool.

Additionally,&#x20;

* There is no limit to the length of the path.
* The path is obtained from the API server. Since we currently have only one pool, the path length is fixed at 2.

### **Transfer mapped/native assets in a parachain**

```
pub fn transfer(
    origin,
    asset_id: AssetId,
    target: <T::Lookup as StaticLookup>::Source,
    amount: T::TokenBalance
)->DispatchResult
```

Parameters:

* origin: Trading account
* asset\_id: Asset id
* target: Recipient address
* amount: Asset amount

Description:&#x20;

* This interface can only transfer assets that are mapped by other parachains to an account in a chain
* such as transfer ABC' on Parachain300 to an account on Parachain300, rather than an account on Parach200.

### **Transfer mapped/native assets to another parachain**

```
pub fn transfer_to_parachain(
   origin,
   asset_id: AssetId,
   para_id: ParaId,
   account: T::AccountId,
   amount: T::TokenBalance
) -> DispatchResult
```

Parameters:

* origin: Trading account
* asset\_id: Asset id
* para\_id: Target chain id
* account: Recipient address
* amount: Asset amount

Description:&#x20;

* This interface can transfer mapped/native assets to an account in another parachain.
* such as transfer ABC' or XYZ on Parachain300 to an account on Parachain200
