Interface

The ZenLink DEX Protocol defines several modules including Assets, Dex. Here is the basic function description, using the Module version written in Rust as an example.

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`.

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.

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.

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.

  • `amount`: the amount of asset to be transferred.

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.

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,

  • 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,

  • 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:

  • 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:

  • 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

Last updated