Skip to content

Commit 65907d5

Browse files
committed
wip
1 parent 1036cc2 commit 65907d5

File tree

7 files changed

+99
-100
lines changed

7 files changed

+99
-100
lines changed

workspace/apps/perpetuals/contracts/src/core/components/assets/assets.cairo

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#[starknet::component]
22
pub mod AssetsComponent {
3-
use RolesComponent::InternalTrait as RolesInternalTrait;
3+
use starknet::SyscallResultTrait;
4+
use RolesComponent::InternalTrait as RolesInternalTrait;
45
use core::cmp::min;
6+
use core::hash::Hash;
7+
use core::iter::{IntoIterator, Iterator};
58
use core::num::traits::Zero;
69
use core::panic_with_felt252;
10+
use core::pedersen::HashState;
711
use openzeppelin::access::accesscontrol::AccessControlComponent;
812
use openzeppelin::introspection::src5::SRC5Component;
913
use openzeppelin::token::erc20::interface::IERC20Dispatcher;
@@ -21,6 +25,11 @@ pub mod AssetsComponent {
2125
UNSORTED_RISK_FACTOR_TIERS, ZERO_MAX_FUNDING_INTERVAL, ZERO_MAX_FUNDING_RATE,
2226
ZERO_MAX_ORACLE_PRICE, ZERO_MAX_PRICE_INTERVAL,
2327
};
28+
use starknet::storage_access::{
29+
StorageBaseAddress, Store, storage_address_from_base, storage_address_from_base_and_offset,
30+
};
31+
use starknet::syscalls::{storage_read_syscall, storage_write_syscall};
32+
use starknet::{ContractAddress, SyscallResult};
2433
use perpetuals::core::components::assets::events;
2534
use perpetuals::core::components::assets::interface::IAssets;
2635
use perpetuals::core::components::operator_nonce::OperatorNonceComponent;
@@ -35,10 +44,14 @@ pub mod AssetsComponent {
3544
Price, PriceMulTrait, SignedPrice, convert_oracle_to_perps_price,
3645
};
3746
use perpetuals::core::types::risk_factor::{RiskFactor, RiskFactorTrait};
38-
use starknet::ContractAddress;
3947
use starknet::storage::{
40-
Map, MutableVecTrait, StorageMapReadAccess, StoragePathEntry, StoragePointerReadAccess,
41-
StoragePointerWriteAccess, Vec, VecTrait,
48+
Map, Mutable, MutableVecTrait, StorageAsPath, StorageAsPointer, StorageMapReadAccess,
49+
StorageMapWriteAccess, StoragePath, StoragePathEntry, StoragePointer0Offset,
50+
StoragePointerReadAccess, StoragePointerWriteAccess, Vec, VecTrait, StorableStoragePointerReadAccess, StoragePathUpdateTrait
51+
};
52+
use starknet::storage::{
53+
IntoIterRange,
54+
StoragePathMutableConversion
4255
};
4356
use starkware_utils::components::pausable::PausableComponent;
4457
use starkware_utils::components::pausable::PausableComponent::InternalTrait as PausableInternal;
@@ -511,11 +524,11 @@ pub mod AssetsComponent {
511524
fn get_synthetic_price(
512525
self: @ComponentState<TContractState>, synthetic_id: AssetId,
513526
) -> Price {
514-
if let Option::Some(data) = self.synthetic_timely_data.read(synthetic_id) {
515-
data.price
516-
} else {
517-
panic_with_felt252(NOT_SYNTHETIC)
518-
}
527+
let entry = self.synthetic_timely_data._inner_map.entry(synthetic_id);
528+
let cool = entry.as_ptr();
529+
let price = storage_read_syscall(0, storage_address_from_base_and_offset(cool.__storage_pointer_address__, 2)).unwrap_syscall();
530+
let x: u64 = price.try_into().unwrap();
531+
x.into()
519532
}
520533

521534
/// Get the risk factor of a synthetic asset.
@@ -533,39 +546,33 @@ pub mod AssetsComponent {
533546
price: Price,
534547
) -> RiskFactor {
535548
let entry = self.synthetic_config.entry(synthetic_id);
536-
537-
if entry.option.read() == 2 {
538-
panic_with_felt252(NOT_SYNTHETIC);
539-
}
540549
let risk_factor_first_tier_boundary = entry.risk_factor_first_tier_boundary.read();
541-
let risk_factor_tier_size = entry.risk_factor_tier_size.read();
542550

543551
let asset_risk_factor_tiers = self.risk_factor_tiers.entry(synthetic_id);
544552
let synthetic_value: u128 = price.mul(rhs: balance).abs();
545553
let index = if synthetic_value < risk_factor_first_tier_boundary {
546-
0_u128
547-
} else {
548-
let tier_size = risk_factor_tier_size;
549-
let first_tier_offset = synthetic_value
550-
- risk_factor_first_tier_boundary;
551-
min(
552-
1_u128 + (first_tier_offset / tier_size),
553-
asset_risk_factor_tiers.len().into() - 1,
554-
)
555-
};
556-
asset_risk_factor_tiers
557-
.at(index.try_into().expect('INDEX_SHOULD_NEVER_OVERFLOW'))
558-
.read()
554+
0_u128
555+
} else {
556+
let risk_factor_tier_size = entry.risk_factor_tier_size.read();
557+
let first_tier_offset = synthetic_value - risk_factor_first_tier_boundary;
558+
min(
559+
1_u128 + (first_tier_offset / risk_factor_tier_size),
560+
asset_risk_factor_tiers.len().into() - 1,
561+
)
562+
};
563+
asset_risk_factor_tiers
564+
.at(index.try_into().expect('INDEX_SHOULD_NEVER_OVERFLOW'))
565+
.read()
559566
}
560567

561568
fn get_funding_index(
562569
self: @ComponentState<TContractState>, synthetic_id: AssetId,
563570
) -> FundingIndex {
564-
if let Option::Some(data) = self.synthetic_timely_data.read(synthetic_id) {
565-
data.funding_index
566-
} else {
567-
panic_with_felt252(NOT_SYNTHETIC)
568-
}
571+
let entry = self.synthetic_timely_data._inner_map.entry(synthetic_id);
572+
let cool = entry.as_ptr();
573+
let funding_index = storage_read_syscall(0, storage_address_from_base_and_offset(cool.__storage_pointer_address__, 4)).unwrap_syscall();
574+
let x: i64 = funding_index.try_into().unwrap();
575+
x.into()
569576
}
570577

571578
fn validate_synthetic_active(self: @ComponentState<TContractState>, synthetic_id: AssetId) {
@@ -694,7 +701,7 @@ pub mod AssetsComponent {
694701
let current: u256 = (*signed_price.signer_public_key).into();
695702
assert(prev < current, SIGNED_PRICES_UNSORTED);
696703
}
697-
previous_public_key_opt = Option::Some(*signed_price.signer_public_key);
704+
previous_public_key_opt = Option::Some((*signed_price.signer_public_key));
698705
}
699706

700707
assert(2 * (lower_amount + equal_amount) >= signed_prices_len, INVALID_MEDIAN);

workspace/apps/perpetuals/contracts/src/core/core.cairo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#[starknet::contract]
22
pub mod Core {
3-
use crate::core::types::asset::synthetic::SyntheticConfig;
4-
use core::dict::{Felt252Dict, Felt252DictTrait};
3+
use core::dict::{Felt252Dict, Felt252DictTrait};
54
use core::nullable::{FromNullableResult, match_nullable};
65
use core::num::traits::Zero;
76
use core::panic_with_felt252;
@@ -70,6 +69,7 @@ use core::dict::{Felt252Dict, Felt252DictTrait};
7069
IterableMapIntoIterImpl, IterableMapReadAccessImpl, IterableMapWriteAccessImpl,
7170
};
7271
use starkware_utils::time::time::{Time, TimeDelta, Timestamp, validate_expiration};
72+
use crate::core::types::asset::synthetic::SyntheticConfig;
7373

7474
component!(path: AccessControlComponent, storage: accesscontrol, event: AccessControlEvent);
7575
component!(path: OperatorNonceComponent, storage: operator_nonce, event: OperatorNonceEvent);
@@ -855,7 +855,7 @@ use core::dict::{Felt252Dict, Felt252DictTrait};
855855
// Validate base asset is inactive synthetic.
856856
let x = self.assets.synthetic_config.read(base_asset_id);
857857

858-
let y : Option<SyntheticConfig> = x.into();
858+
let y: Option<SyntheticConfig> = x.into();
859859
if let Option::Some(config) = y {
860860
assert(config.status == AssetStatus::INACTIVE, SYNTHETIC_IS_ACTIVE);
861861
} else {

workspace/apps/perpetuals/contracts/src/core/types/asset/synthetic.cairo

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,21 @@ pub struct OptionSyntheticConfig {
3333
pub resolution_factor: u64,
3434
}
3535

36-
pub impl OptionSyntheticConfigIntoOptionSyntheticConfig of Into<OptionSyntheticConfig, Option<SyntheticConfig>> {
36+
pub impl OptionSyntheticConfigIntoOptionSyntheticConfig of Into<
37+
OptionSyntheticConfig, Option<SyntheticConfig>,
38+
> {
3739
fn into(self: OptionSyntheticConfig) -> Option<SyntheticConfig> {
3840
if self.option == 1 {
39-
Option::Some(SyntheticConfig {
40-
version: self.version,
41-
status: self.status,
42-
risk_factor_first_tier_boundary: self.risk_factor_first_tier_boundary,
43-
risk_factor_tier_size: self.risk_factor_tier_size,
44-
quorum: self.quorum,
45-
resolution_factor: self.resolution_factor,
46-
})
41+
Option::Some(
42+
SyntheticConfig {
43+
version: self.version,
44+
status: self.status,
45+
risk_factor_first_tier_boundary: self.risk_factor_first_tier_boundary,
46+
risk_factor_tier_size: self.risk_factor_tier_size,
47+
quorum: self.quorum,
48+
resolution_factor: self.resolution_factor,
49+
},
50+
)
4751
} else {
4852
Option::None
4953
}
@@ -53,7 +57,7 @@ pub impl OptionSyntheticConfigIntoOptionSyntheticConfig of Into<OptionSyntheticC
5357
pub impl SyntheticConfigIntoOptionSyntheticConfig of Into<SyntheticConfig, OptionSyntheticConfig> {
5458
fn into(self: SyntheticConfig) -> OptionSyntheticConfig {
5559
OptionSyntheticConfig {
56-
option: 0,
60+
option: 1,
5761
version: self.version,
5862
status: self.status,
5963
risk_factor_first_tier_boundary: self.risk_factor_first_tier_boundary,
@@ -65,7 +69,6 @@ pub impl SyntheticConfigIntoOptionSyntheticConfig of Into<SyntheticConfig, Optio
6569
}
6670

6771

68-
6972
#[derive(Copy, Drop, Serde, starknet::Store)]
7073
pub struct SyntheticTimelyData {
7174
version: u8,

workspace/apps/perpetuals/contracts/src/core/types/funding.cairo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ impl FundingIndexIntoImpl of Into<FundingIndex, i64> {
9090
}
9191
}
9292

93+
impl i64IntoImplFundingIndex of Into<i64, FundingIndex> {
94+
fn into(self: i64) -> FundingIndex {
95+
FundingIndex{value: self}
96+
}
97+
}
98+
99+
93100
/// Validates the funding rate by ensuring that the index difference is bounded by the max funding
94101
/// rate.
95102
///

workspace/apps/perpetuals/contracts/src/core/types/price.cairo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ pub struct Price {
3030
value: u64,
3131
}
3232

33+
impl u64IntoImplPrice of Into<u64, Price> {
34+
fn into(self: u64) -> Price {
35+
Price{value: self}
36+
}
37+
}
3338

3439
#[derive(Copy, Debug, Drop, Serde)]
3540
pub struct SignedPrice {

workspace/apps/perpetuals/contracts/src/tests/flow_tests/unit_flow_tests.cairo

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,9 +2283,6 @@ fn test_funding_index_rounding() {
22832283
state.facade.validate_collateral_balance(user_1.position_id, 1000_i64.into());
22842284
state.facade.validate_collateral_balance(user_2.position_id, 999_i64.into());
22852285
}
2286-
2287-
2288-
22892286
use perpetuals::core::core::Core::{InternalCoreFunctions, SNIP12MetadataImpl};
22902287
use perpetuals::core::interface::{ICoreDispatcher, ICoreDispatcherTrait, Settlement};
22912288
use perpetuals::core::types::order::Order;
@@ -2307,7 +2304,7 @@ use starkware_utils_testing::test_utils::cheat_caller_address_once;
23072304

23082305
#[test]
23092306
#[fork(
2310-
url: "https://starknet-mainnet.blastapi.io/22f0d577-04d4-49c0-ad0d-77dd531b0351/rpc/v0_8",
2307+
url: "https://rpc.starknet.lava.build/",
23112308
block_number: 1844544,
23122309
)]
23132310
fn test_profile() {
@@ -2908,40 +2905,32 @@ fn test_profile() {
29082905
// actual_fee_b: trade_2.actual_fee_b,
29092906
// );
29102907

2911-
let trades: Span<Settlement> = array![trade_1,
2912-
trade_2,
2913-
trade_3,
2914-
trade_4,
2915-
trade_5,
2916-
trade_6,
2917-
trade_7,
2918-
trade_8,
2919-
trade_9,
2920-
trade_10,
2921-
trade_11,
2922-
trade_12,
2923-
].span();
2908+
let trades: Span<Settlement> = array![
2909+
trade_1, trade_2, trade_3, trade_4, trade_5, trade_6, trade_7, trade_8, trade_9, trade_10,
2910+
trade_11, trade_12,
2911+
]
2912+
.span();
29242913

29252914
dispatcher.multi_trade(:operator_nonce, trades: trades);
29262915
// let mut index = 0;
29272916

29282917
// for _trade in trades {
2929-
// cheat_caller_address_once(:contract_address, :caller_address);
2930-
// let trade = *_trade;
2931-
// let x = dispatcher.get_position_tv_tr(position_id: trade.order_a.position_id);
2932-
// let y = dispatcher.get_position_tv_tr(position_id: trade.order_b.position_id);
2933-
// let trade_result = dispatcher
2934-
// .trade(
2935-
// operator_nonce: operator_nonce + index,
2936-
// signature_a: trade.signature_a,
2937-
// signature_b: trade.signature_b,
2938-
// order_a: trade.order_a,
2939-
// order_b: trade.order_b,
2940-
// actual_amount_base_a: trade.actual_amount_base_a,
2941-
// actual_amount_quote_a: trade.actual_amount_quote_a,
2942-
// actual_fee_a: trade.actual_fee_a,
2943-
// actual_fee_b: trade.actual_fee_b,
2944-
// );
2945-
// index = index + 1;
2918+
// cheat_caller_address_once(:contract_address, :caller_address);
2919+
// let trade = *_trade;
2920+
// let x = dispatcher.get_position_tv_tr(position_id: trade.order_a.position_id);
2921+
// let y = dispatcher.get_position_tv_tr(position_id: trade.order_b.position_id);
2922+
// let trade_result = dispatcher
2923+
// .trade(
2924+
// operator_nonce: operator_nonce + index,
2925+
// signature_a: trade.signature_a,
2926+
// signature_b: trade.signature_b,
2927+
// order_a: trade.order_a,
2928+
// order_b: trade.order_b,
2929+
// actual_amount_base_a: trade.actual_amount_base_a,
2930+
// actual_amount_quote_a: trade.actual_amount_quote_a,
2931+
// actual_fee_a: trade.actual_fee_a,
2932+
// actual_fee_b: trade.actual_fee_b,
2933+
// );
2934+
// index = index + 1;
29462935
// }
29472936
}

workspace/apps/perpetuals/contracts/src/tests/unit_tests/unit_tests.cairo

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::core::types::asset::synthetic::SyntheticConfig;
21
use core::num::traits::Zero;
32
use perpetuals::core::components::assets::interface::{
43
IAssets, IAssetsDispatcher, IAssetsDispatcherTrait, IAssetsSafeDispatcher,
@@ -66,6 +65,7 @@ use starkware_utils_testing::test_utils::{
6665
Deployable, TokenTrait, assert_panic_with_error, assert_panic_with_felt_error,
6766
cheat_caller_address_once,
6867
};
68+
use crate::core::types::asset::synthetic::SyntheticConfig;
6969

7070

7171
#[test]
@@ -1074,15 +1074,9 @@ fn test_successful_deactivate_synthetic_asset() {
10741074

10751075
// Setup parameters:
10761076
let synthetic_id = cfg.synthetic_cfg.synthetic_id;
1077-
let x = state
1078-
.assets
1079-
.synthetic_config
1080-
.entry(synthetic_id)
1081-
.read();
1082-
let y : Option<SyntheticConfig> = x.into();
1083-
assert!(
1084-
y.unwrap().status == AssetStatus::ACTIVE,
1085-
);
1077+
let x = state.assets.synthetic_config.entry(synthetic_id).read();
1078+
let y: Option<SyntheticConfig> = x.into();
1079+
assert!(y.unwrap().status == AssetStatus::ACTIVE);
10861080

10871081
// Test:
10881082
cheat_caller_address_once(contract_address: test_address(), caller_address: cfg.app_governor);
@@ -1095,15 +1089,9 @@ fn test_successful_deactivate_synthetic_asset() {
10951089
);
10961090

10971091
// Check:
1098-
let x = state
1099-
.assets
1100-
.synthetic_config
1101-
.entry(synthetic_id)
1102-
.read();
1103-
let y : Option<SyntheticConfig> = x.into();
1104-
assert!(
1105-
y.unwrap().status == AssetStatus::INACTIVE,
1106-
);
1092+
let x = state.assets.synthetic_config.entry(synthetic_id).read();
1093+
let y: Option<SyntheticConfig> = x.into();
1094+
assert!(y.unwrap().status == AssetStatus::INACTIVE);
11071095
}
11081096

11091097
#[test]

0 commit comments

Comments
 (0)