9.0.1 • Published 10 months ago

@galacticcouncil/sdk v9.0.1

Weekly downloads
-
License
-
Repository
github
Last release
10 months ago

Galactic SDK

npm version

Hydration sdk build on top of @polkadot{.js} (Pjs).

Table of contents:

Installation

Install with npm:

npm install @galacticcouncil/sdk

Troubleshooting

Version 8.x

⚠️ Important: In the 8.x release, we upgraded @polkadot/api to version 16.x.

See the changelog for details.

Version 2.x

As of v2.x .wasm files are no longer embedded in bundle but rather deferred to improve load performance & decrease module size (esm only).

For more details visit TROUBLESHOOTING.md

Usage

Use createSdkContext to quickly set up all components of the SDK — pool context, trading logic, client access, and transaction building — in a single call.

import { createSdkContext } from '@galacticcouncil/sdk';
import { ApiPromise, WsProvider } from '@polkadot/api';

const ws = 'wss://hydration-rpc.n.dwellir.com';
const wsProvider = new WsProvider(ws, 2_500, {}, 60_000, 102400, 10 * 60_000);

const api = await ApiPromise.create({
  provider: wsProvider,
});

const sdk = await createSdkContext(api);

// Don't forget to cleanup resources when DONE
sdk.destroy();
api.disconnect();

It handles all necessary setup under the hood. Just plug in your ApiPromise, and you're ready to interact with registry, accounts, pools, router, and more.

⚠️ Note: Make sure to keep only single instance of context per session.

Components

api

  • aave: AaveUtils — Aave-related utilities.
  • router: TradeRouter — Off-chain optimization of trades across pools for best price execution.
  • scheduler: TradeScheduler — Trade orders scheduling.

client

  • asset: AssetClient — Registry metadata and lookup.
  • balance: BalanceClient — Account balance tracking.
  • evm: EvmClient — Interacts with EVM.

ctx

  • pool: PoolService — Internal stateful pool context. Initialized with support for:
    • Aave
    • Omnipool
    • Stableswap
    • XYK pools
    • LBP pools

tx

  • TxBuilderFactory — Factory for generating submittable transaction using fluent APIs.

destroy()

Gracefully cleans up SDK resources. Always call before exiting to avoid memory leaks or stale subscriptions.

API Reference

AaveUtils

MethodDescription
getSummary(user: string): AaveSummaryReturns market summary.
getHealthFactor(user: string): numberCalculate HF.
getHealthFactorAfterWithdraw(user: string, reserve:string, withdrawAmount: string): numberCalculate HF after withdraw.
getHealthFactorAfterSupply(user: string, reserve:string, supplyAmount: string): numberCalculate HF after supply.
getMaxWithdraw(user: string, reserve:string): AmountGet max possible safe withdraw.

➡️ For type definitions visit types.ts

TradeRouter

MethodDescription
getPools(): PoolBase[]Returns the current list of available pools.
getAllPaths(tokenIn: string, tokenOut: string): Hop[][]Computes possible routes between two assets.
getAllAssets(): PoolBase[]Lists all assets that are tradeable through the router.
getAssetPairs(token: string): Asset[]Lists all assets given token is tradeable with.
getBestSell(tokenIn: string, tokenOut: string, amountIn: bigint \| string): TradeFind the best sell trade for given input amount.
getBestBuy(tokenIn: string, tokenOut: string, amountOut: bigint \| string): TradeFind the best buy trade for given output amount.
getBuy(tokenIn: string, tokenOut: string, amountOut: bigint \| string, route?: Hop[]): TradeCalculate a buy using a specific route (optional).
getSell(tokenIn: string, tokenOut: string, amountIn: bigint \| string, route?: Hop[]): TradeCalculate a sell using a specific route (optional).
getBestSpotPrice(tokenIn: string, tokenOut: string): AmountGet the current spot price between two tokens.
getMostLiquidRoute(tokenIn: string, tokenOut: string): Hop[]Find the route with the highest liquidity between two tokens.

➡️ For type definitions visit types.ts

TradeScheduler

