@galacticcouncil/sdk 8.0.0 → 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.
Files changed (35) hide show
  1. package/README.md +107 -126
  2. package/build/index.cjs +2 -2
  3. package/build/index.mjs +2 -2
  4. package/build/types/aave/AaveClient.d.ts +2 -1
  5. package/build/types/aave/AaveUtils.d.ts +11 -3
  6. package/build/types/aave/types.d.ts +3 -3
  7. package/build/types/api/PolkadotApi.d.ts +0 -4
  8. package/build/types/api/index.d.ts +0 -1
  9. package/build/types/api/utils.d.ts +1 -0
  10. package/build/types/client/BalanceClient.d.ts +2 -3
  11. package/build/types/client/ChainParams.d.ts +10 -0
  12. package/build/types/client/index.d.ts +1 -0
  13. package/build/types/evm/client.d.ts +1 -0
  14. package/build/types/factory.d.ts +25 -0
  15. package/build/types/index.d.ts +2 -0
  16. package/build/types/sor/Router.d.ts +0 -5
  17. package/build/types/sor/TradeRouteBuilder.d.ts +4 -0
  18. package/build/types/sor/TradeScheduler.d.ts +96 -0
  19. package/build/types/sor/const.d.ts +7 -0
  20. package/build/types/sor/index.d.ts +2 -1
  21. package/build/types/sor/types.d.ts +43 -5
  22. package/build/types/tx/OrderTxBuilder.d.ts +21 -0
  23. package/build/types/tx/TradeTxBuilder.d.ts +19 -0
  24. package/build/types/tx/TxBuilder.d.ts +19 -0
  25. package/build/types/tx/TxBuilderFactory.d.ts +12 -0
  26. package/build/types/tx/index.d.ts +2 -0
  27. package/build/types/utils/bignumber.d.ts +2 -0
  28. package/build/types/utils/error.d.ts +3 -0
  29. package/build/types/utils/fee.d.ts +6 -0
  30. package/build/types/utils/h160.d.ts +0 -2
  31. package/package.json +1 -1
  32. package/build/index.mjs.map +0 -7
  33. package/build/types/sor/TradeUtils.d.ts +0 -25
  34. package/build/types/utils/mapper.d.ts +0 -6
  35. /package/build/types/{api → tx}/types.d.ts +0 -0
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
 
