This is the example anchor implementation for solana-swap.
- The curve is always
ConstantProduct
in this implementation. - The fee receiver is always Admin.
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
sequenceDiagram
participant Admin
participant PoolManager as -PDA- <br/> Pool Manager
PoolManager ->>+ Admin : mint 1000000000 LP Token
βββββββββββ¬βββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β (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 β
βββββββββββ΄βββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
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
βββββββββββ¬βββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β (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 β
βββββββββββ΄βββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
sequenceDiagram
participant PoolManager as -PDA- <br/> Pool Manager
participant Depositor
Depositor ->>+ PoolManager : send 100000 token A
PoolManager ->>+ Depositor : mint 48760478 LP Token
βββββββββββ¬βββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β (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 β
βββββββββββ΄βββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
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
βββββββββββ¬βββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β (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 β
βββββββββββ΄βββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
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
βββββββββββ¬βββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β (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 β
βββββββββββ΄βββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
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
βββββββββββ¬ββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β (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 β
βββββββββββ΄ββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
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
βββββββββββ¬ββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β (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 β
βββββββββββ΄ββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
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
βββββββββββ¬ββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β (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 β
βββββββββββ΄ββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
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
βββββββββββ¬ββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β (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 β
βββββββββββ΄ββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
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