🏗️Pair creator

The following page is dedicated for reviewing and explaining the source code of the pair creator smart contract which creates trading pairs.

Pair creator contract allows the users to create their own PSP22/AZERO pairs (Later on PSP22/PSP22 trading pairs).

Pair creator source code:

Imports:

We are importing "TradingPairAzeroRef" (Trading pair azero contract reference) in order to reference a new trading pair azero contract and to initialize it and we import "TradingPairPsp22Ref" (Trading pair PSP22 contract reference) which will be used later on.

use trading_pair_psp22::TradingPairPsp22Ref;
use trading_pair_azero::TradingPairAzeroRef;

Contract's struct:

This contract has no struct

Functions:

New (constuctor)

Constructor function used to initialize and deploy the pair creator, we dont pass any argument to the contract.

#[ink(constructor)]
pub fn new() -> Self {
            
            Self{

            }
}

create_azero_trading_pair

Function to create a new instance of trading pair AZERO and deploy it to the network. We pass "azero_trading_pair_hash" as the hash of the compiled trading pair azero contract, "version" as a unique identifier (different ID to create a new contract, existing ID to reference an already deployed contract), "psp22_addrr" as the address of the PSP22 one wished to create the pair with, "fee" as the liquidity pool fee precentage, "panx_contract" as the PANX token address, we use it to check if the user is eligible for reduced LP fee and lastly "vault_address" as the address of the wallet to allocate trader's fee into.

#[ink(message,payable)]
 pub fn create_azero_trading_pair(&mut self,azero_trading_pair_hash: Hash,version:u32,psp22_addrr:AccountId,fee:Balance,panx_contract:AccountId,vault_address:AccountId) -> AccountId {

            
            let salt = version.to_le_bytes();
            let trading_pair = TradingPairAzeroRef::new(psp22_addrr,fee,panx_contract,vault_address)
                .endowment(0)
                .code_hash(azero_trading_pair_hash)
                .salt_bytes(salt)
                .instantiate()
                .unwrap_or_else(|error| {
                    panic!(
                        "failed at instantiating the Azero trading pair contract: {:?}",
                        error
                    )
            });
            let add = trading_pair.get_account_id();

            

            add
        
 
 }

create_psp22_trading_pair

Function to create a new instance of trading pair AZERO and deploy it to the network. We pass "psp22_trading_pair_hash" as the hash of the compiled trading pair azero contract, "version" as a unique identifier (different ID to create a new contract, existing ID to reference an already deployed contract), "psp22_token1_addrr" as the address of the first PSP22 one wished to create the pair with,"psp22_token2_addrr" as the address of the second PSP22 one wished to create pair with , "fee" as the liquidity pool fee precentage, "panx_contract" as the PANX token address, we use it to check if the user is eligible for reduced LP fee and lastly "vault_address" as the address of the wallet to allocate trader's fee into.

#[ink(message,payable)]
pub fn create_psp22_trading_pair(&mut self,psp22_trading_pair_hash: Hash,version:u32,psp22_token1_addrr:AccountId,psp22_token2_addrr:AccountId,fee:Balance,panx_contract:AccountId,vault_address:AccountId) -> AccountId {

            
            let salt = version.to_le_bytes();
            let trading_pair = TradingPairPsp22Ref::new(psp22_token1_addrr,psp22_token2_addrr,fee,panx_contract,vault_address)
                .endowment(0)
                .code_hash(psp22_trading_pair_hash)
                .salt_bytes(salt)
                .instantiate()
                .unwrap_or_else(|error| {
                    panic!(
                        "failed at instantiating the PSP22 trading pair contract: {:?}",
                        error
                    )
            });
            let add = trading_pair.get_account_id();

            

            add
        
 
}

Last updated