2022-12-16 04:10:46 -08:00
|
|
|
use anchor_lang::{AccountDeserialize, Discriminator};
|
2022-07-16 05:37:15 -07:00
|
|
|
|
2022-07-25 07:07:53 -07:00
|
|
|
use mango_v4::state::{Bank, MangoAccount, MangoAccountValue, MintInfo, PerpMarket, Serum3Market};
|
2022-07-16 05:37:15 -07:00
|
|
|
|
2022-07-25 07:07:53 -07:00
|
|
|
use solana_account_decoder::UiAccountEncoding;
|
2022-12-16 04:10:46 -08:00
|
|
|
use solana_client::nonblocking::rpc_client::RpcClient as RpcClientAsync;
|
2022-07-25 07:07:53 -07:00
|
|
|
use solana_client::rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig};
|
2022-12-10 02:16:47 -08:00
|
|
|
use solana_client::rpc_filter::{Memcmp, RpcFilterType};
|
2022-07-16 05:37:15 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
|
2022-12-16 04:10:46 -08:00
|
|
|
pub async fn fetch_mango_accounts(
|
|
|
|
rpc: &RpcClientAsync,
|
|
|
|
program: Pubkey,
|
2022-07-16 05:37:15 -07:00
|
|
|
group: Pubkey,
|
|
|
|
owner: Pubkey,
|
2022-12-16 04:10:46 -08:00
|
|
|
) -> anyhow::Result<Vec<(Pubkey, MangoAccountValue)>> {
|
2022-07-25 07:07:53 -07:00
|
|
|
let config = RpcProgramAccountsConfig {
|
|
|
|
filters: Some(vec![
|
2022-12-10 02:16:47 -08:00
|
|
|
RpcFilterType::Memcmp(Memcmp::new_base58_encoded(
|
|
|
|
0,
|
|
|
|
&MangoAccount::discriminator(),
|
|
|
|
)),
|
|
|
|
RpcFilterType::Memcmp(Memcmp::new_base58_encoded(8, &group.to_bytes())),
|
|
|
|
RpcFilterType::Memcmp(Memcmp::new_base58_encoded(40, &owner.to_bytes())),
|
2022-07-25 07:07:53 -07:00
|
|
|
]),
|
|
|
|
account_config: RpcAccountInfoConfig {
|
|
|
|
encoding: Some(UiAccountEncoding::Base64),
|
|
|
|
..RpcAccountInfoConfig::default()
|
|
|
|
},
|
|
|
|
..RpcProgramAccountsConfig::default()
|
|
|
|
};
|
2022-12-16 04:10:46 -08:00
|
|
|
rpc.get_program_accounts_with_config(&program, config)
|
|
|
|
.await?
|
2022-07-25 07:07:53 -07:00
|
|
|
.into_iter()
|
|
|
|
.map(|(key, account)| Ok((key, MangoAccountValue::from_bytes(&account.data[8..])?)))
|
|
|
|
.collect::<Result<Vec<_>, _>>()
|
2022-07-16 05:37:15 -07:00
|
|
|
}
|
|
|
|
|
2022-12-16 04:10:46 -08:00
|
|
|
pub async fn fetch_anchor_account<T: AccountDeserialize>(
|
|
|
|
rpc: &RpcClientAsync,
|
|
|
|
address: &Pubkey,
|
|
|
|
) -> anyhow::Result<T> {
|
|
|
|
let account = rpc.get_account(address).await?;
|
|
|
|
Ok(T::try_deserialize(&mut (&account.data as &[u8]))?)
|
2022-07-16 05:37:15 -07:00
|
|
|
}
|
|
|
|
|
2022-12-16 04:10:46 -08:00
|
|
|
async fn fetch_anchor_accounts<T: AccountDeserialize + Discriminator>(
|
|
|
|
rpc: &RpcClientAsync,
|
|
|
|
program: Pubkey,
|
|
|
|
filters: Vec<RpcFilterType>,
|
|
|
|
) -> anyhow::Result<Vec<(Pubkey, T)>> {
|
|
|
|
let account_type_filter =
|
|
|
|
RpcFilterType::Memcmp(Memcmp::new_base58_encoded(0, &T::discriminator()));
|
|
|
|
let config = RpcProgramAccountsConfig {
|
|
|
|
filters: Some([vec![account_type_filter], filters].concat()),
|
|
|
|
account_config: RpcAccountInfoConfig {
|
|
|
|
encoding: Some(UiAccountEncoding::Base64),
|
|
|
|
..RpcAccountInfoConfig::default()
|
|
|
|
},
|
|
|
|
..RpcProgramAccountsConfig::default()
|
|
|
|
};
|
|
|
|
rpc.get_program_accounts_with_config(&program, config)
|
|
|
|
.await?
|
|
|
|
.into_iter()
|
|
|
|
.map(|(key, account)| Ok((key, T::try_deserialize(&mut (&account.data as &[u8]))?)))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn fetch_banks(
|
|
|
|
rpc: &RpcClientAsync,
|
|
|
|
program: Pubkey,
|
|
|
|
group: Pubkey,
|
|
|
|
) -> anyhow::Result<Vec<(Pubkey, Bank)>> {
|
|
|
|
fetch_anchor_accounts::<Bank>(
|
|
|
|
rpc,
|
|
|
|
program,
|
|
|
|
vec![RpcFilterType::Memcmp(Memcmp::new_base58_encoded(
|
|
|
|
8,
|
|
|
|
&group.to_bytes(),
|
|
|
|
))],
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn fetch_mint_infos(
|
|
|
|
rpc: &RpcClientAsync,
|
|
|
|
program: Pubkey,
|
2022-07-16 05:37:15 -07:00
|
|
|
group: Pubkey,
|
2022-12-16 04:10:46 -08:00
|
|
|
) -> anyhow::Result<Vec<(Pubkey, MintInfo)>> {
|
|
|
|
fetch_anchor_accounts::<MintInfo>(
|
|
|
|
rpc,
|
|
|
|
program,
|
|
|
|
vec![RpcFilterType::Memcmp(Memcmp::new_base58_encoded(
|
|
|
|
8,
|
|
|
|
&group.to_bytes(),
|
|
|
|
))],
|
|
|
|
)
|
|
|
|
.await
|
2022-07-16 05:37:15 -07:00
|
|
|
}
|
|
|
|
|
2022-12-16 04:10:46 -08:00
|
|
|
pub async fn fetch_serum3_markets(
|
|
|
|
rpc: &RpcClientAsync,
|
|
|
|
program: Pubkey,
|
2022-07-16 05:37:15 -07:00
|
|
|
group: Pubkey,
|
2022-12-16 04:10:46 -08:00
|
|
|
) -> anyhow::Result<Vec<(Pubkey, Serum3Market)>> {
|
|
|
|
fetch_anchor_accounts::<Serum3Market>(
|
|
|
|
rpc,
|
|
|
|
program,
|
|
|
|
vec![RpcFilterType::Memcmp(Memcmp::new_base58_encoded(
|
|
|
|
8,
|
|
|
|
&group.to_bytes(),
|
|
|
|
))],
|
|
|
|
)
|
|
|
|
.await
|
2022-07-16 05:37:15 -07:00
|
|
|
}
|
|
|
|
|
2022-12-16 04:10:46 -08:00
|
|
|
pub async fn fetch_perp_markets(
|
|
|
|
rpc: &RpcClientAsync,
|
|
|
|
program: Pubkey,
|
2022-07-16 05:37:15 -07:00
|
|
|
group: Pubkey,
|
2022-12-16 04:10:46 -08:00
|
|
|
) -> anyhow::Result<Vec<(Pubkey, PerpMarket)>> {
|
|
|
|
fetch_anchor_accounts::<PerpMarket>(
|
|
|
|
rpc,
|
|
|
|
program,
|
|
|
|
vec![RpcFilterType::Memcmp(Memcmp::new_base58_encoded(
|
|
|
|
8,
|
|
|
|
&group.to_bytes(),
|
|
|
|
))],
|
|
|
|
)
|
|
|
|
.await
|
2022-07-16 05:37:15 -07:00
|
|
|
}
|