From 8ebf19ff66aa161fc245a6104eff54966650a83b Mon Sep 17 00:00:00 2001 From: sina <20732540+SinaKhalili@users.noreply.github.com> Date: Fri, 11 Oct 2024 05:55:49 -0700 Subject: [PATCH] Add example for funding rate --- source/includes/_data_api.md | 85 ++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/source/includes/_data_api.md b/source/includes/_data_api.md index 3f8493d..8f118a3 100644 --- a/source/includes/_data_api.md +++ b/source/includes/_data_api.md @@ -21,12 +21,97 @@ Example: [https://data.api.drift.trade/contracts](https://data.api.drift.trade/c Returns the last 30 days of funding rates by marketName or marketIndex. +```python +import requests + +def get_funding_rates(market_symbol='SOL-PERP'): + url = f'https://data.api.drift.trade/fundingRates' + params = {'marketName': market_symbol} + + response = requests.get(url, params=params) + return response.json()['fundingRates'] + +# Example usage, print the funding rates for SOL-PERP +market_symbol = 'SOL-PERP' +rates = get_funding_rates(market_symbol) + +print(f"Funding Rates for {market_symbol}:") +for rate in rates: + funding_rate = float(rate['fundingRate']) / 1e9 + # ... any logic here, for example... + print(f"Slot: {rate['slot']}, Funding Rate: {funding_rate:.9f}") + +``` +```typescript +interface FundingRate { + slot: number; + fundingRate: string; + // Other fields... (view the response section for the full list) +} + +async function getFundingRates(marketSymbol: string = 'SOL-PERP'): Promise { + const url = `https://data.api.drift.trade/fundingRates?marketName=${marketSymbol}`; + + try { + const response = await fetch(url); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); + return data.fundingRates; + } catch (error) { + console.error('Error fetching funding rates:', error); + return []; + } +} + +async function main() { + const marketSymbol = 'SOL-PERP'; + const rates = await getFundingRates(marketSymbol); + + console.log(`Funding Rates for ${marketSymbol}:`); + rates.forEach(rate => { + const fundingRate = parseFloat(rate.fundingRate) / 1e9; + // ... any logic here, for example... + console.log(`Slot: ${rate.slot}, Funding Rate: ${fundingRate.toFixed(9)}`); + }); +} + +main().catch(error => console.error('An error occurred:', error)); +``` + + + + | Parameter | Description | Optional | Values | | ---------------- | ------------------------------------------------ | -------- | --------------------------------------------------- | | marketName or marketIndex | The market name or index for the perp market | NO | | Example: [https://data.api.drift.trade/fundingRates?marketName=SOL-PERP](https://data.api.drift.trade/fundingRates?marketName=SOL-PERP) +### Response +The response is a json object with a `fundingRates` array. Each funding rate entry contains the following fields: + +| Field | Type | Description | +|-------|------|-------------| +| `txSig` | string | Transaction signature | +| `slot` | integer | Slot number | +| `ts` | string | Timestamp | +| `recordId` | string | Record identifier | +| `marketIndex` | integer | Market index | +| `fundingRate` | string | Funding rate (divide by 1e9 for actual rate) | +| `cumulativeFundingRateLong` | string | Cumulative funding rate for long positions | +| `cumulativeFundingRateShort` | string | Cumulative funding rate for short positions | +| `oraclePriceTwap` | string | Oracle price time-weighted average price | +| `markPriceTwap` | string | Mark price time-weighted average price | +| `fundingRateLong` | string | Funding rate for long positions | +| `fundingRateShort` | string | Funding rate for short positions | +| `periodRevenue` | string | Revenue for the period | +| `baseAssetAmountWithAmm` | string | Base asset amount with AMM | +| `baseAssetAmountWithUnsettledLp` | string | Base asset amount with unsettled LP | + ## `GET /DRIFT/`