Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion tokens/token-swap/anchor/programs/token-swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ custom-panic = []
[dependencies]
anchor-lang = { version = "1.0.2", features = ["init-if-needed"] }
anchor-spl = { version = "1.0.2", features = ["metadata"] }
fixed = "1.27.0"

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use anchor_spl::{
associated_token::AssociatedToken,
token::{self, Mint, MintTo, Token, TokenAccount, Transfer},
};
use fixed::types::I64F64;

use crate::{
constants::{AUTHORITY_SEED, LIQUIDITY_SEED, MINIMUM_LIQUIDITY},
Expand Down Expand Up @@ -64,12 +63,12 @@ pub fn deposit_liquidity(
}
};

// Computing the amount of liquidity about to be deposited
let mut liquidity = I64F64::from_num(amount_a)
.checked_mul(I64F64::from_num(amount_b))
// Computing the amount of liquidity about to be deposited.
// Multiply in u128 so the product of two u64 amounts cannot overflow.
let mut liquidity = (amount_a as u128)
.checked_mul(amount_b as u128)
.unwrap()
.sqrt()
.to_num::<u64>();
.isqrt() as u64;

// Lock some minimum liquidity on the first deposit
if pool_creation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use anchor_spl::{
associated_token::AssociatedToken,
token::{self, Mint, Token, TokenAccount, Transfer},
};
use fixed::types::I64F64;

use crate::{
constants::AUTHORITY_SEED,
Expand Down Expand Up @@ -32,28 +31,21 @@ pub fn swap_exact_tokens_for_tokens(

let pool_a = &ctx.accounts.pool_account_a;
let pool_b = &ctx.accounts.pool_account_b;
// Constant-product output, computed in u128 so taxed_input * pool amount
// cannot overflow.
let output = if swap_a {
I64F64::from_num(taxed_input)
.checked_mul(I64F64::from_num(pool_b.amount))
(taxed_input as u128)
.checked_mul(pool_b.amount as u128)
.unwrap()
.checked_div(
I64F64::from_num(pool_a.amount)
.checked_add(I64F64::from_num(taxed_input))
.unwrap(),
)
.checked_div((pool_a.amount as u128).checked_add(taxed_input as u128).unwrap())
.unwrap()
} else {
I64F64::from_num(taxed_input)
.checked_mul(I64F64::from_num(pool_a.amount))
(taxed_input as u128)
.checked_mul(pool_a.amount as u128)
.unwrap()
.checked_div(
I64F64::from_num(pool_b.amount)
.checked_add(I64F64::from_num(taxed_input))
.unwrap(),
)
.checked_div((pool_b.amount as u128).checked_add(taxed_input as u128).unwrap())
.unwrap()
}
.to_num::<u64>();
} as u64;

if output < min_output_amount {
return err!(TutorialError::OutputTooSmall);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use anchor_spl::{
associated_token::AssociatedToken,
token::{self, Burn, Mint, Token, TokenAccount, Transfer},
};
use fixed::types::I64F64;

use crate::{
constants::{AUTHORITY_SEED, LIQUIDITY_SEED, MINIMUM_LIQUIDITY},
Expand All @@ -21,16 +20,13 @@ pub fn withdraw_liquidity(ctx: Context<WithdrawLiquidity>, amount: u64) -> Resul
];
let signer_seeds = &[&authority_seeds[..]];

// Transfer tokens from the pool
let amount_a = I64F64::from_num(amount)
.checked_mul(I64F64::from_num(ctx.accounts.pool_account_a.amount))
// Transfer tokens from the pool. Compute in u128 so amount * pool balance
// cannot overflow.
let amount_a = (amount as u128)
.checked_mul(ctx.accounts.pool_account_a.amount as u128)
.unwrap()
.checked_div(I64F64::from_num(
ctx.accounts.mint_liquidity.supply + MINIMUM_LIQUIDITY,
))
.unwrap()
.floor()
.to_num::<u64>();
.checked_div(ctx.accounts.mint_liquidity.supply as u128 + MINIMUM_LIQUIDITY as u128)
.unwrap() as u64;
token::transfer(
CpiContext::new_with_signer(
ctx.accounts.token_program.key(),
Expand All @@ -44,15 +40,11 @@ pub fn withdraw_liquidity(ctx: Context<WithdrawLiquidity>, amount: u64) -> Resul
amount_a,
)?;

let amount_b = I64F64::from_num(amount)
.checked_mul(I64F64::from_num(ctx.accounts.pool_account_b.amount))
.unwrap()
.checked_div(I64F64::from_num(
ctx.accounts.mint_liquidity.supply + MINIMUM_LIQUIDITY,
))
let amount_b = (amount as u128)
.checked_mul(ctx.accounts.pool_account_b.amount as u128)
.unwrap()
.floor()
.to_num::<u64>();
.checked_div(ctx.accounts.mint_liquidity.supply as u128 + MINIMUM_LIQUIDITY as u128)
.unwrap() as u64;
token::transfer(
CpiContext::new_with_signer(
ctx.accounts.token_program.key(),
Expand Down
2 changes: 1 addition & 1 deletion tokens/token-swap/anchor/programs/token-swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod instructions;
mod state;

// Set the correct key here
declare_id!("AsGVFxWqEn8icRBFQApxJe68x3r9zvfSbmiEzYFATGYn");
declare_id!("J1seUW7nGbsUeDQFBRe4bjC6c8ZRYKFqjV4CwiRSH18V");
Comment thread
dev-jodee marked this conversation as resolved.

#[program]
pub mod swap_example {
Expand Down
Loading