🍁Pirichain Decentralized Exchange (DEX) Sample Full Codes

This pages explained that how to code for building a new dex via Pirichain

Key Functions:

  1. getTokenName() & getParityName(): These return the token name (BNB) and parity (BNB/PIRI).

  2. getCommission(): Returns the slippage value (0.03).

  3. swapInitialize(): Initializes the swap by setting up parity data such as asks, bids, last price, and daily volume if the user is the scenario owner.

  4. checkMyBNBDeposit(): Checks the BNB deposit in the user's wallet, withdraws BNB if needed, and updates liquidity.

  5. depositToken(): Allows depositing PIRI tokens into the liquidity pool.

  6. getMyDepositWallet(): Returns the wallet address for depositing BNB.

  7. checkWalletInitialization(): Ensures that the wallet is initialized with liquidity, asks, bids, and volume data.

  8. blockWallet() & unBlockWallet(): Blocks or unblocks a wallet from performing operations based on the scenario owner's authorization.

  9. determineAmountForBid() & determineAmountForAsk(): These functions calculate how much PIRI or BNB can be bought or sold based on current ask and bid orders.

  10. buyToken(): Facilitates buying PIRI with BNB, checks for blocked wallets, verifies liquidity, and processes the swap.

  11. sellToken(): Facilitates selling PIRI for BNB, checks for blocked wallets, verifies liquidity, and processes the swap.

  12. getAllSlippages(): Returns the slippage fee in both crypto and tokens.

  13. faucetToOwnerForTesting(): Provides liquidity to the owner's wallet for testing purposes.

Error Handling:

  • The script includes robust error handling for situations like insufficient funds, blocked wallets, ongoing transactions, incorrect data formats, and unauthorized actions.

Slippage:

  • Slippage is handled as part of the swap, ensuring that any fees are deducted accordingly when buying or selling tokens.

Example Use Cases

  1. Initializing Trading for a BNB and PIRI Pair: Users can call the swapInitialize function to set up the liquidity and order books for BNB and PIRI coins.

  2. Placing and Removing Orders: Users can place bids and asks at a specific level (level) and quantity (quantity), or remove them if needed.

  3. Withdrawing Liquidity: Users can withdraw their available BNB or PIRI token balances from the system using withdrawBnbWallet or withdrawToken functions.

This structure can be used to build a DEX protocol on Pirichain, or enhance existing exchange integrations by providing automated, blockchain-based trading and liquidity management.

Last updated