Skip to content

Commit

Permalink
program: add pyth1M (#375)
Browse files Browse the repository at this point in the history
* bigz/add-Pyth1M

* pyth1000 -> 1k

* update changelog.md

* add link to changelog, pyth_1m rust test
  • Loading branch information
0xbigz authored Feb 28, 2023
1 parent 5e00275 commit 86cf411
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Features

- program: max number of subaccounts to 3000
- program: add pyth1M/pyth1K as OracleSource ([#375](https://github.com/drift-labs/protocol-v2/pull/375))

### Fixes

Expand Down
13 changes: 12 additions & 1 deletion programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ pub fn handle_initialize_perp_market(
let last_oracle_price_twap = perp_market.amm.get_pyth_twap(&ctx.accounts.oracle, 1)?;
(oracle_price, oracle_delay, last_oracle_price_twap)
}
OracleSource::Pyth1000 => {
OracleSource::Pyth1K => {
let OraclePriceData {
price: oracle_price,
delay: oracle_delay,
Expand All @@ -502,6 +502,17 @@ pub fn handle_initialize_perp_market(
perp_market.amm.get_pyth_twap(&ctx.accounts.oracle, 1000)?;
(oracle_price, oracle_delay, last_oracle_price_twap)
}
OracleSource::Pyth1M => {
let OraclePriceData {
price: oracle_price,
delay: oracle_delay,
..
} = get_pyth_price(&ctx.accounts.oracle, clock_slot, 1000000)?;
let last_oracle_price_twap = perp_market
.amm
.get_pyth_twap(&ctx.accounts.oracle, 1000000)?;
(oracle_price, oracle_delay, last_oracle_price_twap)
}
OracleSource::Switchboard => {
msg!("Switchboard oracle cant be used for perp market");
return Err(ErrorCode::InvalidOracle.into());
Expand Down
6 changes: 4 additions & 2 deletions programs/drift/src/state/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ impl HistoricalIndexData {
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Eq, PartialEq, Debug)]
pub enum OracleSource {
Pyth,
Pyth1000,
Pyth1K,
Pyth1M,
Switchboard,
QuoteAsset,
}
Expand Down Expand Up @@ -130,7 +131,8 @@ pub fn get_oracle_price(
) -> DriftResult<OraclePriceData> {
match oracle_source {
OracleSource::Pyth => get_pyth_price(price_oracle, clock_slot, 1),
OracleSource::Pyth1000 => get_pyth_price(price_oracle, clock_slot, 1000),
OracleSource::Pyth1K => get_pyth_price(price_oracle, clock_slot, 1000),
OracleSource::Pyth1M => get_pyth_price(price_oracle, clock_slot, 1000000),
OracleSource::Switchboard => {
msg!("Switchboard oracle not yet supported");
Err(crate::error::ErrorCode::InvalidOracle)
Expand Down
32 changes: 29 additions & 3 deletions programs/drift/src/state/oracle/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::state::perp_market::AMM;
use crate::test_utils::*;

#[test]
fn pyth_1000() {
fn pyth_1k() {
let mut oracle_price = get_hardcoded_pyth_price(8394, 10);
let oracle_price_key =
Pubkey::from_str("8ihFLu5FimgTQ1Unh4dVyEHUGodJ5gJQCrQf4KUVB9bN").unwrap();
Expand All @@ -21,14 +21,40 @@ fn pyth_1000() {
);

let oracle_price_data =
get_oracle_price(&OracleSource::Pyth1000, &oracle_account_info, 0).unwrap();
get_oracle_price(&OracleSource::Pyth1K, &oracle_account_info, 0).unwrap();
assert_eq!(oracle_price_data.price, 839);

let amm = AMM {
oracle_source: OracleSource::Pyth1000,
oracle_source: OracleSource::Pyth1K,
..AMM::default()
};

let twap = amm.get_oracle_twap(&oracle_account_info).unwrap();
assert_eq!(twap, Some(839));
}

#[test]
fn pyth_1m() {
let mut oracle_price = get_hardcoded_pyth_price(8394, 10);
let oracle_price_key =
Pubkey::from_str("8ihFLu5FimgTQ1Unh4dVyEHUGodJ5gJQCrQf4KUVB9bN").unwrap();
let pyth_program = crate::ids::pyth_program::id();
create_account_info!(
oracle_price,
&oracle_price_key,
&pyth_program,
oracle_account_info
);

let oracle_price_data =
get_oracle_price(&OracleSource::Pyth1M, &oracle_account_info, 0).unwrap();
assert_eq!(oracle_price_data.price, 839400);

let amm = AMM {
oracle_source: OracleSource::Pyth1M,
..AMM::default()
};

let twap = amm.get_oracle_twap(&oracle_account_info).unwrap();
assert_eq!(twap, Some(839400));
}
4 changes: 2 additions & 2 deletions programs/drift/src/state/oracle_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl<'a> OracleMap<'a> {
let pubkey = account_info.key();

let oracle_source = if pubkey == bonk_oracle::id() {
OracleSource::Pyth1000
OracleSource::Pyth1M
} else {
OracleSource::Pyth
};
Expand Down Expand Up @@ -212,7 +212,7 @@ impl<'a> OracleMap<'a> {
if account_info.owner == &pyth_program::id() {
let pubkey = account_info.key();
let oracle_source = if pubkey == bonk_oracle::id() {
OracleSource::Pyth1000
OracleSource::Pyth1M
} else {
OracleSource::Pyth
};
Expand Down
3 changes: 2 additions & 1 deletion programs/drift/src/state/perp_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ impl AMM {
pub fn get_oracle_twap(&self, price_oracle: &AccountInfo) -> DriftResult<Option<i64>> {
match self.oracle_source {
OracleSource::Pyth => Ok(Some(self.get_pyth_twap(price_oracle, 1)?)),
OracleSource::Pyth1000 => Ok(Some(self.get_pyth_twap(price_oracle, 1000)?)),
OracleSource::Pyth1K => Ok(Some(self.get_pyth_twap(price_oracle, 1000)?)),
OracleSource::Pyth1M => Ok(Some(self.get_pyth_twap(price_oracle, 1000000)?)),
OracleSource::Switchboard => Ok(None),
OracleSource::QuoteAsset => {
msg!("Can't get oracle twap for quote asset");
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/constants/perpMarkets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
marketIndex: 4,
oracle: new PublicKey('6bquU99ktV1VRiHDr8gMhDFt3kMfhCQo5nfNrg2Urvsn'),
launchTs: 1677068931000,
oracleSource: OracleSource.PYTH_1000,
oracleSource: OracleSource.PYTH_1K,
},
];

Expand Down
4 changes: 4 additions & 0 deletions sdk/src/factory/oracleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export function getOracleClient(
return new PythClient(connection, new BN(1000));
}

if (isVariant(oracleSource, 'pyth1M')) {
return new PythClient(connection, new BN(1000000));
}

// if (isVariant(oracleSource, 'switchboard')) {
// return new SwitchboardClient(connection);
// }
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -6238,7 +6238,10 @@
"name": "Pyth"
},
{
"name": "Pyth1000"
"name": "Pyth1K"
},
{
"name": "Pyth1M"
},
{
"name": "Switchboard"
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export class DepositDirection {

export class OracleSource {
static readonly PYTH = { pyth: {} };
static readonly PYTH_1000 = { pyth1000: {} };
static readonly PYTH_1K = { pyth1K: {} };
static readonly PYTH_1M = { pyth1M: {} };
// static readonly SWITCHBOARD = { switchboard: {} };
static readonly QUOTE_ASSET = { quoteAsset: {} };
}
Expand Down

0 comments on commit 86cf411

Please sign in to comment.