MethodDescription
getDcaOrder(assetIn: string, assetOut: string, amountInTotal: string, duration: number): TradeDcaOrderCalculate DCA order.
getTwapBuyOrder(assetIn: string, assetOut: string, amountInTotal: string): TradeOrderCalculate TWAP buy order.
getTwapSellOrder(assetIn: string, assetOut: string, amountInTotal: string): TradeOrderCalculate TWAP buy order.

➡️ For type definitions visit types.ts

AssetClient

MethodDescription
getOnChainAssets(includeInvalid?: boolean, external?: ExternalAsset[]): Promise<Asset[]>Returns assets with metadata from registry.

➡️ For type definitions visit types.ts

Examples

All examples assume sdk have been initialized see

TradeRouter

Calculate sell of 1 DOT for HDX & build tx with 5% slippage (default to 1% if not specified)

const { api, tx } = sdk;

const trade = await api.router.getBestSell("5", "10", "10");
const tradeTx = await tx.trade(trade)
  .withBeneficiary(BENEFICIARY)
  .withSlippage(5)
  .build();
console.log(trade.toHuman());
console.log('Transaction hash:', tradeTx.hex);

AssetClient

Get default on-chain data.

const { client } = sdk;

const assets = await client.asset.getOnChainAssets();
console.log(assets);

To demonstrate more full working examples on real chain see script section.

Run: $ npx tsx ./test/script/examples/<examplePackage>/<exampleName>.ts with valid example package & name.

5.3.0

1 year ago

6.1.0

12 months ago

6.1.2

12 months ago

6.1.1

12 months ago

6.1.4

12 months ago

6.1.3

12 months ago

8.1.0

10 months ago

8.1.1

10 months ago

4.4.0

1 year ago

5.4.0

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

6.2.0

11 months ago

7.0.0

11 months ago

7.0.2

11 months ago

7.0.1

11 months ago

4.3.0

1 year ago

9.0.1

10 months ago

9.0.0

10 months ago

5.5.0

1 year ago

5.1.0

1 year ago

7.1.1

11 months ago

7.1.0

11 months ago

6.1.0-pr149-5bac85c

12 months ago

4.6.1

1 year ago

4.6.0

1 year ago

5.6.1

1 year ago

5.6.0

1 year ago

5.2.0

1 year ago

6.0.0

1 year ago

7.2.0

11 months ago

8.0.1

10 months ago

8.0.0

10 months ago

4.5.0

1 year ago

4.5.2

1 year ago

4.5.1

1 year ago

4.2.1

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

4.2.0

2 years ago

4.1.0

2 years ago

3.0.0

2 years ago

2.3.0

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.0.1

2 years ago

2.1.0

2 years ago

2.0.1-beta.0

2 years ago

2.0.1-beta.1

2 years ago

2.0.0

2 years ago

2.0.0-beta.0

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

0.3.1

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.4

2 years ago

0.7.3

2 years ago

0.5.0

3 years ago

0.7.0

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.7.6

2 years ago

0.7.5

2 years ago

0.7.7

2 years ago

0.5.0-beta.1

3 years ago

0.5.0-beta.0

3 years ago

0.8.0-beta.3

2 years ago

0.8.0-beta.4

2 years ago

0.8.0-beta.1

2 years ago

0.8.0-beta.2

2 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.5

3 years ago

0.6.4

3 years ago

0.4.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.3.0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.1-rc0

3 years ago

0.2.1-rc2

3 years ago

0.2.1-rc1

3 years ago

0.1.0

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.1-beta.20

3 years ago

0.0.1

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1-beta.18

3 years ago

0.0.1-beta.17

3 years ago

0.0.1-beta.19

3 years ago

0.0.5

3 years ago

0.0.1-beta.14

3 years ago

0.0.4

3 years ago

0.0.1-beta.16

3 years ago

0.0.1-beta.15

3 years ago

0.0.1-beta.12

3 years ago

0.0.1-beta.13

3 years ago

0.0.1-beta.11

4 years ago

0.0.1-beta.10

4 years ago

0.0.1-beta.9

4 years ago

0.0.1-beta.8

4 years ago

0.0.1-beta.7

4 years ago

0.0.1-beta.6

4 years ago

0.0.1-beta.5

4 years ago

0.0.1-beta.4

4 years ago

0.0.1-beta.3

4 years ago

0.0.1-beta.2

4 years ago

0.0.1-beta.1

4 years ago