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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PHONY: build test format

build:
cargo build
cargo build-sbf

test:
cargo test
Expand Down
1 change: 1 addition & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub const BPS: i64 = 10_000;
pub const T: u64 = 1; // fixed time horizon
pub const PRICE_SCALE: i64 = 1_000_000; // 6-decimal price scaling factor
4 changes: 3 additions & 1 deletion src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use pinocchio::{
};

use crate::instructions::{
process_deposit, process_initialize_market, process_update_config, process_withdraw,
process_deposit, process_execute_swap, process_initialize_market, process_update_config,
process_withdraw,
};

program_entrypoint!(process_instruction);
Expand All @@ -23,6 +24,7 @@ fn process_instruction(
Some((2, data)) => process_update_config(program_id, accounts, data),
Some((3, data)) => process_deposit(program_id, accounts, data),
Some((4, data)) => process_withdraw(program_id, accounts, data),
Some((5, data)) => process_execute_swap(program_id, accounts, data),
_ => Err(ProgramError::InvalidInstructionData),
}
}
134 changes: 134 additions & 0 deletions src/instructions/execute_swap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use pinocchio::{
AccountView, Address, ProgramResult,
cpi::{Seed, Signer},
error::ProgramError,
};
use pinocchio_token::instructions::Transfer;

use crate::{
constants::PRICE_SCALE, math::compute_price_spread, state::MakerConfig, utils::impl_load_ix,
};

#[repr(C)]
#[derive(Clone, Copy)]
pub struct SwapArgs {
pub amount_in: u64,
pub min_amount_out: u64,
pub mid_price: i64,
pub side: u8,
pub _padding: [u8; 7],
}

impl_load_ix!(SwapArgs);

pub fn process_execute_swap(
_program_id: &Address,
accounts: &[AccountView],
data: &[u8],
) -> ProgramResult {
let [
user,
maker_config,
user_base_ata,
vault_base,
user_quote_ata,
vault_quote,
_token_program,
_remaining @ ..,
] = accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};

let args = SwapArgs::load(data)?;

if !user.is_signer() {
return Err(ProgramError::MissingRequiredSignature);
}

if args.side > 1 {
return Err(ProgramError::InvalidInstructionData);
}

let (bump, current_inventory, spread) = {
let config = MakerConfig::load(unsafe { maker_config.borrow_unchecked() })?;
let current_inventory = u64::from_le_bytes(config.current_inventory);
let spread = compute_price_spread(config, args.mid_price, current_inventory)?;
(config.bump, current_inventory, spread)
};

let amount_out: u64 = if args.side == 0 {
if spread.ask <= 0 {
return Err(ProgramError::InvalidArgument);
}
(args.amount_in as i64)
.checked_mul(PRICE_SCALE)
.ok_or(ProgramError::ArithmeticOverflow)?
.checked_div(spread.ask)
.ok_or(ProgramError::ArithmeticOverflow)? as u64
} else {
if spread.bid <= 0 {
return Err(ProgramError::InvalidArgument);
}
(args.amount_in as i64)
.checked_mul(spread.bid)
.ok_or(ProgramError::ArithmeticOverflow)?
.checked_div(PRICE_SCALE)
.ok_or(ProgramError::ArithmeticOverflow)? as u64
};

if amount_out < args.min_amount_out {
return Err(ProgramError::InvalidArgument);
}

let bump_arr = [bump];
let seeds = [Seed::from(b"maker_config" as &[u8]), Seed::from(&bump_arr)];
let signer = Signer::from(&seeds[..]);

let new_inventory: u64 = if args.side == 0 {
Transfer {
from: user_quote_ata,
to: vault_quote,
authority: user,
amount: args.amount_in,
}
.invoke()?;

Transfer {
from: vault_base,
to: user_base_ata,
authority: maker_config,
amount: amount_out,
}
.invoke_signed(&[signer])?;

current_inventory
.checked_sub(amount_out)
.ok_or(ProgramError::ArithmeticOverflow)?
} else {
Transfer {
from: user_base_ata,
to: vault_base,
authority: user,
amount: args.amount_in,
}
.invoke()?;

Transfer {
from: vault_quote,
to: user_quote_ata,
authority: maker_config,
amount: amount_out,
}
.invoke_signed(&[signer])?;

current_inventory
.checked_add(args.amount_in)
.ok_or(ProgramError::ArithmeticOverflow)?
};

let config = MakerConfig::load_mut(unsafe { maker_config.borrow_unchecked_mut() })?;
config.current_inventory = new_inventory.to_le_bytes();

Ok(())
}
1 change: 1 addition & 0 deletions src/instructions/initialize_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn process_initialize_market(
config.risk_aversion = args.risk_aversion.to_le_bytes();
config.variance = args.variance.to_le_bytes();
config.spread_factor = args.spread_factor.to_le_bytes();
config.current_inventory = args.target_inventory.to_le_bytes();
config.market_maker = *maker.address().as_array();
config.bump = args.bump;
config._padding = [0u8; 7];
Expand Down
2 changes: 2 additions & 0 deletions src/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub mod deposit;
pub mod execute_swap;
pub mod initialize_market;
pub mod update_config;
pub mod withdraw;

pub use deposit::*;
pub use execute_swap::*;
pub use initialize_market::*;
pub use update_config::*;
pub use withdraw::*;
1 change: 1 addition & 0 deletions src/state/maker_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct MakerConfig {
pub risk_aversion: [u8; 8], // bps
pub variance: [u8; 8],
pub spread_factor: [u8; 8],
pub current_inventory: [u8; 8], // live base token inventory, updated on every swap
pub market_maker: [u8; 32],
pub bump: u8,
pub _padding: [u8; 7],
Expand Down
4 changes: 4 additions & 0 deletions src/tests/initialize_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ fn test_initialize_market() {
);
assert_eq!(u64::from_le_bytes(config.risk_aversion), args.risk_aversion);
assert_eq!(config.market_maker, admin.pubkey().to_bytes());
assert_eq!(
u64::from_le_bytes(config.current_inventory),
args.target_inventory
);
assert_eq!(config.bump, bump);
}
3 changes: 3 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ pub mod deposit;

#[cfg(test)]
pub mod withdraw;

#[cfg(test)]
pub mod swap;
Loading
Loading