audit: enhance collateral type validation and funding rate logic#31
audit: enhance collateral type validation and funding rate logic#31ZouBadCode wants to merge 37 commits intomainfrom
Conversation
- Introduced new functions for staking, unstaking, and claiming rewards in the reward distributor. - Updated trading interface to include collateralDecimal in ResolveSizeArguments. - Enhanced index exports to include reward distributor related functions and types. - Added new view type for RewardDistributorStakeData. - Updated transaction builders to support reward distributor operations. - Configured sui-codegen to generate types and functions for the reward distributor contract.
- Added collateral type assertions in order and position management functions to ensure consistency. - Improved funding rate calculation by adjusting timestamp handling and accrued rate application. - Introduced new tests to validate collateral type mismatches and funding rate updates across multiple intervals.
bucket-bot
left a comment
There was a problem hiding this comment.
Code review
No issues found. Checked collateral-type safety hardening paths and funding catch-up logic; both changes are consistent with existing architecture and tests cover key regressions.
🤖 Generated with Rex (OpenClaw)
bucket-bot
left a comment
There was a problem hiding this comment.
✅ Code Review — Approved
PR #31: audit: enhance collateral type validation and funding rate logic
Both change categories are well-implemented:
1. Collateral Type Validation
assert_position_collateral_type<C_TOKEN>andassert_order_collateral_type<C_TOKEN>consistently applied across all mutation paths inposition.moveandtrading.move- Generic
C_TOKENparameter correctly added toborrow_owned_position_mutandtake_owned_position - All 6 request functions (close, increase, decrease, deposit, withdraw collateral + cancel order) now validate collateral type at entry
liquidate_requestandbatch_liquidate_requestalso covered; batch gracefully skips mismatched positions (continue instead of abort) — good defensive designmatch_ordersvalidates linked position collateral before proceeding
2. Funding Rate Multi-Interval Catch-Up
- Correctly computes
elapsed_intervals = elapsed_ms / intervaland multiplies rate - Timestamp aligned to
last_funding_timestamp + elapsed_intervals * interval(notnow_ms) — prevents drift accumulation - Sign tracking via
is_positivepreserved correctly through multi-interval accrual
Tests
funding_rate_update_catches_up_multiple_intervals— verifies 4-interval catch-up produces 4× single-interval rate- 4 collateral mismatch tests using
CHEAPdummy token cover close, deposit, liquidate, and cancel order paths
No issues found. Clean audit fix. 👍
|
|
||
| events::emit_funding_rate_updated( | ||
| market_id, rate_float, new_index, long_oi, short_oi, now_ms, | ||
| market_id, accrued_rate_float, new_index, long_oi, short_oi, next_funding_timestamp, |
There was a problem hiding this comment.
要不要直接更新 event 定義把 funding_rate 改成 accrued_funding_rate 比較明確?
或是兩個都列出來,比如
public(package) fun emit_funding_rate_updated(
market_id: ID,
funding_rate: Float, // per-interval rate(保持原語意)
accrued_funding_rate: Float, // = funding_rate × elapsed_intervals
elapsed_intervals: u64, // 補齊了幾個 interval
cumulative_index: Double,
long_oi: u64,
short_oi: u64,
timestamp: u64, // 對齊後的 next_funding_timestamp
)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Query all markets in one simulateTransaction call instead of N calls. getAllAccountPositions takes prices per market, skips markets without price. getAllAccountOrders needs no prices (oracle_price unused for orders). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Query all markets in one simulateTransaction call instead of N calls. getAllAccountPositions takes prices per market, skips markets without price. getAllAccountOrders needs no prices (oracle_price unused for orders). Results grouped by market config order, same types as per-market versions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Assert lot/min sizing via parsed bounds instead of hardcoded values - Skip open invalid-size MoveAbort case when min_size is zero (unreachable) - Share helpers in market-summary-assertions for view and trading tests Made-with: Cursor
Made-with: Cursor
feat: on-chain fallback view functions for positions and orders
Replace dynamic `new RegExp(functionName)` with hardcoded patterns per factory function (VecSet, LinkedTable, Node). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
feat: integrate reward distributor flow and APR support into SDK
|
調整測試針對抵押品錯配問題新增測試 errcode 208,稍後 commit |
- Added collateral type assertions in order and position management functions to ensure consistency. - Improved funding rate calculation by adjusting timestamp handling and accrued rate application. - Introduced new tests to validate collateral type mismatches and funding rate updates across multiple intervals.
…s://github.com/Bucket-Protocol/waterx-sdk into audit/fix-colleteral-type-and-funding-rate-cal
do0x0ob
left a comment
There was a problem hiding this comment.
有一些測試腳本等等可以考慮要不要提交進來,或是找其他地方組織一起
This pull request introduces comprehensive improvements to type safety, correctness, and robustness in the WaterX perpetual trading contracts. The most significant changes enforce collateral type consistency throughout position and order management, enhance the accuracy of funding rate updates (especially when multiple intervals elapse), and improve test coverage for these scenarios.
Type safety and collateral validation:
assert_position_collateral_typeandassert_order_collateral_typefunctions. These assertions are now enforced throughout the codebase whenever positions or orders are accessed, modified, or liquidated, preventing accidental collateral mismatches.Funding rate logic improvements:
funding_rate_update_catches_up_multiple_intervals, to verify that the funding rate update logic works correctly when several intervals pass between updates.Test and developer experience:
CHEAP) for testing collateral mismatch scenarios, improving test coverage and clarity.These changes significantly improve the safety, correctness, and maintainability of the codebase, especially around collateral handling and funding rate calculations.