Skip to content

Commit

Permalink
program: add market index offset trait (#287)
Browse files Browse the repository at this point in the history
* sdk: update description in package.json

* program: add SIZE trait

* program: add trait for market index offset

* add CHANGELOG
  • Loading branch information
crispheaney committed Dec 29, 2022
1 parent 20cdb63 commit cf01d68
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

program: add market index offset trait ([#287](https://github.com/drift-labs/protocol-v2/pull/287))
program: add size trait to accounts and events ([#286](https://github.com/drift-labs/protocol-v2/pull/286))

### Fixes
Expand Down
6 changes: 5 additions & 1 deletion programs/drift/src/state/perp_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::math::stats;

use crate::state::oracle::{HistoricalOracleData, OracleSource};
use crate::state::spot_market::{SpotBalance, SpotBalanceType};
use crate::state::traits::Size;
use crate::state::traits::{MarketIndexOffset, Size};
use crate::{AMM_TO_QUOTE_PRECISION_RATIO, PRICE_PRECISION};
use borsh::{BorshDeserialize, BorshSerialize};

Expand Down Expand Up @@ -142,6 +142,10 @@ impl Size for PerpMarket {
const SIZE: usize = 1216;
}

impl MarketIndexOffset for PerpMarket {
const MARKET_INDEX_OFFSET: usize = 1160;
}

impl PerpMarket {
pub fn is_active(&self, now: i64) -> DriftResult<bool> {
let status_ok = !matches!(
Expand Down
6 changes: 5 additions & 1 deletion programs/drift/src/state/spot_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::math::spot_balance::get_token_amount;

use crate::state::oracle::{HistoricalIndexData, HistoricalOracleData, OracleSource};
use crate::state::perp_market::{MarketStatus, PoolBalance};
use crate::state::traits::Size;
use crate::state::traits::{MarketIndexOffset, Size};

#[account(zero_copy)]
#[derive(PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -133,6 +133,10 @@ impl Size for SpotMarket {
const SIZE: usize = 776;
}

impl MarketIndexOffset for SpotMarket {
const MARKET_INDEX_OFFSET: usize = 684;
}

impl SpotMarket {
pub fn is_active(&self, now: i64) -> DriftResult<bool> {
let status_ok = !matches!(
Expand Down
4 changes: 4 additions & 0 deletions programs/drift/src/state/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ mod tests;
pub trait Size {
const SIZE: usize;
}

pub trait MarketIndexOffset {
const MARKET_INDEX_OFFSET: usize;
}
38 changes: 38 additions & 0 deletions programs/drift/src/state/traits/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,41 @@ mod size {
assert_eq!(actual_size, expected_size);
}
}

mod market_index_offset {
use crate::create_anchor_account_info;
use crate::state::perp_market::PerpMarket;
use crate::state::spot_market::SpotMarket;
use crate::state::traits::MarketIndexOffset;
use crate::test_utils::*;
use anchor_lang::prelude::*;
use arrayref::array_ref;

#[test]
fn spot_market() {
let mut spot_market = SpotMarket {
market_index: 11,
..SpotMarket::default()
};
create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info);

let data = spot_market_account_info.try_borrow_data().unwrap();
let market_index =
u16::from_le_bytes(*array_ref![data, SpotMarket::MARKET_INDEX_OFFSET, 2]);
assert_eq!(market_index, spot_market.market_index);
}

#[test]
fn perp_market() {
let mut perp_market = PerpMarket {
market_index: 11,
..PerpMarket::default()
};
create_anchor_account_info!(perp_market, PerpMarket, perp_market_account_info);

let data = perp_market_account_info.try_borrow_data().unwrap();
let market_index =
u16::from_le_bytes(*array_ref![data, PerpMarket::MARKET_INDEX_OFFSET, 2]);
assert_eq!(market_index, perp_market.market_index);
}
}

0 comments on commit cf01d68

Please sign in to comment.