# Examples

> Use this page for focused runnable snippets. If you want end-to-end trading flows or operational guidance, start with the [Integration guide](./integration-guide.md).

# Examples

Use this page for focused runnable snippets. If you want end-to-end trading flows or operational guidance, start with the [Integration guide](./integration-guide.md).

## Get funding fees

This example shows how to fetch hourly funding fee rates for all markets using the GMX SDK. Funding fees are paid between long and short positions to keep open interest balanced — positive values mean a position receives funding, and negative values mean it pays.

### Prerequisites

Initialize the SDK before running this example:

```typescript

const sdk = new GmxSdk({
  chainId: 42161, // Arbitrum
  rpcUrl: "https://arb1.arbitrum.io/rpc",
  oracleUrl: "https://arbitrum-api.gmxinfra.io",
  subsquidUrl: "https://gmx.squids.live/gmx-synthetics-arbitrum:prod/api/graphql",
});
```

### Fetch and format funding rates

Call `sdk.markets.getMarketsInfo()` to retrieve live market data, then compute the per-period funding factor for each market:

```typescript

const { marketsInfoData } = await sdk.markets.getMarketsInfo();

const fundingRates = Object.values(marketsInfoData ?? {}).map((market) => {
  // 3600n = 1 hour in seconds (BigInt required)
  const longHourly = getFundingFactorPerPeriod(market, true, 3600n);
  const shortHourly = getFundingFactorPerPeriod(market, false, 3600n);

  return {
    market: getMarketFullName(market),
    long: formatRatePercentage(longHourly, { displayDecimals: 2 }),
    short: formatRatePercentage(shortHourly, { displayDecimals: 2 }),
  };
});

console.table(fundingRates);
// Example output:
// [
//   { market: "BTC/USD [WBTC-USDC]", long: "-0.01%", short: "+0.01%" },
//   { market: "ETH/USD [WETH-USDC]", long: "+0.00%", short: "-0.00%" },
// ]
```

`getFundingFactorPerPeriod` returns a `bigint` representing the funding factor scaled to 30 decimal places. A negative value for a position side means that side pays funding for the period. `formatRatePercentage` converts the raw factor to a human-readable percentage string.

:::note
The `periodInSeconds` argument must be a `bigint` (for example, `3600n` for hourly rates). Passing a plain number will cause a TypeScript type error.
:::

### What the result tells you

| Field    | Type     | Description                                                 |
| -------- | -------- | ----------------------------------------------------------- |
| `market` | `string` | Market name in `INDEX/USD [LONG-SHORT]` format              |
| `long`   | `string` | Hourly funding rate for long positions (negative = paying)  |
| `short`  | `string` | Hourly funding rate for short positions (negative = paying) |

The sign convention is: negative means the position is paying funding to the counterpart side; positive means it is receiving funding.

### Related

- [`getFundingFactorPerPeriod`](./exports/utils/fees/fees.md) — function reference and parameter details
- [`formatRatePercentage`](./exports/utils/numbers.md) — number formatting utilities
- [`getMarketFullName`](./exports/utils/markets.md) — market name formatting
