-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pragma price sanity and staleness check (#64)
- Loading branch information
1 parent
dce146e
commit 10dfb3d
Showing
6 changed files
with
172 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
#[cfg(test)] | ||
mod default_interest_rate_model; | ||
|
||
#[cfg(test)] | ||
mod pragma_oracle_adapter; | ||
|
||
#[cfg(test)] | ||
mod z_token; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#[starknet::contract] | ||
mod MockPragmaOracle { | ||
use starknet::ContractAddress; | ||
|
||
use zklend::interfaces::{IPragmaOracle, PragmaOracleSpotMedian}; | ||
|
||
use super::super::IMockPragmaOracle; | ||
|
||
#[storage] | ||
struct Storage { | ||
pair_id: felt252, | ||
price: felt252, | ||
decimals: felt252, | ||
last_updated_timestamp: felt252, | ||
num_sources_aggregated: felt252 | ||
} | ||
|
||
#[external(v0)] | ||
impl IPragmaOracleImpl of IPragmaOracle<ContractState> { | ||
fn get_spot_median(self: @ContractState, pair_id: felt252) -> PragmaOracleSpotMedian { | ||
PragmaOracleSpotMedian { | ||
price: self.price.read(), | ||
decimals: self.decimals.read(), | ||
last_updated_timestamp: self.last_updated_timestamp.read(), | ||
num_sources_aggregated: self.num_sources_aggregated.read() | ||
} | ||
} | ||
} | ||
|
||
#[external(v0)] | ||
impl IMockPragmaOracleImpl of IMockPragmaOracle<ContractState> { | ||
fn set_price( | ||
ref self: ContractState, | ||
pair_id: felt252, | ||
price: felt252, | ||
decimals: felt252, | ||
last_updated_timestamp: felt252, | ||
num_sources_aggregated: felt252 | ||
) { | ||
self.pair_id.write(pair_id); | ||
self.price.write(price); | ||
self.decimals.write(decimals); | ||
self.last_updated_timestamp.write(last_updated_timestamp); | ||
self.num_sources_aggregated.write(num_sources_aggregated); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use test::test_utils::assert_eq; | ||
|
||
use zklend::interfaces::IPriceOracleSourceDispatcherTrait; | ||
|
||
use tests::deploy; | ||
use tests::mock::IMockPragmaOracleDispatcherTrait; | ||
|
||
#[test] | ||
#[available_gas(30000000)] | ||
fn test_not_staled_price() { | ||
let mock_pragma_oracle = deploy::deploy_mock_pragma_oracle(); | ||
let pragma_oracle_adpater = deploy::deploy_pragma_oracle_adapter( | ||
mock_pragma_oracle.contract_address, 'BTC/USD', 500 | ||
); | ||
|
||
// Set last update timestamp to 100 | ||
mock_pragma_oracle.set_price('BTC/USD', 10000_00000000, 8, 100, 5); | ||
|
||
// Current block time is 0. It's okay for the updated time to be in the future. | ||
pragma_oracle_adpater.get_price(); | ||
|
||
// It's still acceptable when the time elasped equals timeout. | ||
starknet::testing::set_block_timestamp(600); | ||
pragma_oracle_adpater.get_price(); | ||
} | ||
|
||
#[test] | ||
#[available_gas(30000000)] | ||
#[should_panic(expected: ('PRAGMA_STALED_PRICE', 'ENTRYPOINT_FAILED'))] | ||
fn test_staled_price() { | ||
let mock_pragma_oracle = deploy::deploy_mock_pragma_oracle(); | ||
let pragma_oracle_adpater = deploy::deploy_pragma_oracle_adapter( | ||
mock_pragma_oracle.contract_address, 'BTC/USD', 500 | ||
); | ||
|
||
// Set last update timestamp to 100 | ||
mock_pragma_oracle.set_price('BTC/USD', 10000_00000000, 8, 100, 5); | ||
|
||
// One second over timeout will be rejected. | ||
starknet::testing::set_block_timestamp(601); | ||
pragma_oracle_adpater.get_price(); | ||
} |