Skip to content

Commit 419cb6b

Browse files
Merge pull request #38 from shivamsoni00/tokenswap
2 parents 79574af + 8fbeb1b commit 419cb6b

23 files changed

+1948
-0
lines changed

tokens/token-swap/README.md

+332
Large diffs are not rendered by default.

tokens/token-swap/anchor/Anchor.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[features]
2+
seeds = false
3+
skip-lint = false
4+
[programs.devnet]
5+
swap_example = "C3ti6PFK6PoYShRFx1BNNTQU3qeY1iVwjwCA6SjJhiuW"
6+
7+
[registry]
8+
url = "https://api.apr.dev"
9+
10+
[provider]
11+
cluster = "Devnet"
12+
wallet = "~/.config/solana/id.json"
13+
14+
[scripts]
15+
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

tokens/token-swap/anchor/Cargo.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[workspace]
2+
members = [
3+
"programs/*"
4+
]
5+
6+
[profile.release]
7+
overflow-checks = true
8+
lto = "fat"
9+
codegen-units = 1
10+
[profile.release.build-override]
11+
opt-level = 3
12+
incremental = false
13+
codegen-units = 1
14+

tokens/token-swap/anchor/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"scripts": {
3+
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
4+
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
5+
},
6+
"dependencies": {
7+
"@coral-xyz/anchor": "^0.27.0",
8+
"@solana/spl-token": "^0.3.8"
9+
},
10+
"devDependencies": {
11+
"@types/bn.js": "^5.1.0",
12+
"@types/chai": "^4.3.0",
13+
"@types/mocha": "^9.0.0",
14+
"chai": "^4.3.4",
15+
"mocha": "^9.0.3",
16+
"prettier": "^2.6.2",
17+
"ts-mocha": "^10.0.0",
18+
"typescript": "^4.3.5"
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "amm-tutorial"
3+
version = "0.1.0"
4+
description = "Created with Anchor"
5+
edition = "2021"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "amm_tutorial"
10+
11+
[features]
12+
no-entrypoint = []
13+
no-idl = []
14+
no-log-ix-name = []
15+
cpi = ["no-entrypoint"]
16+
default = []
17+
18+
[dependencies]
19+
anchor-lang = { version = "0.26.0", features = ["init-if-needed"] }
20+
anchor-spl = { version = "0.26.0" }
21+
fixed = "1.23.1"
22+
half = "=2.2.1"
23+
fixed-sqrt = "0.2.5"
24+
solana-program = "~1.14.19"
25+
winnow = "=0.4.1"
26+
toml_datetime = "=0.6.1"
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.bpfel-unknown-unknown.dependencies.std]
2+
features = []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use anchor_lang::prelude::*;
2+
3+
#[constant]
4+
pub const MINIMUM_LIQUIDITY: u64 = 100;
5+
6+
#[constant]
7+
pub const AUTHORITY_SEED: &str = "authority";
8+
9+
#[constant]
10+
pub const LIQUIDITY_SEED: &str = "liquidity";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use anchor_lang::prelude::*;
2+
3+
#[error_code]
4+
pub enum TutorialError {
5+
#[msg("Invalid fee value")]
6+
InvalidFee,
7+
8+
#[msg("Invalid mint for the pool")]
9+
InvalidMint,
10+
11+
#[msg("Depositing too little liquidity")]
12+
DepositTooSmall,
13+
14+
#[msg("Output is below the minimum expected")]
15+
OutputTooSmall,
16+
17+
#[msg("Invariant does not hold")]
18+
InvariantViolated,
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use anchor_lang::prelude::*;
2+
3+
use crate::{errors::*, state::Amm};
4+
5+
pub fn create_amm(ctx: Context<CreateAmm>, id: Pubkey, fee: u16) -> Result<()> {
6+
let amm = &mut ctx.accounts.amm;
7+
amm.id = id;
8+
amm.admin = ctx.accounts.admin.key();
9+
amm.fee = fee;
10+
11+
Ok(())
12+
}
13+
14+
#[derive(Accounts)]
15+
#[instruction(id: Pubkey, fee: u16)]
16+
pub struct CreateAmm<'info> {
17+
#[account(
18+
init,
19+
payer = payer,
20+
space = Amm::LEN,
21+
seeds = [
22+
id.as_ref()
23+
],
24+
bump,
25+
constraint = fee < 10000 @ TutorialError::InvalidFee,
26+
)]
27+
pub amm: Account<'info, Amm>,
28+
29+
/// The admin of the AMM
30+
/// CHECK: Read only, delegatable creation
31+
pub admin: AccountInfo<'info>,
32+
33+
/// The account paying for all rents
34+
#[account(mut)]
35+
pub payer: Signer<'info>,
36+
37+
/// Solana ecosystem accounts
38+
pub system_program: Program<'info, System>,
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
use anchor_lang::prelude::*;
2+
use anchor_spl::{
3+
associated_token::AssociatedToken,
4+
token::{Mint, Token, TokenAccount},
5+
};
6+
7+
use crate::{
8+
constants::{AUTHORITY_SEED, LIQUIDITY_SEED},
9+
errors::*,
10+
state::{Amm, Pool},
11+
};
12+
13+
pub fn create_pool(ctx: Context<CreatePool>) -> Result<()> {
14+
let pool = &mut ctx.accounts.pool;
15+
pool.amm = ctx.accounts.amm.key();
16+
pool.mint_a = ctx.accounts.mint_a.key();
17+
pool.mint_b = ctx.accounts.mint_b.key();
18+
19+
Ok(())
20+
}
21+
22+
#[derive(Accounts)]
23+
pub struct CreatePool<'info> {
24+
#[account(
25+
seeds = [
26+
amm.id.as_ref()
27+
],
28+
bump,
29+
)]
30+
pub amm: Account<'info, Amm>,
31+
32+
#[account(
33+
init,
34+
payer = payer,
35+
space = Pool::LEN,
36+
seeds = [
37+
amm.key().as_ref(),
38+
mint_a.key().as_ref(),
39+
mint_b.key().as_ref(),
40+
],
41+
bump,
42+
constraint = mint_a.key() < mint_b.key() @ TutorialError::InvalidMint
43+
)]
44+
pub pool: Account<'info, Pool>,
45+
46+
/// CHECK: Read only authority
47+
#[account(
48+
seeds = [
49+
amm.key().as_ref(),
50+
mint_a.key().as_ref(),
51+
mint_b.key().as_ref(),
52+
AUTHORITY_SEED.as_ref(),
53+
],
54+
bump,
55+
)]
56+
pub pool_authority: AccountInfo<'info>,
57+
58+
#[account(
59+
init,
60+
payer = payer,
61+
seeds = [
62+
amm.key().as_ref(),
63+
mint_a.key().as_ref(),
64+
mint_b.key().as_ref(),
65+
LIQUIDITY_SEED.as_ref(),
66+
],
67+
bump,
68+
mint::decimals = 6,
69+
mint::authority = pool_authority,
70+
)]
71+
pub mint_liquidity: Box<Account<'info, Mint>>,
72+
73+
pub mint_a: Box<Account<'info, Mint>>,
74+
75+
pub mint_b: Box<Account<'info, Mint>>,
76+
77+
#[account(
78+
init,
79+
payer = payer,
80+
associated_token::mint = mint_a,
81+
associated_token::authority = pool_authority,
82+
)]
83+
pub pool_account_a: Box<Account<'info, TokenAccount>>,
84+
85+
#[account(
86+
init,
87+
payer = payer,
88+
associated_token::mint = mint_b,
89+
associated_token::authority = pool_authority,
90+
)]
91+
pub pool_account_b: Box<Account<'info, TokenAccount>>,
92+
93+
/// The account paying for all rents
94+
#[account(mut)]
95+
pub payer: Signer<'info>,
96+
97+
/// Solana ecosystem accounts
98+
pub token_program: Program<'info, Token>,
99+
pub associated_token_program: Program<'info, AssociatedToken>,
100+
pub system_program: Program<'info, System>,
101+
}

0 commit comments

Comments
 (0)