The example anchor implementation for solana-swap

Overview

anchor-liquidity-pool

This is the example anchor implementation for solana-swap.

  • The curve is always ConstantProduct in this implementation.
  • The fee receiver is always Admin.

Run

Build the solana program

export ADMIN_PUBKEY=`solana address`
npm run build

Run validator on the localnet

npm run validator

Get 10 SOL by airdrop and deploy program for localnet.

npm run deploy

Run the test(Create Pool / Deposit / Withdrow / Swap).

npm run test

Initialize

  sequenceDiagram
    participant Admin
    participant PoolManager as -PDA- <br/> Pool Manager
    PoolManager ->>+ Admin : mint 1000000000 LP Token
Loading
┌─────────┬────────────────┬────────────────────────────────────────────────┬────────────────────────────────────────────────┬────────────┐
│ (index) │      name      │                    address                     │                     owner                      │   amount   │
├─────────┼────────────────┼────────────────────────────────────────────────┼────────────────────────────────────────────────┼────────────┤
│    0    │ 'A for UserA'  │ 'J8a4ENEMgUHHfAz9VH2BuApK6mS8mjcUZjHeBypKN1KE' │ 'EwznK8BuBdn918if9jeG3oBomZTpRi8ZHxcCeGXokGaL' │  1000000   │
│    1    │ 'B for UserA'  │ 'GmRp3YGmWFc2or9BkBuiANS9GZ7BEqxrg1ZPpCYvRqc2' │ 'EwznK8BuBdn918if9jeG3oBomZTpRi8ZHxcCeGXokGaL' │  1000000   │
│    2    │ 'LP for UserA' │ '7BBxTGPFKxLmKVj6LV3yE6ojG9kFzRpFnunFxrsT7sW6' │ 'EwznK8BuBdn918if9jeG3oBomZTpRi8ZHxcCeGXokGaL' │     0      │
│    3    │  'A for PDA '  │ 'BVJohxH1hogZdcYBrTMuo7EVk9TSrjdGhrya4zzrGC7p' │ 'DtW9VUU8AtGSyrAo2kMH2rvJhjz97oLh5qfg1Ckubt5N' │  1000000   │
│    4    │  'B for PDA '  │ '9HRgCPn4nj415xayTHhLFqQrpWePPzewEmAqbUBDbtgZ' │ 'DtW9VUU8AtGSyrAo2kMH2rvJhjz97oLh5qfg1Ckubt5N' │  1000000   │
│    5    │ 'LP for Admin' │ '7XAnDxYcCRRSKewLdMnqtrwhjnbUd2Kvq5FfG8xMdRsp' │ '9HKfUqNdHdgn4gYMjCrSoLAmY5USgwpfBSPdeW573ycc' │ 1000000000 │
└─────────┴────────────────┴────────────────────────────────────────────────┴────────────────────────────────────────────────┴────────────┘

DepositAll

  sequenceDiagram
    participant PoolManager as -PDA- <br/> Pool Manager
    participant Depositor
    Depositor ->>+ PoolManager : send 10000 token A 
    Depositor ->>+ PoolManager : send 10000 token B
    PoolManager ->>+ Depositor : mint 10000000 LP Token
Loading
┌─────────┬────────────────┬────────────────────────────────────────────────┬────────────┐
│ (index) │      name      │                    address                     │   amount   │
├─────────┼────────────────┼────────────────────────────────────────────────┼────────────┤
│    0    │ 'A for UserA'  │ 'J8a4ENEMgUHHfAz9VH2BuApK6mS8mjcUZjHeBypKN1KE' │   990000   │
│    1    │ 'B for UserA'  │ 'GmRp3YGmWFc2or9BkBuiANS9GZ7BEqxrg1ZPpCYvRqc2' │   990000   │
│    2    │ 'LP for UserA' │ '7BBxTGPFKxLmKVj6LV3yE6ojG9kFzRpFnunFxrsT7sW6' │  10000000  │
│    3    │  'A for PDA '  │ 'BVJohxH1hogZdcYBrTMuo7EVk9TSrjdGhrya4zzrGC7p' │  1010000   │
│    4    │  'B for PDA '  │ '9HRgCPn4nj415xayTHhLFqQrpWePPzewEmAqbUBDbtgZ' │  1010000   │
│    5    │ 'LP for Admin' │ '7XAnDxYcCRRSKewLdMnqtrwhjnbUd2Kvq5FfG8xMdRsp' │ 1000000000 │
└─────────┴────────────────┴────────────────────────────────────────────────┴────────────┘

