Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Akinbola247/satoru
Browse files Browse the repository at this point in the history
  • Loading branch information
Akinbola247 committed Oct 27, 2023
2 parents 2870356 + 888cc0a commit bbea60c
Show file tree
Hide file tree
Showing 25 changed files with 741 additions and 138 deletions.
29 changes: 28 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,35 @@
"contributions": [
"code"
]
},
{
"login": "JeanWoked",
"name": "JeanWoked",
"avatar_url": "https://avatars.githubusercontent.com/u/149107619?v=4",
"profile": "https://github.com/JeanWoked",
"contributions": [
"code"
]
},
{
"login": "vuittont60",
"name": "vuittont60",
"avatar_url": "https://avatars.githubusercontent.com/u/81072379?v=4",
"profile": "https://github.com/vuittont60",
"contributions": [
"code"
]
},
{
"login": "MavericksFive",
"name": "Arnaud Berger",
"avatar_url": "https://avatars.githubusercontent.com/u/95299145?v=4",
"profile": "https://github.com/MavericksFive",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
"linkToUsage": false
}
}
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on: [push, pull_request]

env:
SCARB_VERSION: 0.7.0
STARKNET_FOUNDRY_VERSION: 0.8.3

jobs:
check:
Expand All @@ -14,6 +13,11 @@ jobs:
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "0.7.0"
- uses: foundry-rs/setup-snfoundry@v1
with:
starknet-foundry-version: 0.8.3
- name: Run cairo tests
run: snforge test
# - name: Set up Scarb
#ses: software-mansion/setup-scarb@v1
# Install Scarb from a nightly release
Expand All @@ -22,7 +26,3 @@ jobs:
# wget https://github.com/software-mansion/scarb-nightlies/releases/download/${NIGHTLY_DATE}/scarb-${NIGHTLY_DATE}-x86_64-unknown-linux-gnu.tar.gz
# tar -xvf scarb-${NIGHTLY_DATE}-x86_64-unknown-linux-gnu.tar.gz
# sudo mv scarb-v${SCARB_VERSION}-x86_64-unknown-linux-gnu/bin/scarb /usr/local/bin
- name: Install starknet foundry
run: curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh -s -- -v ${STARKNET_FOUNDRY_VERSION}
- name: Run cairo tests
run: snforge
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://github.com/VictorONN"><img src="https://avatars.githubusercontent.com/u/73134512?v=4?s=100" width="100px;" alt="VictorONN"/><br /><sub><b>VictorONN</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/satoru/commits?author=VictorONN" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/kasteph"><img src="https://avatars.githubusercontent.com/u/3408478?v=4?s=100" width="100px;" alt="kasteph"/><br /><sub><b>kasteph</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/satoru/commits?author=kasteph" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/khaeljy"><img src="https://avatars.githubusercontent.com/u/1810456?v=4?s=100" width="100px;" alt="Khaeljy"/><br /><sub><b>Khaeljy</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/satoru/commits?author=khaeljy" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/JeanWoked"><img src="https://avatars.githubusercontent.com/u/149107619?v=4?s=100" width="100px;" alt="JeanWoked"/><br /><sub><b>JeanWoked</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/satoru/commits?author=JeanWoked" title="Code">💻</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/vuittont60"><img src="https://avatars.githubusercontent.com/u/81072379?v=4?s=100" width="100px;" alt="vuittont60"/><br /><sub><b>vuittont60</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/satoru/commits?author=vuittont60" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/MavericksFive"><img src="https://avatars.githubusercontent.com/u/95299145?v=4?s=100" width="100px;" alt="Arnaud Berger"/><br /><sub><b>Arnaud Berger</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/satoru/commits?author=MavericksFive" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
6 changes: 3 additions & 3 deletions book/src/continuous-integration/workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,18 @@ on:
- main
env:
SCARB_VERSION: 0.7.0
STARKNET_FOUNDRY_VERSION: 0.8.3
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: foundry-rs/setup-snfoundry@v1
with:
starknet-foundry-version: 0.9.0
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "0.7.0"
- name: Install starknet foundry
run: curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh -s -- -v ${STARKNET_FOUNDRY_VERSION}
- name: Run cairo tests
run: snforge
```
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
2 changes: 1 addition & 1 deletion src/adl/error.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ mod AdlError {
'block_no_smaller_than_required';
const INVALID_SIZE_DELTA_FOR_ADL: felt252 = 'invalid_size_delta_for_adl';
const ADL_NOT_ENABLED: felt252 = 'adl_not_enabled';
const POSTION_NOT_VALID: felt252 = 'position_not_valid';
const POSITION_NOT_VALID: felt252 = 'position_not_valid';
}
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
2 changes: 1 addition & 1 deletion src/exchange/withdrawal_handler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ mod WithdrawalHandler {

global_reentrancy_guard::non_reentrant_before(data_store); // Initiates re-entrancy

let starting_gas = starknet_utils::sn_gasleft(array![100]); // Returns 100 for now,
let starting_gas = starknet_utils::sn_gasleft(array![200]); // Returns 200 for now,
let withdrawal = data_store.get_withdrawal(key);

feature_utils::validate_feature(
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
1 change: 0 additions & 1 deletion src/market/error.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod MarketError {
'empty_addr_market_balance_val';
const EMPTY_ADDRESS_TOKEN_BALANCE_VAL: felt252 = 'empty_addr_token_balance_val';
const INVALID_MARKET_TOKEN_BALANCE: felt252 = 'invalid_market_token_balance';
const EmptyAddressInMarketTokenBalanceValidation: felt252 = 'EmptyAddressMarketBalanceVal';
const INVALID_POSITION_MARKET: felt252 = 'invalid_position_market';
const INVALID_COLLATERAL_TOKEN_FOR_MARKET: felt252 = 'invalid_coll_token_for_market';
const UNABLE_TO_GET_OPPOSITE_TOKEN: felt252 = 'unable_to_get_opposite_token';
Expand Down
15 changes: 8 additions & 7 deletions src/market/market_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use satoru::utils::precision;
use satoru::utils::calc::{roundup_division, to_signed, sum_return_int_128, to_unsigned};
use satoru::position::position::Position;
use integer::u128_to_felt252;
use satoru::utils::{i128::i128, error_utils};
use satoru::utils::{i128::{i128, i128_neg}, error_utils};
use satoru::utils::precision::{apply_exponent_factor, float_to_wei, mul_div};
use satoru::data::keys::{skip_borrowing_fee_for_smaller_side, max_swap_path_length};

Expand Down Expand Up @@ -923,7 +923,7 @@ fn apply_delta_to_open_interest(

if is_long {
apply_delta_to_virtual_inventory_for_positions(
data_store, event_emitter, *market.index_token, -delta
data_store, event_emitter, *market.index_token, i128_neg(delta)
);
} else {
apply_delta_to_virtual_inventory_for_positions(
Expand Down Expand Up @@ -1455,7 +1455,9 @@ fn apply_swap_impact_with_cap(

// if there is a positive impact, the impact pool amount should be reduced
// if there is a negative impact, the impact pool amount should be increased
apply_delta_to_swap_impact_pool(data_store, event_emitter, market, token, -impact_amount);
apply_delta_to_swap_impact_pool(
data_store, event_emitter, market, token, i128_neg(impact_amount)
);

return impact_amount;
}
Expand Down Expand Up @@ -1799,6 +1801,7 @@ fn get_borrowing_fees(data_store: IDataStoreDispatcher, position: @Position) ->
let cumulative_borrowing_factor: u128 = get_cumulative_borrowing_factor(
@data_store, *position.market, *position.is_long
);

if (cumulative_borrowing_factor < *position.borrowing_factor) {
MarketError::UNEXCEPTED_BORROWING_FACTOR(
*position.borrowing_factor, cumulative_borrowing_factor
Expand Down Expand Up @@ -2427,8 +2430,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 @@ -2569,8 +2571,7 @@ 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 = data_store
.get_bool(keys::is_market_disabled_key(market.market_token))
.unwrap();
.get_bool(keys::is_market_disabled_key(market.market_token));

if (is_market_disabled) {
MarketError::DISABLED_MARKET(is_market_disabled);
Expand Down
2 changes: 1 addition & 1 deletion src/order/base_order_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ fn get_execution_price_for_decrease(
let adjusted_price_impact_usd = if is_long {
price_impact_usd
} else {
-price_impact_usd
i128_neg(price_impact_usd)
};

if adjusted_price_impact_usd < Zeroable::zero()
Expand Down
2 changes: 1 addition & 1 deletion src/order/error.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod OrderError {
const EMPTY_SIZE_DELTA_IN_TOKENS: felt252 = 'empty_size_delta_in_tokens';
const UNEXPECTED_MARKET: felt252 = 'unexpected market';
const INVALID_SIZE_DELTA_FOR_ADL: felt252 = 'invalid_size_delta_for_adl';
const POSTION_NOT_VALID: felt252 = 'position_not_valid';
const POSITION_NOT_VALID: felt252 = 'position_not_valid';
const ORDER_ALREADY_FROZEN: felt252 = 'order_already_frozen';


Expand Down
2 changes: 1 addition & 1 deletion src/position/increase_position_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fn increase_position(mut params: UpdatePositionParams, collateral_increment_amou
params.contracts.data_store,
params.contracts.event_emitter,
params.market.market_token,
-cache.price_impact_amount
i128_neg(cache.price_impact_amount)
);

cache.next_position_size_in_usd = params.position.size_in_usd + params.order.size_delta_usd;
Expand Down
6 changes: 4 additions & 2 deletions src/position/position_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ fn is_position_liquiditable(
market_utils::get_cached_token_price(position.collateral_token, market, prices);

cache.collateral_usd = position.collateral_amount * cache.collateral_token_price.min;

// calculate the usdDeltaForPriceImpact for fully closing the position
cache.usd_delta_for_price_impact = calc::to_signed(position.size_in_usd, false);
cache
Expand Down Expand Up @@ -543,6 +544,7 @@ fn is_position_liquiditable(
precision::apply_factor_u128(position.size_in_usd, cache.min_collateral_factor),
true
);

if cache.remaining_collateral_usd <= cache.min_collateral_usd_for_leverage {
return (true, 'min collateral for leverage');
}
Expand Down Expand Up @@ -728,10 +730,10 @@ fn update_open_interest(
size_delta_usd
);

market_utils::apply_delta_to_open_interest(
market_utils::apply_delta_to_open_interest_in_tokens(
params.contracts.data_store,
params.contracts.event_emitter,
@params.market,
params.market,
params.position.collateral_token,
params.position.is_long,
size_delta_in_tokens
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
2 changes: 1 addition & 1 deletion src/reader/reader_pricing_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn get_execution_price(
let size_delta_usd_abs = if size_delta_usd > Zeroable::zero() {
size_delta_usd
} else {
-size_delta_usd
i128_neg(size_delta_usd)
};
params.order.size_delta_usd = calc::to_unsigned(size_delta_usd_abs);
params.order.is_long = is_long;
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
Loading

0 comments on commit bbea60c

Please sign in to comment.