Notice: These documents are currently in progress. For any questions or clarifications, please contact the team.Current version: https://www.npmjs.com/package/@gainsnetwork/sdk/v/1.0.0-rc12
v10 Changes - Overview
- New funding fees (skew-based, only on v10 positions)
- New borrowing fees v2 (alongside existing v1)
- New P&L withdrawal feature (withdraw profits without closing)
- New counter trade type (fee discounts for improving skew)
- New skew price impact
- New market max skew limits
- Fees no longer impact position size (exact position sizes)
- Modified borrowing fees v1 (uses dynamic OI)
- Modified partial update requirements (use effective leverage)
- Modified liquidation, pnl, fees calculations
- Pre and post v10 OI stored separately
- Pre-v10 trades cannot partial add
- New accounting: TradeFeesData and UiRealizedPnlData
- New Trade fields: positionSizeToken and isCounterTrade
Developer Notes - Overview
- SDK has been updated to support v10 more effectively than previous versions
- Backend transforms and converters are available (fetching is still separate)
- Transform trading variables:
transformGlobalTradingVariables - Transform global trades:
transformGlobalTrades - All functions expect backend types
- Transform trading variables:
- Additional feature support
- Holding fees
- Action fees
- Price impact
- PnL
- Liquidations
- Counter trade validation
- Effective leverage
- Market holding rates
- Market leverage requirements
- Market open interest
- Market price
- New context builders
- More declarative development pattern
- SDK functions mirror contract functions. But they don’t fetch data, instead rely on all data provided
- Provide large trading variables object and context builder will prepare struct for specific functions with all expected data
- Backend transforms and converters are available (fetching is still separate)
Integration Guide
NOTE: FortradingVariables and any trade struct (trade, tradeInfo, etc), consume from backends first: Backend
1. New Funding Fees
Funding fees are skew-based fees that balance long/short exposure. Trade-Specific Funding Fees:2. New Borrowing Fees v2
Borrowing fees v2 work alongside funding fees with a simplified rate-based model. Context Builder & Calculation:3. P&L Withdrawal Feature
Users can withdraw profits without closing their position. This maintains leverage while extracting gains. Contract Interaction:amountCollateral exceeds available positive PnL, the contract will automatically withdraw only the maximum available amount (no revert).
To withdraw all available PnL:
- Calculate withdrawable P&L:
tradeValue - initialCollateral(when positive) - Show withdrawable amount in UI when position is in profit
- Listen for
TradePositivePnlWithdrawnevent to update UI - Update position display after withdrawal:
collateralAmountincreasespositionSizeTokenremains unchanged- Track
pnlWithdrawnCollateralfor total withdrawn
4. Counter Trade Type
Counter trades improve market balance and receive fee discounts. Creating a Counter Trade:5. Skew Price Impact
Price impact based on market skew (imbalance between long/short OI). NOTE: The SDK offers wrapper utilities for open and close price impact which can be used to calculate all price impact data. More in SDK convenience functions. Context Builder & Calculation:6. Market Max Skew Limits
Markets have maximum allowed skew to prevent excessive imbalance. Check skew using computeOiValues:validateCounterTrade (see section 5).
7. Effective Leverage in Partial Updates
Partial position updates now validate against effective leverage (accounts for unrealized P&L). Calculation:- Effective leverage must not exceed
pairMaxLeverage - Counter trades must not exceed
pairCounterTradeMaxLeverage - Adjusted initial leverage must be between 0.1x and max uint24 (~16,777x)
- Pre-v10 trades cannot partial add
- No effective leverage maximum check
- Only validates adjusted initial leverage (≥ 0.1x)
- Same as position increases - effective leverage validated
- Same as position decreases - no effective leverage check
8. Modified Borrowing Fees v1
Borrowing fees v1 now use dynamic OI (position size adjusted by current price vs entry price). Context Builder & Calculation:9. Modified Liquidation Calculations
Liquidation now accounts for pending fees and realized P&L. Context Builder & Calculation:10. Modified PnL Calculations
Comprehensive PnL includes all v10 components: funding fees, borrowing fees, and realized P&L. Context Builder & Calculation:11. New Trade Fields
v10 trades have additional fields that must be parsed and stored. New fields in Trade struct:12. V10 OI Tracking
OI is tracked separately for pre-v10 and post-v10 trades. OI Functions:- Funding fees only apply to v10 OI
- Skew calculations use v10 OI only
- Max OI checks use combined OI (pre-v10 + v10)
- Counter trade validation uses v10 skew
Market Price Concept
Market price is a v10 feature that applies to markets with funding fees and skew price impact. It represents the oracle price adjusted for current market skew. Calculate Market Price:SDK Wrapper Functions
The SDK provides high-level wrapper functions that combine multiple calculations for common operations.Price Impact
Opening a Trade:Action Fees (Trading Fees)
Calculate Total Trading Fees:Summary
The SDK provides all necessary functions to integrate v10 features. Key patterns:- Use Context Builders: Most complex calculations require a context object built from trading variables
- Transform Backend Data: Use
transformGlobalTradingVariablesandtransformGlobalTradesbefore passing to SDK functions - Handle Pre-v10 Trades: Check
trade.contractsVersionto determine if a trade uses v10 features - Display Market Rates: Use functions in “Current Market Rates” section for market-wide statistics
