Skip to content

Commit

Permalink
feat: additional tests + corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyRo1 committed Dec 2, 2024
1 parent b81035c commit 18a8b82
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 24 deletions.
23 changes: 2 additions & 21 deletions pragma-oracle/src/oracle/oracle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ trait IOracleABI<TContractState> {
fn register_tokenized_vault(
ref self: TContractState, token: felt252, token_address: ContractAddress
);
fn delete_tokenized_vault(ref self: TContractState, token: felt252);
fn upgrade(ref self: TContractState, impl_hash: ClassHash);
}

Expand Down Expand Up @@ -203,6 +202,7 @@ mod Oracle {
use option::OptionTrait;
const BACKWARD_TIMESTAMP_BUFFER: u64 = 3600; // 1 hour
const FORWARD_TIMESTAMP_BUFFER: u64 = 420; // 7 minutes
const ONE_E18: u256 = 1000000000000000000;


#[storage]
Expand Down Expand Up @@ -557,9 +557,7 @@ mod Oracle {
// Compute adjusted price
// `preview_mint` takes as argument an e18 and returns an e18
// We operate under u256 to avoid overflow
let price: u256 = response.price.into()
* pool.preview_mint(1000000000000000000)
/ 1000000000000000000;
let price: u256 = response.price.into() * pool.preview_mint(ONE_E18) / ONE_E18;

// The conversion should not fail because we scaled the price to response.decimals
let converted_price: u128 = price.try_into().expect('Conversion should not fail');
Expand Down Expand Up @@ -1835,26 +1833,9 @@ mod Oracle {
) {
OracleInternal::assert_only_admin();
assert(token != 0, 'Token cannot be 0');
assert(
token_address != starknet::contract_address_const::<0>(),
'Token address cannot be 0'
);
self.tokenized_vault.write((token, 'STRK'), token_address)
}

// @notice delete a registered tokenized vault
// @dev Callable only by the admin
// @param token The token to be removed
fn delete_tokenized_vault(ref self: ContractState, token: felt252) {
OracleInternal::assert_only_admin();
assert(token != 0, 'Token cannot be 0');

let token_address = self.tokenized_vault.read((token, 'STRK'));
assert(token_address != starknet::contract_address_const::<0>(), 'Already deleted');
self.tokenized_vault.write((token, 'STRK'), starknet::contract_address_const::<0>());
}


// @notice set a new checkpoint for a given data type and and aggregation mode
// @param data_type: an enum of DataType (e.g : DataType::SpotEntry(ASSET_ID) or DataType::FutureEntry((ASSSET_ID, expiration_timestamp)))
// @param aggregation_mode: the aggregation method to be used
Expand Down
95 changes: 92 additions & 3 deletions pragma-oracle/src/tests/test_oracle.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use array::{ArrayTrait, SpanTrait};
use option::OptionTrait;
use result::ResultTrait;
use integer::BoundedInt;
use starknet::ContractAddress;
use pragma::entry::structs::{
BaseEntry, SpotEntry, Currency, Pair, DataType, PragmaPricesResponse, Checkpoint,
Expand Down Expand Up @@ -1404,7 +1405,7 @@ fn test_delete_tokenized_vault() {
set_contract_address(admin);
oracle.register_tokenized_vault(token, token_address);
assert(oracle.get_tokenized_vaults(token) == token_address, 'Failed to register token');
oracle.delete_tokenized_vault(token);
oracle.register_tokenized_vault(token, contract_address_const::<0>());
assert(
oracle.get_tokenized_vaults(token) == contract_address_const::<0>(),
'Failed to delete token'
Expand Down Expand Up @@ -1435,7 +1436,7 @@ fn test_get_conversion_rate_price() {
.add_currency(
Currency {
id: 'STRK',
decimals: 8,
decimals: 18,
is_abstract_currency: false,
starknet_address: 0.try_into().unwrap(),
ethereum_address: 0.try_into().unwrap(),
Expand All @@ -1445,7 +1446,7 @@ fn test_get_conversion_rate_price() {
.add_currency(
Currency {
id: 'xSTRK',
decimals: 8,
decimals: 18,
is_abstract_currency: false,
starknet_address: 0.try_into().unwrap(),
ethereum_address: 0.try_into().unwrap(),
Expand All @@ -1471,3 +1472,91 @@ fn test_get_conversion_rate_price() {
res.price == (68250000 * 1002465544733197129) / 1000000000000000000, 'Computation failed'
);
}

#[test]
#[should_panic(expected: ('No pool address for given token', 'ENTRYPOINT_FAILED'))]
#[available_gas(20000000000000)]
fn test_get_conversion_rate_price_fails_if_pool_address_not_given() {
let now = 100000;
let (publisher_registry, oracle) = setup();
let admin = contract_address_const::<0x123456789>();
set_contract_address(admin);
oracle
.add_currency(
Currency {
id: 'STRK',
decimals: 18,
is_abstract_currency: false,
starknet_address: 0.try_into().unwrap(),
ethereum_address: 0.try_into().unwrap(),
}
);
oracle
.add_currency(
Currency {
id: 'xSTRK',
decimals: 18,
is_abstract_currency: false,
starknet_address: 0.try_into().unwrap(),
ethereum_address: 0.try_into().unwrap(),
}
);
oracle.add_pair(Pair { id: 'STRK/USD', base_currency_id: 'STRK', quote_currency_id: 'USD', });
oracle.add_pair(Pair { id: 'xSTRK/USD', base_currency_id: 'xSTRK', quote_currency_id: 'USD', });
oracle
.publish_data(
PossibleEntries::Spot(
SpotEntry {
base: BaseEntry { timestamp: now, source: 2, publisher: 1 },
pair_id: 'STRK/USD',
price: 68250000,
volume: 0
}
)
);
let res = oracle.get_data(DataType::SpotEntry('xSTRK/USD'), AggregationMode::ConversionRate);
}


#[test]
#[should_panic(expected: ('Asset not registered', 'ENTRYPOINT_FAILED'))]
#[available_gas(20000000000000)]
fn test_get_conversion_rate_price_fails_if_asset_not_registered() {
let now = 100000;
let (publisher_registry, oracle) = setup();
let admin = contract_address_const::<0x123456789>();
set_contract_address(admin);
oracle
.add_currency(
Currency {
id: 'STRK',
decimals: 18,
is_abstract_currency: false,
starknet_address: 0.try_into().unwrap(),
ethereum_address: 0.try_into().unwrap(),
}
);
oracle
.add_currency(
Currency {
id: 'xSTRK',
decimals: 18,
is_abstract_currency: false,
starknet_address: 0.try_into().unwrap(),
ethereum_address: 0.try_into().unwrap(),
}
);
oracle.add_pair(Pair { id: 'STRK/USD', base_currency_id: 'STRK', quote_currency_id: 'USD', });
oracle
.publish_data(
PossibleEntries::Spot(
SpotEntry {
base: BaseEntry { timestamp: now, source: 2, publisher: 1 },
pair_id: 'STRK/USD',
price: 68250000,
volume: 0
}
)
);
let res = oracle.get_data(DataType::SpotEntry('xSTRK/USD'), AggregationMode::ConversionRate);
}

0 comments on commit 18a8b82

Please sign in to comment.