Class: GSwap
Main class for interacting with the gSwap decentralized exchange. Provides methods for trading, liquidity management, and position handling.
Constructors
Constructor
new GSwap(options?): GSwap;
Creates a new SDK instance.
Parameters
Parameter | Type | Description |
---|---|---|
options? | { bundlerBaseUrl? : string ; bundlingAPIBasePath? : string ; dexBackendBaseUrl? : string ; dexContractBasePath? : string ; gatewayBaseUrl? : string ; httpRequestor? : HttpRequestor ; signer? : GalaChainSigner ; tokenContractBasePath? : string ; transactionWaitTimeoutMs? : number ; walletAddress? : string ; } | Customization options. |
options.bundlerBaseUrl? | string | Base URL for the DEX transaction bundling API. |
options.bundlingAPIBasePath? | string | Base path for transaction bundling API endpoints. |
options.dexBackendBaseUrl? | string | Base URL for the DEX backend API (for user assets and other data). |
options.dexContractBasePath? | string | Base path for DEX contract API endpoints, within the GalaChain Gateway API. |
options.gatewayBaseUrl? | string | Base URL for the GalaChain Gateway API. |
options.httpRequestor? | HttpRequestor | Custom HTTP requestor to use for API calls. Defaults to the global fetch function. |
options.signer? | GalaChainSigner | The signer to use for authenticated operations. Required if you use any functionality that creates transactions (such as swapping). Not required for readonly operations. |
options.tokenContractBasePath? | string | Base path for token contract API endpoints, within the GalaChain Gateway API. |
options.transactionWaitTimeoutMs? | number | Timeout in milliseconds for waiting for transactions to complete. Defaults to 300,000 milliseconds (five minutes). |
options.walletAddress? | string | Optional default wallet address for operations that require a wallet address (e.x. swapping). If not provided, you must specify the wallet address in each method call. |
Returns
GSwap
Properties
assets
readonly assets: Assets;
User asset management operations. Use this to retrieve user token balances and asset information.
Example
// Get user assets with pagination
const assets = await gSwap.assets.getUserAssets('eth|123...abc', 1, 20);
console.log(`User has ${assets.count} different tokens`);
bundlerBaseUrl
readonly bundlerBaseUrl: string;
bundlingAPIBasePath
readonly bundlingAPIBasePath: string;
dexBackendBaseUrl
readonly dexBackendBaseUrl: string;
dexContractBasePath
readonly dexContractBasePath: string;
gatewayBaseUrl
readonly gatewayBaseUrl: string;
pools
readonly pools: Pools;
positions
readonly positions: Positions;
Position management operations for liquidity positions. Use this to manage liquidity positions including creation, modification, and fee collection.
Example
// Get all user positions
const positions = await gSwap.positions.getUserPositions('eth|123...abc');
// Add liquidity to a position using price range
const result = await gSwap.positions.addLiquidityByPrice({
walletAddress: 'eth|123...abc',
positionId: '',
token0: 'GALA|Unit|none|none',
token1: 'GUSDC|Unit|none|none',
fee: 500,
tickSpacing: 10,
minPrice: '0.45',
maxPrice: '0.55',
amount0Desired: '100',
amount1Desired: '50',
amount0Min: '95',
amount1Min: '47.5'
});
quoting
readonly quoting: Quoting;
Quoting functionality for price discovery and trade estimation. Use this to get quotes for token swaps without executing transactions.
Example
// Get the best quote across all fee tiers
const quote = await gSwap.quoting.quoteExactInput(tokenA, tokenB, amount);
signer?
readonly optional signer: GalaChainSigner;
swaps
readonly swaps: Swaps;
Token swap operations for trading tokens. Use this to execute token swaps on the DEX.
Example
// Execute an exact input swap: sell 100 GALA for USDC
const result = await gSwap.swaps.swap(
'GALA|Unit|none|none',
'GUSDC|Unit|none|none',
500,
{ exactIn: '100', amountOutMinimum: '45' },
'eth|123...abc', // your wallet address
);
tokenContractBasePath
readonly tokenContractBasePath: string;
transactionWaitTimeoutMs
readonly transactionWaitTimeoutMs: number;
events
readonly static events: Events = Events.instance;
Event management operations for real-time socket connections. Use this to manage event streaming and transaction status updates.
Example
// Connect to event socket for transaction updates
await GSwap.events.connectEventSocket();
// Check if socket is connected
const isConnected = GSwap.events.eventSocketConnected();