Skip to content

Commit

Permalink
Feat/add pragma lib (#667)
Browse files Browse the repository at this point in the history
* refactor integration tests

* added pragma lib
  • Loading branch information
sparqet authored Jun 18, 2024
1 parent 3f3a1c0 commit 56d39cd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
6 changes: 6 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ name = "alexandria_storage"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?tag=cairo-v2.3.0-rc0#ae1d5149ff601a7ac5b39edc867d33ebd83d7f4f"

[[package]]
name = "pragma_lib"
version = "1.0.0"
source = "git+https://github.com/astraly-labs/pragma-lib#fe9a46743254182ac331da488ccfc05e0185cdd0"

[[package]]
name = "satoru"
version = "0.1.0"
Expand All @@ -43,6 +48,7 @@ dependencies = [
"alexandria_math",
"alexandria_sorting",
"alexandria_storage",
"pragma_lib",
"snforge_std",
]

Expand Down
1 change: 1 addition & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.g
alexandria_storage = { git = "https://github.com/keep-starknet-strange/alexandria.git", tag = "cairo-v2.3.0-rc0" }
alexandria_sorting = { git = "https://github.com/keep-starknet-strange/alexandria.git", tag = "cairo-v2.3.0-rc0" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.9.1" }
pragma_lib = { git = "https://github.com/astraly-labs/pragma-lib" }


[tool.snforge]
Expand Down
3 changes: 0 additions & 3 deletions src/exchange/liquidation_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ mod LiquidationHandler {

use starknet::{ContractAddress, get_caller_address, get_contract_address, ClassHash};

use debug::PrintTrait;

// Local imports.
use super::ILiquidationHandler;
use satoru::role::role_store::{IRoleStoreSafeDispatcher, IRoleStoreSafeDispatcherTrait};
Expand Down Expand Up @@ -191,7 +189,6 @@ mod LiquidationHandler {
params.contracts.data_store,
execute_order_feature_disabled_key(get_contract_address(), params.order.order_type)
);
'pass everything'.print();
state_base.order_utils_lib.read().execute_order_utils(params);
// with_oracle_prices_after(state_base.oracle.read());

Expand Down
26 changes: 23 additions & 3 deletions src/oracle/oracle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use satoru::oracle::{
oracle_utils::{SetPricesParams, ReportInfo}, error::OracleError,
};
use satoru::price::price::Price;
use pragma_lib::types::DataType;


// *************************************************************************
// Interface of the `Oracle` contract.
Expand All @@ -46,6 +48,10 @@ trait IOracle<TContractState> {
/// The primary price of a token.
fn get_primary_price(self: @TContractState, token: ContractAddress) -> Price;

fn get_asset_price_median(
self: @TContractState, oracle_address: ContractAddress, asset: DataType
) -> u128;

fn set_primary_price(ref self: TContractState, token: ContractAddress, price: u256);

fn set_price_testing_eth(ref self: TContractState, new_price: u256);
Expand Down Expand Up @@ -131,16 +137,16 @@ mod Oracle {
use satoru::oracle::{
oracle_store::{IOracleStoreDispatcher, IOracleStoreDispatcherTrait}, oracle_utils,
oracle_utils::{SetPricesParams, ReportInfo}, error::OracleError,
price_feed::{
IPriceFeedDispatcher, IPriceFeedDispatcherTrait, DataType, PragmaPricesResponse,
}
price_feed::{IPriceFeedDispatcher, IPriceFeedDispatcherTrait}
};
use satoru::role::role_module::{
IRoleModule, RoleModule
}; //::role_store::IInternalContractMemberStateTrait as RoleModuleStateTrait;
use satoru::role::role_store::{IRoleStoreDispatcher, IRoleStoreDispatcherTrait};
use satoru::utils::{arrays, arrays::pow, bits, calc, precision};
use satoru::utils::u256_mask::{Mask, MaskTrait, validate_unique_and_set_index};
use pragma_lib::abi::{IPragmaABIDispatcher, IPragmaABIDispatcherTrait};
use pragma_lib::types::{AggregationMode, DataType, PragmaPricesResponse};

use super::{IOracle, SetPricesCache, SetPricesInnerCache, ValidatedPrice};

Expand Down Expand Up @@ -239,6 +245,20 @@ mod Oracle {
// TODO add security check keeper
self.primary_prices.write(token, Price { min: price, max: price });
}

fn get_asset_price_median(
self: @ContractState, oracle_address: ContractAddress, asset: DataType
) -> u128 {
let oracle_dispatcher = IPragmaABIDispatcher { contract_address: oracle_address };
let output: PragmaPricesResponse = oracle_dispatcher
.get_data(asset, AggregationMode::Median(()));
return output.price;
}
//USAGE
// let KEY :felt252 = 18669995996566340; // felt252 conversion of "BTC/USD", can also write const KEY : felt252 = 'BTC/USD';
// Sepolia contract address : 0x36031daa264c24520b11d93af622c848b2499b66b41d611bac95e13cfca131a
// let oracle_address : ContractAddress = contract_address_const::<0x06df335982dddce41008e4c03f2546fa27276567b5274c7d0c1262f3c2b5d167>();
// let price = get_asset_price_median(oracle_address, DataType::SpotEntry(KEY));
}

// *************************************************************************
Expand Down
5 changes: 1 addition & 4 deletions src/position/decrease_position_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,7 @@ fn decrease_position(mut params: UpdatePositionParams) -> DecreasePositionResult
params, cache.next_position_size_in_usd, cache.next_position_borrowing_factor
);
params.position.size_in_usd = cache.next_position_size_in_usd;
params
.position
.size_in_tokens -= values
.size_delta_in_tokens;
params.position.size_in_tokens -= values.size_delta_in_tokens;
params.position.collateral_amount = values.remaining_collateral_amount;
params.position.decreased_at_block = starknet::info::get_block_number();

Expand Down

0 comments on commit 56d39cd

Please sign in to comment.