Backend

Details how to fetch trade data from off-chain services

Backend services, for the purpose of managing trades, do not provide anything that can't be fetched or listened to directly on-chain.

It is recommended to use gTrade backends when possible however to improve resilience against RPC issues - service is optimized for uptime.

DNS

Common DNS syntax - backend-<network>.gains.trade

NOTE: No auth is required for accessing REST GET endpoints and the WebSocket event stream. However, rate limiting is in place, so please fetch data only as necessary and rely on the event stream to keep your data updated.

Common Endpoints

Trading Variables

Provides main data necessary for opening, closing, managing trades. Payload also contains all active trades (allTrades).

Request:

GET /trading-variables

Response:

{
  lastRefreshed: "2024-05-20T21:07:02.133Z", // string, timestamp 
  refreshId: 6180, // number, unique identifier
  tradingState: 0,
  maxGainP: 900, // number, maximum percentage gain per single trade
  marketOrdersTimeoutBlocks: 200, // number of blocks after which an unsuccessful order's collateral can be reclaimed
  pairs : [ // array, containing objects for each trading pair
    {
      from: "ETH",
      to: "USD",
      spreadP: "400000000",
      groupIndex: "0",
      feeIndex: "0"
    },
    // Other trading pairs...
  ],
  groups: [ // array, containing objects for each trading group
    {
        name: "crypto",
        minLeverage: "2",
        maxLeverage: "150"
    },
    // Other trading groups...
  ],
  fees: [ // array, containing objects for each fee category
    {
      openFeeP: "300000000",
      closeFeeP: "600000000",
      oracleFeeP: "1",
      triggerOrderFeeP: "200000000",
      minPositionSizeUsd: "10000000000000000000000"
    },
    // Other fee categories...
  ],
  pairInfos: {
    maxLeverages: [], // array, leverage overrides for each pair index for risk management
    pairDepts: [] // array, 1% depth (in USD) for current price per each pair index
  },
  collaterals: [ // array, containing objects for each supported collateral
    {
      collateralIndex: 1,
      collateral: "0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1", // string, collateral token address e.g., WETH, USDC, DAI
      symbol: "DAI",
      isActive: true,
      prices: {}, // object, collateral / GNS USD prices
      collateralConfig: {}, // object, collateral precision and decimals
      gToken: {}, // object, address and market cap of gToken e.g., gWETH, gUSDC, gDAI
      borrowingFees: {} // object, fees for every group and pair index
    },
    // Other collaterals...
  ],
  sssTokenBalance: "23314652901338666091114242", // string, amount of $GNS staked in single-side staking
  sssLegacyTokenBalance: "112675327174399043872284", // string, legacy single-side staking balance
  sssRewardTokens: ["0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1", ...], // array, addresses of tokens (collaterals) distributed as rewards
  vaultClosingFeeP: "50", // number, vault closing fee percentage
  maxNegativePnlOnOpenP: 400000000000, // number, maximum negative PnL threshold upon trade opening
  blockConfirmations: 1, // number, required block confirmations
  oiWindowsSettings: { // object, settings for open interest windows
      startTs: 1704401556,
      windowsDuration: 7200,
      windowsCount: 3
  },
  oiWindows: [ // array, open interest window objects for each trading pair
    {
      "1645": {
        oiLongUsd: "8484501256212500000000",
        oiShortUsd: "17262712465625000000000"
      },
      // Other windows...
    },
    // Other trading pairs...
  ], 
  feeTiers: { // object, gCredit system fee tiers
    tiers: [ // array, all possible point tiers/thresholds
      {
          feeMultiplier: "975",
          pointsThreshold: "6000000"
      },
      // Other tiers...
    ],
    multipliers: [ // array, point multipliers for each trading group (by index)
      "8000",
      // Other multipliers...
    ]
  },
  allTrades: [ // array, objects representing each active trade/order
    { 
      trade: {
        user: "0x7152BC14A1eFBfD553D1Cbc7D4E49e63Dae5A669",
        index: "0", // user's trade index
        pairIndex: "19",
        leverage: "150000", // number, leverage supports up to 3 decimals
        long: true,
        isOpen: true,
        collateralIndex: "1",
        tradeType: "2", // number, 0 - MARKET, 1 - LIMIT, 2 - STOP
        collateralAmount: "550000000000000000000",
        openPrice: "7700000000",
        tp: "8162000000",
        sl: "0"
      },
      tradeInfo: {
        createdBlock: "192695790",
        tpLastUpdatedBlock: "192695790",
        slLastUpdatedBlock: "192695790",
        maxSlippageP: "1000",
        lastOiUpdateTs: 0,
        collateralPriceUsd: "0"
      },
      initialAccFees: {
        accPairFee: "0",
        accGroupFee: "0",
        block: "0"
      }
    },
    // Other trades...
  ],
  currentBlock: 213452457,
  currentL1Block: 19916646,
  isForexOpen: true,
  isStocksOpen: false,
  isIndicesOpen: false,
  isCommoditiesOpen: true,
}

User Trading Variables

Provides useful trader specific data, such as pending order IDs and fee tiers.

Request:

GET /user-trading-variables/<address>

Response:

{
    pendingMarketOrdersIds: [], 
    pendingMarketOrders: [], // array, user's market orders that are currently being processed
    feeTiers: { // object, gCredit system details
      traderInfo: {
          lastDayUpdated: 19864, // number, last recorded day on-chain
          trailingPoints: "199553481323944469547134920" // string, cumulative user's gCredit points
      },
      lastDayUpdatedPoints: "5397470676054960000000000", // string, user's on-chain points as of the last update
      inboundPoints: "5397470676054960000000000", // string, points expected to be added the next day
      outboundPoints: "0", // string, points expected to expire the next day
      expiredPoints: [] // array of strings, points for each expired day not yet recorded on-chain
    },
    collaterals: [ // array, user holdings and allowances for each collateral
      {
        balance: "5000086815959570800000", // string, user's collateral balance
        allowance: "5000086815959570800000", // string, user's collateral allowance
        decimals: 18 // number, number of decimal places for the collateral
      },
      // Other collaterals...
    ]
}

Historical Trades

Provides all historical trades (open & closed) for a given address

Request:

GET /personal-trading-history-table/<address>

Provides all open & closed trades in last 24hrs

GET /trading-history-24h

Response (same for both historical endpoints):

[
  {
    date: "2024-05-21T12:04:44.000Z", // string, timestamp
    pair: "W/USD",
    address: "0x1454A3be2322B60B813713E11af36bbAF4CfeeA7",
    action: "TradeOpenedMarket", // string, type of trade action (see TradeAction type below)
    price: 0.5697297128, 
    collateralPriceUsd: 1.0003745, 
    long: 1, // number, position direction (1 for long, 0 for short)
    size: 3160.946482, // number, trade collateral size
    leverage: 7, 
    pnl_net: 0, // number, net profit and loss after fees
    tx: "0x7e71dd845bb791803c16b13e4e5fe6c1a764dec4aef8205dd1f7ab8c91309aff",
    collateralIndex: 1
  },
  // Other trades...
]


// Possible values for TradeAction
type TradeAction = [
    'TradeOpenedMarket', 
    'TradeClosedMarket', 
    'TradeOpenedLimit',
    'TradeClosedLimit', 
    'TradeClosedTP',
    'TradeClosedSL', 
    'TradeClosedLIQ'
];

Event Stream

Backends also publish structured, post-processed events based on what on-chain event has just taken place. For example, registerTrade provides a full trade struct following the raw event MarketOrderExecuted. This is to limit how much clients need to fetch additional data.

Structure:

{
    name: <event name>,
    value: { <payload> },
}

Common event names:

  • liveEvents - raw on-chain events. Payload is identical to on-chain event.

  • registerTrade - trade has been registered

  • unregisterTrade - trade has been closed

  • updateTrade - trade has been updated

  • tradingVariables - updated /trading-variables payload

  • currentBlock - L2 block number for network

  • currentL1Block - L1 block number for network

  • deepReorg - Reorg has been detected - refresh data

  • new-trade-history - New trade history document

Last updated