DepositSingle(Deposit Token A)

  sequenceDiagram
    participant PoolManager as -PDA- <br/> Pool Manager
    participant Depositor
    Depositor ->>+ PoolManager : send 100000 token A 
    PoolManager ->>+ Depositor : mint 48760478 LP Token
Loading
┌─────────┬────────────────┬────────────────────────────────────────────────┬────────────┐
│ (index) │      name      │                    address                     │   amount   │
├─────────┼────────────────┼────────────────────────────────────────────────┼────────────┤
│    0    │ 'A for UserA'  │ 'J8a4ENEMgUHHfAz9VH2BuApK6mS8mjcUZjHeBypKN1KE' │   890000   │
│    1    │ 'B for UserA'  │ 'GmRp3YGmWFc2or9BkBuiANS9GZ7BEqxrg1ZPpCYvRqc2' │   990000   │
│    2    │ 'LP for UserA' │ '7BBxTGPFKxLmKVj6LV3yE6ojG9kFzRpFnunFxrsT7sW6' │  58760478  │
│    3    │  'A for PDA '  │ 'BVJohxH1hogZdcYBrTMuo7EVk9TSrjdGhrya4zzrGC7p' │  1110000   │
│    4    │  'B for PDA '  │ '9HRgCPn4nj415xayTHhLFqQrpWePPzewEmAqbUBDbtgZ' │  1010000   │
│    5    │ 'LP for Admin' │ '7XAnDxYcCRRSKewLdMnqtrwhjnbUd2Kvq5FfG8xMdRsp' │ 1000000000 │
└─────────┴────────────────┴────────────────────────────────────────────────┴────────────┘

WithdrawAll

  sequenceDiagram
    participant Admin
    participant PoolManager as -PDA- <br/> Pool Manager
    participant Depositor
    PoolManager ->>+ Depositor : send 104 token A 
    PoolManager ->>+ Depositor : send 95 token B
    PoolManager ->>+ Depositor : burn 100000 LP Token
    Depositor ->>+ Admin : send 0 LP Token as fee
Loading
┌─────────┬────────────────┬────────────────────────────────────────────────┬────────────┐
│ (index) │      name      │                    address                     │   amount   │
├─────────┼────────────────┼────────────────────────────────────────────────┼────────────┤
│    0    │ 'A for UserA'  │ 'J8a4ENEMgUHHfAz9VH2BuApK6mS8mjcUZjHeBypKN1KE' │   890104   │
│    1    │ 'B for UserA'  │ 'GmRp3YGmWFc2or9BkBuiANS9GZ7BEqxrg1ZPpCYvRqc2' │   990095   │
│    2    │ 'LP for UserA' │ '7BBxTGPFKxLmKVj6LV3yE6ojG9kFzRpFnunFxrsT7sW6' │  58660478  │
│    3    │  'A for PDA '  │ 'BVJohxH1hogZdcYBrTMuo7EVk9TSrjdGhrya4zzrGC7p' │  1109896   │
│    4    │  'B for PDA '  │ '9HRgCPn4nj415xayTHhLFqQrpWePPzewEmAqbUBDbtgZ' │  1009905   │
│    5    │ 'LP for Admin' │ '7XAnDxYcCRRSKewLdMnqtrwhjnbUd2Kvq5FfG8xMdRsp' │ 1000000000 │
└─────────┴────────────────┴────────────────────────────────────────────────┴────────────┘

WithdrawSingle(Withdraw Token A)

  sequenceDiagram
    participant Admin
    participant PoolManager as -PDA- <br/> Pool Manager
    participant Depositor
    PoolManager ->>+ Depositor : send 100000 token A 
    PoolManager ->>+ Depositor : burn 48754928 LP Token
    Depositor ->>+ Admin : send 0 LP Token as fee
