Skip to content

Commit

Permalink
impl ZeroCopyDeserialize for jup
Browse files Browse the repository at this point in the history
  • Loading branch information
skrrb committed Feb 7, 2024
1 parent deeda93 commit 7f72218
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
15 changes: 9 additions & 6 deletions lib/client/src/jup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anchor_lang::AccountDeserialize;
use anchor_lang::__private::bytemuck::Zeroable;
use anchor_lang::prelude::*;
use anchor_spl::token::Token;
Expand All @@ -14,6 +13,7 @@ use openbook_v2::{
use crate::{
book::{amounts_from_book, Amounts},
remaining_accounts_to_crank,
util::ZeroCopyDeserialize,
};
use jupiter_amm_interface::{
AccountMap, Amm, KeyedAccount, Quote, QuoteParams, Side as JupiterSide, Swap,
Expand Down Expand Up @@ -59,7 +59,8 @@ impl Amm for OpenBookMarket {
}

fn from_keyed_account(keyed_account: &KeyedAccount) -> Result<Self> {
let market = Market::try_deserialize(&mut keyed_account.account.data.as_slice())?;
let market =
Market::try_deserialize_from_slice(&mut keyed_account.account.data.as_slice())?;
let mut related_accounts = vec![
market.bids,
market.asks,
Expand Down Expand Up @@ -91,13 +92,14 @@ impl Amm for OpenBookMarket {

fn update(&mut self, account_map: &AccountMap) -> Result<()> {
let bids_data = account_map.get(&self.market.bids).unwrap();
self.bids = BookSide::try_deserialize(&mut bids_data.data.as_slice()).unwrap();
self.bids = BookSide::try_deserialize_from_slice(&mut bids_data.data.as_slice()).unwrap();

let asks_data = account_map.get(&self.market.asks).unwrap();
self.asks = BookSide::try_deserialize(&mut asks_data.data.as_slice()).unwrap();
self.asks = BookSide::try_deserialize_from_slice(&mut asks_data.data.as_slice()).unwrap();

let event_heap_data = account_map.get(&self.market.event_heap).unwrap();
self.event_heap = EventHeap::try_deserialize(&mut event_heap_data.data.as_slice()).unwrap();
self.event_heap =
EventHeap::try_deserialize_from_slice(&mut event_heap_data.data.as_slice()).unwrap();

let clock_data = account_map.get(&clock::ID).unwrap();
let clock: Clock = bincode::deserialize(clock_data.data.as_slice())?;
Expand Down Expand Up @@ -401,7 +403,8 @@ mod test {
let user_input_account = Pubkey::new_unique();
let user_output_account = Pubkey::new_unique();

let market_data = Market::try_deserialize(&mut market_account.account.data.as_slice())?;
let market_data =
Market::try_deserialize_from_slice(&mut market_account.account.data.as_slice())?;

add_token_account(user_input_account, user.pubkey(), input_mint);
add_token_account(user_output_account, user.pubkey(), output_mint);
Expand Down
17 changes: 16 additions & 1 deletion lib/client/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anchor_lang::{error::Error, AccountDeserialize, ZeroCopy};
use solana_client::{
client_error::Result as ClientResult, rpc_client::RpcClient, rpc_request::RpcError,
};
Expand All @@ -6,9 +7,23 @@ use solana_sdk::{
clock::Slot, commitment_config::CommitmentConfig, signature::Signature,
transaction::uses_durable_nonce,
};

use std::{thread, time};

/// zero copy accounts could be larger than the underlying struct
/// https://github.com/coral-xyz/anchor/blob/08110e63fa5a7369830db972d4abdcfa1aaa7f2e/lang/src/accounts/account_loader.rs#L165
/// https://github.com/coral-xyz/anchor/blob/08110e63fa5a7369830db972d4abdcfa1aaa7f2e/lang/attribute/account/src/lib.rs#L195
pub trait ZeroCopyDeserialize {
fn try_deserialize_from_slice(buf: &mut &[u8]) -> Result<Self, Error>
where
Self: Sized;
}

impl<T: AccountDeserialize + ZeroCopy> ZeroCopyDeserialize for T {
fn try_deserialize_from_slice(buf: &mut &[u8]) -> Result<Self, Error> {
Self::try_deserialize(&mut &buf[..std::mem::size_of::<Self>() + 8])
}
}

/// Some Result<> types don't convert to anyhow::Result nicely. Force them through stringification.
pub trait AnyhowWrap {
type Value;
Expand Down

0 comments on commit 7f72218

Please sign in to comment.