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

Commit 6c1bb34

Browse files
authored
Merge pull request #8 from askibin/custom
Rename test oracle to custom
2 parents 83fd610 + e44e604 commit 6c1bb34

File tree

18 files changed

+81
-77
lines changed

18 files changed

+81
-77
lines changed

app/src/client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ export class PerpetualsClient {
132132
return (await this.getCustody(poolName, tokenMint)).oracle.oracleAccount;
133133
};
134134

135-
getCustodyTestOracleAccountKey = (poolName: string, tokenMint: PublicKey) => {
135+
getCustodyCustomOracleAccountKey = (
136+
poolName: string,
137+
tokenMint: PublicKey
138+
) => {
136139
return this.findProgramAddress("oracle_account", [
137140
this.getPoolKey(poolName),
138141
tokenMint,

programs/perpetuals/src/instructions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ pub mod remove_custody;
66
pub mod remove_pool;
77
pub mod set_admin_signers;
88
pub mod set_custody_config;
9+
pub mod set_custom_oracle_price;
910
pub mod set_permissions;
1011
pub mod upgrade_custody;
1112
pub mod withdraw_fees;
1213
pub mod withdraw_sol_fees;
1314

1415
// test instructions
15-
pub mod set_test_oracle_price;
1616
pub mod set_test_time;
1717
pub mod test_init;
1818

@@ -45,7 +45,7 @@ pub use {
4545
get_liquidation_state::*, get_lp_token_price::*, get_oracle_price::*, get_pnl::*,
4646
get_remove_liquidity_amount_and_fee::*, get_swap_amount_and_fees::*, init::*, liquidate::*,
4747
open_position::*, remove_collateral::*, remove_custody::*, remove_liquidity::*, remove_pool::*,
48-
set_admin_signers::*, set_custody_config::*, set_permissions::*, set_test_oracle_price::*,
48+
set_admin_signers::*, set_custody_config::*, set_custom_oracle_price::*, set_permissions::*,
4949
set_test_time::*, swap::*, test_init::*, upgrade_custody::*, withdraw_fees::*,
5050
withdraw_sol_fees::*,
5151
};

programs/perpetuals/src/instructions/set_test_oracle_price.rs renamed to programs/perpetuals/src/instructions/set_custom_oracle_price.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
//! SetTestOraclePrice instruction handler
1+
//! SetCustomOraclePrice instruction handler
22
33
use {
44
crate::state::{
55
custody::Custody,
66
multisig::{AdminInstruction, Multisig},
7-
oracle::TestOracle,
7+
oracle::CustomOracle,
88
perpetuals::Perpetuals,
99
pool::Pool,
1010
},
1111
anchor_lang::prelude::*,
1212
};
1313

1414
#[derive(Accounts)]
15-
pub struct SetTestOraclePrice<'info> {
15+
pub struct SetCustomOraclePrice<'info> {
1616
#[account(mut)]
1717
pub admin: Signer<'info>,
1818

@@ -47,37 +47,37 @@ pub struct SetTestOraclePrice<'info> {
4747
#[account(
4848
init_if_needed,
4949
payer = admin,
50-
space = TestOracle::LEN,
50+
space = CustomOracle::LEN,
5151
//constraint = oracle_account.key() == custody.oracle.oracle_account,
5252
seeds = [b"oracle_account",
5353
pool.key().as_ref(),
5454
custody.mint.as_ref()],
5555
bump
5656
)]
57-
pub oracle_account: Box<Account<'info, TestOracle>>,
57+
pub oracle_account: Box<Account<'info, CustomOracle>>,
5858

5959
system_program: Program<'info, System>,
6060
}
6161

6262
#[derive(AnchorSerialize, AnchorDeserialize, Copy, Clone)]
63-
pub struct SetTestOraclePriceParams {
63+
pub struct SetCustomOraclePriceParams {
6464
pub price: u64,
6565
pub expo: i32,
6666
pub conf: u64,
6767
pub publish_time: i64,
6868
}
6969

70-
pub fn set_test_oracle_price<'info>(
71-
ctx: Context<'_, '_, '_, 'info, SetTestOraclePrice<'info>>,
72-
params: &SetTestOraclePriceParams,
70+
pub fn set_custom_oracle_price<'info>(
71+
ctx: Context<'_, '_, '_, 'info, SetCustomOraclePrice<'info>>,
72+
params: &SetCustomOraclePriceParams,
7373
) -> Result<u8> {
7474
// validate signatures
7575
let mut multisig = ctx.accounts.multisig.load_mut()?;
7676

7777
let signatures_left = multisig.sign_multisig(
7878
&ctx.accounts.admin,
7979
&Multisig::get_account_infos(&ctx)[1..],
80-
&Multisig::get_instruction_data(AdminInstruction::SetTestOraclePrice, params)?,
80+
&Multisig::get_instruction_data(AdminInstruction::SetCustomOraclePrice, params)?,
8181
)?;
8282
if signatures_left > 0 {
8383
msg!(

programs/perpetuals/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ pub mod perpetuals {
105105
instructions::upgrade_custody(ctx, &params)
106106
}
107107

108+
pub fn set_custom_oracle_price<'info>(
109+
ctx: Context<'_, '_, '_, 'info, SetCustomOraclePrice<'info>>,
110+
params: SetCustomOraclePriceParams,
111+
) -> Result<u8> {
112+
instructions::set_custom_oracle_price(ctx, &params)
113+
}
114+
108115
// test instructions
109116

110117
pub fn test_init(ctx: Context<TestInit>, params: TestInitParams) -> Result<()> {
111118
instructions::test_init(ctx, &params)
112119
}
113120

114-
pub fn set_test_oracle_price<'info>(
115-
ctx: Context<'_, '_, '_, 'info, SetTestOraclePrice<'info>>,
116-
params: SetTestOraclePriceParams,
117-
) -> Result<u8> {
118-
instructions::set_test_oracle_price(ctx, &params)
119-
}
120-
121121
pub fn set_test_time<'info>(
122122
ctx: Context<'_, '_, '_, 'info, SetTestTime<'info>>,
123123
params: SetTestTimeParams,

programs/perpetuals/src/state/multisig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub enum AdminInstruction {
3333
SetBorrowRate,
3434
WithdrawFees,
3535
WithdrawSolFees,
36-
SetTestOraclePrice,
36+
SetCustomOraclePrice,
3737
SetTestTime,
3838
UpgradeCustody,
3939
}

programs/perpetuals/src/state/oracle.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const ORACLE_MAX_PRICE: u64 = (1 << 28) - 1;
1313
#[derive(Copy, Clone, PartialEq, AnchorSerialize, AnchorDeserialize, Debug)]
1414
pub enum OracleType {
1515
None,
16-
Test,
16+
Custom,
1717
Pyth,
1818
}
1919

@@ -39,15 +39,15 @@ pub struct OracleParams {
3939

4040
#[account]
4141
#[derive(Default, Debug)]
42-
pub struct TestOracle {
42+
pub struct CustomOracle {
4343
pub price: u64,
4444
pub expo: i32,
4545
pub conf: u64,
4646
pub publish_time: i64,
4747
}
4848

49-
impl TestOracle {
50-
pub const LEN: usize = 8 + std::mem::size_of::<TestOracle>();
49+
impl CustomOracle {
50+
pub const LEN: usize = 8 + std::mem::size_of::<CustomOracle>();
5151
}
5252

5353
impl PartialOrd for OraclePrice {
@@ -89,7 +89,7 @@ impl OraclePrice {
8989
use_ema: bool,
9090
) -> Result<Self> {
9191
match oracle_params.oracle_type {
92-
OracleType::Test => Self::get_test_price(
92+
OracleType::Custom => Self::get_custom_price(
9393
oracle_account,
9494
oracle_params.max_price_error,
9595
oracle_params.max_price_age_sec,
@@ -226,22 +226,22 @@ impl OraclePrice {
226226
}
227227

228228
// private helpers
229-
fn get_test_price(
230-
test_price_info: &AccountInfo,
229+
fn get_custom_price(
230+
custom_price_info: &AccountInfo,
231231
max_price_error: u64,
232232
max_price_age_sec: u32,
233233
current_time: i64,
234234
) -> Result<OraclePrice> {
235235
require!(
236-
!Perpetuals::is_empty_account(test_price_info)?,
236+
!Perpetuals::is_empty_account(custom_price_info)?,
237237
PerpetualsError::InvalidOracleAccount
238238
);
239239

240-
let oracle_acc = Account::<TestOracle>::try_from(test_price_info)?;
240+
let oracle_acc = Account::<CustomOracle>::try_from(custom_price_info)?;
241241

242242
let last_update_age_sec = math::checked_sub(current_time, oracle_acc.publish_time)?;
243243
if last_update_age_sec > max_price_age_sec as i64 {
244-
msg!("Error: Test oracle price is stale");
244+
msg!("Error: Custom oracle price is stale");
245245
return err!(PerpetualsError::StaleOraclePrice);
246246
}
247247

@@ -251,7 +251,7 @@ impl OraclePrice {
251251
oracle_acc.price as u128,
252252
)? > max_price_error as u128
253253
{
254-
msg!("Error: Test oracle price is out of bounds");
254+
msg!("Error: Custom oracle price is out of bounds");
255255
return err!(PerpetualsError::InvalidOraclePrice);
256256
}
257257

programs/perpetuals/src/state/pool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ mod test {
10461046

10471047
let oracle = OracleParams {
10481048
oracle_account: Pubkey::default(),
1049-
oracle_type: OracleType::Test,
1049+
oracle_type: OracleType::Custom,
10501050
max_price_error: 100,
10511051
max_price_age_sec: 1,
10521052
};

programs/perpetuals/tests/anchor/basic.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe("perpetuals", () => {
133133
oracleConfig = {
134134
maxPriceError: new BN(10000),
135135
maxPriceAgeSec: 60,
136-
oracleType: { test: {} },
136+
oracleType: { custom: {} },
137137
oracleAccount: tc.custodies[0].oracleAccount,
138138
};
139139
pricing = {
@@ -224,7 +224,7 @@ describe("perpetuals", () => {
224224
isVirtual,
225225
oracle: {
226226
oracleAccount: tc.custodies[0].oracleAccount,
227-
oracleType: { test: {} },
227+
oracleType: { custom: {} },
228228
maxPriceError: "10000",
229229
maxPriceAgeSec: 60,
230230
},
@@ -385,11 +385,11 @@ describe("perpetuals", () => {
385385
expect(JSON.stringify(token)).to.equal(JSON.stringify(tokenExpected));
386386
});
387387

388-
it("setTestOraclePrice", async () => {
389-
await tc.setTestOraclePrice(123, tc.custodies[0]);
390-
await tc.setTestOraclePrice(200, tc.custodies[1]);
388+
it("setCustomOraclePrice", async () => {
389+
await tc.setCustomOraclePrice(123, tc.custodies[0]);
390+
await tc.setCustomOraclePrice(200, tc.custodies[1]);
391391

392-
let oracle = await tc.program.account.testOracle.fetch(
392+
let oracle = await tc.program.account.customOracle.fetch(
393393
tc.custodies[0].oracleAccount
394394
);
395395
let oracleExpected = {
@@ -539,7 +539,7 @@ describe("perpetuals", () => {
539539
tc.users[0].positionAccountsLong[0],
540540
tc.custodies[0]
541541
);
542-
await tc.setTestOraclePrice(80, tc.custodies[0]);
542+
await tc.setCustomOraclePrice(80, tc.custodies[0]);
543543
await tc.liquidate(
544544
tc.users[0],
545545
tc.users[0].tokenAccounts[0],

programs/perpetuals/tests/anchor/test_client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,14 +665,14 @@ export class TestClient {
665665
}
666666
};
667667

668-
setTestOraclePrice = async (price: number, custody) => {
668+
setCustomOraclePrice = async (price: number, custody) => {
669669
let multisig = await this.program.account.multisig.fetch(
670670
this.multisig.publicKey
671671
);
672672
for (let i = 0; i < multisig.minSignatures; ++i) {
673673
try {
674674
await this.program.methods
675-
.setTestOraclePrice({
675+
.setCustomOraclePrice({
676676
price: new BN(price * 1000),
677677
expo: -3,
678678
conf: new BN(0),

programs/perpetuals/tests/native/instructions/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ pub mod test_liquidate;
88
pub mod test_open_position;
99
pub mod test_remove_liquidity;
1010
pub mod test_set_custody_config;
11-
pub mod test_set_test_oracle_price;
11+
pub mod test_set_custom_oracle_price;
1212
pub mod test_swap;
1313

1414
pub use {
1515
test_add_custody::*, test_add_liquidity::*, test_add_pool::*, test_close_position::*,
1616
test_get_lp_token_price::*, test_init::*, test_liquidate::*, test_open_position::*,
17-
test_remove_liquidity::*, test_set_custody_config::*, test_set_test_oracle_price::*,
17+
test_remove_liquidity::*, test_set_custody_config::*, test_set_custom_oracle_price::*,
1818
test_swap::*,
1919
};

0 commit comments

Comments
 (0)