Aptos Move by Example
  • 🚀Getting Started
  • Set-Up
  • Why is Move Secure
    • Move prover
  • Move vs Solidity
    • Resources
    • Parallel Processing
    • Reentrancy attacks
    • Memory management
    • Smart contract verification
    • Compiled language
  • Basic Concepts
    • Move.toml
    • Primary data-types
    • Strings
    • Comments
    • Functions
    • Function Visibilities
    • Control flow and expressions
    • Loops
    • Error
    • Struct and its Abilities
    • Scripts
    • Operations
  • Intermediate Concepts
    • Local variables
    • Constants
    • Signer
    • Vector
    • Address
    • Uses and Aliases
    • Maps
    • Hash functions
    • References
    • Unit test
    • Generics
    • Type Arguments
    • Type Inference
  • Advanced Concepts
    • Global Storage Structure
    • Global Storage Operations
    • Phantom Type Parameters
    • Timestamps
    • Ownership
    • Move coding conventions
    • View functions
    • Aptos account
    • Aptos Coin
    • Aptos Token(Nft)
    • Object
    • Token V2
  • Applications
    • First App
    • ToDoList
    • Voting System
    • Basic Tokens
    • Storage using Generics
    • Company
    • Collection
    • Football Card
    • Staking Module
    • MultiSender Wallet
    • English Auction
    • Dutch Auction
    • Attendance Sheet
    • Polling Contract
    • Lottery Contract
  • Decentralized Finance
    • Simple Swap Protocol Contract
    • Code of Swapping Protocol
  • Hacks
    • Coming soon
  • Hands on tutorials
    • Indexer tutorials
Powered by GitBook
On this page
  • Overview
  • Structs:
  • Functions:
  • Helper Functions:
Edit on GitHub
  1. Decentralized Finance

Simple Swap Protocol Contract

Overview

The Simple Swap Protocol contract implements a basic swap protocol for generating LP (Liquidity Provider) tokens, creating liquidity pools, adding/removing liquidity, and swapping tokens. It allows users to participate in liquidity provision and token swapping in a decentralized manner.

Structs:

  1. LP<phantom X, phantom Y>

    • A generic struct representing LP tokens for a specific token pair X-Y in the contract.

    • LP tokens represent ownership in liquidity pools.

  2. Pair<phantom X, phantom Y>

    • A generic struct representing a liquidity pool for a specific token pair X-Y in the contract.

    • Contains fields to store the reserves of token X and Y, locked LP tokens, and mint/burn capabilities for LP tokens.

Functions:

generate_lp_name_symbol<X, Y>(): String

  • Generates a name symbol for LP tokens corresponding to the token pair X-Y.

  • Concatenates "LP-", the symbol of token X, "-", and the symbol of token Y to form the LP token name symbol.

create_pool<X, Y>(sender: &signer)

  • Creates a new liquidity pool for the token pair X-Y.

  • Ensures no duplicate pools are created for the same token pair.

  • Initializes the LP token for the pool to track liquidity provided by users.

add_liquidity<X, Y>(sender: &signer, x_amount: u64, y_amount: u64) acquires Pair

  • Allows users to add liquidity to the pool by providing amounts of token X and Y.

  • Calculates the optimal amount of Y tokens to be added given the amount of X tokens (or vice versa) to maintain a balanced pool.

  • Mints LP tokens corresponding to the provided liquidity and deposits them into the user's account.

remove_liquidity<X, Y>(sender: &signer, liquidity: u64) acquires Pair

  • Allows users to remove liquidity from the pool by burning their LP tokens.

  • Calculates the amounts of token X and Y that the user will receive based on the proportion of their LP tokens to the total supply.

  • Deposits the corresponding amounts of X and Y tokens into the user's account.

swap_x_to_y<X, Y>(sender: &signer, amount_in: u64) acquires Pair

  • Allows users to swap token X for token Y.

  • Calculates the amount of token Y that the user will receive in exchange for the provided amount of token X, considering a 0.3% fee.

  • Deposits the received token Y into the user's account.

Helper Functions:

pair_exist<X, Y>(addr: address) -> bool

  • Checks if a liquidity pool exists for the token pair X-Y or Y-X.

  • Returns true if a pool exists, otherwise false.

quote(x_amount: u128, x_reserve: u128, y_reserve: u128) -> u128

  • Calculates the optimal amount of token Y given the amount of token X, X reserve, and Y reserve.

get_amount_out(amount_in: u128, in_reserve: u128, out_reserve: u128) -> u128

  • Calculates the amount of output token for a given amount of input token in a swap, considering a 0.3% fee.

get_coin<X, Y>() -> (u64, u64) acquires Pair

  • Returns the current reserves of token X and Y in the liquidity pool.

PreviousDecentralized FinanceNextCode of Swapping Protocol

Last updated 1 year ago