Loading
┌─────────┬────────────────┬────────────────────────────────────────────────┬────────────┐
│ (index) │      name      │                    address                     │   amount   │
├─────────┼────────────────┼────────────────────────────────────────────────┼────────────┤
│    0    │ 'A for UserA'  │ 'J8a4ENEMgUHHfAz9VH2BuApK6mS8mjcUZjHeBypKN1KE' │   990104   │
│    1    │ 'B for UserA'  │ 'GmRp3YGmWFc2or9BkBuiANS9GZ7BEqxrg1ZPpCYvRqc2' │   990095   │
│    2    │ 'LP for UserA' │ '7BBxTGPFKxLmKVj6LV3yE6ojG9kFzRpFnunFxrsT7sW6' │  9905550   │
│    3    │  'A for PDA '  │ 'BVJohxH1hogZdcYBrTMuo7EVk9TSrjdGhrya4zzrGC7p' │  1009896   │
│    4    │  'B for PDA '  │ '9HRgCPn4nj415xayTHhLFqQrpWePPzewEmAqbUBDbtgZ' │  1009905   │
│    5    │ 'LP for Admin' │ '7XAnDxYcCRRSKewLdMnqtrwhjnbUd2Kvq5FfG8xMdRsp' │ 1000000000 │
└─────────┴────────────────┴────────────────────────────────────────────────┴────────────┘

Swap 1 (Token A -> Token B)

  sequenceDiagram
    participant Admin
    participant PoolManager as -PDA- <br/> Pool Manager
    participant Swapper
    Swapper ->>+ PoolManager : send 1999995 token A 
    PoolManager ->>+ Swapper : send 670379 token B
    PoolManager ->>+ Admin : mint 167611 LP Token as fee
Loading
┌─────────┬─────────────────┬────────────────────────────────────────────────┬────────────┐
│ (index) │      name       │                    address                     │   amount   │
├─────────┼─────────────────┼────────────────────────────────────────────────┼────────────┤
│    0    │  'A for userA'  │ 'J8a4ENEMgUHHfAz9VH2BuApK6mS8mjcUZjHeBypKN1KE' │   990104   │
│    1    │  'B for userA'  │ 'GmRp3YGmWFc2or9BkBuiANS9GZ7BEqxrg1ZPpCYvRqc2' │   990095   │
│    2    │ 'LP for userA'  │ '7BBxTGPFKxLmKVj6LV3yE6ojG9kFzRpFnunFxrsT7sW6' │  9905550   │
│    3    │  'A for PDA '   │ 'BVJohxH1hogZdcYBrTMuo7EVk9TSrjdGhrya4zzrGC7p' │  3009891   │
│    4    │  'B for PDA '   │ '9HRgCPn4nj415xayTHhLFqQrpWePPzewEmAqbUBDbtgZ' │   339526   │
│    5    │ 'LP for Admin'  │ '7XAnDxYcCRRSKewLdMnqtrwhjnbUd2Kvq5FfG8xMdRsp' │ 1000167611 │
│    6    │ 'A for swapper' │ '7NCo7QQyP1a1PSQMZZCFmxjYnAaWguX7r7MkuQcWAYu2' │  8000005   │
│    7    │ 'B for swapper' │ 'AKUKWMmVPSjiVymMPshfd9HLhtKdDDg5CjmKQ7YePJfe' │  10670379  │
└─────────┴─────────────────┴────────────────────────────────────────────────┴────────────┘

Swap 2 (Token A -> Token B)

  sequenceDiagram
    participant Admin
    participant PoolManager as -PDA- <br/> Pool Manager
    participant Swapper
    Swapper ->>+ PoolManager : send 1999984 token A 
    PoolManager ->>+ Swapper : send 135297 token B
    PoolManager ->>+ Admin : mint 100713 LP Token as fee
