@galacticcouncil/sdk-next 0.1.0 → 0.3.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 (40) hide show
  1. package/README.md +101 -128
  2. package/build/index.cjs +1 -1
  3. package/build/index.mjs +1 -1
  4. package/build/types/aave/AaveClient.d.ts +76 -0
  5. package/build/types/aave/AaveUtils.d.ts +58 -0
  6. package/build/types/aave/abi/AavePool.d.ts +208 -0
  7. package/build/types/aave/abi/AavePoolDataProvider.d.ts +384 -0
  8. package/build/types/aave/abi/index.d.ts +2 -0
  9. package/build/types/aave/const.d.ts +6 -0
  10. package/build/types/aave/index.d.ts +4 -0
  11. package/build/types/aave/types.d.ts +15 -0
  12. package/build/types/api/Papi.d.ts +4 -381
  13. package/build/types/api/index.d.ts +1 -1
  14. package/build/types/api/utils.d.ts +1 -0
  15. package/build/types/client/ChainParams.d.ts +9 -0
  16. package/build/types/client/index.d.ts +1 -0
  17. package/build/types/evm/chain.d.ts +2 -0
  18. package/build/types/evm/client.d.ts +12 -0
  19. package/build/types/evm/index.d.ts +2 -0
  20. package/build/types/factory.d.ts +25 -0
  21. package/build/types/index.d.ts +4 -0
  22. package/build/types/sor/TradeRouteBuilder.d.ts +4 -0
  23. package/build/types/sor/TradeScheduler.d.ts +95 -0
  24. package/build/types/sor/const.d.ts +7 -0
  25. package/build/types/sor/index.d.ts +2 -1
  26. package/build/types/sor/types.d.ts +43 -4
  27. package/build/types/tx/OrderTxBuilder.d.ts +21 -0
  28. package/build/types/tx/TradeTxBuilder.d.ts +18 -0
  29. package/build/types/tx/TxBuilder.d.ts +17 -0
  30. package/build/types/tx/TxBuilderFactory.d.ts +12 -0
  31. package/build/types/tx/index.d.ts +2 -0
  32. package/build/types/tx/types.d.ts +12 -0
  33. package/build/types/tx/utils.d.ts +6 -0
  34. package/build/types/utils/big.d.ts +2 -1
  35. package/build/types/utils/erc20.d.ts +3 -3
  36. package/build/types/utils/h160.d.ts +9 -0
  37. package/build/types/utils/index.d.ts +1 -1
  38. package/package.json +2 -2
  39. package/build/types/sor/TradeUtils.d.ts +0 -12
  40. package/build/types/utils/evm.d.ts +0 -3
package/README.md CHANGED
@@ -4,15 +4,26 @@
4
4
 
