A TypeScript reference implementation for fetching and processing data from Ostium's subgraph. This repository demonstrates how to properly format and normalize data from the Ostium protocol, including proper handling of decimals, BigNumber conversions, and type safety.
- Proper decimal handling for all numeric values
- Type-safe data structures
- Normalized data formats
- Comprehensive examples of subgraph queries
- BigNumber handling for precise calculations
- Protocol-specific calculations and formulae
# Clone the repository
git clone https://github.com/0xOstium/ostium-data-ts.git
# Install dependencies
cd ostium-data-ts
npm install- Node.js >= 14
- TypeScript >= 4.5
- ethers.js
- graphql-request
// Subgraph Data Fetching
import {
getFormattedPairs,
getFormattedOpenTrades,
getFormattedOrders,
getFormattedRecentHistory
} from './src';
// Example: Fetch and format trading pairs
const pairs = await getFormattedPairs();
// Example: Get open trades for an address
const trades = await getFormattedOpenTrades('0x...');
// Protocol Calculations
import {
GetCollateralInputFromNotional,
GetTradeLiquidationPrice,
CurrentTradeProfitP,
GetTradeValue,
GetOpeningFee
} from './src/utils/formulae';
// Example: Calculate liquidation price
const liqPrice = GetTradeLiquidationPrice(
openPrice,
isLong,
collateral,
leverage,
rolloverFee,
fundingFee,
maxLeverage
);
// Example: Calculate trade profit percentage
const profitP = CurrentTradeProfitP(
openPrice,
currentPrice,
isLong,
leverage,
highestLeverage
);The implementation handles various precision levels:
- PRECISION_18 (1e18) for prices and rates
- PRECISION_6 (1e6) for fees and amounts
- PRECISION_2 (1e2) for leverage and percentages
getFormattedPairs(): Get all trading pairs with normalized datagetFormattedPairDetails(pairId): Get specific pair detailsgetFormattedOpenTrades(address): Get open trades for an addressgetFormattedTradeById(tradeId): Get specific trade detailsgetFormattedOrders(address): Get active ordersgetFormattedOrderById(orderId): Get specific order detailsgetFormattedRecentHistory(address, limit): Get order history
GetCollateralInputFromNotional(): Calculate required collateral from notional valueGetTradeLiquidationPrice(): Calculate trade liquidation priceGetTradeLiquidationMargin(): Calculate trade liquidation marginCurrentTradeProfitP(): Calculate current trade profit percentageCurrentTradeProfitRaw(): Calculate current trade profit in raw valueCurrentTotalProfitRaw(): Calculate total profit including feesGetTradeValue(): Get current trade value and liquidation marginGetTradeRolloverFee(): Calculate trade rollover feeGetTradeFundingFee(): Calculate trade funding feeGetTakeProfitPrice(): Calculate take profit priceGetStopLossPrice(): Calculate stop loss priceGetOpeningFee(): Calculate trade opening feeGetPriceImpact(): Calculate price impact for a tradeIsDayTradeClosed(): Check if day trading is closed
{
id: number;
from: string;
to: string;
group: string;
longOI: number;
shortOI: number;
maxOI: number;
makerFeeP: number;
takerFeeP: number;
minLeverage: number;
maxLeverage: number;
makerMaxLeverage: number;
groupMaxCollateralP: number;
minLevPos: number;
lastFundingRate: number;
curFundingLong: number;
curFundingShort: number;
lastFundingBlock: number;
overnightMaxLeverage?: number;
}{
id: string;
isOpen: boolean;
leverage: number;
notional: number;
openPrice: number;
isBuy: boolean;
index: number;
highestLeverage: number;
funding: number;
collateral: number;
pairId: string;
rollover: number;
stopLossPrice: number | null;
takeProfitPrice: number | null;
timestamp: Date | null;
tradeID: string;
tradeNotional: number;
tradeType: string;
trader: string;
}{
id: string;
trader: string;
vaultFee: number;
tradeNotional: number;
totalProfitPercent: number;
price: number;
orderType: string;
orderAction: string;
notional: number;
leverage: number;
isBuy: boolean;
collateral: number;
pairId: string;
}To run TypeScript files directly:
npx ts-node src/your-file.tsTo build the project:
npm run buildFeel free to use this implementation as a reference for your own projects or contribute improvements via pull requests.
ISC