Loading
┌─────────┬─────────────────┬────────────────────────────────────────────────┬────────────┐
│ (index) │      name       │                    address                     │   amount   │
├─────────┼─────────────────┼────────────────────────────────────────────────┼────────────┤
│    0    │  'A for userA'  │ 'J8a4ENEMgUHHfAz9VH2BuApK6mS8mjcUZjHeBypKN1KE' │   990104   │
│    1    │  'B for userA'  │ 'GmRp3YGmWFc2or9BkBuiANS9GZ7BEqxrg1ZPpCYvRqc2' │   990095   │
│    2    │ 'LP for userA'  │ '7BBxTGPFKxLmKVj6LV3yE6ojG9kFzRpFnunFxrsT7sW6' │  9905550   │
│    3    │  'A for PDA '   │ 'BVJohxH1hogZdcYBrTMuo7EVk9TSrjdGhrya4zzrGC7p' │  5009875   │
│    4    │  'B for PDA '   │ '9HRgCPn4nj415xayTHhLFqQrpWePPzewEmAqbUBDbtgZ' │   204229   │
│    5    │ 'LP for Admin'  │ '7XAnDxYcCRRSKewLdMnqtrwhjnbUd2Kvq5FfG8xMdRsp' │ 1000268324 │
│    6    │ 'A for swapper' │ '7NCo7QQyP1a1PSQMZZCFmxjYnAaWguX7r7MkuQcWAYu2' │  6000021   │
│    7    │ 'B for swapper' │ 'AKUKWMmVPSjiVymMPshfd9HLhtKdDDg5CjmKQ7YePJfe' │  10805676  │
└─────────┴─────────────────┴────────────────────────────────────────────────┴────────────┘

Swap 3 (Token A -> Token B)

  sequenceDiagram
    participant Admin
    participant PoolManager as -PDA- <br/> Pool Manager
    participant Swapper
    Swapper ->>+ PoolManager : send 1999957 token A 
    PoolManager ->>+ Swapper : send 58143 token B
    PoolManager ->>+ Admin : mint 71985 LP Token as fee
Loading
┌─────────┬─────────────────┬────────────────────────────────────────────────┬────────────┐
│ (index) │      name       │                    address                     │   amount   │
├─────────┼─────────────────┼────────────────────────────────────────────────┼────────────┤
│    0    │  'A for userA'  │ 'J8a4ENEMgUHHfAz9VH2BuApK6mS8mjcUZjHeBypKN1KE' │   990104   │
│    1    │  'B for userA'  │ 'GmRp3YGmWFc2or9BkBuiANS9GZ7BEqxrg1ZPpCYvRqc2' │   990095   │
│    2    │ 'LP for userA'  │ '7BBxTGPFKxLmKVj6LV3yE6ojG9kFzRpFnunFxrsT7sW6' │  9905550   │
│    3    │  'A for PDA '   │ 'BVJohxH1hogZdcYBrTMuo7EVk9TSrjdGhrya4zzrGC7p' │  7009832   │
│    4    │  'B for PDA '   │ '9HRgCPn4nj415xayTHhLFqQrpWePPzewEmAqbUBDbtgZ' │   146086   │
│    5    │ 'LP for Admin'  │ '7XAnDxYcCRRSKewLdMnqtrwhjnbUd2Kvq5FfG8xMdRsp' │ 1000340309 │
│    6    │ 'A for swapper' │ '7NCo7QQyP1a1PSQMZZCFmxjYnAaWguX7r7MkuQcWAYu2' │  4000064   │
│    7    │ 'B for swapper' │ 'AKUKWMmVPSjiVymMPshfd9HLhtKdDDg5CjmKQ7YePJfe' │  10863819  │
└─────────┴─────────────────┴────────────────────────────────────────────────┴────────────┘

Swap 4 (Token A -> Token B)

  sequenceDiagram
    participant Admin
    participant PoolManager as -PDA- <br/> Pool Manager
    participant Swapper
    Swapper ->>+ PoolManager : send 1999970 token A 
    PoolManager ->>+ Swapper : send 32352 token B
    PoolManager ->>+ Admin : mint 56010 LP Token as fee
