PositionUtils
The PositionUtils is a library contract that provides utility functions for position management across the Satoshi Perps protocol. It encapsulates common position-related logic for reuse across different contracts.Contract Overview
The PositionUtils handles:- Utility functions for fee calculations
- Helper functions for position operations
- Standardized implementations of common position logic
As a library, PositionUtils does not store state and is designed to be used by other contracts.
Key Functions
Fee Utilities
Position Operations
Usage in Other Contracts
The PositionUtils library is utilized by various contracts in the Satoshi Perps protocol:- BasePositionManager: Uses utility functions for position operations
- PositionManager: Inherits position utility functions through BasePositionManager
- PositionRouter: Uses fee utility functions to determine fee application
Implementation Details
Fee Deduction Logic
TheshouldDeductFee
function implements the following logic:
- If the path length is 1 (no swap), check if amountIn is greater than 0
- If it’s a long position with collateral token same as index token, no fee
- For all other cases, apply the fee
Integration Example
Here’s how the PositionUtils library is typically used in other contracts:Benefits of Library Approach
Using a library for position utilities offers several advantages:- Code Reusability: Common logic is defined once and reused across contracts
- Gas Efficiency: Library code is deployed once and shared by all contracts
- Consistency: Ensures uniform behavior across different contract implementations
- Maintainability: Changes to the utility functions need to be made in only one place
Library contracts like PositionUtils help maintain consistent behavior across the protocol and make the codebase more maintainable.