Skip to content

Commit

Permalink
Merge branch 'main' into Feat--implement-apply_exponent_factor-functi…
Browse files Browse the repository at this point in the history
…on-in-precision
  • Loading branch information
StarkFishinator authored Oct 5, 2023
2 parents 46a85aa + 55c97b4 commit 243629e
Show file tree
Hide file tree
Showing 20 changed files with 1,088 additions and 272 deletions.
12 changes: 12 additions & 0 deletions src/bank/strict_bank.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ trait IStrictBank<TContractState> {
/// # Returns
/// * The new balance.
fn sync_token_balance(ref self: TContractState, token: ContractAddress) -> u128;
/// Records a token transfer into the contract.
/// # Arguments
/// * `token` - The token address to transfer.
/// # Returns
/// * The amount of tokens transferred.
fn record_transfer_in(ref self: TContractState, token: ContractAddress) -> u128;
}

#[starknet::contract]
Expand Down Expand Up @@ -105,6 +111,12 @@ mod StrictBank {
}

fn sync_token_balance(ref self: ContractState, token: ContractAddress) -> u128 {
// TODO
0
}

fn record_transfer_in(ref self: ContractState, token: ContractAddress) -> u128 {
// TODO
0
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/event/event_emitter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ use starknet::{ContractAddress, ClassHash};
// Local imports.
use satoru::deposit::deposit::Deposit;
use satoru::withdrawal::withdrawal::Withdrawal;
use satoru::position::position::Position;
use satoru::market::market_pool_value_info::MarketPoolValueInfo;
use satoru::pricing::swap_pricing_utils::SwapFees;
use satoru::position::position_event_utils::PositionIncreaseParams;
use satoru::position::position_utils::DecreasePositionCollateralValues;
use satoru::order::order::OrderType;
use satoru::position::{
position::Position, position_event_utils::PositionIncreaseParams,
position_utils::DecreasePositionCollateralValues
};
use satoru::price::price::Price;
use satoru::pricing::position_pricing_utils::PositionFees;
use satoru::order::order::{Order, SecondaryOrderType};
use satoru::utils::span32::{Span32, DefaultSpan32};
use satoru::utils::i128::{I128Div, I128Mul, I128Store, I128Serde};
use satoru::order::order::{Order, SecondaryOrderType, OrderType};
use satoru::utils::{
i128::{I128Div, I128Mul, I128Store, I128Serde}, span32::{Span32, DefaultSpan32}
};


//TODO: OrderCollatDeltaAmountAutoUpdtd must be renamed back to OrderCollateralDeltaAmountAutoUpdated when string will be allowed as event argument
Expand Down Expand Up @@ -57,7 +58,7 @@ trait IEventEmitter<TContractState> {

/// Emits the `PositionImpactPoolAmountUpdated` event.
fn emit_position_impact_pool_amount_updated(
ref self: TContractState, market: ContractAddress, delta: u128, next_value: u128,
ref self: TContractState, market: ContractAddress, delta: i128, next_value: u128,
);

/// Emits the `SwapImpactPoolAmountUpdated` event.
Expand Down Expand Up @@ -182,7 +183,7 @@ trait IEventEmitter<TContractState> {
ref self: TContractState,
order_key: felt252,
position_collateral_amount: u128,
base_pnl_usd: u128,
base_pnl_usd: i128,
remaining_cost_usd: u128
);

Expand Down Expand Up @@ -800,7 +801,7 @@ mod EventEmitter {
#[derive(Drop, starknet::Event)]
struct PositionImpactPoolAmountUpdated {
market: ContractAddress,
delta: u128,
delta: i128,
next_value: u128,
}

Expand Down Expand Up @@ -988,7 +989,7 @@ mod EventEmitter {
struct InsolventClose {
order_key: felt252,
position_collateral_amount: u128,
base_pnl_usd: u128,
base_pnl_usd: i128,
remaining_cost_usd: u128
}

Expand Down Expand Up @@ -1607,7 +1608,7 @@ mod EventEmitter {

/// Emits the `PositionImpactPoolAmountUpdated` event.
fn emit_position_impact_pool_amount_updated(
ref self: ContractState, market: ContractAddress, delta: u128, next_value: u128,
ref self: ContractState, market: ContractAddress, delta: i128, next_value: u128,
) {
self.emit(PositionImpactPoolAmountUpdated { market, delta, next_value, });
}
Expand Down Expand Up @@ -1947,7 +1948,7 @@ mod EventEmitter {
ref self: ContractState,
order_key: felt252,
position_collateral_amount: u128,
base_pnl_usd: u128,
base_pnl_usd: i128,
remaining_cost_usd: u128
) {
self
Expand Down
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ mod utils {
mod error_utils;
mod starknet_utils;
mod traits;
mod default;
}

// `liquidation` function to help with liquidations.
Expand Down
7 changes: 3 additions & 4 deletions src/market/market_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ fn get_max_open_interest(
/// * `delta` - The amount to increment by.
fn increment_claimable_collateral_amount(
data_store: IDataStoreDispatcher,
chain: IChainDispatcher,
event_emitter: IEventEmitterDispatcher,
market_address: ContractAddress,
token: ContractAddress,
Expand All @@ -507,7 +506,7 @@ fn increment_claimable_collateral_amount(
let divisor = data_store.get_u128(keys::claimable_collateral_time_divisor());
error_utils::check_division_by_zero(divisor, 'increment_claimable_collateral');
// Get current timestamp.
let current_timestamp = chain.get_block_timestamp().into();
let current_timestamp = get_block_timestamp().into();
let time_key = current_timestamp / divisor;

// Increment the collateral amount for the account.
Expand Down Expand Up @@ -870,11 +869,11 @@ fn apply_delta_to_position_impact_pool(
data_store: IDataStoreDispatcher,
event_emitter: IEventEmitterDispatcher,
market_address: ContractAddress,
delta: u128
delta: i128
) -> u128 {
// Increment the position impact pool amount.
let next_value = data_store
.increment_u128(keys::position_impact_pool_amount_key(market_address), delta);
.apply_bounded_delta_to_u128(keys::position_impact_pool_amount_key(market_address), delta);

// Emit event.
event_emitter.emit_position_impact_pool_amount_updated(market_address, delta, next_value);
Expand Down
3 changes: 2 additions & 1 deletion src/order/order.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ enum OrderType {
}

/// To help further differentiate orders.
#[derive(Drop, Copy, starknet::Store, Serde)]
#[derive(Drop, starknet::Store, Serde, PartialEq, Copy, Default)]
enum SecondaryOrderType {
#[default]
None,
Adl,
}
Expand Down
22 changes: 18 additions & 4 deletions src/order/order_vault.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ trait IOrderVault<TContractState> {
fn transfer_out(
ref self: TContractState, token: ContractAddress, receiver: ContractAddress, amount: u128,
);

/// Records a token transfer into the contract.
/// # Arguments
/// * `token` - The token address to transfer.
/// # Returns
/// * The amount of tokens transferred.
fn record_transfer_in(ref self: TContractState, token: ContractAddress) -> u128;
/// Updates the `token_balances` in case of token burns or similar balance changes.
/// The `prev_balance` is not validated to be more than the `next_balance` as this
/// could allow someone to block this call by transferring into the contract.
/// # Arguments
/// * `token` - The token to record the burn for.
/// # Returns
/// * The new balance.
fn sync_token_balance(ref self: TContractState, token: ContractAddress) -> u128;
}

#[starknet::contract]
Expand Down Expand Up @@ -77,12 +84,19 @@ mod OrderVault {
token: ContractAddress,
receiver: ContractAddress,
amount: u128,
) { // TODO
) {
let mut state: StrictBank::ContractState = StrictBank::unsafe_new_contract_state();
IStrictBank::transfer_out(ref state, token, receiver, amount);
}

fn sync_token_balance(ref self: ContractState, token: ContractAddress) -> u128 {
let mut state: StrictBank::ContractState = StrictBank::unsafe_new_contract_state();
IStrictBank::sync_token_balance(ref state, token)
}

fn record_transfer_in(ref self: ContractState, token: ContractAddress) -> u128 {
// TODO
0
let mut state: StrictBank::ContractState = StrictBank::unsafe_new_contract_state();
IStrictBank::record_transfer_in(ref state, token)
}
}
}
Loading

0 comments on commit 243629e

Please sign in to comment.