Loading
┌─────────┬─────────────────┬────────────────────────────────────────────────┬────────────┐
│ (index) │      name       │                    address                     │   amount   │
├─────────┼─────────────────┼────────────────────────────────────────────────┼────────────┤
│    0    │  'A for userA'  │ 'J8a4ENEMgUHHfAz9VH2BuApK6mS8mjcUZjHeBypKN1KE' │   990104   │
│    1    │  'B for userA'  │ 'GmRp3YGmWFc2or9BkBuiANS9GZ7BEqxrg1ZPpCYvRqc2' │   990095   │
│    2    │ 'LP for userA'  │ '7BBxTGPFKxLmKVj6LV3yE6ojG9kFzRpFnunFxrsT7sW6' │  9905550   │
│    3    │  'A for PDA '   │ 'BVJohxH1hogZdcYBrTMuo7EVk9TSrjdGhrya4zzrGC7p' │  9009802   │
│    4    │  'B for PDA '   │ '9HRgCPn4nj415xayTHhLFqQrpWePPzewEmAqbUBDbtgZ' │   113734   │
│    5    │ 'LP for Admin'  │ '7XAnDxYcCRRSKewLdMnqtrwhjnbUd2Kvq5FfG8xMdRsp' │ 1000396319 │
│    6    │ 'A for swapper' │ '7NCo7QQyP1a1PSQMZZCFmxjYnAaWguX7r7MkuQcWAYu2' │  2000094   │
│    7    │ 'B for swapper' │ 'AKUKWMmVPSjiVymMPshfd9HLhtKdDDg5CjmKQ7YePJfe' │  10896171  │
└─────────┴─────────────────┴────────────────────────────────────────────────┴────────────┘

Relation

  flowchart LR
  Admin[-Account- <br/> Admin]
  PoolManager[-PDA- <br/> Pool Manager]
  Depositor[-Account- <br/> Depositor]
  Swapper[-Account- <br/> Swapper]
  SwapPair[-Account- <br/> SwapPair]
  ThisProgram[-Program- <br/> This program]
  A[-Mint- <br/> A Token]
  B[-Mint- <br/> B Token]
  LP[-Mint- <br/> LP Token]
  DepositorTokenA[-TokenAccount- <br/> A for Depositor]
  DepositorTokenB[-TokenAccount- <br/> B for Deposit0r]
  DepositorTokenLP[-TokenAccount- <br/> LP for Depositor]
  PoolManagerTokenA[-TokenAccount- <br/> A for Pool Manager]
  PoolManagerTokenB[-TokenAccount- <br/> B for Pool Manager]
  AdminTokenA[-TokenAccount- <br/> A for Admin]
  AdminTokenB[-TokenAccount- <br/> A for Admin]
  AdminTokenLP[-TokenAccount- <br/> LP for Admin]
  SwapperTokenA[-TokenAccount- <br/> A for Swapper]
  SwapperTokenB[-TokenAccount- <br/> A for Swapper]
  A -- authority --> Admin
  B -- authority --> Admin
  LP -- authority --> PoolManager
  DepositorTokenA -- mint --> A
  DepositorTokenB -- mint --> B
  DepositorTokenLP -- mint --> LP 
  DepositorTokenA -- owner --> Depositor
  DepositorTokenB -- owner --> Depositor
  DepositorTokenLP -- owner --> Depositor
  PoolManagerTokenA -- mint --> A
  PoolManagerTokenB -- mint --> B
  PoolManagerTokenLP -- mint --> LP 
  PoolManagerTokenA -- owner --> PoolManager
  PoolManagerTokenB -- owner --> PoolManager
  AdminTokenA -- mint --> A
  AdminTokenB -- mint --> B
  AdminTokenLP -- mint --> LP 
  AdminTokenA -- owner --> Admin
  AdminTokenB -- owner --> Admin
  AdminTokenLP -- owner --> Admin
  SwapperTokenA -- mint --> A
  SwapperTokenB -- mint --> B
  SwapperTokenA -- owner --> Swapper
  SwapperTokenB -- owner --> Swapper
  SwapPair -- owner --> ThisProgram
Loading
You might also like...
Ray Tracing: The Next Week implementation in Rust
Ray Tracing: The Next Week implementation in Rust

rttnw Ray Tracing: The Next Week implementation in Rust How to run Install Rust: Link here. Run project git clone https://github.com/luliic2/rttnw cd

Rust implementation of µKanren, a featherweight relational programming language.

µKanren-rs This is a Rust implementation of µKanren, a featherweight relational programming language. See the original Scheme implementation here for

