@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 +103 -104
- package/build/index.cjs +2 -2
- package/build/index.mjs +2 -2
- package/build/types/aave/AaveClient.d.ts +2 -1
- package/build/types/aave/AaveUtils.d.ts +11 -3
- package/build/types/aave/types.d.ts +3 -3
- package/build/types/api/PolkadotApi.d.ts +0 -4
- package/build/types/api/index.d.ts +0 -1
- package/build/types/api/utils.d.ts +1 -0
- package/build/types/client/BalanceClient.d.ts +2 -3
- package/build/types/client/ChainParams.d.ts +10 -0
- package/build/types/client/index.d.ts +1 -0
- package/build/types/evm/client.d.ts +1 -0
- package/build/types/factory.d.ts +25 -0
- package/build/types/index.d.ts +2 -0
- package/build/types/sor/Router.d.ts +0 -5
- package/build/types/sor/TradeRouteBuilder.d.ts +4 -0
- package/build/types/sor/TradeScheduler.d.ts +96 -0
- package/build/types/sor/const.d.ts +7 -0
- package/build/types/sor/index.d.ts +2 -1
- package/build/types/sor/types.d.ts +43 -5
- package/build/types/tx/OrderTxBuilder.d.ts +21 -0
- package/build/types/tx/TradeTxBuilder.d.ts +19 -0
- package/build/types/tx/TxBuilder.d.ts +19 -0
- package/build/types/tx/TxBuilderFactory.d.ts +12 -0
- package/build/types/tx/index.d.ts +2 -0
- package/build/types/utils/bignumber.d.ts +2 -0
- package/build/types/utils/error.d.ts +3 -0
- package/build/types/utils/fee.d.ts +6 -0
- package/build/types/utils/h160.d.ts +0 -2
- package/package.json +1 -1
- package/build/types/sor/TradeUtils.d.ts +0 -25
- package/build/types/utils/mapper.d.ts +0 -6
- /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
|
|
7
|
+
Table of contents:
|
|
8
8
|
|
|
9
9
|
- [Installation](#installation)
|
|
10
10
|
- [Troubleshooting](#troubleshooting)
|
|
11
11
|
- [Usage](#usage)
|
|
12
|
-
|
|
13
|
-
- [
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
68
|
-
|
|
58
|
+
const api = await ApiPromise.create({
|
|
59
|
+
provider: wsProvider,
|
|
60
|
+
});
|
|
69
61
|
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
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
|
-
|
|
71
|
+
⚠️ **Note:** Make sure to keep only single instance of context per session.
|
|
78
72
|
|
|
79
|
-
|
|
73
|
+
## Components
|
|
80
74
|
|
|
81
|
-
|
|
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
|
-
|
|
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
|
-
|
|
81
|
+
### `client`
|
|
91
82
|
|
|
92
|
-
|
|
83
|
+
- `asset: AssetClient` — Registry metadata and lookup.
|
|
84
|
+
- `balance: BalanceClient` — Account balance tracking.
|
|
85
|
+
- `evm: EvmClient` — Interacts with EVM.
|
|
93
86
|
|
|
94
|
-
|
|
95
|
-
import { ApiPromise, WsProvider } from '@polkadot/api';
|
|
96
|
-
import { TradeRouter, PoolService, PoolType } from '@galacticcouncil/sdk';
|
|
87
|
+
### `ctx`
|
|
97
88
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
103
|
-
const router = new Router(poolService);
|
|
96
|
+
### `tx`
|
|
104
97
|
|
|
105
|
-
|
|
106
|
-
console.log(assets);
|
|
98
|
+
- `TxBuilderFactory` — Factory for generating submittable transaction using fluent APIs.
|
|
107
99
|
|
|
108
|
-
|
|
109
|
-
poolService.destroy();
|
|
110
|
-
api.disconnect();
|
|
111
|
-
```
|
|
100
|
+
### `destroy()`
|
|
112
101
|
|
|
113
|
-
|
|
102
|
+
Gracefully cleans up SDK resources. Always call before exiting to avoid memory leaks or stale subscriptions.
|
|
114
103
|
|
|
115
|
-
|
|
104
|
+
## API Reference
|
|
116
105
|
|
|
117
|
-
|
|
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
|
-
| `
|
|
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
|
-
|
|
135
|
+
### TradeScheduler
|
|
131
136
|
|
|
132
|
-
|
|
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
|
-
|
|
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
|
-
|
|
139
|
-
|
|
140
|
-
|
|
145
|
+
### AssetClient
|
|
146
|
+
|
|
147
|
+
| Method | Description |
|
|
148
|
+
| :----- | :----------- |
|
|
149
|
+
| `getOnChainAssets(includeInvalid?: boolean, external?: ExternalAsset[]): Promise<Asset[]>` | Returns assets with metadata from registry. |
|
|
141
150
|
|
|
142
|
-
|
|
143
|
-
const txUtils = new TradeUtils(api);
|
|
151
|
+
➡️ For type definitions visit [types.ts](src/types.ts)<br />
|
|
144
152
|
|
|
145
|
-
|
|
153
|
+
## Examples
|
|
146
154
|
|
|
147
|
-
|
|
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
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|