Health: Make FixedOrderAccountRetriever more flexible
This commit is contained in:
parent
b3568b7f3e
commit
40023fcef1
|
@ -8,12 +8,16 @@ use std::{cell::Ref, mem};
|
||||||
/// AccountInfo and AccountSharedData. That way the functions become usable in the program
|
/// AccountInfo and AccountSharedData. That way the functions become usable in the program
|
||||||
/// and in client code.
|
/// and in client code.
|
||||||
// NOTE: would love to use solana's ReadableAccount, but that's in solana_sdk -- unavailable for programs
|
// NOTE: would love to use solana's ReadableAccount, but that's in solana_sdk -- unavailable for programs
|
||||||
// TODO: Maybe have KeyedAccountReader that also includes fn key() -> &Pukey?
|
|
||||||
pub trait AccountReader {
|
pub trait AccountReader {
|
||||||
fn owner(&self) -> &Pubkey;
|
fn owner(&self) -> &Pubkey;
|
||||||
fn data(&self) -> &[u8];
|
fn data(&self) -> &[u8];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Like AccountReader, but can also get the account pubkey
|
||||||
|
pub trait KeyedAccountReader: AccountReader {
|
||||||
|
fn key(&self) -> &Pubkey;
|
||||||
|
}
|
||||||
|
|
||||||
/// A Ref to an AccountInfo - makes AccountInfo compatible with AccountReader
|
/// A Ref to an AccountInfo - makes AccountInfo compatible with AccountReader
|
||||||
pub struct AccountInfoRef<'a, 'info: 'a> {
|
pub struct AccountInfoRef<'a, 'info: 'a> {
|
||||||
pub key: &'info Pubkey,
|
pub key: &'info Pubkey,
|
||||||
|
@ -75,6 +79,18 @@ impl<'info, 'a> AccountReader for AccountInfoRefMut<'info, 'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'info, 'a> KeyedAccountReader for AccountInfoRef<'info, 'a> {
|
||||||
|
fn key(&self) -> &Pubkey {
|
||||||
|
self.key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'info, 'a> KeyedAccountReader for AccountInfoRefMut<'info, 'a> {
|
||||||
|
fn key(&self) -> &Pubkey {
|
||||||
|
self.key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Common traits for loading from account data.
|
// Common traits for loading from account data.
|
||||||
//
|
//
|
||||||
|
|
|
@ -43,14 +43,14 @@ pub trait AccountRetriever {
|
||||||
/// 2. n_banks oracle accounts, one for each bank in the same order
|
/// 2. n_banks oracle accounts, one for each bank in the same order
|
||||||
/// 3. PerpMarket accounts, in the order of account.perps.iter_active_accounts()
|
/// 3. PerpMarket accounts, in the order of account.perps.iter_active_accounts()
|
||||||
/// 4. serum3 OpenOrders accounts, in the order of account.serum3.iter_active()
|
/// 4. serum3 OpenOrders accounts, in the order of account.serum3.iter_active()
|
||||||
pub struct FixedOrderAccountRetriever<'a, 'info> {
|
pub struct FixedOrderAccountRetriever<T: KeyedAccountReader> {
|
||||||
ais: Vec<AccountInfoRef<'a, 'info>>,
|
ais: Vec<T>,
|
||||||
n_banks: usize,
|
n_banks: usize,
|
||||||
begin_perp: usize,
|
begin_perp: usize,
|
||||||
begin_serum3: usize,
|
begin_serum3: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'info> AccountRetriever for FixedOrderAccountRetriever<'a, 'info> {
|
impl<T: KeyedAccountReader> AccountRetriever for FixedOrderAccountRetriever<T> {
|
||||||
fn bank_and_oracle(
|
fn bank_and_oracle(
|
||||||
&self,
|
&self,
|
||||||
group: &Pubkey,
|
group: &Pubkey,
|
||||||
|
@ -61,7 +61,7 @@ impl<'a, 'info> AccountRetriever for FixedOrderAccountRetriever<'a, 'info> {
|
||||||
require!(&bank.group == group, MangoError::SomeError);
|
require!(&bank.group == group, MangoError::SomeError);
|
||||||
require!(bank.token_index == token_index, MangoError::SomeError);
|
require!(bank.token_index == token_index, MangoError::SomeError);
|
||||||
let oracle = &self.ais[cm!(self.n_banks + account_index)];
|
let oracle = &self.ais[cm!(self.n_banks + account_index)];
|
||||||
require!(&bank.oracle == oracle.key, MangoError::SomeError);
|
require!(&bank.oracle == oracle.key(), MangoError::SomeError);
|
||||||
Ok((bank, oracle_price(oracle, bank.mint_decimals)?))
|
Ok((bank, oracle_price(oracle, bank.mint_decimals)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ impl<'a, 'info> AccountRetriever for FixedOrderAccountRetriever<'a, 'info> {
|
||||||
|
|
||||||
fn serum_oo(&self, account_index: usize, key: &Pubkey) -> Result<&OpenOrders> {
|
fn serum_oo(&self, account_index: usize, key: &Pubkey) -> Result<&OpenOrders> {
|
||||||
let ai = &self.ais[cm!(self.begin_serum3 + account_index)];
|
let ai = &self.ais[cm!(self.begin_serum3 + account_index)];
|
||||||
require!(key == ai.key, MangoError::SomeError);
|
require!(key == ai.key(), MangoError::SomeError);
|
||||||
serum3_cpi::load_open_orders(ai)
|
serum3_cpi::load_open_orders(ai)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue