v10 migration

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-rc12arrow-up-right

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

    • 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

Integration Guide

NOTE: For tradingVariables and any trade struct (trade, tradeInfo, etc), consume from backends first: v10 Update

1. New Funding Fees

Funding fees are skew-based fees that balance long/short exposure.

Trade-Specific Funding Fees:

Display helpers:

2. New Borrowing Fees v2

Borrowing fees v2 work alongside funding fees with a simplified rate-based model.

Context Builder & Calculation:

Display helpers:

3. P&L Withdrawal Feature

Users can withdraw profits without closing their position. This maintains leverage while extracting gains.

Contract Interaction:

Important behavior: If amountCollateral exceeds available positive PnL, the contract will automatically withdraw only the maximum available amount (no revert).

To withdraw all available PnL:

Integration requirements:

  • Calculate withdrawable P&L: tradeValue - initialCollateral (when positive)

  • Show withdrawable amount in UI when position is in profit

  • Listen for TradePositivePnlWithdrawn event to update UI

  • Update position display after withdrawal:

    • collateralAmount increases

    • positionSizeToken remains unchanged

    • Track pnlWithdrawnCollateral for total withdrawn

4. Counter Trade Type

Counter trades improve market balance and receive fee discounts.

Creating a Counter Trade:

Note: The contract will validate if the trade actually qualifies as a counter trade. If not, it will be rejected. If the trade size is larger than the max skew limit, it will be reduced in size but still opened.

Pre-validation:

Detection (for existing trades):

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:

Helper functions:

6. Market Max Skew Limits

Markets have maximum allowed skew to prevent excessive imbalance.

Check skew using computeOiValues:

Note: For counter trade validation and position sizing, use validateCounterTrade (see section 5).

7. Effective Leverage in Partial Updates

Partial position updates now validate against effective leverage (accounts for unrealized P&L).

Calculation:

Validation Requirements:

For Position Increases (Partial Add):

  • 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

For Position Decreases (Partial Close):

  • No effective leverage maximum check

  • Only validates adjusted initial leverage (β‰₯ 0.1x)

For Leverage Increases:

  • Same as position increases - effective leverage validated

For Leverage Decreases (Add Collateral):

  • 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:

Utility functions:

9. Modified Liquidation Calculations

Liquidation now accounts for pending fees and realized P&L.

Context Builder & Calculation:

Additional functions:

10. Modified PnL Calculations

Comprehensive PnL includes all v10 components: funding fees, borrowing fees, and realized P&L.

Context Builder & Calculation:

Helper functions:

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:

Comprehensive OI Values:

Important notes:

  • 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:

Understanding Price Impact Results:

Closing a Trade:

Action Fees (Trading Fees)

Calculate Total Trading Fees:

Get Detailed Fee Breakdown:

Calculate Pending Holding Fees:

Summary

The SDK provides all necessary functions to integrate v10 features. Key patterns:

  1. Use Context Builders: Most complex calculations require a context object built from trading variables

  2. Transform Backend Data: Use transformGlobalTradingVariables and transformGlobalTrades before passing to SDK functions

  3. Handle Pre-v10 Trades: Check trade.contractsVersion to determine if a trade uses v10 features

  4. Display Market Rates: Use functions in "Current Market Rates" section for market-wide statistics

Last updated

Was this helpful?