ESP32 implementation of RustZX Spectrum emulator for ESP32-USB-OTG

RustZX for ESP32 - experimental version Goal of the project: Run ZX Spectrum on ESP32 HW: ESP32 OTG USB with ST7789 display References Rust code for E

An implementation of Olm and Megolm in pure Rust.

A Rust implementation of Olm and Megolm vodozemac is a Rust implementation of libolm, a cryptographic library used for end-to-end encryption in Matrix

Pure Rust Implementation of secp256k1.

SECP256K1 implementation in pure Rust Cargo Documentation SECP256K1 implementation with no_std support. Currently we have implementation for: Convert

A Rust implementation of generic prefix tree (trie) map with wildcard capture support

prefix_tree_map A Rust implementation of generic prefix tree (trie) map with wildcard capture support. Design Trie is a good data structure for storin

fast rust implementation of online nonnegative matrix factorization as laid out in the paper "detect and track latent factors with online nonnegative matrix factorization"

ONMF status: early work in progress. still figuring this out. code still somewhat messy. api still in flux. fast rust implementation of online nonnega

An implementation of a predicative polymorphic language with bidirectional type inference and algebraic data types

Vinilla Lang Vanilla is a pure functional programming language based on System F, a classic but powerful type system. Merits Simple as it is, Vanilla

An implementation of Ngo et al's GenericJoin in timely dataflow.

dataflow-join A streaming implementation of Ngo et al's GenericJoin in timely dataflow. Ngo et al presented a very cool join algorithm, some details o

Owner
Something-Something
Hello, world!
Something-Something
🕶 Assorted checks and validations for writing safer Solana programs.

vipers ?? Assorted checks and validations for writing safer Solana programs. Motivation Solana's fee mechanism is unlike Ethereum's, in that the numbe

Saber 131 Sep 14, 2022
A simple heads or tails (cross and pile) on Solana

cross-pile (Coin flip) A heads or tails (cross and pile) on Solana. It's meant to serve as an example of how to use solrand, a randomness Oracle: http

Evan Marshall 22 Nov 19, 2022
Solana SVM, modularized.

The Case for a Modular SVM An effort is underway at Anza to extract most of the transaction processing pipeline out of the validator and into what wil

Joe C 42 Jul 26, 2024
An example of a Kubernetes operator implemented in Rust

Rust Kubernetes operator example A Kubernetes operator built on top of kube-rs project. There is an explanatory article available. Steps to run on Lin

Pavel Pscheidl 91 Jan 1, 2023
Complete code for the larger example programs from the book.

Code Examples for Programming Rust This repository contains complete code for the larger example programs from the book “Programming Rust”, by Jim Bla

Programming Rust 670 Jan 1, 2023
ARM TrustZone-M example application in Rust, both secure world side and non-secure world side

ARM TrustZone-M example application in Rust, both secure world side and non-secure world side; projects are modified from generated result of cortex-m-quickstart.

null 44 Dec 4, 2022
A Domain Driven Design example application in Rust.

Rust Domain Driven Design Example rust-ddd Rust Domain-Driven-Design (DDD) Summery This repository is used to present how I find implementing DDD in R

Behrouz R.Farsi 6 Nov 15, 2022
Example (with solution) for the Rust Fundamentals Workshop

Workshop Repo: Rust Fundamentals NOTE: This is Work-In-Progress! Please check for updates a day before the workshop. This Github repository contains a

Stefan Baumgartner 5 Jan 31, 2023
example codes for CIS198 https://cis198-2016s.github.io/

CIS198: RUST 编程语言 学习背景 rust 和 c/c++/Java/Python/golang 不太一样 rust 学习曲线比较陡峭 rust 有很多颠覆认知的特性: 所有权,生命周期,借用检测 cargo 工具 函数式+命令式支持 视频讲解见 B站 课程大纲 Timeline Lec

Jinghui Hu 3 Apr 9, 2024
Experimental Quantum Computer Simulator + Quantum Chess Implementation

Quantum Chess A somewhat hacky implementation of this paper (made in a week over a holiday). It's not heavily tested and probably has some bugs still

null 19 Jan 21, 2022