A Node.js application for comparing on-chain and offline DEX quotes on the GalaChain network. This tool validates local quote accuracy by fetching pool data from the GalaChain API and performing local quote calculations using the GalaChain DEX SDK, then comparing results with on-chain quotes to ensure consistency.
The script demonstrates how to construct composite pool data from API responses, convert string values to proper BigNumber objects, and perform offline quote calculations that match on-chain results. This enables developers to build applications that can calculate DEX quotes locally without requiring blockchain calls for every quote request.
import { TokenClassKey, TokenBalance } from "@gala-chain/api";
import { quoteExactAmount, GetCompositePoolDto, QuoteExactAmountDto, CompositePoolDto, Pool, TickData } from "@gala-chain/dex";
import axios from "axios";
import BigNumber from "bignumber.js";
// 1. Fetch pool data from GalaChain
const getCompositePoolDto = new GetCompositePoolDto(token0, token1, fee);
const response = await axios.post("https://gateway-mainnet.galachain.com/api/asset/dexv3-contract/GetCompositePool", getCompositePoolDto);
// 2. Convert response data to proper objects with BigNumber conversions
const compositePoolData = createCompositePoolDtoFromResponse(response.data.Data);
// 3. Perform local quote calculation
const quoteDto = new QuoteExactAmountDto(token0, token1, fee, amount, zeroForOne, compositePoolData);
const quoteResult = await quoteExactAmount(null, quoteDto);
// Result: { amount0: "1000", amount1: "-14.366203", currentSqrtPrice: "...", newSqrtPrice: "..." }- Offline Quote Calculation: Perform DEX quotes locally using pool data fetched from GalaChain
- On-Chain Quote Comparison: Compare local quotes with on-chain quotes from the DEX backend
- Pool Data Fetching: Retrieve comprehensive pool data including balances, ticks, and token decimals
- GalaChain Integration: Built specifically for GalaChain's DEX v3 protocol
- Node.js (v16 or higher)
- npm or yarn package manager
- Clone the repository:
git clone <repository-url>
cd local-quote- Install dependencies:
npm install@gala-chain/api(v2.4.3) - GalaChain API SDK@gala-chain/dex(v1.0.18) - GalaChain DEX SDK@gala-chain/gswap-sdk(v0.0.7) - GalaChain GSwap SDKaxios(^1.6.0) - HTTP client for API requestsbignumber.js(^9.1.2) - Big number arithmeticjs-sha3(^0.9.2) - SHA-3 hashing functionstypescript(^5.6.3) - TypeScript support
Execute the main script to compare quotes:
node quote-local.mjsThis will:
- Fetch pool data for GALA/GUSDC pair with 1% fee
- Perform an offline quote calculation for 1000 GALA
- Fetch the corresponding on-chain quote
- Display both results for comparison
Performs a local quote calculation using:
- Pool data fetched from GalaChain GraphQL API
- Composite pool data construction
- Local quote calculation using GalaChain DEX SDK
Fetches quotes from the on-chain DEX backend API.
Main comparison function that:
- Sets up GALA/GUSDC token pair
- Executes both offline and on-chain quotes
- Returns comparison results
The query-api-queries.mjs module provides functions to fetch pool data:
getPoolData(token0Key, token1Key, fee)- Main function to get complete pool datagetSpecificPool(token0Key, token1Key, fee)- Fetch specific pool informationgetTokenBalance(owner)- Get token balances for a poolgetPoolTicks(poolHash)- Fetch tick data for a poolgetTokenDecimals(tokenKey)- Get decimal precision for tokens
The application uses several GalaChain endpoints:
- GraphQL Query API:
https://int-query-api-chain-platform-prod-chain-platform-eks.prod.galachain.com/graphql - DEX Backend API:
https://dex-backend-prod1.defi.gala.com - Quote API:
https://gateway-mainnet.galachain.com/api/asset/dexv3-contract/QuoteExactAmount - Composite Pool API:
https://int-galachain-gateway-chain-platform-stage-chain-platform-eks.stage.galachain.com/api/asset/dexv3-contract/GetCompositePool
Default tokens used in the comparison:
- Token 0: GALA (collection: "GALA", category: "Unit", type: "none", additionalKey: "none")
- Token 1: GUSDC (collection: "GUSDC", category: "Unit", type: "none", additionalKey: "none")
- Fee: 1% (DexFeePercentageTypes.FEE_1_PERCENT)
- Amount: 1000 GALA
local-quote/
├── package.json # Project dependencies and configuration
├── quote-local.mjs # Main application logic and quote comparison
├── query-api-queries.mjs # GraphQL queries and pool data fetching
├── .gitignore # Git ignore rules
└── README.md # This file
The project includes TypeScript as a dev dependency for type checking and development support.
The application includes comprehensive error handling for:
- API request failures
- Pool data not found
- Invalid response formats
- Quote calculation errors
The application uses several GraphQL queries to fetch data:
- Pool Data Query: Fetches specific pool information using token keys and fee
- Balance Query: Retrieves token balances for pool addresses
- Tick Data Query: Gets tick information for liquidity calculations
- Token Decimals Query: Fetches decimal precision for tokens
- Pool Identification: Generate pool hash using token keys and fee
- Data Fetching: Retrieve pool, balance, tick, and decimal data
- Composite Pool Construction: Build complete pool data structure
- Quote Calculation: Perform local quote using DEX SDK
- Comparison: Compare with on-chain quote results
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is part of the GalaChain ecosystem. Please refer to GalaChain's licensing terms.
For issues and questions:
- Check the GalaChain documentation
- Review the DEX SDK documentation
- Open an issue in this repository