mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 07:26:51 +00:00
cleanup
This commit is contained in:
@@ -7,40 +7,40 @@ use super::Market;
|
||||
impl Market {
|
||||
/// Returns the constant product invariant.
|
||||
pub(crate) fn k(&self) -> u128 {
|
||||
(self.base.reserves() * self.quote.reserves()).saturating_sub(1)
|
||||
(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.reserves()
|
||||
- (self.k() / (self.quote.reserves() + quote_in)).saturating_add(1);
|
||||
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.reserves() - (self.k() / (self.base.reserves() + base_in)).saturating_add(1);
|
||||
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.reserves() {
|
||||
if base_out >= self.base.liquidity() {
|
||||
return Err(OreError::InsufficientVaultReserves.into());
|
||||
}
|
||||
let quote_in = (self.k() / (self.base.reserves() - base_out)).saturating_add(1)
|
||||
- self.quote.reserves();
|
||||
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.reserves() {
|
||||
if quote_out >= self.quote.liquidity() {
|
||||
return Err(OreError::InsufficientVaultReserves.into());
|
||||
}
|
||||
let base_in = (self.k() / (self.quote.reserves() - quote_out)).saturating_add(1)
|
||||
- self.base.reserves();
|
||||
let base_in = (self.k() / (self.quote.liquidity() - quote_out)).saturating_add(1)
|
||||
- self.base.liquidity();
|
||||
Ok(base_in)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ pub struct TokenParams {
|
||||
}
|
||||
|
||||
impl TokenParams {
|
||||
pub fn reserves(&self) -> u128 {
|
||||
pub fn liquidity(&self) -> u128 {
|
||||
(self.balance + self.balance_virtual) as u128
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,8 @@ impl Market {
|
||||
/// ```
|
||||
pub fn get_virtual_limit_order(&self, direction: SwapDirection) -> VirtualLimitOrder {
|
||||
// Upcast data.
|
||||
let base_balance = self.base.reserves();
|
||||
let quote_balance = self.quote.reserves();
|
||||
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;
|
||||
|
||||
@@ -126,8 +126,8 @@ impl Market {
|
||||
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.reserves() as u64;
|
||||
self.snapshot.quote_balance = self.quote.reserves() as u64;
|
||||
self.snapshot.base_balance = self.base.liquidity() as u64;
|
||||
self.snapshot.quote_balance = self.quote.liquidity() as u64;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ use steel::*;
|
||||
pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let [signer_info, block_info, market_info, market_hash_info, market_ore_info, mint_hash_info, mint_ore_info, recipient_info, treasury_info, system_program, token_program] =
|
||||
let [signer_info, block_info, market_info, market_ore_info, mint_base_info, mint_quote_info, recipient_info, treasury_info, vault_base_info, vault_quote_info, system_program, token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
@@ -17,12 +17,12 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
let market = market_info
|
||||
.as_account_mut::<Market>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.id == block.id)?;
|
||||
let market_hash =
|
||||
market_hash_info.as_associated_token_account(market_info.key, mint_hash_info.key)?;
|
||||
let market_ore =
|
||||
market_ore_info.as_associated_token_account(market_info.key, mint_ore_info.key)?;
|
||||
mint_hash_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
mint_ore_info.has_address(&market.quote.mint)?.as_mint()?;
|
||||
mint_base_info.has_address(&market.base.mint)?.as_mint()?;
|
||||
mint_quote_info.has_address(&market.quote.mint)?.as_mint()?;
|
||||
let vault_base =
|
||||
vault_base_info.as_associated_token_account(market_info.key, mint_base_info.key)?;
|
||||
let vault_quote =
|
||||
vault_quote_info.as_associated_token_account(market_info.key, mint_quote_info.key)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
@@ -30,7 +30,7 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
if block.best_miner != Pubkey::default() {
|
||||
recipient_info.as_associated_token_account(&block.best_miner, &MINT_ADDRESS)?;
|
||||
mint_to_signed(
|
||||
mint_ore_info,
|
||||
mint_quote_info,
|
||||
recipient_info,
|
||||
treasury_info,
|
||||
token_program,
|
||||
@@ -39,23 +39,23 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
)?;
|
||||
}
|
||||
|
||||
// Burn hash token liquidity.
|
||||
// Burn base liquidity.
|
||||
burn_signed(
|
||||
market_hash_info,
|
||||
mint_hash_info,
|
||||
vault_base_info,
|
||||
mint_base_info,
|
||||
market_info,
|
||||
token_program,
|
||||
market_hash.amount(),
|
||||
&[MARKET, &market.id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
// Burn ORE liquidity.
|
||||
// Burn quote liquidity.
|
||||
burn_signed(
|
||||
market_ore_info,
|
||||
mint_ore_info,
|
||||
vault_quote_info,
|
||||
mint_quote_info,
|
||||
market_info,
|
||||
token_program,
|
||||
market_ore.amount(),
|
||||
vault_quote.amount(),
|
||||
&[MARKET, &market.id.to_le_bytes()],
|
||||
)?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user