@galacticcouncil/sdk 8.0.1 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,14 +4,23 @@
4
4
 
5
5
  Hydration sdk build on top of [@polkadot{.js} (Pjs)](https://polkadot.js.org/).
6
6
 
7
- Table of content:
7
+ Table of contents:
8
8
 
9
9
  - [Installation](#installation)
10
10
  - [Troubleshooting](#troubleshooting)
11
11
  - [Usage](#usage)
12
- - [PoolService](#poolservice)
13
- - [Router](#router)
12
+ - [Components](#components)
13
+ - [api](#api)
14
+ - [client](#client)
15
+ - [ctx](#ctx)
16
+ - [tx](#tx)
17
+ - [API Reference](#api-reference)
18
+ - [AaveUtils](#aaveutils)
14
19
  - [TradeRouter](#traderouter)
20
+ - [TradeScheduler](#tradescheduler)
21
+ - [BalanceClient](#balanceclient)
22
+ - [AssetClient](#assetclient)
23
+ - [Examples](#examples)
15
24
 
16
25
  ## Installation
17
26
 
@@ -37,151 +46,141 @@ For more details visit [TROUBLESHOOTING.md](TROUBLESHOOTING.md)
37
46
 
38
47
  ## Usage
39
48
 
40
- ### PoolService
49
+ Use `createSdkContext` to quickly set up all components of the SDK — pool context, trading logic, client access, and transaction building — in a single call.
41
50
 
42
- Build and subscribe to the given AMM types, supplying data for the Router API.
43
-
44
- ⚠️ **Note:** Make sure to keep only single instance of service per session.
45
-
46
- #### API Reference (internal)
47
-
48
- | Method | Description |
49
- | :----- | :----------- |
50
- | `getPools(): Promise<PoolBase[]>` | Returns list of available pools. |
51
- | `getPoolFees(pool: PoolBase, feeAsset: string): Promise<PoolFees>` | Returns current pool fees |
52
-
53
- ➡️ For type definitions visit [types.ts](src/pool/types.ts)<br />
54
-
55
- #### Example
56
-
57
- Initialize pool context and sync registry.
58
-
59
- ```typescript
51
+ ```ts
52
+ import { createSdkContext } from '@galacticcouncil/sdk';
60
53
  import { ApiPromise, WsProvider } from '@polkadot/api';
61
- import { TradeRouter, PoolService, PoolType } from '@galacticcouncil/sdk';
62
54
 
63
55
  const ws = 'wss://hydration-rpc.n.dwellir.com';
64
56
  const wsProvider = new WsProvider(ws, 2_500, {}, 60_000, 102400, 10 * 60_000);
65
- const api = await ApiPromise.create({ provider: wsProvider });
66
57
 
67
- const poolService = new PoolService(api);
68
- await poolService.syncRegistry();
58
+ const api = await ApiPromise.create({
59
+ provider: wsProvider,
60
+ });
69
61
 
70
- // Don't forget to cleanup the resources
71
- poolService.destroy();
62
+ const sdk = await createSdkContext(api);
63
+
64
+ // Don't forget to cleanup resources when DONE
65
+ sdk.destroy();
72
66
  api.disconnect();
73
67
  ```
74
68
 
75
- ### Router
69
+ 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.
76
70
 
77
- Off-chain routing, build to find the most suitable routes across the pools. Building block for `TradeRouter`.
71
+ ⚠️ **Note:** Make sure to keep only single instance of context per session.
78
72
 
79
- #### API Reference
73
+ ## Components
80
74
 
81
- | Method | Description |
82
- | :----- | :----------- |
83
- | `getPools(): PoolBase[]` | Returns the current list of available pools. |
84
- | `getAllPaths(tokenIn: string, tokenOut: string): Hop[][]` | Computes possible routes between two assets. |
85
- | `getAllAssets(): PoolBase[]` | Lists all assets that are tradeable through the router. |
86
- | `getAssetPairs(token: string): Asset[]` | Lists all assets given token is tradeable with. |
75
+ ### `api`
87
76
 
88
- ➡️ For type definitions visit [types.ts](src/sor/types.ts)<br />
77
+ - `aave: AaveUtils` Aave-related utilities.
78
+ - `router: TradeRouter` — Off-chain optimization of trades across pools for best price execution.
79
+ - `scheduler: TradeScheduler` — Trade orders scheduling.
89
80
 
90
- #### Example
81
+ ### `client`
91
82
 
92
- List all tradable assets available in the current pool context.
83
+ - `asset: AssetClient` Registry metadata and lookup.
84
+ - `balance: BalanceClient` — Account balance tracking.
85
+ - `evm: EvmClient` — Interacts with EVM.
93
86
 
94
- ```typescript
95
- import { ApiPromise, WsProvider } from '@polkadot/api';
96
- import { TradeRouter, PoolService, PoolType } from '@galacticcouncil/sdk';
87
+ ### `ctx`
97
88
 
98
- const ws = 'wss://hydration-rpc.n.dwellir.com';
99
- const wsProvider = new WsProvider(ws, 2_500, {}, 60_000, 102400, 10 * 60_000);
100
- const api = await ApiPromise.create({ provider: wsProvider });
89
+ - `pool: PoolService` — Internal stateful pool context. Initialized with support for:
90
+ - Aave
91
+ - Omnipool
92
+ - Stableswap
93
+ - XYK pools
94
+ - LBP pools
101
95
 
102
- const poolService = new PoolService(api);
103
- const router = new Router(poolService);
96
+ ### `tx`
104
97
 
105
- const assets = await router.getAllAssets();
106
- console.log(assets);
98
+ - `TxBuilderFactory` Factory for generating submittable transaction using fluent APIs.
107
99
 
108
- // Don't forget to cleanup the resources
109
- poolService.destroy();
110
- api.disconnect();
111
- ```
100
+ ### `destroy()`
112
101
 
113
- ### TradeRouter
102
+ Gracefully cleans up SDK resources. Always call before exiting to avoid memory leaks or stale subscriptions.
114
103
 
115
- Off-chain optimization of orders across pools for best price execution. TradeRouter does not perform any on-chain transations.
104
+ ## API Reference
116
105
 
117
- #### Api Reference
106
+ ### AaveUtils
118
107
 
119
108
  | Method | Description |
120
109
  | :----- | :----------- |
110
+ | `getSummary(user: string): AaveSummary` | Returns market summary. |
111
+ | `getHealthFactor(user: string): number` | Calculate HF. |
112
+ | `getHealthFactorAfterWithdraw(user: string, reserve:string, withdrawAmount: string): number` | Calculate HF after withdraw. |
113
+ | `getHealthFactorAfterSupply(user: string, reserve:string, supplyAmount: string): number` | Calculate HF after supply. |
114
+ | `getMaxWithdraw(user: string, reserve:string): Amount` | Get max possible safe withdraw. |
115
+
116
+ ➡️ For type definitions visit [types.ts](src/aave/types.ts)<br />
117
+
118
+ ### TradeRouter
119
+
120
+ | Method | Description |
121
+ | :----- | :----------- |
122
+ | `getPools(): PoolBase[]` | Returns the current list of available pools. |
123
+ | `getAllPaths(tokenIn: string, tokenOut: string): Hop[][]` | Computes possible routes between two assets. |
124
+ | `getAllAssets(): PoolBase[]` | Lists all assets that are tradeable through the router. |
125
+ | `getAssetPairs(token: string): Asset[]` | Lists all assets given token is tradeable with. |
121
126
  | `getBestSell(tokenIn: string, tokenOut: string, amountIn: bigint \| string): Trade` | Find the best sell trade for given input amount. |
122
127
  | `getBestBuy(tokenIn: string, tokenOut: string, amountOut: bigint \| string): Trade` | Find the best buy trade for given output amount. |
123
128
  | `getBuy(tokenIn: string, tokenOut: string, amountOut: bigint \| string, route?: Hop[]): Trade` | Calculate a buy using a specific route (optional). |
124
129
  | `getSell(tokenIn: string, tokenOut: string, amountIn: bigint \| string, route?: Hop[]): Trade` | Calculate a sell using a specific route (optional). |
125
- | `getSpotPrice(tokenIn: string, tokenOut: string): Amount` | Get the current spot price between two tokens. |
130
+ | `getBestSpotPrice(tokenIn: string, tokenOut: string): Amount` | Get the current spot price between two tokens. |
126
131
  | `getMostLiquidRoute(tokenIn: string, tokenOut: string): Hop[]` | Find the route with the highest liquidity between two tokens. |
127
132
 
128
133
  ➡️ For type definitions visit [types.ts](src/sor/types.ts)<br />
129
134
 
130
- #### Example
135
+ ### TradeScheduler
131
136
 
132
- Calculate sell of 1 DOT for HDX & build tx with 5% slippage (default to 1% if not specified)
137
+ | Method | Description |
138
+ | :----- | :----------- |
139
+ | `getDcaOrder(assetIn: string, assetOut: string, amountInTotal: string, duration: number): TradeDcaOrder` | Calculate DCA order. |
140
+ | `getTwapBuyOrder(assetIn: string, assetOut: string, amountInTotal: string): TradeOrder` | Calculate TWAP buy order. |
141
+ | `getTwapSellOrder(assetIn: string, assetOut: string, amountInTotal: string): TradeOrder` | Calculate TWAP buy order. |
133
142
 
134
- ```typescript
135
- import { ApiPromise, WsProvider } from '@polkadot/api';
136
- import { TradeRouter, PoolService, PoolType } from '@galacticcouncil/sdk';
143
+ ➡️ For type definitions visit [types.ts](src/sor/types.ts)<br />
137
144
 
138
- const ws = 'wss://hydration-rpc.n.dwellir.com';
139
- const wsProvider = new WsProvider(ws, 2_500, {}, 60_000, 102400, 10 * 60_000);
140
- const api = await ApiPromise.create({ provider: wsProvider });
145
+ ### AssetClient
146
+
147
+ | Method | Description |
148
+ | :----- | :----------- |
149
+ | `getOnChainAssets(includeInvalid?: boolean, external?: ExternalAsset[]): Promise<Asset[]>` | Returns assets with metadata from registry. |
141
150
 
142
- const poolService = new PoolService(api);
143
- const txUtils = new TradeUtils(api);
151
+ ➡️ For type definitions visit [types.ts](src/types.ts)<br />
144
152
 
145
- const router = new TradeRouter(poolService);
153
+ ## Examples
146
154
 
147
- const sell = await router.getBestSell("5", "0", "1");
148
- const tx = await utils.buildSellTx(sell, 5);
149
- console.log(sell.toHuman());
150
- console.log('Transaction hash: ' + transaction.hex);
155
+ All examples assume sdk have been initialized [see](#usage)
151
156
 
152
- // Don't forget to cleanup the resources
153
- poolService.destroy();
154
- api.disconnect();
157
+ ### TradeRouter
158
+
159
+ Calculate sell of 1 DOT for HDX & build tx with 5% slippage (default to 1% if not specified)
160
+
161
+ ```typescript
162
+ const { api, tx } = sdk;
163
+
164
+ const trade = await api.router.getBestSell("5", "10", "10");
165
+ const tradeTx = await tx.trade(trade)
166
+ .withBeneficiary(BENEFICIARY)
167
+ .withSlippage(5)
168
+ .build();
169
+ console.log(trade.toHuman());
170
+ console.log('Transaction hash:', tradeTx.hex);
155
171
  ```
156
172
 
157
- ## Examples
173
+ ### AssetClient
174
+
175
+ Get default on-chain data.
176
+
177
+ ```typescript
178
+ const { client } = sdk;
179
+
180
+ const assets = await client.asset.getOnChainAssets();
181
+ console.log(assets);
182
+ ```
158
183
 
159
184
  To demonstrate more full working examples on real chain see [script](test/script/examples) section.
160
185
 
161
- ### Execution
162
-
163
- Run: `$ npx tsx ./test/script/examples/<examplePackage>/<exampleName>.ts` with valid example package & name.
164
-
165
- ## Roadmap
166
-
167
- Component list and current status ⬇️
168
-
169
- - 🧪 Done
170
- - 🛠 Work in progress
171
- - ⏳ Planning to build
172
-
173
- | Name | Type | |
174
- | ----------- | :--: | --: |
175
- | Dca | API | ⏳ |
176
- | Router | API | 🧪 |
177
- | TradeRouter | API | 🧪 |
178
- | Twap | API | ⏳ |
179
- | LBP | Math | 🧪 |
180
- | LBP | Pool | 🧪 |
181
- | Omni | Math | 🧪 |
182
- | Omni | Pool | 🧪 |
183
- | Stable | Math | 🧪 |
184
- | Stable | Pool | 🧪 |
185
- | XYK | Math | 🧪 |
186
- | XYK | Pool | 🧪 |
187
- | Aave | Pool | 🧪 |
186
+ Run: `$ npx tsx ./test/script/examples/<examplePackage>/<exampleName>.ts` with valid example package & name.