2022-07-16 05:37:15 -07:00
|
|
|
use std::sync::{Arc, RwLock};
|
2022-08-07 11:04:19 -07:00
|
|
|
use std::thread;
|
|
|
|
use std::time::{Duration, Instant};
|
2022-07-16 05:37:15 -07:00
|
|
|
|
|
|
|
use crate::chain_data::*;
|
|
|
|
|
2022-08-01 10:08:59 -07:00
|
|
|
use anchor_lang::Discriminator;
|
|
|
|
|
2022-07-16 05:37:15 -07:00
|
|
|
use mango_v4::accounts_zerocopy::LoadZeroCopy;
|
2022-08-01 10:08:59 -07:00
|
|
|
use mango_v4::state::{MangoAccount, MangoAccountValue};
|
2022-07-16 05:37:15 -07:00
|
|
|
|
|
|
|
use anyhow::Context;
|
|
|
|
|
|
|
|
use solana_client::rpc_client::RpcClient;
|
2022-07-25 07:07:53 -07:00
|
|
|
use solana_sdk::account::{AccountSharedData, ReadableAccount};
|
2022-08-07 11:04:19 -07:00
|
|
|
use solana_sdk::clock::Slot;
|
2022-07-16 05:37:15 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2022-08-07 11:04:19 -07:00
|
|
|
use solana_sdk::signature::Signature;
|
2022-07-16 05:37:15 -07:00
|
|
|
|
2022-07-21 04:03:28 -07:00
|
|
|
pub struct AccountFetcher {
|
2022-07-16 05:37:15 -07:00
|
|
|
pub chain_data: Arc<RwLock<ChainData>>,
|
|
|
|
pub rpc: RpcClient,
|
|
|
|
}
|
|
|
|
|
2022-07-21 04:03:28 -07:00
|
|
|
impl AccountFetcher {
|
2022-07-16 05:37:15 -07:00
|
|
|
// loads from ChainData
|
|
|
|
pub fn fetch<T: anchor_lang::ZeroCopy + anchor_lang::Owner>(
|
|
|
|
&self,
|
|
|
|
address: &Pubkey,
|
|
|
|
) -> anyhow::Result<T> {
|
2022-08-30 04:46:39 -07:00
|
|
|
Ok(*self
|
2022-07-16 05:37:15 -07:00
|
|
|
.fetch_raw(address)?
|
|
|
|
.load::<T>()
|
2022-08-30 04:46:39 -07:00
|
|
|
.with_context(|| format!("loading account {}", address))?)
|
2022-07-16 05:37:15 -07:00
|
|
|
}
|
|
|
|
|
2022-07-25 07:07:53 -07:00
|
|
|
pub fn fetch_mango_account(&self, address: &Pubkey) -> anyhow::Result<MangoAccountValue> {
|
|
|
|
let acc = self.fetch_raw(address)?;
|
2022-08-01 10:08:59 -07:00
|
|
|
|
|
|
|
let data = acc.data();
|
|
|
|
let disc_bytes = &data[0..8];
|
2022-08-30 04:46:39 -07:00
|
|
|
if disc_bytes != MangoAccount::discriminator() {
|
2022-08-01 10:08:59 -07:00
|
|
|
anyhow::bail!("not a mango account at {}", address);
|
|
|
|
}
|
|
|
|
|
2022-08-30 04:46:39 -07:00
|
|
|
MangoAccountValue::from_bytes(&data[8..])
|
|
|
|
.with_context(|| format!("loading mango account {}", address))
|
2022-07-25 07:07:53 -07:00
|
|
|
}
|
|
|
|
|
2022-07-16 05:37:15 -07:00
|
|
|
// fetches via RPC, stores in ChainData, returns new version
|
|
|
|
pub fn fetch_fresh<T: anchor_lang::ZeroCopy + anchor_lang::Owner>(
|
|
|
|
&self,
|
|
|
|
address: &Pubkey,
|
|
|
|
) -> anyhow::Result<T> {
|
|
|
|
self.refresh_account_via_rpc(address)?;
|
|
|
|
self.fetch(address)
|
|
|
|
}
|
|
|
|
|
2022-07-25 07:07:53 -07:00
|
|
|
pub fn fetch_fresh_mango_account(&self, address: &Pubkey) -> anyhow::Result<MangoAccountValue> {
|
|
|
|
self.refresh_account_via_rpc(address)?;
|
|
|
|
self.fetch_mango_account(address)
|
|
|
|
}
|
|
|
|
|
2022-07-16 05:37:15 -07:00
|
|
|
pub fn fetch_raw(&self, address: &Pubkey) -> anyhow::Result<AccountSharedData> {
|
|
|
|
let chain_data = self.chain_data.read().unwrap();
|
|
|
|
Ok(chain_data
|
|
|
|
.account(address)
|
|
|
|
.with_context(|| format!("fetch account {} via chain_data", address))?
|
|
|
|
.clone())
|
|
|
|
}
|
|
|
|
|
2022-08-07 11:04:19 -07:00
|
|
|
pub fn refresh_account_via_rpc(&self, address: &Pubkey) -> anyhow::Result<Slot> {
|
2022-07-16 05:37:15 -07:00
|
|
|
let response = self
|
|
|
|
.rpc
|
2022-08-30 04:46:39 -07:00
|
|
|
.get_account_with_commitment(address, self.rpc.commitment())
|
2022-07-16 05:37:15 -07:00
|
|
|
.with_context(|| format!("refresh account {} via rpc", address))?;
|
2022-08-07 11:04:19 -07:00
|
|
|
let slot = response.context.slot;
|
2022-07-16 05:37:15 -07:00
|
|
|
let account = response
|
|
|
|
.value
|
|
|
|
.ok_or(anchor_client::ClientError::AccountNotFound)
|
|
|
|
.with_context(|| format!("refresh account {} via rpc", address))?;
|
|
|
|
|
|
|
|
let mut chain_data = self.chain_data.write().unwrap();
|
|
|
|
chain_data.update_from_rpc(
|
|
|
|
address,
|
2022-07-21 04:03:28 -07:00
|
|
|
AccountAndSlot {
|
2022-07-16 05:37:15 -07:00
|
|
|
slot: response.context.slot,
|
|
|
|
account: account.into(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-08-07 11:04:19 -07:00
|
|
|
Ok(slot)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the maximum slot reported for the processing of the signatures
|
|
|
|
pub fn transaction_max_slot(&self, signatures: &[Signature]) -> anyhow::Result<Slot> {
|
|
|
|
let statuses = self.rpc.get_signature_statuses(signatures)?.value;
|
|
|
|
Ok(statuses
|
|
|
|
.iter()
|
|
|
|
.map(|status_opt| status_opt.as_ref().map(|status| status.slot).unwrap_or(0))
|
|
|
|
.max()
|
|
|
|
.unwrap_or(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return success once all addresses have data >= min_slot
|
|
|
|
pub fn refresh_accounts_via_rpc_until_slot(
|
|
|
|
&self,
|
|
|
|
addresses: &[Pubkey],
|
|
|
|
min_slot: Slot,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
let start = Instant::now();
|
|
|
|
for address in addresses {
|
|
|
|
loop {
|
|
|
|
if start.elapsed() > timeout {
|
|
|
|
anyhow::bail!(
|
|
|
|
"timeout while waiting for data for {} that's newer than slot {}",
|
|
|
|
address,
|
|
|
|
min_slot
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let data_slot = self.refresh_account_via_rpc(address)?;
|
|
|
|
if data_slot >= min_slot {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
thread::sleep(Duration::from_millis(500));
|
|
|
|
}
|
|
|
|
}
|
2022-07-16 05:37:15 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 04:03:28 -07:00
|
|
|
impl crate::AccountFetcher for AccountFetcher {
|
2022-07-16 05:37:15 -07:00
|
|
|
fn fetch_raw_account(&self, address: Pubkey) -> anyhow::Result<solana_sdk::account::Account> {
|
2022-07-19 00:59:30 -07:00
|
|
|
self.fetch_raw(&address).map(|a| a.into())
|
2022-07-16 05:37:15 -07:00
|
|
|
}
|
|
|
|
}
|