5
5
  Next gen hydration sdk build on top of [Polkadot API (Papi)](https://papi.how/).
6
6
 
7
- Table of content:
7
+ ⚠️ **Disclaimer:** Next is not prod ready yet. Official `1.x` release coming soon.
8
+
9
+ Missing functionality & progress tracked [here](https://github.com/galacticcouncil/sdk/issues/119)
10
+
11
+ Table of contents:
8
12
 
9
13
  - [Installation](#installation)
10
14
  - [Usage](#usage)
11
- - [PoolContextProvider](#poolcontextprovider)
12
- - [Router](#router)
15
+ - [Components](#components)
16
+ - [api](#api)
17
+ - [client](#client)
18
+ - [ctx](#ctx)
19
+ - [tx](#tx)
20
+ - [API Reference](#api-reference)
21
+ - [AaveUtils](#aaveutils)
13
22
  - [TradeRouter](#traderouter)
23
+ - [TradeScheduler](#tradescheduler)
14
24
  - [BalanceClient](#balanceclient)
15
25
  - [AssetClient](#assetclient)
26
+ - [Examples](#examples)
16
27
 
17
28
  ## Installation
18
29
 
@@ -22,87 +33,75 @@ Install with [npm](https://www.npmjs.com/):
22
33
 
23
34
  ## Usage
24
35
 
25
- ### PoolContextProvider
26
-
27
- Build and subscribe to the given AMM types, supplying data for the Router API.
28
-
29
- ⚠️ **Note:** Make sure to keep only single instance of context per session.
30
-
31
- #### API Reference (internal)
32
-
33
- | Method | Description |
34
- | :----- | :----------- |
35
- | `getPools(): Promise<PoolBase[]>` | Returns list of available pools. |
36
- | `getPoolFees(pool: PoolBase, feeAsset: number): Promise<PoolFees>` | Returns current pool fees |
37
-
38
- ➡️ For type definitions visit [types.ts](src/pool/types.ts)<br />
39
-
40
- #### Example
41
-
42
- Initialize pool context supporting Omnipool, Stableswap & XYK pools.
36
+ Use `createSdkContext` to quickly set up all components of the SDK — pool context, trading logic, client access, and transaction building — in a single call.
43
37
 
44
- ```typescript
45
- import { api, pool, sor } from '@galacticcouncil/sdk-next';
38
+ ```ts
39
+ import { api, createSdkContext } from '@galacticcouncil/sdk-next';
46
40
 
47
41
  const client = await api.getWs('wss://hydradx-rpc.dwellir.com');
42
+ const sdk = await createSdkContext(client);
48
43
 
49
- const ctx = new pool.PoolContextProvider(client)
50
- .withOmnipool()
51
- .withStableswap()
52
- .withXyk();
53
-
54
- // Don't forget to cleanup the resources
55
- ctx.destroy();
44
+ // Don't forget to cleanup resources when DONE
45
+ sdk.destroy();
56
46
  client.destroy();
57
47
  ```
58
48
 
59
- ### Router
49
+ It handles all necessary setup under the hood. Just plug in your PolkadotClient, and you're ready to interact with registry, accounts, pools, router, and more.
60
50
 
61
- Off-chain routing, build to find the most suitable routes across the pools. Building block for `TradeRouter`.
51
+ ⚠️ **Note:** Make sure to keep only single instance of context per session.
62
52
 
63
- #### API Reference
53
+ ## Components
64
54
 
65
- | Method | Description |
66
- | :----- | :----------- |
67
- | `getPools(): PoolBase[]` | Returns the current list of available pools. |
68
- | `getRoutes(assetIn: number, assetOut: number): Hop[][]` | Computes possible routes between two assets. |
69
- | `getTradeableAssets(): number[]` | Lists all assets that are tradeable through the router. |
55
+ ### `api`
70
56
 
71
- ➡️ For type definitions visit [types.ts](src/sor/types.ts)<br />
57
+ - `aave: AaveUtils` Aave-related utilities.
58
+ - `router: TradeRouter` — Off-chain optimization of trades across pools for best price execution.
59
+ - `scheduler: TradeScheduler` — Trade orders scheduling.
72
60
 
73
- #### Example
61
+ ### `client`
74
62
 
75
- List all tradable assets available in the current pool context.
63
+ - `asset: AssetClient` Registry metadata and lookup.
64
+ - `balance: BalanceClient` — Account balance tracking.
65
+ - `evm: EvmClient` — Interacts with EVM.
76
66
 
77
- ```typescript
78
- import { api, pool, sor } from '@galacticcouncil/sdk-next';
67
+ ### `ctx`
79
68
 
80
- const client = await api.getWs('wss://hydradx-rpc.dwellir.com');
69
+ - `pool: PoolContextProvider` Internal stateful pool context. Initialized with support for:
70
+ - Aave
71
+ - Omnipool
72
+ - Stableswap
73
+ - XYK pools
74
+ - LBP pools
81
75
 
82
- // Make sure to keep only single instance of context per session
83
- const ctx = new pool.PoolContextProvider(client)
84
- .withOmnipool()
85
- .withStableswap()
86
- .withXyk();
76
+ ### `tx`
87
77
 
88
- const router = new sor.Router(ctx);
78
+ - `TxBuilderFactory` Factory for generating submittable transaction using fluent APIs.
89
79
 
90
- const assets = await router.getTradeableAssets();
91
- console.log(assets);
80
+ ### `destroy()`
92
81
 
93
- // Don't forget to cleanup the resources
94
- ctx.destroy();
95
- client.destroy();
96
- ```
82
+ Gracefully cleans up SDK resources. Always call before exiting to avoid memory leaks or stale subscriptions.
97
83
 
98
- ### TradeRouter
84
+ ## API Reference
85
+
86
+ ### AaveUtils
87
+
88
+ | Method | Description |
89
+ | :----- | :----------- |
90
+ | `getSummary(user: string): AaveSummary` | Returns market summary. |
91
+ | `getHealthFactor(user: string): number` | Calculate HF. |
92
+ | `getHealthFactorAfterWithdraw(user: string, reserve:number, withdrawAmount: string): number` | Calculate HF after withdraw. |
93
+ | `getHealthFactorAfterSupply(user: string, reserve:number, supplyAmount: string): number` | Calculate HF after supply. |
94
+ | `getMaxWithdraw(user: string, reserve:number): Amount` | Get max possible safe withdraw. |
99
95
 
100
- Off-chain optimization of orders across pools for best price execution. TradeRouter does not perform any on-chain transations.
96
+ ➡️ For type definitions visit [types.ts](src/aave/types.ts)<br />
101
97
 
102
- #### Api Reference
98
+ ### TradeRouter
103
99
 
104
100
  | Method | Description |
105
101
  | :----- | :----------- |
102
+ | `getPools(): PoolBase[]` | Returns the current list of available pools. |
103
+ | `getRoutes(assetIn: number, assetOut: number): Hop[][]` | Computes possible routes between two assets. |
104
+ | `getTradeableAssets(): number[]` | Lists all assets that are tradeable through the router. |
106
105
  | `getBestSell(tokenIn: number, tokenOut: number, amountIn: bigint \| string): Trade` | Find the best sell trade for given input amount. |
107
106
  | `getBestBuy(tokenIn: number, tokenOut: number, amountOut: bigint \| string): Trade` | Find the best buy trade for given output amount. |
108
107
  | `getBuy(tokenIn: number, tokenOut: number, amountOut: bigint \| string, route?: Hop[]): Trade` | Calculate a buy using a specific route (optional). |
@@ -112,112 +111,86 @@ Off-chain optimization of orders across pools for best price execution. TradeRou
112
111
 
113
112
  ➡️ For type definitions visit [types.ts](src/sor/types.ts)<br />
114
113
 
115
- #### Example
114
+ ### TradeScheduler
116
115
 
117
- Calculate sell of 1 DOT for HDX & build tx with 5% slippage (default to 1% if not specified)
116
+ | Method | Description |
117
+ | :----- | :----------- |
118
+ | `getDcaOrder(assetIn: number, assetOut: number, amountInTotal: string, duration: number): TradeDcaOrder` | Calculate DCA order. |
119
+ | `getTwapBuyOrder(assetIn: number, assetOut: number, amountInTotal: string): TradeOrder` | Calculate TWAP buy order. |
120
+ | `getTwapSellOrder(assetIn: number, assetOut: number, amountInTotal: string): TradeOrder` | Calculate TWAP buy order. |
118
121
 
119
- ```typescript
120
- import { api, pool, sor } from '@galacticcouncil/sdk-next';
122
+ ➡️ For type definitions visit [types.ts](src/sor/types.ts)<br />
121
123
 
122
- const client = await api.getWs('wss://hydradx-rpc.dwellir.com');
124
+ ### BalanceClient
123
125
 
124
- const ctx = new pool.PoolContextProvider(client)
125
- .withOmnipool()
126
- .withStableswap()
127
- .withXyk();
126
+ | Method | Description |
127
+ | :----- | :----------- |
128
+ | `subscribeSystemBalance(address: string): Observable<AssetAmount>` | Subscribe native account balance. |
129
+ | `subscribeTokenBalance(address: string, assetId: number): Observable<AssetAmount>` | Subscribe token account balance. |
130
+ | `subscribeTokensBalance(address: string): Observable<AssetAmount[]>` | Subscribe tokens account balances. |
131
+ | `subscribeErc20Balance(address: string, includeOnly?: number[]): Observable<AssetAmount[]>` | Subscribe erc20 assets balances |
128
132
 
129
- const router = new sor.TradeRouter(ctx);
130
- const utils = new sor.TradeUtils(client);
133
+ ➡️ For type definitions visit [types.ts](src/types.ts)<br />
131
134
 
132
- const sell = await router.getBestSell(5, 0, 10_000_000_000n);
133
- const tx = await utils.buildSellTx(sell, 5);
134
- console.log(sell.toHuman());
135
- console.log('Transaction hash:', tx.asHex());
135
+ ### AssetClient
136
136
 
137
- // Don't forget to cleanup the resources
138
- ctx.destroy();
139
- client.destroy();
140
- ```
137
+ | Method | Description |
138
+ | :----- | :----------- |
139
+ | `getOnChainAssets(includeInvalid?: boolean, external?: ExternalAsset[]): Promise<Asset[]>` | Returns assets with metadata from registry. |
141
140
 
142
- **Note:** For convenience, the router amount can be specified either as a native bigint or as a human-readable string.
141
+ ➡️ For type definitions visit [types.ts](src/types.ts)<br />
143
142
 
144
- For example, `"1"` DOT (string) is equivalent to `10_000_000_000n` (bigint), as DOT has 10 decimals.
143
+ ## Examples
145
144
 
146
- ### BalanceClient
145
+ All examples assume sdk have been initialized [see](#usage)
147
146
 
148
- Helper class supporting the following standards:
147
+ ### TradeRouter
149
148
 
150
- - "Native assets"
151
- - "Token assets"
152
- - "ERC-20 tokens"
149
+ Calculate sell of 1 DOT for HDX & build tx with 5% slippage (default to 1% if not specified)
153
150
 
154
- #### API Reference
151
+ ```typescript
152
+ const { api, tx } = sdk;
153
+
154
+ const trade = await api.router.getBestSell(5, 10, 10_000_000_000n);
155
+ const tradeTx = await tx.trade(trade)
156
+ .withBeneficiary(BENEFICIARY)
157
+ .withSlippage(5)
158
+ .build();
159
+ const tradeCall = await tradeTx.get().getEncodedData();
160
+ console.log(trade.toHuman());
161
+ console.log('Transaction hash:', tradeCall.asHex());
162
+ ```
155
163
 
156
- | Method | Description |
157
- | :----- | :----------- |
158
- | `subscribeSystemBalance(address: string): Observable<AssetAmount>` | Subscribe native account balance. |
159
- | `subscribeTokenBalance(address: string, assetId: number): Observable<AssetAmount>` | Subscribe token account balance. |
160
- | `subscribeTokensBalance(address: string): Observable<AssetAmount[]>` | Subscribe tokens account balances. |
161
- | `subscribeErc20Balance(address: string, includeOnly?: number[]): Observable<AssetAmount[]>` | Subscribe erc20 assets balances |
164
+ **Note:** For convenience, the router amount can be specified either as a native bigint or as a human-readable string.
162
165
 
163
- ➡️ For type definitions visit [types.ts](src/types.ts)<br />
166
+ For example, `"1"` DOT (string) is equivalent to `10_000_000_000n` (bigint), as DOT has 10 decimals.
164
167
 
165
- #### Example
168
+ ### BalanceClient
166
169
 
167
170
  Subscribe hydration treasury account.
168
171
 
169
172
  ```typescript
170
- import { api, client as c } from '@galacticcouncil/sdk-next';
173
+ const { client } = sdk;
171
174
 
172
175
  const account = "7L53bUTBbfuj14UpdCNPwmgzzHSsrsTWBHX5pys32mVWM3C1"
173
-
174
- const client = await api.getWs('wss://hydradx-rpc.dwellir.com');
175
- const balanceClient = new c.BalanceClient(client);
176
-
177
- const subscription = balanceClient
176
+ const subscription = client.balance
178
177
  .subscribeBalance(account)
179
178
  .subscribe((balances) => {
180
179
  console.log(balances);
181
180
  });
182
-
183
- // Don't forget to cleanup the resources
184
- subscription.unsubscribe();
185
- client.destroy();
186
181
  ```
187
182
 
188
183
  ### AssetClient
189
184
 
190
- Helper class to fetch registry metadata.
191
-
192
- #### API Reference
193
-
194
- | Method | Description |
195
- | :----- | :----------- |
196
- | `getOnChainAssets(includeInvalid?: boolean, external?: ExternalAsset[]): Promise<Asset[]>` | Returns assets with metadata from registry. |
197
-
198
- ➡️ For type definitions visit [types.ts](src/types.ts)<br />
199
-
200
- #### Example
201
-
202
185
  Get default on-chain data.
203
186
 
204
187
  ```typescript
205
- import { api, client as c } from '@galacticcouncil/sdk-next';
206
-
207
- const client = await api.getWs('wss://hydradx-rpc.dwellir.com');
208
- const assetClient = new c.AssetClient(client);
188
+ const { client } = sdk;
209
189
 
210
- const assets = await assetClient.getOnChainAssets();
190
+ const assets = await client.asset.getOnChainAssets();
211
191
  console.log(assets);
212
-
213
- // Don't forget to cleanup the resources
214
- client.destroy();
215
192
  ```
216
193
 
217
- ## Examples
218
-
219
194
  To demonstrate more full working examples on real chain see [script](test/script/examples) section.
220
195
 
221
- ### Execution
222
-
223
196
  Run: `$ npx tsx ./test/script/examples/<examplePackage>/<exampleName>.ts` with valid example package & name.