Skip to content

Conversation

@RoeeGross
Copy link
Collaborator

@RoeeGross RoeeGross commented Oct 5, 2025

Note

Packs risk config into optimized storage and migrates data, updates risk-factor lookup, refactors trade order validation reuse, and adjusts tests/interface.

  • Assets:
    • Introduce packed RiskConfig with StorePacking and new storage: risk_config and risk_factor_tiers_opt.
    • Add migrate_risk() to populate new structures from existing asset_config and risk_factor_tiers.
    • Update get_asset_risk_factor to use risk_config and risk_factor_tiers_opt.read.
  • Core:
    • Refactor _validate_order to accept optional now/collateral_id and return them; reuse in _validate_trade.
    • Remove active-asset validation in _validate_trade (commented out).
  • Types:
    • Add RiskConfig struct and StorePacking<RiskConfig, felt252> implementation.
  • Interface/Tests:
    • Expose migrate_risk in IAssets and call it in performance test before multi_trade.

Written by Cursor Bugbot for commit fecd82d. This will update automatically on new commits. Configure here.


This change is Reviewable

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@RoeeGross RoeeGross force-pushed the RoeeGross/opt-risk-factor branch from d3bc482 to 69289b1 Compare October 5, 2025 14:03
@RoeeGross RoeeGross self-assigned this Oct 8, 2025
cursor[bot]

This comment was marked as outdated.

@RoeeGross RoeeGross force-pushed the RoeeGross/opt-risk-factor branch from cc7e83b to ca18c61 Compare October 12, 2025 07:49
cursor[bot]

This comment was marked as outdated.

@RoeeGross RoeeGross force-pushed the RoeeGross/opt-risk-factor branch from ca18c61 to 5043da9 Compare October 12, 2025 08:56
cursor[bot]

This comment was marked as outdated.

@RoeeGross RoeeGross force-pushed the RoeeGross/opt-risk-factor branch from 19b6d86 to e0bd885 Compare October 12, 2025 12:51
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Timestamp Overwrite Causes Inconsistent Validation

The _validate_order function recomputes the now timestamp on line 1306, overwriting the value initially set from the optional parameter. This defeats the optimization to reuse a consistent timestamp across multiple order validations, leading to redundant Time::now() calls and potentially inconsistent expiration checks.

workspace/apps/perpetuals/contracts/src/core/core.cairo#L1305-L1306

// Expiration check.
let now = Time::now();

Fix in Cursor Fix in Web


@RoeeGross RoeeGross force-pushed the RoeeGross/opt-risk-factor branch 2 times, most recently from d9699ee to b9373a3 Compare November 6, 2025 07:21
self.assets.get_collateral_id()
} else {
collateral_id.unwrap()
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Redundant Time::now() Call Breaks Timestamp Consistency

Duplicate Time::now() call in _validate_order function. The now variable is already computed at lines 1108-1112, but line 1127 calls Time::now() again, overwriting the parameter value. This defeats the purpose of passing now as an optional parameter for reuse between order_a and order_b validation, potentially causing inconsistent timestamp checks.

Fix in Cursor Fix in Web

self
.risk_factor_tiers_opt
.entry(asset_id)
.read(index.try_into().expect('INDEX_SHOULD_NEVER_OVERFLOW'))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: New Assets Lack Risk Config Initialization

get_asset_risk_factor reads from risk_config and risk_factor_tiers_opt storage fields, but the _add_asset function (which adds new assets) does not populate these fields. Only the migrate_risk function populates them. This means newly added assets will have default/zero values for risk configuration, causing incorrect risk factor calculations until migrate_risk is manually called. The _add_asset function should populate these new storage fields when adding an asset.

Fix in Cursor Fix in Web

@RoeeGross RoeeGross force-pushed the RoeeGross/opt-risk-factor branch from b9373a3 to fecd82d Compare November 6, 2025 07:24
Copy link
Contributor

@remollemo remollemo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 7 files reviewed, 8 unresolved discussions (waiting on @RoeeGross)


workspace/apps/perpetuals/contracts/src/core/types/asset.cairo line 117 at r9 (raw file):

}

/// ┌──────────────────────────────────────────────────────────────┐

almost good
make really good


workspace/apps/perpetuals/contracts/src/core/components/assets/assets.cairo line 179 at r9 (raw file):

                    let value = vec.at(i.into()).read();
                    self.risk_factor_tiers_opt.entry(asset_id).write(i.into(), value);
                }

rethink, redo

  1. not in the same place
  2. consider alternative to map

Code quote:

                let vec = self.risk_factor_tiers.entry(asset_id);
                for i in 0..vec.len() {
                    let value = vec.at(i.into()).read();
                    self.risk_factor_tiers_opt.entry(asset_id).write(i.into(), value);
                }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants