This repository contains the smart contract code for an Automated Market Making (AMM) pool. The AMM pool facilitates decentralized token exchange between supported tokens.
The smart contract relies on PSP22 implementation.
This contract is based on Balancer multi asset LP design and all formulas are taken from the Balancer's whitepaper
- Swap functionality to exchange tokens within the pool.
- Liquidity provisioning for users to contribute to the pool and earn rewards. (Can be added later)
- Fee collection on swaps to incentivize liquidity providers. (Can be added later)
- Permissioned withdrawal for authorized accounts to remove liquidity.
- Rust programming language with tools installed ([Rust compiler, Cargo package manager](https://doc.rust-lang.org/cargo/getting-started/installation.html))
- Cargo contract (https://github.com/paritytech/cargo-contract)
- Familiarity with smart contract development principles
git clone https://github.com/inkdevhub/amm_pool.git
cd amm_pool
cargo contract build --release
cargo test
Use the swap(token_in, token_out, amount_token_in, min_amount_token_out)
function to exchange tokens within the pool, where:
token_in
: The token type being provided for the swap (PSP22 token address).token_out
: The token type desired in return for the swap (PSP22 token address).amount_token_in
: The amount of token_in the user is willing to spend (U128 integer representing the amount).min_amount_token_out
: The minimum amount oftoken_out
the user expects to receive (U128 integer representing the amount).
Currently, the provided code doesn't include functionality for liquidity provisioning. However, you can implement features like:
- A function to deposit tokens into the pool to become a liquidity provider.
- A function for liquidity providers to withdraw their share of the pool's reserves.
- Permissions and Configuration (For Authorized Accounts):
The is_authorized(account)
function can be used internally to verify if an account has permission to perform specific actions (like withdrawals).
The set_swap_fee_percentage(swap_fee_percentage)
function allows authorized accounts to adjust the swap fee percentage (if implemented).
Important Notes:
- Ensure the calling account has granted sufficient allowance to the AMM contract to transfer the specified amount_token_in before calling the swap function.
- Only authorized accounts (typically the contract owner) can perform actions like withdrawals and fee adjustments.
This code example is for educational purposes only and should not be used in production environments without proper testing, security audits, and legal considerations.