Skip to content

Commit

Permalink
fix option return value in data_store.cairo
Browse files Browse the repository at this point in the history
  • Loading branch information
sparqet committed Oct 26, 2023
1 parent d57ac9c commit 420f6b9
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 39 deletions.
10 changes: 1 addition & 9 deletions src/adl/adl_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,7 @@ fn set_latest_adl_block(
fn get_adl_enabled(
data_store: IDataStoreDispatcher, market: ContractAddress, is_long: bool
) -> bool { // TODO
let result = data_store.get_bool(keys::is_adl_enabled_key(market, is_long));
match result {
Option::Some(data) => {
return data;
},
Option::None => {
return false;
}
}
data_store.get_bool(keys::is_adl_enabled_key(market, is_long))
}

/// Set whether ADL is enabled.
Expand Down
10 changes: 5 additions & 5 deletions src/data/data_store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ trait IDataStore<TContractState> {
/// * `key` - The key to get the value for.
/// # Returns
/// The value for the given key.
fn get_bool(self: @TContractState, key: felt252) -> Option<bool>;
fn get_bool(self: @TContractState, key: felt252) -> bool;

/// Set a bool value for the given key.
/// # Arguments
Expand Down Expand Up @@ -511,7 +511,7 @@ mod DataStore {
u128_values: LegacyMap::<felt252, u128>,
i128_values: LegacyMap::<felt252, i128>,
address_values: LegacyMap::<felt252, ContractAddress>,
bool_values: LegacyMap::<felt252, Option<bool>>,
bool_values: LegacyMap::<felt252, bool>,
/// Market storage
market_values: LegacyMap::<ContractAddress, Market>,
markets: List<Market>,
Expand Down Expand Up @@ -800,22 +800,22 @@ mod DataStore {
// *************************************************************************
// Bool related functions.
// *************************************************************************
fn get_bool(self: @ContractState, key: felt252) -> Option<bool> {
fn get_bool(self: @ContractState, key: felt252) -> bool {
self.bool_values.read(key)
}

fn set_bool(ref self: ContractState, key: felt252, value: bool) {
// Check that the caller has permission to set the value.
self.role_store.read().assert_only_role(get_caller_address(), role::CONTROLLER);
// Set the value.
self.bool_values.write(key, Option::Some(value));
self.bool_values.write(key, value);
}

fn remove_bool(ref self: ContractState, key: felt252) {
// Check that the caller has permission to delete the value.
self.role_store.read().assert_only_role(get_caller_address(), role::CONTROLLER);
// Delete the value.
self.bool_values.write(key, Option::None);
self.bool_values.write(key, false);
}

// *************************************************************************
Expand Down
5 changes: 1 addition & 4 deletions src/feature/feature_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ use satoru::feature::error::FeatureError;
/// # Returns
/// whether the feature is disabled.
fn is_feature_disabled(data_store: IDataStoreDispatcher, key: felt252) -> bool {
match data_store.get_bool(key) {
Option::Some(value) => value,
Option::None => false
}
data_store.get_bool(key)
}

/// Validate whether a feature is enabled, reverts if the feature is disabled.
Expand Down
10 changes: 3 additions & 7 deletions src/market/market_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2429,8 +2429,7 @@ fn get_borrowing_factor_per_second(
// if skipBorrowingFeeForSmallerSide is true, and the longOpenInterest is exactly the same as the shortOpenInterest
// then the borrowing fee would be charged for both sides, this should be very rare
let skip_borrowing_fee_for_smaller_side: bool = data_store
.get_bool(keys::skip_borrowing_fee_for_smaller_side())
.unwrap();
.get_bool(keys::skip_borrowing_fee_for_smaller_side());

let market_snap = @market;
if (skip_borrowing_fee_for_smaller_side) {
Expand Down Expand Up @@ -2570,11 +2569,8 @@ fn validate_enabled_market_check(
fn validate_enabled_market(data_store: IDataStoreDispatcher, market: Market) {
assert(market.market_token != 0.try_into().unwrap(), MarketError::EMPTY_MARKET);

let is_market_disabled: bool =
match data_store.get_bool(keys::is_market_disabled_key(market.market_token)) {
Option::Some(value) => value,
Option::None => false
};
let is_market_disabled: bool = data_store
.get_bool(keys::is_market_disabled_key(market.market_token));

if (is_market_disabled) {
MarketError::DISABLED_MARKET(is_market_disabled);
Expand Down
3 changes: 1 addition & 2 deletions src/reader/reader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,7 @@ mod Reader {
let virtual_inventory = self.get_virtual_inventory(data_store, market);

let is_disabled = data_store
.get_bool(keys::is_market_disabled_key(market.market_token))
.expect('get_bool failed');
.get_bool(keys::is_market_disabled_key(market.market_token));
MarketInfo {
market,
borrowing_factor_per_second_for_longs,
Expand Down
3 changes: 1 addition & 2 deletions src/swap/swap_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ fn swap(params: @SwapParams) -> (ContractAddress, u128) {
}
let market: Market = *params.swap_path_markets[i];
let flag_exists = (*params.data_store)
.get_bool(keys::swap_path_market_flag_key(market.market_token))
.expect('get_bool failed');
.get_bool(keys::swap_path_market_flag_key(market.market_token));
if (flag_exists) {
SwapError::DUPLICATED_MARKET_IN_SWAP_PATH(market.market_token);
}
Expand Down
5 changes: 1 addition & 4 deletions src/utils/global_reentrancy_guard.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ const REENTRANCY_GUARD_STATUS: felt252 = 'REENTRANCY_GUARD_STATUS';
/// Modifier to avoid reentrancy.
fn non_reentrant_before(data_store: IDataStoreDispatcher) {
// Read key from Data Store.
let status = match data_store.get_bool(REENTRANCY_GUARD_STATUS) {
Option::Some(v) => v,
Option::None => false,
};
let status = data_store.get_bool(REENTRANCY_GUARD_STATUS);

// Check if reentrancy is happening.
assert(!status, ReentrancyGuardError::REENTRANT_CALL);
Expand Down
4 changes: 2 additions & 2 deletions tests/data/test_data_store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ fn given_normal_conditions_when_bool_functions_then_expected_results() {
// Safe to unwrap because we know that the key exists and if it doesn't the test should fail.
let value = data_store.get_bool(1);
// Check that the value read is true.
assert(value.unwrap() == true, 'Invalid value');
assert(value == true, 'Invalid value');

// Remove key 1.
data_store.remove_bool(1);
// Check that the key was removed.
assert(data_store.get_bool(1) == Option::None, 'Key was not deleted');
assert(data_store.get_bool(1) == false, 'Key was not deleted');

// *********************************************************************************************
// * TEARDOWN *
Expand Down
8 changes: 4 additions & 4 deletions tests/utils/test_reentrancy_guard.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ fn given_normal_conditions_when_non_reentrancy_before_and_after_then_works() {
let initial_value = data_store.get_bool('REENTRANCY_GUARD_STATUS');

// Initial value should be false.
assert(initial_value.is_none(), 'Initial value wrong');
assert(initial_value == false, 'Initial value wrong');

// Sets value to true
non_reentrant_before(data_store);

// Gets value after non_reentrant_before call
let entrant = data_store.get_bool('REENTRANCY_GUARD_STATUS').unwrap();
let entrant = data_store.get_bool('REENTRANCY_GUARD_STATUS');
assert(entrant, 'Entered value wrong');

non_reentrant_after(data_store); // This should set value false.
// Gets final value
let after: bool = data_store.get_bool('REENTRANCY_GUARD_STATUS').unwrap();
let after: bool = data_store.get_bool('REENTRANCY_GUARD_STATUS');

assert(!after, 'Final value wrong');
}
Expand All @@ -48,7 +48,7 @@ fn given_reentrant_call_when_reentrancy_before_and_after_then_fails() {
non_reentrant_before(data_store);

// Gets value after non_reentrant_before
let entraant: bool = data_store.get_bool('REENTRANCY_GUARD_STATUS').unwrap();
let entraant: bool = data_store.get_bool('REENTRANCY_GUARD_STATUS');
assert(entraant, 'Entered value wrong');

// This should revert, means reentrant call happened.
Expand Down

0 comments on commit 420f6b9

Please sign in to comment.