Created
April 27, 2024 10:37
-
-
Save quangkeu95/fe27dff8171f7061928fb1a3dacd695c to your computer and use it in GitHub Desktop.
Parse Meteora Dynamic AMM pools locked info
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
// Fetch lock info from rpc | |
pub async fn fetch_lock_info_from_rpc(rpc_client: &RpcClient, pair_id: &str) -> Result<LockInfo> { | |
let pool_pubkey = Pubkey::from_str(pair_id)?; | |
// find lp mint of the pool | |
let pool_account = rpc_client.get_account(&pool_pubkey).await?; | |
let pool = PoolState::try_deserialize(&mut pool_account.data.as_ref())?; | |
let lp_mint = pool.lp_mint; | |
let lp_mint_account = rpc_client.get_account(&lp_mint).await?; | |
let lp_mint_state = Mint::try_deserialize(&mut lp_mint_account.data.as_ref())?; | |
let total_supply = BigDecimal::from(lp_mint_state.supply); | |
// find the balance of lp mint in the pool ATA | |
let token_account_pubkey = get_associated_token_address(&pool_pubkey, &lp_mint); | |
let token_balance = rpc_client | |
.get_token_account_balance(&token_account_pubkey) | |
.await?; | |
let locked_amount = BigDecimal::from_str(&token_balance.amount)?; | |
// sum with locked info in pool state | |
let locked_amount = locked_amount + BigDecimal::from(pool.total_locked_lp); | |
let locked_percentage = (locked_amount * BigDecimal::from(100) / total_supply) | |
.to_f64() | |
.ok_or_eyre("error convert locked_percentage to f64")?; | |
Ok(LockInfo { | |
pair_id: pair_id.to_string(), | |
locked_amount: token_balance.ui_amount.unwrap_or_default(), | |
locked_percentage, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment