When analyzing the given smart contract code for a perpetual trading platform, it is essential to identify and establish the properties and invariants that must hold to ensure the contract operates securely and as expected. Here are some key properties and invariants to consider:
General Properties and Invariants
Governance Control:
The gov address should always be a valid address.
Only the gov can update the gov address or link contracts.
Contract Linking:
chainlink, pool, and store should always be linked before performing any trading operations.
Balance Management:
Users should not be able to withdraw more than their balance.
Balance increments and decrements should correctly reflect deposits, withdrawals, and trading fees.
Margin and Leverage:
Users' equity should always be sufficient to cover their locked margin.
Ensure leverage constraints are respected when opening positions.
Order and Position Management:
Orders should only be executable if they meet specified conditions regarding market price and order type.
Ensure positions are properly updated or removed when orders are executed or canceled.
Funding Rates and PnL Calculation:
Funding rates should be calculated and updated correctly based on open interest.
PnL (Profit and Loss) should be calculated accurately based on current prices and funding fees.
Specific Invariants
Governance
Governance Updates:
require(_gov != address(0), "!address");
Ensure new gov address is not zero.
Contract Linking:
function link(address _chainlink, address _pool, address _store) external onlyGov {
require(_chainlink != address(0) && _pool != address(0) && _store != address(0), "!address");
// ... rest of the code
}
Deposit and Withdrawals
Deposit:
require(amount > 0, "!amount");
Ensure deposit amount is greater than zero.
Withdrawal:
Ensure the amount to withdraw does not exceed the user's balance.
Ensure equity after withdrawal is not less than the locked margin:
Ensure users are liquidated only if their margin level is below the minimum required margin level.
if (marginLevel < store.minimumMarginLevel()) {
// User is eligible for liquidation
}
By formalizing these properties and invariants, you can apply formal verification methods to analyze the smart contract and ensure that it behaves as expected under all possible scenarios. This can help identify and prevent potential bugs and vulnerabilities, leading to a more secure and robust trading platform.
When analyzing the given smart contract code for a perpetual trading platform, it is essential to identify and establish the properties and invariants that must hold to ensure the contract operates securely and as expected. Here are some key properties and invariants to consider:
General Properties and Invariants
Governance Control:
gov
address should always be a valid address.gov
can update thegov
address or link contracts.Contract Linking:
chainlink
,pool
, andstore
should always be linked before performing any trading operations.Balance Management:
Margin and Leverage:
Order and Position Management:
Funding Rates and PnL Calculation:
Specific Invariants
Governance
Governance Updates:
gov
address is not zero.Contract Linking:
Deposit and Withdrawals
Deposit:
Withdrawal:
Order Handling
Submit Order:
Order Execution:
Position Management
Increase Position:
Decrease Position:
Liquidation
By formalizing these properties and invariants, you can apply formal verification methods to analyze the smart contract and ensure that it behaves as expected under all possible scenarios. This can help identify and prevent potential bugs and vulnerabilities, leading to a more secure and robust trading platform.