Rust Snippet
const PROGRAM_ID: &str = "BSwp6bEBihVLdqJRKGgzjcGLHkcTuzmSo1TQkHepzH8p";
Derive Pool Address
fn derive_pool_address(
program_id: &Pubkey,
token_x: &Pubkey,
token_y: &Pubkey,
) -> Result<Pubkey> {
let (pool_address, _) = Pubkey::find_program_address(
&[
b"bonkswappoolv1",
token_x.as_ref(),
token_y.as_ref(),
],
program_id,
);
Ok(pool_address)
}
Pool structure
pub struct Pool {
pub token_x: Pubkey,
pub token_y: Pubkey,
pub pool_x_account: Pubkey,
pub pool_y_account: Pubkey,
pub admin: Pubkey,
pub project_owner: Pubkey,
pub token_x_reserve: Token,
pub token_y_reserve: Token,
pub self_shares: Token,
pub all_shares: Token,
pub buyback_amount_x: Token,
pub buyback_amount_y: Token,
pub project_amount_x: Token,
pub project_amount_y: Token,
pub mercanti_amount_x: Token,
pub mercanti_amount_y: Token,
pub lp_accumulator_x: FixedPoint,
pub lp_accumulator_y: FixedPoint,
pub const_k: Product,
pub price: FixedPoint,
pub lp_fee: FixedPoint,
pub buyback_fee: FixedPoint,
pub project_fee: FixedPoint,
pub mercanti_fee: FixedPoint,
pub farm_count: u64,
pub bump: u8,
}
- 200 bytes is the offset for token_x_reserve
- 208 bytes is the offset for token_y_reserve
- 328 bytes is the offset for price (calculated on-chain after every op)
Quote Calculation
const DEFAULT_DENOMINATOR: u128 = 10u128.pow(12); // 1_000_000_000_000
pub fn calculate_price(
token_x_reserve: u64,
token_y_reserve: u64,
) -> Result<u128, &'static str> {
if token_x_reserve == 0 {
return Err("division by zero");
}
let num = u128::from(token_y_reserve)
.checked_mul(DEFAULT_DENOMINATOR)
.ok_or("overflow in numerator")?;
Ok(num / u128::from(token_x_reserve))
}