Skip to content

Commit

Permalink
add token swap example
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamSspirit committed Jul 31, 2023
1 parent 79574af commit dd5611d
Show file tree
Hide file tree
Showing 23 changed files with 1,621 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tokens/token-swap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Token swap example amm in anchor rust
## coming soon
15 changes: 15 additions & 0 deletions tokens/token-swap/anchor/Anchor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[features]
seeds = false
skip-lint = false
[programs.devnet]
amm_tutorial = "C3ti6PFK6PoYShRFx1BNNTQU3qeY1iVwjwCA6SjJhiuW"

[registry]
url = "https://api.apr.dev"

[provider]
cluster = "Devnet"
wallet = "/Users/shivamsoni/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
14 changes: 14 additions & 0 deletions tokens/token-swap/anchor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[workspace]
members = [
"programs/*"
]

[profile.release]
overflow-checks = true
lto = "fat"
codegen-units = 1
[profile.release.build-override]
opt-level = 3
incremental = false
codegen-units = 1

20 changes: 20 additions & 0 deletions tokens/token-swap/anchor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"scripts": {
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
},
"dependencies": {
"@project-serum/anchor": "^0.26.0",
"@solana/spl-token": "^0.3.8"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0",
"chai": "^4.3.4",
"mocha": "^9.0.3",
"prettier": "^2.6.2",
"ts-mocha": "^10.0.0",
"typescript": "^4.3.5"
}
}
27 changes: 27 additions & 0 deletions tokens/token-swap/anchor/programs/token-swap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "amm-tutorial"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "amm_tutorial"

[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = { version = "0.26.0", features = ["init-if-needed"] }
anchor-spl = { version = "0.26.0" }
fixed = "1.23.1"
half = "=2.2.1"
fixed-sqrt = "0.2.5"
solana-program = "~1.14.19"
winnow = "=0.4.1"
toml_datetime = "=0.6.1"

2 changes: 2 additions & 0 deletions tokens/token-swap/anchor/programs/token-swap/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
10 changes: 10 additions & 0 deletions tokens/token-swap/anchor/programs/token-swap/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use anchor_lang::prelude::*;

#[constant]
pub const MINIMUM_LIQUIDITY: u64 = 100;

#[constant]
pub const AUTHORITY_SEED: &str = "authority";

#[constant]
pub const LIQUIDITY_SEED: &str = "liquidity";
19 changes: 19 additions & 0 deletions tokens/token-swap/anchor/programs/token-swap/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anchor_lang::prelude::*;

#[error_code]
pub enum TutorialError {
#[msg("Invalid fee value")]
InvalidFee,

#[msg("Invalid mint for the pool")]
InvalidMint,

#[msg("Depositing too little liquidity")]
DepositTooSmall,

#[msg("Output is below the minimum expected")]
OutputTooSmall,

#[msg("Invariant does not hold")]
InvariantViolated,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use anchor_lang::prelude::*;

use crate::{errors::*, state::Amm};

pub fn create_amm(ctx: Context<CreateAmm>, id: Pubkey, fee: u16) -> Result<()> {
let amm = &mut ctx.accounts.amm;
amm.id = id;
amm.admin = ctx.accounts.admin.key();
amm.fee = fee;

Ok(())
}

#[derive(Accounts)]
#[instruction(id: Pubkey, fee: u16)]
pub struct CreateAmm<'info> {
#[account(
init,
payer = payer,
space = Amm::LEN,
seeds = [
id.as_ref()
],
bump,
constraint = fee < 10000 @ TutorialError::InvalidFee,
)]
pub amm: Account<'info, Amm>,

/// The admin of the AMM
/// CHECK: Read only, delegatable creation
pub admin: AccountInfo<'info>,

/// The account paying for all rents
#[account(mut)]
pub payer: Signer<'info>,

/// Solana ecosystem accounts
pub system_program: Program<'info, System>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use anchor_lang::prelude::*;
use anchor_spl::{
associated_token::AssociatedToken,
token::{Mint, Token, TokenAccount},
};

use crate::{
constants::{AUTHORITY_SEED, LIQUIDITY_SEED},
errors::*,
state::{Amm, Pool},
};

pub fn create_pool(ctx: Context<CreatePool>) -> Result<()> {
let pool = &mut ctx.accounts.pool;
pool.amm = ctx.accounts.amm.key();
pool.mint_a = ctx.accounts.mint_a.key();
pool.mint_b = ctx.accounts.mint_b.key();

Ok(())
}

#[derive(Accounts)]
pub struct CreatePool<'info> {
#[account(
seeds = [
amm.id.as_ref()
],
bump,
)]
pub amm: Account<'info, Amm>,

#[account(
init,
payer = payer,
space = Pool::LEN,
seeds = [
amm.key().as_ref(),
mint_a.key().as_ref(),
mint_b.key().as_ref(),
],
bump,
constraint = mint_a.key() < mint_b.key() @ TutorialError::InvalidMint
)]
pub pool: Account<'info, Pool>,

/// CHECK: Read only authority
#[account(
seeds = [
amm.key().as_ref(),
mint_a.key().as_ref(),
mint_b.key().as_ref(),
AUTHORITY_SEED.as_ref(),
],
bump,
)]
pub pool_authority: AccountInfo<'info>,

#[account(
init,
payer = payer,
seeds = [
amm.key().as_ref(),
mint_a.key().as_ref(),
mint_b.key().as_ref(),
LIQUIDITY_SEED.as_ref(),
],
bump,
mint::decimals = 6,
mint::authority = pool_authority,
)]
pub mint_liquidity: Box<Account<'info, Mint>>,

pub mint_a: Box<Account<'info, Mint>>,

pub mint_b: Box<Account<'info, Mint>>,

#[account(
init,
payer = payer,
associated_token::mint = mint_a,
associated_token::authority = pool_authority,
)]
pub pool_account_a: Box<Account<'info, TokenAccount>>,

#[account(
init,
payer = payer,
associated_token::mint = mint_b,
associated_token::authority = pool_authority,
)]
pub pool_account_b: Box<Account<'info, TokenAccount>>,

/// The account paying for all rents
#[account(mut)]
pub payer: Signer<'info>,

/// Solana ecosystem accounts
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub system_program: Program<'info, System>,
}
Loading

0 comments on commit dd5611d

Please sign in to comment.