mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 07:26:51 +00:00
cleanup
This commit is contained in:
21
api/Cargo.toml
Normal file
21
api/Cargo.toml
Normal file
@@ -0,0 +1,21 @@
|
||||
[package]
|
||||
name = "ore-api"
|
||||
description.workspace = true
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
homepage.workspace = true
|
||||
documentation.workspace = true
|
||||
repository.workspace = true
|
||||
keywords.workspace = true
|
||||
|
||||
[dependencies]
|
||||
bytemuck.workspace = true
|
||||
const-crypto.workspace = true
|
||||
mpl-token-metadata.workspace = true
|
||||
num_enum.workspace = true
|
||||
solana-program.workspace = true
|
||||
spl-token.workspace = true
|
||||
spl-associated-token-account.workspace = true
|
||||
steel.workspace = true
|
||||
thiserror.workspace = true
|
||||
77
api/src/consts.rs
Normal file
77
api/src/consts.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use const_crypto::ed25519;
|
||||
use solana_program::{pubkey, pubkey::Pubkey};
|
||||
|
||||
/// The authority allowed to initialize the program.
|
||||
pub const ADMIN_ADDRESS: Pubkey = pubkey!("HBUh9g46wk2X89CvaNN15UmsznP59rh6od1h8JwYAopk");
|
||||
|
||||
/// The decimal precision of the ORE token.
|
||||
/// There are 100 billion indivisible units per ORE (called "grams").
|
||||
pub const TOKEN_DECIMALS: u8 = 11;
|
||||
|
||||
/// One ORE token, denominated in indivisible units.
|
||||
pub const ONE_ORE: u64 = 10u64.pow(TOKEN_DECIMALS as u32);
|
||||
|
||||
/// The duration of one minute, in seconds.
|
||||
pub const ONE_MINUTE: i64 = 60;
|
||||
|
||||
/// The maximum token supply (5 million).
|
||||
pub const MAX_SUPPLY: u64 = ONE_ORE * 5_000_000;
|
||||
|
||||
/// The seed of the block account PDA.
|
||||
pub const BLOCK: &[u8] = b"block";
|
||||
|
||||
/// The seed of the stake account PDA.
|
||||
pub const STAKE: &[u8] = b"stake";
|
||||
|
||||
/// The seed of the config account PDA.
|
||||
pub const CONFIG: &[u8] = b"config";
|
||||
|
||||
/// The seed of the market account PDA.
|
||||
pub const MARKET: &[u8] = b"market";
|
||||
|
||||
/// The seed of the miner account PDA.
|
||||
pub const MINER: &[u8] = b"miner";
|
||||
|
||||
/// The seed of the mint account PDA.
|
||||
pub const MINT: &[u8] = b"mint";
|
||||
|
||||
/// The seed of the permit account PDA.
|
||||
pub const PERMIT: &[u8] = b"permit";
|
||||
|
||||
/// The seed of the treasury account PDA.
|
||||
pub const TREASURY: &[u8] = b"treasury";
|
||||
|
||||
/// Program id for const pda derivations
|
||||
const PROGRAM_ID: [u8; 32] = unsafe { *(&crate::id() as *const Pubkey as *const [u8; 32]) };
|
||||
|
||||
/// The address of the config account.
|
||||
pub const CONFIG_ADDRESS: Pubkey =
|
||||
Pubkey::new_from_array(ed25519::derive_program_address(&[CONFIG], &PROGRAM_ID).0);
|
||||
|
||||
/// The address of the mint account.
|
||||
pub const MINT_ADDRESS: Pubkey = pubkey!("Kw7itxU5N79d6xGkMwPqd3CHHkwr3ZJsG6WFqDivcTz");
|
||||
|
||||
/// The address of the treasury account.
|
||||
pub const TREASURY_ADDRESS: Pubkey =
|
||||
Pubkey::new_from_array(ed25519::derive_program_address(&[TREASURY], &PROGRAM_ID).0);
|
||||
|
||||
/// Denominator for protocol fee calculations.
|
||||
pub const FEE_RATE_BPS: u64 = 100;
|
||||
|
||||
/// Denominator for fee calculations.
|
||||
pub const DENOMINATOR_BPS: u64 = 10_000;
|
||||
|
||||
/// Slot window size, used for sandwich resistance.
|
||||
pub const SLOT_WINDOW: u64 = 4;
|
||||
|
||||
/// Amount of hash tokens to mint to market.
|
||||
pub const HASH_TOKEN_SUPPLY: u64 = 10_000_000;
|
||||
|
||||
/// The virtual liquidity to seed the markets with (in ORE).
|
||||
pub const VIRTUAL_LIQUIDITY: u64 = ONE_ORE * 5;
|
||||
|
||||
/// The minimum difficulty required for payout.
|
||||
pub const MIN_DIFFICULTY: u64 = 10;
|
||||
|
||||
/// The reward rate per satisfying hash (0.002048 ORE).
|
||||
pub const REWARD_RATE: u64 = 204_800_000;
|
||||
19
api/src/error.rs
Normal file
19
api/src/error.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use steel::*;
|
||||
|
||||
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
|
||||
#[repr(u32)]
|
||||
pub enum OreError {
|
||||
#[error("Placeholder error")]
|
||||
Dummy = 0,
|
||||
|
||||
#[error("Insufficient vault reserves")]
|
||||
InsufficientVaultReserves = 1,
|
||||
|
||||
#[error("Invariant violation")]
|
||||
InvariantViolation = 2,
|
||||
|
||||
#[error("Insufficient liquidity")]
|
||||
InsufficientLiquidity = 3,
|
||||
}
|
||||
|
||||
error!(OreError);
|
||||
42
api/src/event.rs
Normal file
42
api/src/event.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use steel::*;
|
||||
|
||||
use crate::state::SwapDirection;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
|
||||
pub struct SwapEvent {
|
||||
/// The block id.
|
||||
pub block_id: u64,
|
||||
|
||||
/// Swap direction.
|
||||
pub direction: u64,
|
||||
|
||||
/// Amount of base tokens to transfer.
|
||||
pub base_to_transfer: u64,
|
||||
|
||||
/// Amount of quote tokens to transfer.
|
||||
pub quote_to_transfer: u64,
|
||||
|
||||
/// Amount of base tokens swapped via virtual limit order.
|
||||
pub base_via_order: u64,
|
||||
|
||||
/// Amount of quote tokens swapped via virtual limit order.
|
||||
pub quote_via_order: u64,
|
||||
|
||||
/// Amount of base tokens swapped via curve.
|
||||
pub base_via_curve: u64,
|
||||
|
||||
/// Amount of quote tokens swapped via curve.
|
||||
pub quote_via_curve: u64,
|
||||
|
||||
/// Amount of quote tokens taken in fees.
|
||||
pub quote_fee: u64,
|
||||
}
|
||||
|
||||
impl SwapEvent {
|
||||
pub fn direction(&self) -> SwapDirection {
|
||||
SwapDirection::try_from(self.direction as u8).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
event!(SwapEvent);
|
||||
73
api/src/instruction.rs
Normal file
73
api/src/instruction.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use steel::*;
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
|
||||
pub enum OreInstruction {
|
||||
Open = 0,
|
||||
Close = 1,
|
||||
Commit = 2,
|
||||
Decommit = 3,
|
||||
Deposit = 4,
|
||||
Mine = 5,
|
||||
Swap = 6,
|
||||
Withdraw = 7,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Open {
|
||||
pub id: [u8; 8],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Close {}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Commit {
|
||||
pub amount: [u8; 8],
|
||||
pub executor: [u8; 32],
|
||||
pub fee: [u8; 8],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Decommit {
|
||||
pub amount: [u8; 8],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Deposit {
|
||||
pub amount: [u8; 8],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Mine {
|
||||
pub amount: [u8; 8],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Swap {
|
||||
pub amount: [u8; 8],
|
||||
pub direction: u8,
|
||||
pub precision: u8,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
|
||||
pub struct Withdraw {
|
||||
pub amount: [u8; 8],
|
||||
}
|
||||
|
||||
instruction!(OreInstruction, Open);
|
||||
instruction!(OreInstruction, Close);
|
||||
instruction!(OreInstruction, Commit);
|
||||
instruction!(OreInstruction, Decommit);
|
||||
instruction!(OreInstruction, Deposit);
|
||||
instruction!(OreInstruction, Mine);
|
||||
instruction!(OreInstruction, Swap);
|
||||
instruction!(OreInstruction, Withdraw);
|
||||
18
api/src/lib.rs
Normal file
18
api/src/lib.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
pub mod consts;
|
||||
pub mod error;
|
||||
pub mod event;
|
||||
pub mod instruction;
|
||||
pub mod sdk;
|
||||
pub mod state;
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::consts::*;
|
||||
pub use crate::error::*;
|
||||
pub use crate::event::*;
|
||||
pub use crate::instruction::*;
|
||||
pub use crate::state::*;
|
||||
}
|
||||
|
||||
use steel::*;
|
||||
|
||||
declare_id!("EmxGq9Bj8q6V998KDq3v19ch2DnARwhcNL2uXtgDFbra");
|
||||
126
api/src/sdk.rs
Normal file
126
api/src/sdk.rs
Normal file
@@ -0,0 +1,126 @@
|
||||
use spl_associated_token_account::get_associated_token_address;
|
||||
use steel::*;
|
||||
|
||||
use crate::{
|
||||
consts::{MINT_ADDRESS, TREASURY_ADDRESS},
|
||||
instruction::*,
|
||||
state::*,
|
||||
};
|
||||
|
||||
pub fn open(signer: Pubkey, id: u64) -> Instruction {
|
||||
let block_adddress = block_pda(id).0;
|
||||
let market_address = market_pda(id).0;
|
||||
let base_mint_address = mint_pda(id).0;
|
||||
let vault_base_address = get_associated_token_address(&market_address, &base_mint_address);
|
||||
let vault_quote_address = get_associated_token_address(&market_address, &MINT_ADDRESS);
|
||||
Instruction {
|
||||
program_id: crate::ID,
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(block_adddress, false),
|
||||
AccountMeta::new(market_address, false),
|
||||
AccountMeta::new(base_mint_address, false),
|
||||
AccountMeta::new(MINT_ADDRESS, false),
|
||||
AccountMeta::new(vault_base_address, false),
|
||||
AccountMeta::new(vault_quote_address, false),
|
||||
AccountMeta::new_readonly(system_program::ID, false),
|
||||
AccountMeta::new_readonly(spl_token::ID, false),
|
||||
AccountMeta::new_readonly(spl_associated_token_account::ID, false),
|
||||
AccountMeta::new_readonly(sysvar::rent::ID, false),
|
||||
],
|
||||
data: Open {
|
||||
id: id.to_le_bytes(),
|
||||
}
|
||||
.to_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close(signer: Pubkey, recipient: Pubkey, id: u64) -> Instruction {
|
||||
let block_adddress = block_pda(id).0;
|
||||
let market_address = market_pda(id).0;
|
||||
let base_mint_address = mint_pda(id).0;
|
||||
let vault_base = get_associated_token_address(&market_address, &base_mint_address);
|
||||
let vault_quote = get_associated_token_address(&market_address, &MINT_ADDRESS);
|
||||
Instruction {
|
||||
program_id: crate::ID,
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(block_adddress, false),
|
||||
AccountMeta::new(market_address, false),
|
||||
AccountMeta::new(base_mint_address, false),
|
||||
AccountMeta::new(MINT_ADDRESS, false),
|
||||
AccountMeta::new(recipient, false),
|
||||
AccountMeta::new_readonly(TREASURY_ADDRESS, false),
|
||||
AccountMeta::new(vault_base, false),
|
||||
AccountMeta::new(vault_quote, false),
|
||||
AccountMeta::new_readonly(system_program::ID, false),
|
||||
AccountMeta::new_readonly(spl_token::ID, false),
|
||||
],
|
||||
data: Close {}.to_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mine(signer: Pubkey, id: u64, amount: u64) -> Instruction {
|
||||
let block_adddress = block_pda(id).0;
|
||||
let market_address = market_pda(id).0;
|
||||
let base_mint_address = mint_pda(id).0;
|
||||
let miner_address = miner_pda(signer).0;
|
||||
let sender = get_associated_token_address(&signer, &base_mint_address);
|
||||
Instruction {
|
||||
program_id: crate::ID,
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(block_adddress, false),
|
||||
AccountMeta::new(market_address, false),
|
||||
AccountMeta::new(miner_address, false),
|
||||
AccountMeta::new(MINT_ADDRESS, false),
|
||||
AccountMeta::new(sender, false),
|
||||
AccountMeta::new_readonly(system_program::ID, false),
|
||||
AccountMeta::new_readonly(spl_token::ID, false),
|
||||
AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
|
||||
],
|
||||
data: Mine {
|
||||
amount: amount.to_le_bytes(),
|
||||
}
|
||||
.to_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn swap(
|
||||
signer: Pubkey,
|
||||
id: u64,
|
||||
amount: u64,
|
||||
direction: SwapDirection,
|
||||
precision: SwapPrecision,
|
||||
) -> Instruction {
|
||||
let block_adddress = block_pda(id).0;
|
||||
let market_address = market_pda(id).0;
|
||||
let base_mint_address = mint_pda(id).0;
|
||||
let tokens_base_address = get_associated_token_address(&signer, &base_mint_address);
|
||||
let tokens_quote_address = get_associated_token_address(&signer, &MINT_ADDRESS);
|
||||
let vault_base_address = get_associated_token_address(&market_address, &base_mint_address);
|
||||
let vault_quote_address = get_associated_token_address(&market_address, &MINT_ADDRESS);
|
||||
Instruction {
|
||||
program_id: crate::ID,
|
||||
accounts: vec![
|
||||
AccountMeta::new(signer, true),
|
||||
AccountMeta::new(block_adddress, false),
|
||||
AccountMeta::new(market_address, false),
|
||||
AccountMeta::new(base_mint_address, false),
|
||||
AccountMeta::new(MINT_ADDRESS, false),
|
||||
AccountMeta::new(tokens_base_address, false),
|
||||
AccountMeta::new(tokens_quote_address, false),
|
||||
AccountMeta::new(vault_base_address, false),
|
||||
AccountMeta::new(vault_quote_address, false),
|
||||
AccountMeta::new_readonly(system_program::ID, false),
|
||||
AccountMeta::new_readonly(spl_token::ID, false),
|
||||
AccountMeta::new_readonly(spl_associated_token_account::ID, false),
|
||||
],
|
||||
data: Swap {
|
||||
amount: amount.to_le_bytes(),
|
||||
direction: direction as u8,
|
||||
precision: precision as u8,
|
||||
}
|
||||
.to_bytes(),
|
||||
}
|
||||
}
|
||||
41
api/src/state/block.rs
Normal file
41
api/src/state/block.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use steel::*;
|
||||
|
||||
use crate::state::block_pda;
|
||||
|
||||
use super::OreAccount;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct Block {
|
||||
/// The block number.
|
||||
pub id: u64,
|
||||
|
||||
/// The minimum difficulty required for payout.
|
||||
pub min_difficulty: u64,
|
||||
|
||||
/// The reward rate per satisfying hash.
|
||||
pub reward_rate: u64,
|
||||
|
||||
/// The hash of the starting slot.
|
||||
pub slot_hash: [u8; 32],
|
||||
|
||||
/// The starting slot of the block.
|
||||
pub start_slot: u64,
|
||||
|
||||
/// The total number of hashes submitted to the block.
|
||||
pub total_hashes: u64,
|
||||
|
||||
/// The total amount of rewards paid out to miners.
|
||||
pub total_rewards: u64,
|
||||
|
||||
/// The total number of hashes submitted to the block.
|
||||
pub winning_hashes: u64,
|
||||
}
|
||||
|
||||
impl Block {
|
||||
pub fn pda(&self) -> (Pubkey, u8) {
|
||||
block_pda(self.id)
|
||||
}
|
||||
}
|
||||
|
||||
account!(OreAccount, Block);
|
||||
19
api/src/state/config.rs
Normal file
19
api/src/state/config.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use steel::*;
|
||||
|
||||
use crate::state::config_pda;
|
||||
|
||||
use super::OreAccount;
|
||||
|
||||
// TODO Config stuff
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct Config {}
|
||||
|
||||
impl Config {
|
||||
pub fn pda() -> (Pubkey, u8) {
|
||||
config_pda()
|
||||
}
|
||||
}
|
||||
|
||||
account!(OreAccount, Config);
|
||||
78
api/src/state/market/buy_exact_in.rs
Normal file
78
api/src/state/market/buy_exact_in.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use crate::error::OreError;
|
||||
|
||||
use super::{Market, SwapDirection, TokenType, VirtualLimitOrder};
|
||||
use crate::event::SwapEvent;
|
||||
|
||||
impl Market {
|
||||
pub fn buy_exact_in(&mut self, quote_in: u64) -> Result<SwapEvent, OreError> {
|
||||
// Get fee from quote side.
|
||||
let quote_fee = self.fee(quote_in);
|
||||
let quote_in_post_fee = quote_in - quote_fee;
|
||||
|
||||
// Upcast data.
|
||||
let quote_in_post_fee = quote_in_post_fee as u128;
|
||||
|
||||
// Get virtual limit order.
|
||||
let VirtualLimitOrder {
|
||||
size_in_base: ask_size_in_base,
|
||||
size_in_quote: ask_size_in_quote,
|
||||
} = self.get_virtual_limit_order(SwapDirection::Buy);
|
||||
|
||||
// Execute swap.
|
||||
let (base_via_ask, quote_via_ask, base_via_curve, quote_via_curve) =
|
||||
if !self.sandwich_resistance_enabled() {
|
||||
// Fill entire swap via curve.
|
||||
let quote_via_curve = quote_in_post_fee;
|
||||
let base_via_curve = self.get_base_out(quote_via_curve);
|
||||
self.update_reserves(base_via_curve, quote_via_curve, SwapDirection::Buy)?;
|
||||
(0, 0, base_via_curve, quote_via_curve)
|
||||
} else if ask_size_in_quote >= quote_in_post_fee {
|
||||
// Fill entire swap via virtual limit order.
|
||||
let quote_via_ask = quote_in_post_fee;
|
||||
let base_via_ask = self.get_complementary_limit_order_size(
|
||||
quote_in_post_fee,
|
||||
SwapDirection::Buy,
|
||||
TokenType::Quote,
|
||||
);
|
||||
self.update_reserves(base_via_ask, quote_via_ask, SwapDirection::Buy)?;
|
||||
(base_via_ask, quote_via_ask, 0, 0)
|
||||
} else {
|
||||
// Partially fill swap via virtual limit order.
|
||||
let base_via_ask = ask_size_in_base;
|
||||
let quote_via_ask = ask_size_in_quote;
|
||||
self.update_reserves(base_via_ask, quote_via_ask, SwapDirection::Buy)?;
|
||||
|
||||
// Fill remaining swap amount via curve.
|
||||
let quote_via_curve = quote_in_post_fee - ask_size_in_quote;
|
||||
let base_via_curve = self.get_base_out(quote_via_curve);
|
||||
self.update_reserves(base_via_curve, quote_via_curve, SwapDirection::Buy)?;
|
||||
(base_via_ask, quote_via_ask, base_via_curve, quote_via_curve)
|
||||
};
|
||||
|
||||
// Produce swap result.
|
||||
let base_out = base_via_ask + base_via_curve;
|
||||
let swap_event = SwapEvent {
|
||||
block_id: 0,
|
||||
direction: SwapDirection::Buy as u64,
|
||||
base_to_transfer: base_out as u64,
|
||||
quote_to_transfer: quote_in,
|
||||
base_via_order: base_via_ask as u64,
|
||||
quote_via_order: quote_via_ask as u64,
|
||||
base_via_curve: base_via_curve as u64,
|
||||
quote_via_curve: quote_via_curve as u64,
|
||||
quote_fee: quote_fee as u64,
|
||||
};
|
||||
|
||||
// Sanity check swap event.
|
||||
assert!(
|
||||
swap_event.base_to_transfer == swap_event.base_via_order + swap_event.base_via_curve
|
||||
);
|
||||
assert!(
|
||||
swap_event.quote_to_transfer
|
||||
== swap_event.quote_via_order + swap_event.quote_via_curve + swap_event.quote_fee
|
||||
);
|
||||
|
||||
// Return
|
||||
Ok(swap_event)
|
||||
}
|
||||
}
|
||||
83
api/src/state/market/buy_exact_out.rs
Normal file
83
api/src/state/market/buy_exact_out.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use crate::error::OreError;
|
||||
|
||||
use super::{Market, SwapDirection, TokenType, VirtualLimitOrder};
|
||||
use crate::event::SwapEvent;
|
||||
|
||||
impl Market {
|
||||
pub fn buy_exact_out(&mut self, base_out: u64) -> Result<SwapEvent, OreError> {
|
||||
// Check if there is enough liquidity.
|
||||
if self.base.balance < base_out {
|
||||
return Err(OreError::InsufficientLiquidity);
|
||||
}
|
||||
|
||||
// Upcast data.
|
||||
let base_out = base_out as u128;
|
||||
|
||||
// Get virtual limit order.
|
||||
let VirtualLimitOrder {
|
||||
size_in_base: ask_size_in_base,
|
||||
size_in_quote: ask_size_in_quote,
|
||||
} = self.get_virtual_limit_order(SwapDirection::Buy);
|
||||
|
||||
// Execute swap.
|
||||
let (base_via_ask, quote_via_ask, base_via_curve, quote_via_curve) =
|
||||
if !self.sandwich_resistance_enabled() {
|
||||
// Fill entire swap via curve.
|
||||
let base_via_curve = base_out;
|
||||
let quote_via_curve = self.get_quote_in(base_via_curve)?;
|
||||
self.update_reserves(base_via_curve, quote_via_curve, SwapDirection::Buy)?;
|
||||
(0, 0, base_via_curve, quote_via_curve)
|
||||
} else if ask_size_in_base >= base_out {
|
||||
// Fill entire swap through virtual limit order.
|
||||
let base_via_ask = base_out;
|
||||
let quote_via_ask = self.get_complementary_limit_order_size(
|
||||
base_via_ask,
|
||||
SwapDirection::Buy,
|
||||
TokenType::Base,
|
||||
);
|
||||
self.update_reserves(base_via_ask, quote_via_ask, SwapDirection::Buy)?;
|
||||
(base_via_ask, quote_via_ask, 0, 0)
|
||||
} else {
|
||||
// Partially fill swap through virtual limit order.
|
||||
let base_via_ask = ask_size_in_base;
|
||||
let quote_via_ask = ask_size_in_quote;
|
||||
self.update_reserves(base_via_ask, quote_via_ask, SwapDirection::Buy)?;
|
||||
|
||||
// Fill remaining swap amount through pool.
|
||||
let base_via_curve = base_out - base_via_ask;
|
||||
let quote_via_curve = self.get_quote_in(base_via_curve)?;
|
||||
self.update_reserves(base_via_curve, quote_via_curve, SwapDirection::Buy)?;
|
||||
(base_via_ask, quote_via_ask, base_via_curve, quote_via_curve)
|
||||
};
|
||||
|
||||
// Calculate fee.
|
||||
let quote_post_fee = quote_via_ask + quote_via_curve;
|
||||
let quote_in = self.pre_fee(quote_post_fee as u64) as u128;
|
||||
let quote_fee = quote_in - quote_post_fee;
|
||||
|
||||
// Produce swap result.
|
||||
let swap_event = SwapEvent {
|
||||
block_id: 0,
|
||||
direction: SwapDirection::Buy as u64,
|
||||
base_to_transfer: base_out as u64,
|
||||
quote_to_transfer: quote_in as u64,
|
||||
base_via_order: base_via_ask as u64,
|
||||
quote_via_order: quote_via_ask as u64,
|
||||
base_via_curve: base_via_curve as u64,
|
||||
quote_via_curve: quote_via_curve as u64,
|
||||
quote_fee: quote_fee as u64,
|
||||
};
|
||||
|
||||
// Sanity check swap result.
|
||||
assert!(
|
||||
swap_event.base_to_transfer == swap_event.base_via_order + swap_event.base_via_curve
|
||||
);
|
||||
assert!(
|
||||
swap_event.quote_to_transfer
|
||||
== swap_event.quote_via_order + swap_event.quote_via_curve + swap_event.quote_fee
|
||||
);
|
||||
|
||||
// Return.
|
||||
Ok(swap_event)
|
||||
}
|
||||
}
|
||||
46
api/src/state/market/curve.rs
Normal file
46
api/src/state/market/curve.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use crate::error::OreError;
|
||||
|
||||
use super::Market;
|
||||
|
||||
// TODO Add weights.
|
||||
|
||||
impl Market {
|
||||
/// Returns the constant product invariant.
|
||||
pub(crate) fn k(&self) -> u128 {
|
||||
(self.base.liquidity() * self.quote.liquidity()).saturating_sub(1)
|
||||
}
|
||||
|
||||
/// Returns the amount of base tokens that can be bought from a given amount of quote tokens.
|
||||
pub fn get_base_out(&self, quote_in: u128) -> u128 {
|
||||
let base_out = self.base.liquidity()
|
||||
- (self.k() / (self.quote.liquidity() + quote_in)).saturating_add(1);
|
||||
base_out
|
||||
}
|
||||
|
||||
/// Returns the amount of quote tokens received from selling a given amount of base tokens.
|
||||
pub fn get_quote_out(&self, base_in: u128) -> u128 {
|
||||
let quote_out = self.quote.liquidity()
|
||||
- (self.k() / (self.base.liquidity() + base_in)).saturating_add(1);
|
||||
quote_out
|
||||
}
|
||||
|
||||
/// Returns the amount of quote tokens needed to buy a given amount of base tokens.
|
||||
pub fn get_quote_in(&self, base_out: u128) -> Result<u128, OreError> {
|
||||
if base_out >= self.base.liquidity() {
|
||||
return Err(OreError::InsufficientVaultReserves.into());
|
||||
}
|
||||
let quote_in = (self.k() / (self.base.liquidity() - base_out)).saturating_add(1)
|
||||
- self.quote.liquidity();
|
||||
Ok(quote_in)
|
||||
}
|
||||
|
||||
/// Returns the amount of base tokens which must be sold to receive a given amount of quote tokens.
|
||||
pub fn get_base_in(&self, quote_out: u128) -> Result<u128, OreError> {
|
||||
if quote_out >= self.quote.liquidity() {
|
||||
return Err(OreError::InsufficientVaultReserves.into());
|
||||
}
|
||||
let base_in = (self.k() / (self.quote.liquidity() - quote_out)).saturating_add(1)
|
||||
- self.base.liquidity();
|
||||
Ok(base_in)
|
||||
}
|
||||
}
|
||||
24
api/src/state/market/fees.rs
Normal file
24
api/src/state/market/fees.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use crate::consts::*;
|
||||
|
||||
use super::Market;
|
||||
|
||||
impl Market {
|
||||
pub(crate) fn apply_fees(&mut self, quote_fee: u64) {
|
||||
// Process protocol fees.
|
||||
self.fee.cumulative += quote_fee;
|
||||
self.fee.uncollected += quote_fee;
|
||||
}
|
||||
|
||||
/// Calculates the fee from a quote amount.
|
||||
pub(crate) fn fee(&self, quote_size: u64) -> u64 {
|
||||
quote_size * self.fee.rate / DENOMINATOR_BPS
|
||||
}
|
||||
|
||||
/// Calculates the pre-fee quote from a post-fee quote amount.
|
||||
pub(crate) fn pre_fee(&self, quote_post_fee: u64) -> u64 {
|
||||
// x * 10000 / (10000 - fee) is approximately equivalent to x * (1 - fee / 10000)
|
||||
let numerator = quote_post_fee * DENOMINATOR_BPS;
|
||||
let denominator = DENOMINATOR_BPS - self.fee.rate;
|
||||
return numerator / denominator;
|
||||
}
|
||||
}
|
||||
455
api/src/state/market/market.rs
Normal file
455
api/src/state/market/market.rs
Normal file
@@ -0,0 +1,455 @@
|
||||
use spl_associated_token_account::get_associated_token_address;
|
||||
use steel::*;
|
||||
|
||||
use crate::state::{market_pda, OreAccount};
|
||||
|
||||
// TODO Bonding curve stuff
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct Market {
|
||||
/// Base token parameters.
|
||||
pub base: TokenParams,
|
||||
|
||||
/// Quote token parameters.
|
||||
pub quote: TokenParams,
|
||||
|
||||
/// Fee parameters.
|
||||
pub fee: FeeParams,
|
||||
|
||||
/// Snapshot of the market state at the time of the last swap.
|
||||
pub snapshot: Snapshot,
|
||||
|
||||
/// The id of the block this market is associated with.
|
||||
pub id: u64,
|
||||
}
|
||||
|
||||
impl Market {
|
||||
pub fn pda(&self) -> (Pubkey, u8) {
|
||||
market_pda(self.id)
|
||||
}
|
||||
|
||||
pub fn base_vault(&self) -> Pubkey {
|
||||
get_associated_token_address(&self.pda().0, &self.base.mint)
|
||||
}
|
||||
|
||||
pub fn quote_vault(&self) -> Pubkey {
|
||||
get_associated_token_address(&self.pda().0, &self.quote.mint)
|
||||
}
|
||||
|
||||
pub fn sandwich_resistance_enabled(&self) -> bool {
|
||||
self.snapshot.enabled == 1
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct TokenParams {
|
||||
/// Mint of the token.
|
||||
pub mint: Pubkey,
|
||||
|
||||
/// Amount of tokens held in liquidity.
|
||||
pub balance: u64,
|
||||
|
||||
/// Amount of virtual tokens held in liquidity.
|
||||
pub balance_virtual: u64,
|
||||
}
|
||||
|
||||
impl TokenParams {
|
||||
pub fn liquidity(&self) -> u128 {
|
||||
(self.balance + self.balance_virtual) as u128
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct FeeParams {
|
||||
/// Cumulative protocol fees.
|
||||
pub cumulative: u64,
|
||||
|
||||
/// Fee rate in basis points.
|
||||
pub rate: u64,
|
||||
|
||||
/// Current uncollected protocol fees.
|
||||
pub uncollected: u64,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct Snapshot {
|
||||
/// Whether sandwich resistance is enabled.
|
||||
pub enabled: u64,
|
||||
|
||||
/// Base token balance at the time of the snapshot.
|
||||
pub base_balance: u64,
|
||||
|
||||
/// Quote token balance at the time of the snapshot.
|
||||
pub quote_balance: u64,
|
||||
|
||||
/// Slot at which the snapshot was taken.
|
||||
pub slot: u64,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
|
||||
pub struct VirtualLimitOrder {
|
||||
/// Size of the virtual limit order in base tokens.
|
||||
pub size_in_base: u128,
|
||||
|
||||
/// Size of the virtual limit order in quote tokens.
|
||||
pub size_in_quote: u128,
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
|
||||
pub enum SwapDirection {
|
||||
/// Swap quote tokens for base tokens.
|
||||
Buy = 0,
|
||||
|
||||
/// Swap base tokens for quote tokens.
|
||||
Sell = 1,
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
|
||||
pub enum SwapPrecision {
|
||||
/// Swap with precision exact in amount.
|
||||
ExactIn = 0,
|
||||
|
||||
/// Swap with precision exact out amount.
|
||||
ExactOut = 1,
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
|
||||
pub enum TokenType {
|
||||
/// Base token.
|
||||
Base = 0,
|
||||
|
||||
/// Quote token.
|
||||
Quote = 1,
|
||||
}
|
||||
|
||||
account!(OreAccount, Market);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::consts::FEE_RATE_BPS;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_fees_buy_exact_in() {
|
||||
let mut market = new_market();
|
||||
let swap = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Buy,
|
||||
SwapPrecision::ExactIn,
|
||||
Clock::default(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(swap.quote_via_curve, 99_000);
|
||||
assert_eq!(market.fee.uncollected, 1000); // Protocol fee is 1%
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fees_sell_exact_in() {
|
||||
let mut market = new_market();
|
||||
let swap = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Sell,
|
||||
SwapPrecision::ExactIn,
|
||||
Clock::default(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(swap.quote_via_curve, 98_991);
|
||||
assert_eq!(market.fee.uncollected, 999);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fees_buy_exact_out() {
|
||||
let mut market = new_market();
|
||||
let swap = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Buy,
|
||||
SwapPrecision::ExactOut,
|
||||
Clock::default(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(swap.quote_via_curve, 100_011);
|
||||
assert_eq!(market.fee.uncollected, 1010);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fees_sell_exact_out() {
|
||||
let mut market = new_market();
|
||||
let swap = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Sell,
|
||||
SwapPrecision::ExactOut,
|
||||
Clock::default(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(swap.quote_via_curve, 101_010);
|
||||
assert_eq!(market.fee.uncollected, 1010);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fills() {
|
||||
let mut market = new_market();
|
||||
let mut clock = Clock::default();
|
||||
clock.slot = 10;
|
||||
|
||||
// Small buy
|
||||
// Assert swap is filled via curve.
|
||||
// Post swap, price is above snapshot.
|
||||
let swap_1 = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Buy,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(swap_1.base_via_curve > 0 && swap_1.quote_via_curve > 0);
|
||||
assert!(swap_1.base_via_order == 0 && swap_1.quote_via_order == 0);
|
||||
|
||||
// Large sell
|
||||
// Assert swap is partially filled via order and partially filled via curve.
|
||||
// Post swap, price is below snapshot.
|
||||
let swap_2 = market
|
||||
.swap(
|
||||
1_000_000,
|
||||
SwapDirection::Sell,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(swap_2.base_via_curve > 0 && swap_2.quote_via_curve > 0);
|
||||
assert!(swap_2.base_via_order > 0 && swap_2.quote_via_order > 0);
|
||||
|
||||
// Small buy
|
||||
// Assert swap is filled via order
|
||||
// Post swap, price is still below snapshot.
|
||||
let swap_3 = market
|
||||
.swap(
|
||||
1_000,
|
||||
SwapDirection::Buy,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(swap_3.base_via_curve == 0 && swap_3.quote_via_curve == 0);
|
||||
assert!(swap_3.base_via_order > 0 && swap_3.quote_via_order > 0);
|
||||
|
||||
// Large buy
|
||||
// Assert swap is partially filled via order and partially filled via curve.
|
||||
// Post swap, price is above snapshot.
|
||||
let swap_4 = market
|
||||
.swap(
|
||||
1_000_000,
|
||||
SwapDirection::Buy,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(swap_4.base_via_curve > 0 && swap_4.quote_via_curve > 0);
|
||||
assert!(swap_4.base_via_order > 0 && swap_4.quote_via_order > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sandwich() {
|
||||
let mut market = new_market();
|
||||
market.fee.rate = 0;
|
||||
market.snapshot = Snapshot {
|
||||
enabled: 0,
|
||||
base_balance: 0,
|
||||
quote_balance: 0,
|
||||
slot: 0,
|
||||
};
|
||||
|
||||
let mut clock = Clock::default();
|
||||
clock.slot = 10;
|
||||
|
||||
// Open sandwich
|
||||
let swap_1 = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Buy,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
let amount_base_1 = swap_1.base_to_transfer;
|
||||
assert!(swap_1.base_via_curve > 0 && swap_1.quote_via_curve > 0);
|
||||
assert!(swap_1.base_via_order == 0 && swap_1.quote_via_order == 0);
|
||||
|
||||
// Victim buys
|
||||
let swap_2 = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Buy,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(swap_2.base_via_curve > 0 && swap_2.quote_via_curve > 0);
|
||||
assert!(swap_2.base_via_order == 0 && swap_2.quote_via_order == 0);
|
||||
|
||||
// Close sandwich
|
||||
let swap_3 = market
|
||||
.swap(
|
||||
amount_base_1,
|
||||
SwapDirection::Sell,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(swap_3.base_via_curve > 0 && swap_3.quote_via_curve > 0);
|
||||
assert!(swap_3.base_via_order == 0 && swap_3.quote_via_order == 0);
|
||||
|
||||
// Assert sandwich attack succeeded
|
||||
assert!(swap_3.quote_to_transfer > swap_1.quote_to_transfer);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sandwich_resistance() {
|
||||
let mut market = new_market();
|
||||
market.fee.rate = 0;
|
||||
|
||||
let mut clock = Clock::default();
|
||||
clock.slot = 10;
|
||||
|
||||
// Open sandwich
|
||||
let swap_1 = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Buy,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
let amount_base_1 = swap_1.base_to_transfer;
|
||||
assert!(swap_1.base_via_curve > 0 && swap_1.quote_via_curve > 0);
|
||||
assert!(swap_1.base_via_order == 0 && swap_1.quote_via_order == 0);
|
||||
|
||||
// Victim buys
|
||||
let swap_2 = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Buy,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(swap_2.base_via_curve > 0 && swap_2.quote_via_curve > 0);
|
||||
assert!(swap_2.base_via_order == 0 && swap_2.quote_via_order == 0);
|
||||
|
||||
// Close sandwich
|
||||
let swap_3 = market
|
||||
.swap(
|
||||
amount_base_1,
|
||||
SwapDirection::Sell,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(swap_3.base_via_curve == 0 && swap_3.quote_via_curve == 0);
|
||||
assert!(swap_3.base_via_order > 0 && swap_3.quote_via_order > 0);
|
||||
|
||||
// Assert sandwich attack failed
|
||||
assert!(swap_3.quote_to_transfer <= swap_1.quote_to_transfer);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_virtual_liquidity() {
|
||||
let mut market = new_market();
|
||||
market.fee.rate = 0;
|
||||
market.quote.balance_virtual = 1_000_000_000;
|
||||
market.quote.balance = 0;
|
||||
|
||||
let mut clock = Clock::default();
|
||||
clock.slot = 10;
|
||||
|
||||
// Sell
|
||||
// Assert swap fails without real liquidity to satisfy order.
|
||||
let swap = market.swap(
|
||||
100_000,
|
||||
SwapDirection::Sell,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
);
|
||||
assert!(swap.is_err());
|
||||
|
||||
// Buy
|
||||
// Assert buy succeeds adding liquidity.
|
||||
let swap = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Buy,
|
||||
SwapPrecision::ExactIn,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(swap.base_via_curve > 0 && swap.quote_via_curve > 0);
|
||||
assert!(swap.base_via_order == 0 && swap.quote_via_order == 0);
|
||||
assert_eq!(market.quote.balance, 100_000);
|
||||
assert_eq!(market.quote.balance_virtual, 1_000_000_000);
|
||||
|
||||
// Sell
|
||||
// Assert sell fails if there is insufficient liquidity.
|
||||
let swap = market.swap(
|
||||
100_001,
|
||||
SwapDirection::Sell,
|
||||
SwapPrecision::ExactOut,
|
||||
clock.clone(),
|
||||
);
|
||||
assert!(swap.is_err());
|
||||
|
||||
// Sell
|
||||
// Assert sell succeeds removing liquidity.
|
||||
let swap = market
|
||||
.swap(
|
||||
100_000,
|
||||
SwapDirection::Sell,
|
||||
SwapPrecision::ExactOut,
|
||||
clock.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(swap.base_via_curve > 0 && swap.quote_via_curve > 0);
|
||||
assert!(swap.base_via_order > 0 && swap.quote_via_order > 0);
|
||||
assert_eq!(market.quote.balance, 0);
|
||||
assert_eq!(market.quote.balance_virtual, 1_000_000_000);
|
||||
}
|
||||
|
||||
fn new_market() -> Market {
|
||||
Market {
|
||||
base: TokenParams {
|
||||
mint: Pubkey::new_unique(),
|
||||
balance: 1_000_000_000,
|
||||
balance_virtual: 0,
|
||||
},
|
||||
quote: TokenParams {
|
||||
mint: Pubkey::new_unique(),
|
||||
balance: 1_000_000_000,
|
||||
balance_virtual: 0,
|
||||
},
|
||||
fee: FeeParams {
|
||||
cumulative: 0,
|
||||
uncollected: 0,
|
||||
rate: FEE_RATE_BPS,
|
||||
},
|
||||
snapshot: Snapshot {
|
||||
enabled: 1,
|
||||
base_balance: 0,
|
||||
quote_balance: 0,
|
||||
slot: 0,
|
||||
},
|
||||
id: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
12
api/src/state/market/mod.rs
Normal file
12
api/src/state/market/mod.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
mod buy_exact_in;
|
||||
mod buy_exact_out;
|
||||
mod curve;
|
||||
mod fees;
|
||||
mod market;
|
||||
mod sell_exact_in;
|
||||
mod sell_exact_out;
|
||||
mod swap;
|
||||
mod vaults;
|
||||
mod virtual_limit_order;
|
||||
|
||||
pub use market::*;
|
||||
86
api/src/state/market/sell_exact_in.rs
Normal file
86
api/src/state/market/sell_exact_in.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
use crate::error::OreError;
|
||||
|
||||
use super::{Market, SwapDirection, TokenType, VirtualLimitOrder};
|
||||
use crate::event::SwapEvent;
|
||||
|
||||
impl Market {
|
||||
pub fn sell_exact_in(&mut self, base_in: u64) -> Result<SwapEvent, OreError> {
|
||||
// Get fee from quote side.
|
||||
let mut quote_fee = 0;
|
||||
|
||||
// Upcast data.
|
||||
let base_in = base_in as u128;
|
||||
|
||||
// Get virtual limit order.
|
||||
let VirtualLimitOrder {
|
||||
size_in_base: bid_size_in_base,
|
||||
size_in_quote: bid_size_in_quote,
|
||||
} = self.get_virtual_limit_order(SwapDirection::Sell);
|
||||
|
||||
// Execute swap.
|
||||
let (base_via_bid, quote_via_bid, base_via_curve, quote_via_curve) =
|
||||
if !self.sandwich_resistance_enabled() {
|
||||
// Fill entire swap via curve.
|
||||
let base_via_curve = base_in;
|
||||
let mut quote_via_curve = self.get_quote_out(base_via_curve);
|
||||
self.update_reserves(base_via_curve, quote_via_curve, SwapDirection::Sell)?;
|
||||
let swap_fee = self.fee(quote_via_curve as u64);
|
||||
quote_fee += swap_fee;
|
||||
quote_via_curve -= swap_fee as u128;
|
||||
(0, 0, base_via_curve, quote_via_curve)
|
||||
} else if bid_size_in_base >= base_in {
|
||||
// Fill entire swap through virtual limit order.
|
||||
let base_via_bid = base_in;
|
||||
let mut quote_via_bid = self.get_complementary_limit_order_size(
|
||||
base_in,
|
||||
SwapDirection::Sell,
|
||||
TokenType::Base,
|
||||
);
|
||||
quote_fee += self.fee(quote_via_bid as u64);
|
||||
self.update_reserves(base_via_bid, quote_via_bid, SwapDirection::Sell)?;
|
||||
quote_via_bid -= quote_fee as u128;
|
||||
(base_via_bid, quote_via_bid, 0, 0)
|
||||
} else {
|
||||
// Partially fill swap through virtual limit order.
|
||||
let base_via_bid = bid_size_in_base;
|
||||
let mut quote_via_bid = bid_size_in_quote;
|
||||
quote_fee += self.fee(quote_via_bid as u64);
|
||||
self.update_reserves(base_via_bid, quote_via_bid, SwapDirection::Sell)?;
|
||||
quote_via_bid -= quote_fee as u128;
|
||||
|
||||
// Fill remaining swap through pool.
|
||||
let base_via_curve = base_in - base_via_bid;
|
||||
let mut quote_via_curve = self.get_quote_out(base_via_curve);
|
||||
self.update_reserves(base_via_curve, quote_via_curve, SwapDirection::Sell)?;
|
||||
let swap_fee = self.fee(quote_via_curve as u64);
|
||||
quote_fee += swap_fee;
|
||||
quote_via_curve -= swap_fee as u128;
|
||||
(base_via_bid, quote_via_bid, base_via_curve, quote_via_curve)
|
||||
};
|
||||
|
||||
// Produce swap result.
|
||||
let quote_out = quote_via_bid + quote_via_curve;
|
||||
let swap_event = SwapEvent {
|
||||
block_id: 0,
|
||||
direction: SwapDirection::Sell as u64,
|
||||
base_to_transfer: base_in as u64,
|
||||
quote_to_transfer: quote_out as u64,
|
||||
base_via_order: base_via_bid as u64,
|
||||
quote_via_order: quote_via_bid as u64,
|
||||
base_via_curve: base_via_curve as u64,
|
||||
quote_via_curve: quote_via_curve as u64,
|
||||
quote_fee: quote_fee as u64,
|
||||
};
|
||||
|
||||
// Sanity check swap result.
|
||||
assert!(
|
||||
swap_event.base_to_transfer == swap_event.base_via_order + swap_event.base_via_curve
|
||||
);
|
||||
assert!(
|
||||
swap_event.quote_to_transfer == swap_event.quote_via_order + swap_event.quote_via_curve
|
||||
);
|
||||
|
||||
// Return.
|
||||
Ok(swap_event)
|
||||
}
|
||||
}
|
||||
85
api/src/state/market/sell_exact_out.rs
Normal file
85
api/src/state/market/sell_exact_out.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use crate::error::OreError;
|
||||
|
||||
use super::{Market, SwapDirection, TokenType, VirtualLimitOrder};
|
||||
use crate::event::SwapEvent;
|
||||
|
||||
impl Market {
|
||||
pub fn sell_exact_out(&mut self, quote_out: u64) -> Result<SwapEvent, OreError> {
|
||||
// Check if there is enough liquidity.
|
||||
if self.quote.balance < quote_out {
|
||||
return Err(OreError::InsufficientLiquidity);
|
||||
}
|
||||
|
||||
// Calculate fee.
|
||||
let quote_out_pre_fee = self.pre_fee(quote_out) as u128;
|
||||
let quote_fee = quote_out_pre_fee - quote_out as u128;
|
||||
|
||||
// Upcast data.
|
||||
let quote_out = quote_out as u128;
|
||||
|
||||
// Get virtual limit order.
|
||||
let VirtualLimitOrder {
|
||||
size_in_base: bid_size_in_base,
|
||||
size_in_quote: bid_size_in_quote,
|
||||
} = self.get_virtual_limit_order(SwapDirection::Sell);
|
||||
|
||||
// Execute swap.
|
||||
let (base_via_bid, quote_via_bid, base_via_curve, quote_via_curve) =
|
||||
if !self.sandwich_resistance_enabled() {
|
||||
// Fill entire swap via curve.
|
||||
let quote_via_curve = quote_out_pre_fee;
|
||||
let base_via_curve = self.get_base_in(quote_via_curve)?;
|
||||
self.update_reserves(base_via_curve, quote_via_curve, SwapDirection::Sell)?;
|
||||
(0, 0, base_via_curve, quote_via_curve)
|
||||
} else if bid_size_in_quote >= quote_out {
|
||||
// Fill entire swap through virtual limit order.
|
||||
let quote_via_bid = quote_out_pre_fee;
|
||||
let base_via_bid = self.get_complementary_limit_order_size(
|
||||
quote_via_bid,
|
||||
SwapDirection::Sell,
|
||||
TokenType::Quote,
|
||||
);
|
||||
self.update_reserves(base_via_bid, quote_via_bid, SwapDirection::Sell)?;
|
||||
(base_via_bid, quote_via_bid, 0, 0)
|
||||
} else {
|
||||
// Partially fill swap through virtual limit order.
|
||||
let base_via_bid = bid_size_in_base;
|
||||
let quote_via_bid = bid_size_in_quote;
|
||||
self.update_reserves(base_via_bid, quote_via_bid, SwapDirection::Sell)?;
|
||||
|
||||
// Fill remaining swap amount through pool.
|
||||
let quote_via_curve = quote_out_pre_fee - quote_via_bid;
|
||||
let base_via_curve = self.get_base_in(quote_via_curve)?;
|
||||
self.update_reserves(base_via_curve, quote_via_curve, SwapDirection::Sell)?;
|
||||
(base_via_bid, quote_via_bid, base_via_curve, quote_via_curve)
|
||||
};
|
||||
|
||||
// Calculate fee.
|
||||
let base_in = base_via_bid + base_via_curve;
|
||||
|
||||
// Produce swap result.
|
||||
let swap_event = SwapEvent {
|
||||
block_id: 0,
|
||||
direction: SwapDirection::Sell as u64,
|
||||
base_to_transfer: base_in as u64,
|
||||
quote_to_transfer: quote_out as u64,
|
||||
base_via_order: base_via_bid as u64,
|
||||
quote_via_order: quote_via_bid as u64,
|
||||
base_via_curve: base_via_curve as u64,
|
||||
quote_via_curve: quote_via_curve as u64,
|
||||
quote_fee: quote_fee as u64,
|
||||
};
|
||||
|
||||
// Sanity check swap result.
|
||||
assert!(
|
||||
swap_event.base_to_transfer == swap_event.base_via_order + swap_event.base_via_curve
|
||||
);
|
||||
assert!(
|
||||
swap_event.quote_to_transfer
|
||||
== swap_event.quote_via_order + swap_event.quote_via_curve - swap_event.quote_fee
|
||||
);
|
||||
|
||||
// Return.
|
||||
Ok(swap_event)
|
||||
}
|
||||
}
|
||||
42
api/src/state/market/swap.rs
Normal file
42
api/src/state/market/swap.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use steel::Clock;
|
||||
|
||||
use crate::error::OreError;
|
||||
|
||||
use super::{Market, SwapDirection, SwapPrecision};
|
||||
use crate::event::SwapEvent;
|
||||
|
||||
impl Market {
|
||||
pub fn swap(
|
||||
&mut self,
|
||||
amount: u64,
|
||||
direction: SwapDirection,
|
||||
precision: SwapPrecision,
|
||||
clock: Clock,
|
||||
) -> Result<SwapEvent, OreError> {
|
||||
// Update snapshot.
|
||||
self.update_snapshot(clock);
|
||||
|
||||
// Get invariant.
|
||||
let k_pre = self.k();
|
||||
|
||||
// Execute swap.
|
||||
let swap_event = match (direction, precision) {
|
||||
(SwapDirection::Buy, SwapPrecision::ExactIn) => self.buy_exact_in(amount)?,
|
||||
(SwapDirection::Buy, SwapPrecision::ExactOut) => self.buy_exact_out(amount)?,
|
||||
(SwapDirection::Sell, SwapPrecision::ExactIn) => self.sell_exact_in(amount)?,
|
||||
(SwapDirection::Sell, SwapPrecision::ExactOut) => self.sell_exact_out(amount)?,
|
||||
};
|
||||
|
||||
// Check invariant.
|
||||
let k_post = self.k();
|
||||
if k_pre > k_post {
|
||||
return Err(OreError::InvariantViolation.into());
|
||||
}
|
||||
|
||||
// Apply fees.
|
||||
self.apply_fees(swap_event.quote_fee);
|
||||
|
||||
// Return.
|
||||
Ok(swap_event)
|
||||
}
|
||||
}
|
||||
43
api/src/state/market/vaults.rs
Normal file
43
api/src/state/market/vaults.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use solana_program::log::sol_log;
|
||||
use steel::*;
|
||||
|
||||
use crate::error::OreError;
|
||||
|
||||
use super::Market;
|
||||
|
||||
/// Vault reserve checks.
|
||||
impl Market {
|
||||
/// Sanity check that vaults have reserves for all market debts.
|
||||
/// Assumes the token accounts have already been validated as the market's base and quote vaults.
|
||||
pub fn check_vaults(
|
||||
&self,
|
||||
base_vault: &TokenAccount,
|
||||
quote_vault: &TokenAccount,
|
||||
) -> Result<(), OreError> {
|
||||
self.check_base_vault(base_vault)?;
|
||||
self.check_quote_vault(quote_vault)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sanity check that base vault has reserves for all market debts.
|
||||
/// Assumes the token account has already been validated as the market's base vault.
|
||||
pub fn check_base_vault(&self, base_vault: &TokenAccount) -> Result<(), OreError> {
|
||||
if base_vault.amount() < self.base.balance {
|
||||
sol_log(&format!("A base_vault.amount: {}", base_vault.amount()));
|
||||
sol_log(&format!("A self.base.balance: {}", self.base.balance));
|
||||
sol_log("Insufficient base vault reserves");
|
||||
return Err(OreError::InsufficientVaultReserves.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sanity check that quote vault has reserves for all market debts.
|
||||
/// Assumes the token account has already been validated as the market's quote vault.
|
||||
pub fn check_quote_vault(&self, quote_vault: &TokenAccount) -> Result<(), OreError> {
|
||||
if quote_vault.amount() < self.quote.balance + self.fee.uncollected {
|
||||
sol_log("Insufficient quote vault reserves");
|
||||
return Err(OreError::InsufficientVaultReserves.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
158
api/src/state/market/virtual_limit_order.rs
Normal file
158
api/src/state/market/virtual_limit_order.rs
Normal file
@@ -0,0 +1,158 @@
|
||||
use steel::Clock;
|
||||
|
||||
use crate::{consts::SLOT_WINDOW, error::OreError};
|
||||
|
||||
use super::{Market, SwapDirection, TokenType, VirtualLimitOrder};
|
||||
|
||||
impl Market {
|
||||
/// This function solves the closed-form solution for the size of the virtual limit order
|
||||
/// in the pool. The virutal limit order is always priced at the snapshot price.
|
||||
///
|
||||
/// The size of the limit order is determined by the following constraint:
|
||||
///
|
||||
/// ```no_run
|
||||
/// (quote_snapshot / base_snapshot) = (quote_reserves + ∆_quote) / (base_reserves + ∆_base)
|
||||
/// ```
|
||||
///
|
||||
/// Note that the signs of ∆_quote and ∆_base are always flipped.
|
||||
///
|
||||
/// This means that the size of the limit order is set such that the new pool price
|
||||
/// after the swap is equal to the price at the snapshot.
|
||||
///
|
||||
/// Because we know the limit order is priced at the snapshot price, we can derive
|
||||
/// the following equations:
|
||||
/// - ∆_base = -∆_quote * base_snapshot / quote_snapshot
|
||||
/// - ∆_quote = -∆_base * quote_snapshot / base_snapshot
|
||||
///
|
||||
///
|
||||
/// We can then solve for ∆_base and ∆_quote after substituting the above equations. There are separate cases
|
||||
/// for buy and sell
|
||||
///
|
||||
/// - Limit order on the buy side (bid)
|
||||
/// ```no_run
|
||||
/// ∆_base = (base_snapshot * quote_reserves - quote_snapshot * base_reserves) / (2 * quote_snapshot)
|
||||
/// ∆_quote = (base_snapshot * quote_reserves - quote_snapshot * base_reserves) / (2 * base_snapshot)
|
||||
/// ```
|
||||
///
|
||||
/// - Limit order on the sell side (ask)
|
||||
/// ```no_run
|
||||
/// ∆_base = (quote_snapshot * base_reserves - base_snapshot * quote_reserves) / (2 * quote_snapshot)
|
||||
/// ∆_quote = (quote_snapshot * base_reserves - base_snapshot * quote_reserves) / (2 * base_snapshot)
|
||||
/// ```
|
||||
pub fn get_virtual_limit_order(&self, direction: SwapDirection) -> VirtualLimitOrder {
|
||||
// Upcast data.
|
||||
let base_balance = self.base.liquidity();
|
||||
let quote_balance = self.quote.liquidity();
|
||||
let base_snapshot = self.snapshot.base_balance as u128;
|
||||
let quote_snapshot = self.snapshot.quote_balance as u128;
|
||||
|
||||
// Get virtual limit order.
|
||||
match direction {
|
||||
SwapDirection::Buy => {
|
||||
let ask = if quote_snapshot * base_balance > base_snapshot * quote_balance {
|
||||
let size_in_quote = (quote_snapshot * base_balance
|
||||
- base_snapshot * quote_balance)
|
||||
/ (2 * base_snapshot);
|
||||
let size_in_base = size_in_quote * base_snapshot / quote_snapshot;
|
||||
VirtualLimitOrder {
|
||||
size_in_base,
|
||||
size_in_quote,
|
||||
}
|
||||
} else {
|
||||
VirtualLimitOrder::default()
|
||||
};
|
||||
ask
|
||||
}
|
||||
SwapDirection::Sell => {
|
||||
let bid = if base_snapshot * quote_balance > quote_snapshot * base_balance {
|
||||
let size_in_base = (base_snapshot * quote_balance
|
||||
- quote_snapshot * base_balance)
|
||||
/ (2 * quote_snapshot);
|
||||
let size_in_quote = size_in_base * quote_snapshot / base_snapshot;
|
||||
VirtualLimitOrder {
|
||||
size_in_base,
|
||||
size_in_quote,
|
||||
}
|
||||
} else {
|
||||
VirtualLimitOrder::default()
|
||||
};
|
||||
bid
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// This function returns the size of the virtual limit order in the complementary token type
|
||||
/// given an `amount` and the `input_token_type`.
|
||||
/// - If the `input_token_type` is Base, then the size of the limit order in Quote is computed.
|
||||
/// - If the `input_token_type` is Quote, then the size of the limit order in Base is computed.
|
||||
pub(crate) fn get_complementary_limit_order_size(
|
||||
&self,
|
||||
amount: u128,
|
||||
direction: SwapDirection,
|
||||
token_type: TokenType,
|
||||
) -> u128 {
|
||||
if amount == 0 {
|
||||
return 0;
|
||||
}
|
||||
let quote_snapshot = self.snapshot.quote_balance as u128;
|
||||
let base_snapshot = self.snapshot.base_balance as u128;
|
||||
|
||||
match direction {
|
||||
SwapDirection::Buy => {
|
||||
match token_type {
|
||||
// If `amount` is in base, then the size of the limit order in quote is computed and rounded up
|
||||
TokenType::Base => ((amount * quote_snapshot).saturating_sub(1)
|
||||
/ base_snapshot)
|
||||
.saturating_add(1),
|
||||
// If `amount` is in quote, then the size of the limit order in base is computed
|
||||
TokenType::Quote => amount * base_snapshot / quote_snapshot,
|
||||
}
|
||||
}
|
||||
SwapDirection::Sell => {
|
||||
match token_type {
|
||||
// If `amount` is in base, then the size of the limit order in quote is computed
|
||||
TokenType::Base => amount * quote_snapshot / base_snapshot,
|
||||
// If `amount` is in quote, then the size of the limit order in base is computed and rounded up
|
||||
TokenType::Quote => ((amount * base_snapshot).saturating_sub(1)
|
||||
/ quote_snapshot)
|
||||
.saturating_add(1),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn update_snapshot(&mut self, clock: Clock) {
|
||||
let slot = clock.slot;
|
||||
let snapshot_slot = (slot / SLOT_WINDOW) * SLOT_WINDOW;
|
||||
if snapshot_slot != self.snapshot.slot {
|
||||
self.snapshot.slot = snapshot_slot;
|
||||
self.snapshot.base_balance = self.base.liquidity() as u64;
|
||||
self.snapshot.quote_balance = self.quote.liquidity() as u64;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn update_reserves(
|
||||
&mut self,
|
||||
base: u128,
|
||||
quote: u128,
|
||||
direction: SwapDirection,
|
||||
) -> Result<(), OreError> {
|
||||
match direction {
|
||||
SwapDirection::Buy => {
|
||||
if base > self.base.balance as u128 {
|
||||
return Err(OreError::InsufficientVaultReserves.into());
|
||||
}
|
||||
self.base.balance -= base as u64;
|
||||
self.quote.balance += quote as u64;
|
||||
}
|
||||
SwapDirection::Sell => {
|
||||
if quote > self.quote.balance as u128 {
|
||||
return Err(OreError::InsufficientVaultReserves.into());
|
||||
}
|
||||
self.base.balance += base as u64;
|
||||
self.quote.balance -= quote as u64;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
35
api/src/state/miner.rs
Normal file
35
api/src/state/miner.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use steel::*;
|
||||
|
||||
use crate::state::miner_pda;
|
||||
|
||||
use super::OreAccount;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct Miner {
|
||||
/// The authority of this miner account.
|
||||
pub authority: Pubkey,
|
||||
|
||||
/// The ID of the last block this miner mined in.
|
||||
pub block_id: u64,
|
||||
|
||||
/// The hash of the last block this miner mined in.
|
||||
pub hash: [u8; 32],
|
||||
|
||||
/// The total number of hashes this miner has submitted.
|
||||
pub total_hashes: u64,
|
||||
|
||||
/// The amount of ORE this miner has mined.
|
||||
pub total_rewards: u64,
|
||||
|
||||
/// The number of winning hashes this miner has submitted.
|
||||
pub winning_hashes: u64,
|
||||
}
|
||||
|
||||
impl Miner {
|
||||
pub fn pda(&self) -> (Pubkey, u8) {
|
||||
miner_pda(self.authority)
|
||||
}
|
||||
}
|
||||
|
||||
account!(OreAccount, Miner);
|
||||
69
api/src/state/mod.rs
Normal file
69
api/src/state/mod.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
mod block;
|
||||
mod config;
|
||||
mod market;
|
||||
mod miner;
|
||||
mod permit;
|
||||
mod stake;
|
||||
mod treasury;
|
||||
|
||||
pub use block::*;
|
||||
pub use config::*;
|
||||
pub use market::*;
|
||||
pub use miner::*;
|
||||
pub use permit::*;
|
||||
pub use stake::*;
|
||||
pub use treasury::*;
|
||||
|
||||
use crate::consts::*;
|
||||
|
||||
use steel::*;
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
|
||||
pub enum OreAccount {
|
||||
Block = 100,
|
||||
Config = 101,
|
||||
Market = 102,
|
||||
Miner = 103,
|
||||
Permit = 104,
|
||||
Stake = 105,
|
||||
Treasury = 106,
|
||||
}
|
||||
|
||||
pub fn block_pda(id: u64) -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(&[BLOCK, &id.to_le_bytes()], &crate::ID)
|
||||
}
|
||||
|
||||
pub fn config_pda() -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(&[CONFIG], &crate::ID)
|
||||
}
|
||||
|
||||
pub fn market_pda(id: u64) -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(&[MARKET, &id.to_le_bytes()], &crate::ID)
|
||||
}
|
||||
|
||||
pub fn miner_pda(authority: Pubkey) -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(&[MINER, &authority.to_bytes()], &crate::ID)
|
||||
}
|
||||
|
||||
pub fn mint_pda(id: u64) -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(&[MINT, &id.to_le_bytes()], &crate::ID)
|
||||
}
|
||||
|
||||
pub fn permit_pda(authority: Pubkey, block_id: u64) -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(
|
||||
&[PERMIT, &authority.to_bytes(), &block_id.to_le_bytes()],
|
||||
&crate::ID,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn stake_pda(authority: Pubkey, block_id: u64) -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(
|
||||
&[STAKE, &authority.to_bytes(), &block_id.to_le_bytes()],
|
||||
&crate::ID,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn treasury_pda() -> (Pubkey, u8) {
|
||||
Pubkey::find_program_address(&[TREASURY], &crate::ID)
|
||||
}
|
||||
32
api/src/state/permit.rs
Normal file
32
api/src/state/permit.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use steel::*;
|
||||
|
||||
use crate::state::permit_pda;
|
||||
|
||||
use super::OreAccount;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct Permit {
|
||||
/// The amount of hash tokens this miner has committed to the block.
|
||||
pub amount: u64,
|
||||
|
||||
/// The authority of the miner account.
|
||||
pub authority: Pubkey,
|
||||
|
||||
/// The ID of the block this permit is for.
|
||||
pub block_id: u64,
|
||||
|
||||
/// The executor of the permit.
|
||||
pub executor: Pubkey,
|
||||
|
||||
/// The fee paid to the executor.
|
||||
pub fee: u64,
|
||||
}
|
||||
|
||||
impl Permit {
|
||||
pub fn pda(&self) -> (Pubkey, u8) {
|
||||
permit_pda(self.authority, self.block_id)
|
||||
}
|
||||
}
|
||||
|
||||
account!(OreAccount, Permit);
|
||||
29
api/src/state/stake.rs
Normal file
29
api/src/state/stake.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use steel::*;
|
||||
|
||||
use crate::state::stake_pda;
|
||||
|
||||
use super::OreAccount;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct Stake {
|
||||
/// The authority of the miner account.
|
||||
pub authority: Pubkey,
|
||||
|
||||
/// The ID of the block this collateral is associated with.
|
||||
pub block_id: u64,
|
||||
|
||||
/// The amount of ORE this miner can commit to the block.
|
||||
pub capacity: u64,
|
||||
|
||||
/// The amount of ORE this miner has committed to the block.
|
||||
pub utilization: u64,
|
||||
}
|
||||
|
||||
impl Stake {
|
||||
pub fn pda(&self) -> (Pubkey, u8) {
|
||||
stake_pda(self.authority, self.block_id)
|
||||
}
|
||||
}
|
||||
|
||||
account!(OreAccount, Stake);
|
||||
19
api/src/state/treasury.rs
Normal file
19
api/src/state/treasury.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use steel::*;
|
||||
|
||||
use crate::state::treasury_pda;
|
||||
|
||||
use super::OreAccount;
|
||||
|
||||
/// Treasury is a singleton account which is the mint authority for the ORE token and the authority of
|
||||
/// the program's global token account.
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct Treasury {}
|
||||
|
||||
impl Treasury {
|
||||
pub fn pda() -> (Pubkey, u8) {
|
||||
treasury_pda()
|
||||
}
|
||||
}
|
||||
|
||||
account!(OreAccount, Treasury);
|
||||
Reference in New Issue
Block a user