Automated market makers smart contract
This code has NOT been auditted, use on your own risk.
read https://docs.solana.com/developing/programming-model/overview
- Owner
- User
-
Create
Owner creats amm pool, transfer two kinds of tokens when creating.
Then we have the fixed k, k value is the product of tokens' amount.
-
Swap
User transfers token into amm pool, for swapping another token.
Amounts of transfer is follow the formula below.
$$ (a-∆a)*(b+∆b)=k $$ In fact, because of calculate resolution is limited, there will be a little error.
-
Terminate
Owner terminate the amm pool, withdrawal all tokens, and close all account.
-
fee
User transfers some extra token as fee, when swapping.
The fee mint and rate is configured by owner, when creating.
Owner could withdrawal fee any time.
Owner will withdrawal all fee when terminating.
-
install rustc v1.56.1, read https://www.rust-lang.org/tools/install
-
install solana cli v1.9.5, read https://docs.solana.com/cli/install-solana-cli-tools
-
confirm id.json file path
-
build
cargo build-bpf
-
confirm id.json file wallet has enough SOL.
-
deploy
solana program deploy target/deploy/amm.so
-
edit AmmProgramId which is in js/index.js
-
test
npm run test
- src/ smart contract code
- js/ js code for calling smart contract
- client/ client application
- When token amount is zero, or it would be zero, swap will fail.
- Fee is calculated base on token amount, it might be error because of decimals.
- There isn't a reasonable for checking swap calculation error.
super swap
There is two amm pool, they has token a/b and b/c.
Then it could swap twice, swap a/c by using b as middle template.
export PATH="/home/ubuntu/.local/share/solana/install/active_release/bin:$PATH"
solana program deploy amm.so