@@ -21,6 +30,14 @@ Install with [npm](https://www.npmjs.com/):
21
30
 
22
31
  ## Troubleshooting
23
32
 
33
+ ### Version 8.x
34
+
35
+ ⚠️ **Important:** In the 8.x release, we upgraded `@polkadot/api` to version **16.x**.
36
+
37
+ See the [changelog](https://github.com/galacticcouncil/sdk/blob/master/packages/sdk/CHANGELOG.md#800) for details.
38
+
39
+ ### Version 2.x
40
+
24
41
  As of **v2.x** .wasm files are no longer embedded in bundle
25
42
  but rather deferred to improve load performance & decrease
26
43
  module size (esm only).
@@ -29,72 +46,76 @@ For more details visit [TROUBLESHOOTING.md](TROUBLESHOOTING.md)
29
46
 
30
47
  ## Usage
31
48
 
32
- ⚠️ Important: In the 8.x release, we upgraded `@polkadot/api` to version **16.x**.
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.
33
50
 
34
- > 🐛 **Note:** A **TTL-based LRU cache** was introduced starting from
35
- `@polkadot/api` **v14.1.1**, which can break router behavior if not
36
- addressed (eviction issue).
51
+ ```ts
52
+ import { createSdkContext } from '@galacticcouncil/sdk';
53
+ import { ApiPromise, WsProvider } from '@polkadot/api';
37
54
 
38
- - 📄 [Release notes – v14.1.1](https://github.com/polkadot-js/api/releases/tag/v14.1.1)
39
- - 🐞 [GitHub Issue #6154](https://github.com/polkadot-js/api/issues/6154)
40
- - 🐞 [GitHub Issue #6122](https://github.com/polkadot-js/api/issues/6122)
55
+ const ws = 'wss://hydration-rpc.n.dwellir.com';
56
+ const wsProvider = new WsProvider(ws, 2_500, {}, 60_000, 102400, 10 * 60_000);
41
57
 
42
- To ensure the router works as expected, **either**:
58
+ const api = await ApiPromise.create({
59
+ provider: wsProvider,
60
+ });
43
61
 
44
- 1. Use a custom `WsProvider` configuration with cache TTL at least 10 minutes
45
- 2. Use a custom `WsProvider` configuration with cache TTL disabled (null)
62
+ const sdk = await createSdkContext(api);
46
63
 
47
- ```typescript
48
- const wsProvider = new WsProvider(
49
- ws,
50
- 2_500, // autoConnect (2.5 seconds)
51
- {}, // headers
52
- 60_000, // request timeout (60 seconds)
53
- 102400, // cache capacity
54
- 10 * 60_000 // cache TTL (10 minutes)
55
- );
64
+ // Don't forget to cleanup resources when DONE
65
+ sdk.destroy();
66
+ api.disconnect();
56
67
  ```
57
68
 
58
- ### PoolService
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.
59
70
 
60
- Build and subscribe to the given AMM types, supplying data for the Router API.
71
+ ⚠️ **Note:** Make sure to keep only single instance of context per session.
61
72
 
62
- ⚠️ **Note:** Make sure to keep only single instance of service per session.
73
+ ## Components
63
74
 
64
- #### API Reference (internal)
75
+ ### `api`
65
76
 
66
- | Method | Description |
67
- | :----- | :----------- |
68
- | `getPools(): Promise<PoolBase[]>` | Returns list of available pools. |
69
- | `getPoolFees(pool: PoolBase, feeAsset: string): Promise<PoolFees>` | Returns current pool fees |
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.
70
80
 
71
- ➡️ For type definitions visit [types.ts](src/pool/types.ts)<br />
81
+ ### `client`
72
82
 
73
- #### Example
83
+ - `asset: AssetClient` — Registry metadata and lookup.
84
+ - `balance: BalanceClient` — Account balance tracking.
85
+ - `evm: EvmClient` — Interacts with EVM.
74
86
 
75
- Initialize pool context and sync registry.
87
+ ### `ctx`
76
88
 
77
- ```typescript
78
- import { ApiPromise, WsProvider } from '@polkadot/api';
79
- import { TradeRouter, PoolService, PoolType } from '@galacticcouncil/sdk';
89
+ - `pool: PoolService` — Internal stateful pool context. Initialized with support for:
90
+ - Aave
91
+ - Omnipool
92
+ - Stableswap
93
+ - XYK pools
94
+ - LBP pools
80
95
 
81
- const ws = 'wss://hydration-rpc.n.dwellir.com';
82
- const wsProvider = new WsProvider(ws, 2_500, {}, 60_000, 102400, 10 * 60_000);
83
- const api = await ApiPromise.create({ provider: wsProvider });
96
+ ### `tx`
84
97
 
85
- const poolService = new PoolService(api);
86
- await poolService.syncRegistry();
98
+ - `TxBuilderFactory` Factory for generating submittable transaction using fluent APIs.
87
99
 
88
- // Don't forget to cleanup the resources
89
- poolService.destroy();
90
- api.disconnect();
91
- ```
100
+ ### `destroy()`
92
101
 
93
- ### Router
102
+ Gracefully cleans up SDK resources. Always call before exiting to avoid memory leaks or stale subscriptions.
94
103
 
95
- Off-chain routing, build to find the most suitable routes across the pools. Building block for `TradeRouter`.
104
+ ## API Reference
96
105
 
97
- #### API Reference
106
+ ### AaveUtils
107
+
108
+ | Method | Description |
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
98
119
 
99
120
  | Method | Description |
100
121
  | :----- | :----------- |
@@ -102,104 +123,64 @@ Off-chain routing, build to find the most suitable routes across the pools. Buil
102
123
  | `getAllPaths(tokenIn: string, tokenOut: string): Hop[][]` | Computes possible routes between two assets. |
103
124
  | `getAllAssets(): PoolBase[]` | Lists all assets that are tradeable through the router. |
104
125
  | `getAssetPairs(token: string): Asset[]` | Lists all assets given token is tradeable with. |
126
+ | `getBestSell(tokenIn: string, tokenOut: string, amountIn: bigint \| string): Trade` | Find the best sell trade for given input amount. |
127
+ | `getBestBuy(tokenIn: string, tokenOut: string, amountOut: bigint \| string): Trade` | Find the best buy trade for given output amount. |
128
+ | `getBuy(tokenIn: string, tokenOut: string, amountOut: bigint \| string, route?: Hop[]): Trade` | Calculate a buy using a specific route (optional). |
129
+ | `getSell(tokenIn: string, tokenOut: string, amountIn: bigint \| string, route?: Hop[]): Trade` | Calculate a sell using a specific route (optional). |
130
+ | `getBestSpotPrice(tokenIn: string, tokenOut: string): Amount` | Get the current spot price between two tokens. |
131
+ | `getMostLiquidRoute(tokenIn: string, tokenOut: string): Hop[]` | Find the route with the highest liquidity between two tokens. |
105
132
 
106
133
  ➡️ For type definitions visit [types.ts](src/sor/types.ts)<br />
107
134
 
108
- #### Example
109
-
110
- List all tradable assets available in the current pool context.
135
+ ### TradeScheduler
111
136
 
112
- ```typescript
113
- import { ApiPromise, WsProvider } from '@polkadot/api';
114
- import { TradeRouter, PoolService, PoolType } from '@galacticcouncil/sdk';
115
-
116
- const ws = 'wss://hydration-rpc.n.dwellir.com';
117
- const wsProvider = new WsProvider(ws, 2_500, {}, 60_000, 102400, 10 * 60_000);
118
- const api = await ApiPromise.create({ provider: wsProvider });
119
-
120
- const poolService = new PoolService(api);
121
- const router = new Router(poolService);
122
-
123
- const assets = await router.getAllAssets();
124
- console.log(assets);
125
-
126
- // Don't forget to cleanup the resources
127
- poolService.destroy();
128
- api.disconnect();
129
- ```
130
-
131
- ### TradeRouter
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. |
132
142
 
133
- Off-chain optimization of orders across pools for best price execution. TradeRouter does not perform any on-chain transations.
143
+ ➡️ For type definitions visit [types.ts](src/sor/types.ts)<br />
134
144
 
135
- #### Api Reference
145
+ ### AssetClient
136
146
 
137
147
  | Method | Description |
138
148
  | :----- | :----------- |
139
- | `getBestSell(tokenIn: string, tokenOut: string, amountIn: bigint \| string): Trade` | Find the best sell trade for given input amount. |
140
- | `getBestBuy(tokenIn: string, tokenOut: string, amountOut: bigint \| string): Trade` | Find the best buy trade for given output amount. |
141
- | `getBuy(tokenIn: string, tokenOut: string, amountOut: bigint \| string, route?: Hop[]): Trade` | Calculate a buy using a specific route (optional). |
142
- | `getSell(tokenIn: string, tokenOut: string, amountIn: bigint \| string, route?: Hop[]): Trade` | Calculate a sell using a specific route (optional). |
143
- | `getSpotPrice(tokenIn: string, tokenOut: string): Amount` | Get the current spot price between two tokens. |
144
- | `getMostLiquidRoute(tokenIn: string, tokenOut: string): Hop[]` | Find the route with the highest liquidity between two tokens. |
149
+ | `getOnChainAssets(includeInvalid?: boolean, external?: ExternalAsset[]): Promise<Asset[]>` | Returns assets with metadata from registry. |
145
150
 
146
- ➡️ For type definitions visit [types.ts](src/sor/types.ts)<br />
151
+ ➡️ For type definitions visit [types.ts](src/types.ts)<br />
152
+
153
+ ## Examples
147
154
 
148
- #### Example
155
+ All examples assume sdk have been initialized [see](#usage)
156
+
157
+ ### TradeRouter
149
158
 
150
159
  Calculate sell of 1 DOT for HDX & build tx with 5% slippage (default to 1% if not specified)
151
160
 
152
161
  ```typescript
153
- import { ApiPromise, WsProvider } from '@polkadot/api';
154
- import { TradeRouter, PoolService, PoolType } from '@galacticcouncil/sdk';
155
-
156
- const ws = 'wss://hydration-rpc.n.dwellir.com';
157
- const wsProvider = new WsProvider(ws, 2_500, {}, 60_000, 102400, 10 * 60_000);
158
- const api = await ApiPromise.create({ provider: wsProvider });
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);
171
+ ```
159
172
 
160
- const poolService = new PoolService(api);
161
- const txUtils = new TradeUtils(api);
173
+ ### AssetClient
162
174
 
163
- const router = new TradeRouter(poolService);
175
+ Get default on-chain data.
164
176
 
165
- const sell = await router.getBestSell("5", "0", "1");
166
- const tx = await utils.buildSellTx(sell, 5);
167
- console.log(sell.toHuman());
168
- console.log('Transaction hash: ' + transaction.hex);
177
+ ```typescript
178
+ const { client } = sdk;
169
179
 
170
- // Don't forget to cleanup the resources
171
- poolService.destroy();
172
- api.disconnect();
180
+ const assets = await client.asset.getOnChainAssets();
181
+ console.log(assets);
173
182
  ```
174
183
 
175
- ## Examples
176
-
177
184
  To demonstrate more full working examples on real chain see [script](test/script/examples) section.
178
185
 
179
- ### Execution
180
-
181
- Run: `$ npx tsx ./test/script/examples/<examplePackage>/<exampleName>.ts` with valid example package & name.
182
-
183
- ## Roadmap
184
-
185
- Component list and current status ⬇️
186
-
187
- - 🧪 Done
188
- - 🛠 Work in progress
189
- - ⏳ Planning to build
190
-
191
- | Name | Type | |
192
- | ----------- | :--: | --: |
193
- | Dca | API | ⏳ |
194
- | Router | API | 🧪 |
195
- | TradeRouter | API | 🧪 |
196
- | Twap | API | ⏳ |
197
- | LBP | Math | 🧪 |
198
- | LBP | Pool | 🧪 |
199
- | Omni | Math | 🧪 |
200
- | Omni | Pool | 🧪 |
201
- | Stable | Math | 🧪 |
202
- | Stable | Pool | 🧪 |
203
- | XYK | Math | 🧪 |
204
- | XYK | Pool | 🧪 |
205
- | Aave | Pool | 🧪 |
186
+ Run: `$ npx tsx ./test/script/examples/<examplePackage>/<exampleName>.ts` with valid example package & name.