Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 7b2cec9

Browse files
committed
Add EMA support for custom oracle
1 parent 2bf9971 commit 7b2cec9

File tree

8 files changed

+20
-3
lines changed

8 files changed

+20
-3
lines changed

programs/perpetuals/src/instructions/set_custom_oracle_price.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub struct SetCustomOraclePriceParams {
6464
pub price: u64,
6565
pub expo: i32,
6666
pub conf: u64,
67+
pub ema: u64,
6768
pub publish_time: i64,
6869
}
6970

@@ -92,6 +93,7 @@ pub fn set_custom_oracle_price<'info>(
9293
oracle_account.price = params.price;
9394
oracle_account.expo = params.expo;
9495
oracle_account.conf = params.conf;
96+
oracle_account.ema = params.ema;
9597
oracle_account.publish_time = params.publish_time;
9698

9799
Ok(0)

programs/perpetuals/src/state/oracle.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct CustomOracle {
4343
pub price: u64,
4444
pub expo: i32,
4545
pub conf: u64,
46+
pub ema: u64,
4647
pub publish_time: i64,
4748
}
4849

@@ -94,6 +95,7 @@ impl OraclePrice {
9495
oracle_params.max_price_error,
9596
oracle_params.max_price_age_sec,
9697
current_time,
98+
use_ema,
9799
),
98100
OracleType::Pyth => Self::get_pyth_price(
99101
oracle_account,
@@ -231,6 +233,7 @@ impl OraclePrice {
231233
max_price_error: u64,
232234
max_price_age_sec: u32,
233235
current_time: i64,
236+
use_ema: bool,
234237
) -> Result<OraclePrice> {
235238
require!(
236239
!Perpetuals::is_empty_account(custom_price_info)?,
@@ -244,11 +247,16 @@ impl OraclePrice {
244247
msg!("Error: Custom oracle price is stale");
245248
return err!(PerpetualsError::StaleOraclePrice);
246249
}
250+
let price = if use_ema {
251+
oracle_acc.ema
252+
} else {
253+
oracle_acc.price
254+
};
247255

248-
if oracle_acc.price == 0
256+
if price == 0
249257
|| math::checked_div(
250258
math::checked_mul(oracle_acc.conf as u128, Perpetuals::BPS_POWER)?,
251-
oracle_acc.price as u128,
259+
price as u128,
252260
)? > max_price_error as u128
253261
{
254262
msg!("Error: Custom oracle price is out of bounds");
@@ -257,7 +265,7 @@ impl OraclePrice {
257265

258266
Ok(OraclePrice {
259267
// price is i64 and > 0 per check above
260-
price: oracle_acc.price,
268+
price,
261269
exponent: oracle_acc.expo,
262270
})
263271
}

programs/perpetuals/tests/anchor/basic.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ describe("perpetuals", () => {
396396
price: new BN(123000),
397397
expo: -3,
398398
conf: new BN(0),
399+
ema: new BN(123000),
399400
publishTime: oracle.publishTime,
400401
};
401402
expect(JSON.stringify(oracle)).to.equal(JSON.stringify(oracleExpected));

programs/perpetuals/tests/anchor/test_client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ export class TestClient {
676676
price: new BN(price * 1000),
677677
expo: -3,
678678
conf: new BN(0),
679+
ema: new BN(price * 1000),
679680
publishTime: new BN(this.getTime()),
680681
})
681682
.accounts({

programs/perpetuals/tests/native/tests_suite/lp_token/lp_token_price.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub async fn lp_token_price() {
108108
price: utils::scale(1_650, ETH_DECIMALS),
109109
expo: -(ETH_DECIMALS as i32),
110110
conf: utils::scale(10, ETH_DECIMALS),
111+
ema: utils::scale(1_650, ETH_DECIMALS),
111112
publish_time,
112113
},
113114
&multisig_signers,
@@ -151,6 +152,7 @@ pub async fn lp_token_price() {
151152
price: utils::scale(1_320, ETH_DECIMALS),
152153
expo: -(ETH_DECIMALS as i32),
153154
conf: utils::scale(10, ETH_DECIMALS),
155+
ema: utils::scale(1_320, ETH_DECIMALS),
154156
publish_time,
155157
},
156158
&multisig_signers,

programs/perpetuals/tests/native/tests_suite/position/liquidate_position.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ pub async fn liquidate_position() {
151151
price: utils::scale(1_350, ETH_DECIMALS),
152152
expo: -(ETH_DECIMALS as i32),
153153
conf: utils::scale(10, ETH_DECIMALS),
154+
ema: utils::scale(1_350, ETH_DECIMALS),
154155
publish_time,
155156
},
156157
&multisig_signers,

programs/perpetuals/tests/native/tests_suite/position/max_user_profit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ pub async fn max_user_profit() {
133133
price: utils::scale(3_000, ETH_DECIMALS),
134134
expo: -(ETH_DECIMALS as i32),
135135
conf: utils::scale(10, ETH_DECIMALS),
136+
ema: utils::scale(3_000, ETH_DECIMALS),
136137
publish_time,
137138
},
138139
&multisig_signers,

programs/perpetuals/tests/native/utils/test_setup.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ impl TestSetup {
348348
price: custody_param.setup_custody_params.initial_price,
349349
expo: -(mint_info.decimals as i32),
350350
conf: custody_param.setup_custody_params.initial_conf,
351+
ema: custody_param.setup_custody_params.initial_price,
351352
publish_time,
352353
},
353354
&multisig_signers,

0 commit comments

Comments
 (0)