Created
June 7, 2024 17:59
-
-
Save mgild/3b9c5cef4517886b7cd43a1e8e621b7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use solana_client::nonblocking::rpc_client::RpcClient; | |
use std::cmp::Ordering; | |
use std::str::FromStr; | |
use tokio; | |
use base64::{engine::general_purpose::STANDARD as base64, Engine as _}; | |
use libsecp256k1; | |
use hex; | |
use sha2::{Sha256, Digest}; | |
use solana_sdk::pubkey::Pubkey; | |
use solana_sdk::transaction::VersionedTransaction; | |
use solana_sdk::signature::Signer; | |
use solana_client::rpc_config::RpcAccountInfoConfig; | |
use solana_account_decoder::UiDataSliceConfig; | |
use solana_client::rpc_config::RpcProgramAccountsConfig; | |
use solana_sdk::commitment_config::CommitmentLevel::Processed; | |
use solana_sdk::commitment_config::CommitmentConfig; | |
use bytemuck; | |
use spl_token; | |
use bytemuck::{Pod, Zeroable}; | |
#[repr(packed)] | |
#[derive(Default, Debug, Clone, Copy)] | |
pub struct RewardInfo { | |
/// Reward state | |
pub reward_state: u8, | |
/// Reward open time | |
pub open_time: u64, | |
/// Reward end time | |
pub end_time: u64, | |
/// Reward last update time | |
pub last_update_time: u64, | |
/// Q64.64 number indicates how many tokens per second are earned per unit of liquidity. | |
pub emissions_per_second_x64: u128, | |
/// The total amount of reward emissioned | |
pub reward_total_emissioned: u64, | |
/// The total amount of claimed reward | |
pub reward_claimed: u64, | |
/// Reward token mint. | |
pub token_mint: Pubkey, | |
/// Reward vault token account. | |
pub token_vault: Pubkey, | |
/// The owner that has permission to set reward param | |
pub authority: Pubkey, | |
/// Q64.64 number that tracks the total tokens earned per unit of liquidity since the reward | |
/// emissions were turned on. | |
pub reward_growth_global_x64: u128, | |
} | |
unsafe impl Pod for RewardInfo {} | |
unsafe impl Zeroable for RewardInfo {} | |
#[repr(packed)] | |
#[derive(Default, Debug, Clone, Copy)] | |
pub struct PoolState { | |
/// Bump to identify PDA | |
pub bump: [u8; 1], | |
// Which config the pool belongs | |
pub amm_config: Pubkey, | |
// Pool creator | |
pub owner: Pubkey, | |
/// Token pair of the pool, where token_mint_0 address < token_mint_1 address | |
pub token_mint_0: Pubkey, | |
pub token_mint_1: Pubkey, | |
/// Token pair vault | |
pub token_vault_0: Pubkey, | |
pub token_vault_1: Pubkey, | |
/// observation account key | |
pub observation_key: Pubkey, | |
/// mint0 and mint1 decimals | |
pub mint_decimals_0: u8, | |
pub mint_decimals_1: u8, | |
/// The minimum number of ticks between initialized ticks | |
pub tick_spacing: u16, | |
/// The currently in range liquidity available to the pool. | |
pub liquidity: u128, | |
/// The current price of the pool as a sqrt(token_1/token_0) Q64.64 value | |
pub sqrt_price_x64: u128, | |
/// The current tick of the pool, i.e. according to the last tick transition that was run. | |
pub tick_current: i32, | |
/// the most-recently updated index of the observations array | |
pub observation_index: u16, | |
pub observation_update_duration: u16, | |
/// The fee growth as a Q64.64 number, i.e. fees of token_0 and token_1 collected per | |
/// unit of liquidity for the entire life of the pool. | |
pub fee_growth_global_0_x64: u128, | |
pub fee_growth_global_1_x64: u128, | |
/// The amounts of token_0 and token_1 that are owed to the protocol. | |
pub protocol_fees_token_0: u64, | |
pub protocol_fees_token_1: u64, | |
/// The amounts in and out of swap token_0 and token_1 | |
pub swap_in_amount_token_0: u128, | |
pub swap_out_amount_token_1: u128, | |
pub swap_in_amount_token_1: u128, | |
pub swap_out_amount_token_0: u128, | |
/// Bitwise representation of the state of the pool | |
/// bit0, 1: disable open position and increase liquidity, 0: normal | |
/// bit1, 1: disable decrease liquidity, 0: normal | |
/// bit2, 1: disable collect fee, 0: normal | |
/// bit3, 1: disable collect reward, 0: normal | |
/// bit4, 1: disable swap, 0: normal | |
pub status: u8, | |
/// Leave blank for future use | |
pub padding: [u8; 7], | |
pub reward_infos: [RewardInfo; 3], | |
/// Packed initialized tick array state | |
pub tick_array_bitmap: [u64; 16], | |
/// except protocol_fee and fund_fee | |
pub total_fees_token_0: u64, | |
/// except protocol_fee and fund_fee | |
pub total_fees_claimed_token_0: u64, | |
pub total_fees_token_1: u64, | |
pub total_fees_claimed_token_1: u64, | |
pub fund_fees_token_0: u64, | |
pub fund_fees_token_1: u64, | |
// The timestamp allowed for swap in the pool. | |
pub open_time: u64, | |
// Unused bytes for future upgrades. | |
pub padding1: [u64; 25], | |
pub padding2: [u64; 32], | |
} | |
unsafe impl Pod for PoolState {} | |
unsafe impl Zeroable for PoolState {} | |
pub const Q64: u128 = (u64::MAX as u128) + 1; // 2^64 | |
pub fn from_x64_price(price: u128) -> f64 { | |
price as f64 / Q64 as f64 | |
} | |
pub fn sqrt_price_x64_to_price(price: u128, decimals_0: u8, decimals_1: u8) -> f64 { | |
from_x64_price(price).powi(2) * multipler(decimals_0) / multipler(decimals_1) | |
} | |
pub fn multipler(decimals: u8) -> f64 { | |
(10_i32).checked_pow(decimals.try_into().unwrap()).unwrap() as f64 | |
} | |
#[tokio::main] | |
async fn main() { | |
let oracle_key = Pubkey::from_str("BfvoZHYSxgJe4P7jbt9HTqJHip7Lf3xRB1QNszEPrhCx").unwrap(); | |
let url = "https://switchboard.rpcpool.com/eXXX".to_string(); | |
let client = RpcClient::new(url); | |
let account_info = client.get_account_data(&Pubkey::from_str("8sLbNZoA1cfnvMJLPfp98ZLAnFSYCFApfJKMbiXNLwxj").unwrap()).await.unwrap(); | |
let token_program = spl_token::ID; | |
let account_info = &account_info[8..]; | |
let pool = bytemuck::try_from_bytes::<PoolState>(&account_info),unwrap(); | |
println!("{:?}", sqrt_price_x64_to_price(pool.sqrt_price_x64, pool.mint_decimals_0, pool.mint_decimals_1)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment