@paraspell/sdk-pjs 8.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 ParaSpell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,323 @@
1
+ <br /><br />
2
+
3
+ <div align="center">
4
+ <h1 align="center">@paraspell/sdk-pjs</h1>
5
+ <h4 align="center"> SDK for handling XCM asset transfers across Polkadot and Kusama ecosystems. </h4>
6
+ <p align="center">
7
+ <a href="https://npmjs.com/package/@paraspell/sdk-pjs">
8
+ <img alt="version" src="https://img.shields.io/npm/v/@paraspell/sdk-pjs?style=flat-square" />
9
+ </a>
10
+ <a href="https://npmjs.com/package/@paraspell/sdk-pjs">
11
+ <img alt="downloads" src="https://img.shields.io/npm/dm/@paraspell/sdk-pjs?style=flat-square" />
12
+ </a>
13
+ <a href="https://github.com/paraspell/xcm-sdk/actions">
14
+ <img alt="build" src="https://github.com/paraspell/xcm-sdk/actions/workflows/release.yml/badge.svg" />
15
+ </a>
16
+ <a href="https://snyk.io/test/github/paraspell/sdk">
17
+ <img alt="snyk" src="https://snyk.io/test/github/paraspell/sdk/badge.svg" />
18
+ </a>
19
+ </p>
20
+ <p>Supporting every XCM Active Parachain <a href = "https://paraspell.github.io/docs/supported.html"\>[list]</p>
21
+ <p>SDK documentation <a href = "https://paraspell.github.io/docs/" \>[here]</p>
22
+ <p>SDK starter template project <a href = "https://github.com/paraspell/xcm-sdk-template" \>[here]</p>
23
+ </div>
24
+
25
+ <br /><br />
26
+ <br /><br />
27
+
28
+ ## Installation
29
+
30
+ ### Install dependencies
31
+
32
+ ParaSpell XCM SDK is the 🥇 in the ecosystem to support both **PolkadotJS** and **PolkadotAPI**.
33
+
34
+ **This version of SDK uses PolkadotJS** if you wish to use **PolkadotAPI** version please reffer to [following package](https://github.com/paraspell/xcm-tools/tree/main/packages/sdk).
35
+
36
+
37
+ ```bash
38
+ #PolkadotJS peer dependencies
39
+ pnpm | npm install || yarn add @polkadot/api @polkadot/types @polkadot/api-base @polkadot/util @polkadot/util-crypto
40
+ ```
41
+
42
+ ### Install SDK
43
+
44
+ ```bash
45
+ pnpm | npm install || yarn add @paraspell/sdk-pjs
46
+ ```
47
+
48
+ ### Importing package to your project
49
+
50
+ Builder pattern:
51
+ ```js
52
+ import { Builder } from '@paraspell/sdk-pjs'
53
+ ```
54
+
55
+ Other patterns:
56
+ ```js
57
+ // ESM
58
+ import * as paraspell from '@paraspell/sdk-pjs'
59
+
60
+ // CommonJS
61
+ const paraspell = require('@paraspell/sdk-pjs')
62
+ ```
63
+
64
+ Interaction with further asset symbol abstraction:
65
+ ```js
66
+ import { Native, Foreign, ForeignAbstract } from '@paraspell/sdk-pjs'; //Only needed when advanced asset symbol selection is used. PJS version.
67
+ ```
68
+ ## Implementation
69
+
70
+ ```
71
+ NOTES:
72
+ - If you wish to transfer from Parachain that uses long IDs for example Moonbeam you have to add the character 'n' to the end of currencyID. Eg: .currency({id: 42259045809535163221576417993425387648n}) will mean you transfer xcDOT.
73
+ - You can now use custom ParachainIDs if you wish to test in TestNet. Just add parachainID as an additional parameter eg: .to('Basilisk', 2948).
74
+ - POLKADOT <> KUSAMA Bridge is now available! Try sending DOT or KSM between AssetHubs - More information here: https://paraspell.github.io/docs/sdk/xcmPallet.html#ecosystem-bridges.
75
+ - You can now customize XCM Version! Try using .xcmVersion parameter after address in builder.
76
+ - POLKADOT <> ETHEREUM Bridge is now available! Try sending WETH between the ecosystems - More information here: https://paraspell.github.io/docs/sdk/xcmPallet.html#ecosystem-bridges.
77
+ - ParaSpell now offers advanced asset symbol selection {symbol: "symbol"} for non duplicate assets, {symbol: Native("symbol")} or {symbol: Foreign("symbol")} if the duplicates are between native and foreign assets and {symbol: ForeignAbstract("symbol")} if the duplicates are in foreign assets only. You will get an error that will guide you further if you simply start with {symbol: "symbol"}.
78
+ - You can now select assets by multilocation by simply using { multilocation: string | JSON }. The custom multilocation selection remains supported, but in order to use it you now have to use override - {multilocation: Override('Custom Multilocation')}.
79
+ - The balance queries also support multilocation asset selection
80
+ ```
81
+
82
+ ```
83
+ Latest news:
84
+ - You can now query foreign asset minimal deposits also.
85
+ - Since v8, amount moved closer to currency selection and specifying from and to parameters is no longer optional to save code.
86
+ - More information on v8 major breaking change: https://github.com/paraspell/xcm-tools/pull/554
87
+ - XCM SDK Now supports API Failsafe - If one endpoint doesn't work it automatically switches to the next one.
88
+ ```
89
+
90
+ ### Builder pattern:
91
+
92
+ ##### Transfer assets from Parachain to Parachain
93
+ ```ts
94
+ await Builder(/*node api/ws_url_string/ws_url_array - optional*/)
95
+ .from(NODE)
96
+ .to(NODE /*,customParaId - optional*/ | Multilocation object /*Only works for PolkadotXCM pallet*/)
97
+ .currency({id: currencyID, amount: amount} | {symbol: currencySymbol, amount: amount} | {symbol: Native('currencySymbol'), amount: amount} | {symbol: Foreign('currencySymbol'), amount: amount} | {symbol: ForeignAbstract('currencySymbol'), amount: amount} | {multilocation: AssetMultilocationString, amount: amount | AssetMultilocationJson, amount: amount} | {multilocation: Override('Custom Multilocation'), amount: amount} | {multiasset: {currencySelection, isFeeAsset?: true /* for example symbol: symbol or id: id, or multilocation: multilocation*/, amount: amount}})
98
+ .address(address | Multilocation object /*If you are sending through xTokens, you need to pass the destination and address multilocation in one object (x2)*/)
99
+ /*.xcmVersion(Version.V1/V2/V3/V4) //Optional parameter for manual override of XCM Version used in call
100
+ .customPallet('Pallet','pallet_function') //Optional parameter for manual override of XCM Pallet and function used in call (If they are named differently on some node but syntax stays the same). Both pallet name and function required. Pallet name must be CamelCase, function name snake_case.*/
101
+ .build()
102
+ /*
103
+ EXAMPLE:
104
+ const tx = await Builder()
105
+ .from('Acala')
106
+ .to('Astar')
107
+ .currency({
108
+ symbol: 'ACA',
109
+ amount: '1000000000'
110
+ })
111
+ .address(address)
112
+ .build();
113
+ */
114
+ ```
115
+ ##### Transfer assets from the Relay chain to Parachain
116
+ ```ts
117
+ await Builder(/*node api/ws_url_string/ws_url_array - optional*/)
118
+ .from(RELAY_NODE) //Kusama or Polkadot
119
+ .to(NODE/*,customParaId - optional*/ | Multilocation object)
120
+ .currency({symbol: 'DOT', amount: amount})
121
+ .address(address | Multilocation object)
122
+ /*.xcmVersion(Version.V1/V2/V3/V4) //Optional parameter for manual override of XCM Version used in call
123
+ .customPallet('Pallet','pallet_function') //Optional parameter for manual override of XCM Pallet and function used in call (If they are named differently on some node but syntax stays the same). Both pallet name and function required. Pallet name must be CamelCase, function name snake_case.*/
124
+ .build()
125
+ /*
126
+ EXAMPLE:
127
+ const tx = await Builder()
128
+ .from('Polkadot')
129
+ .to('Astar')
130
+ .currency({
131
+ symbol: 'DOT',
132
+ amount: '1000000000'
133
+ })
134
+ .address(address)
135
+ .build();
136
+ */
137
+ ```
138
+ ##### Transfer assets from Parachain to Relay chain
139
+ ```ts
140
+ await Builder(/*node api/ws_url_string/ws_url_array - optional*/)
141
+ .from(NODE)
142
+ .to(RELAY_NODE) //Kusama or Polkadot
143
+ .currency({symbol: 'DOT', amount: amount})
144
+ .address(address | Multilocation object)
145
+ /*.xcmVersion(Version.V1/V2/V3/V4) //Optional parameter for manual override of XCM Version used in call
146
+ .customPallet('Pallet','pallet_function') //Optional parameter for manual override of XCM Pallet and function used in call (If they are named differently on some node but syntax stays the same). Both pallet name and function required. Pallet name must be CamelCase, function name snake_case.*/
147
+ .build()
148
+ /*
149
+ EXAMPLE:
150
+ const tx = await Builder()
151
+ .from('Astar')
152
+ .to('Polkadot')
153
+ .currency({
154
+ symbol: 'DOT',
155
+ amount: '1000000000'
156
+ })
157
+ .address(address)
158
+ .build();
159
+ */
160
+ ```
161
+
162
+ ##### Batch calls
163
+ You can batch XCM calls and execute multiple XCM calls within one call. All three scenarios (Para->Para, Para->Relay, Relay->Para) can be used and combined.
164
+ ```js
165
+ await Builder(/*node api/ws_url_string/ws_url_array - optional*/)
166
+ .from(NODE) //Ensure, that origin node is the same in all batched XCM Calls.
167
+ .to(NODE_2) //Any compatible Parachain
168
+ .currency({currencySelection, amount}) //Currency to transfer - options as in scenarios above
169
+ .address(address | Multilocation object)
170
+ .addToBatch()
171
+
172
+ .from(NODE) //Ensure, that origin node is the same in all batched XCM Calls.
173
+ .to(NODE_3) //Any compatible Parachain
174
+ .currency({currencySelection, amount}) //Currency to transfer - options as in scenarios above
175
+ .address(address | Multilocation object)
176
+ .addToBatch()
177
+ .buildBatch({
178
+ // This settings object is optional and batch all is the default option
179
+ mode: BatchMode.BATCH_ALL //or BatchMode.BATCH
180
+ })
181
+ ```
182
+
183
+ ### Dry run your XCM Calls:
184
+ ```ts
185
+ //Builder pattern
186
+ const result = await Builder(API /*optional*/)
187
+ .from(NODE)
188
+ .to(NODE_2)
189
+ .currency({id: currencyID, amount: amount} | {symbol: currencySymbol, amount: amount} | {symbol: Native('currencySymbol'), amount: amount} | {symbol: Foreign('currencySymbol'), amount: amount} | {symbol: ForeignAbstract('currencySymbol'), amount: amount} | {multilocation: AssetMultilocationString, amount: amount | AssetMultilocationJson, amount: amount} | {multilocation: Override('Custom Multilocation'), amount: amount} | {multiasset: {currencySelection, isFeeAsset?: true /* for example symbol: symbol or id: id, or multilocation: multilocation*/, amount: amount}})
190
+ .address(ADDRESS)
191
+ .dryRun()
192
+
193
+ //Function pattern
194
+ getDryRun({api /*optional*/, node, address, tx /* Extrinsic object */})
195
+ ```
196
+
197
+ ### Asset claim:
198
+ ```ts
199
+ //Claim XCM trapped assets from the selected chain
200
+ await Builder(/*node api/ws_url_string/ws_url_array - optional*/)
201
+ .claimFrom(NODE)
202
+ .fungible(MultilocationArray (Only one multilocation allowed) [{Multilocation}])
203
+ .account(address | Multilocation object)
204
+ /*.xcmVersion(Version.V3) Optional parameter, by default V3. XCM Version ENUM if a different XCM version is needed (Supported V2 & V3). Requires importing Version enum.*/
205
+ .build()
206
+ ```
207
+
208
+ ### Asset queries:
209
+
210
+ ```ts
211
+ // Retrieve assets object from assets.json for particular node including information about native and foreign assets
212
+ paraspell.assets.getAssetsObject(node: TNode)
213
+
214
+ // Retrieve foreign assetId for a particular node and asset symbol
215
+ paraspell.assets.getAssetId(node: TNode, symbol: string)
216
+
217
+ // Retrieve the symbol of the relay chain for a particular node. Either "DOT" or "KSM"
218
+ paraspell.assets.getRelayChainSymbol(node: TNode)
219
+
220
+ // Retrieve string array of native assets symbols for particular node
221
+ paraspell.assets.getNativeAssets(node: TNode)
222
+
223
+ // Retrieve object array of foreign assets for a particular node. Each object has a symbol and assetId property
224
+ paraspell.assets.getOtherAssets(node: TNode)
225
+
226
+ // Retrieve string array of all assets symbols. (native and foreign assets are merged into a single array)
227
+ paraspell.assets.getAllAssetsSymbols(node: TNode)
228
+
229
+ // Check if a node supports a particular asset. (Both native and foreign assets are searched). Returns boolean
230
+ paraspell.assets.hasSupportForAsset(node: TNode, symbol: string)
231
+
232
+ // Get decimals for specific asset
233
+ paraspell.assets.getAssetDecimals(node: TNode, symbol: string)
234
+
235
+ // Get specific node id
236
+ paraspell.assets.getParaId(node: TNode)
237
+
238
+ // Get specific TNode from nodeID
239
+ paraspell.assets.getTNode(nodeID: number)
240
+
241
+ // Import all compatible nodes as constant
242
+ paraspell.NODE_NAMES
243
+ ```
244
+
245
+ ### Parachain XCM Pallet queries
246
+ ```ts
247
+ import { getDefaultPallet, getSupportedPallets, SUPPORTED_PALLETS } from '@paraspell/sdk-pjs';
248
+
249
+ //Retrieve default pallet for specific Parachain
250
+ getDefaultPallet(node: TNode)
251
+
252
+ // Returns an array of supported pallets for a specific Parachain
253
+ getSupportedPallets(node: TNode)
254
+
255
+ // Print all pallets that are currently supported
256
+ console.log(SUPPORTED_PALLETS)
257
+ ```
258
+
259
+ ### Existential deposit queries
260
+ ```ts
261
+ import { getExistentialDeposit } from "@paraspell/sdk-pjs";
262
+
263
+ //Currency is an optional parameter. If you wish to query native asset, currency parameter is not necessary.
264
+ //Currency can be either {symbol: assetSymbol}, {id: assetId}, {multilocation: assetMultilocation}.
265
+ const ed = getExistentialDeposit(node, currency?)
266
+ ```
267
+
268
+ ### XCM Transfer info
269
+ ```ts
270
+ import { getTransferInfo, getBalanceForeign, getBalanceNative, getOriginFeeDetails, getMaxNativeTransferableAmount, getMaxForeignTransferableAmount, getTransferableAmount } from "@paraspell/sdk-pjs";
271
+
272
+ //Get balance of foreign currency
273
+ await getBalanceForeign({address, node, currency /*- {id: currencyID} | {symbol: currencySymbol} | {symbol: Native('currencySymbol')} | {symbol: Foreign('currencySymbol')} | {symbol: ForeignAbstract('currencySymbol')} | {multilocation: AssetMultilocationString | AssetMultilocationJson}*/, api /* api/ws_url_string optional */})
274
+
275
+ //Get balance of native currency
276
+ await getBalanceNative({address, node, api /* api/ws_url_string optional */})
277
+
278
+ //Get fee information regarding XCM call
279
+ await getOriginFeeDetails({from, to, currency /*- {id: currencyID} | {symbol: currencySymbol} | {symbol: Native('currencySymbol')} | {symbol: Foreign('currencySymbol')} | {symbol: ForeignAbstract('currencySymbol')} | {multilocation: AssetMultilocationString | AssetMultilocationJson}*/, amount, originAddress, destinationAddress, api /* api/ws_url_string optional */, feeMargin /* 10% by default */})
280
+
281
+ //Retrieves the asset balance for a given account on a specified node. (You do not need to specify if it is native or foreign).
282
+ await getAssetBalance({address, node, currency /*- {id: currencyID} | {symbol: currencySymbol} | {symbol: Native('currencySymbol')} | {symbol: Foreign('currencySymbol')} | {symbol: ForeignAbstract('currencySymbol')} | {multilocation: AssetMultilocationString | AssetMultilocationJson}*/, api /* api/ws_url_string optional */});
283
+
284
+ //Retrieves maximal transferable balance of chain's native asset (Balance-AssetED) (If a node has more native assets, the asset selection has to be provided. Otherwise the parameter is optional).
285
+ await getMaxNativeTransferableAmount({address, node, currency /*- {symbol: currencySymbol} */})
286
+
287
+ //Retrives maximal transferable balance of chain's foreign asset (Balance-AssetED)
288
+ await getMaxForeignTransferableAmount({address, node, currency /*- {id: currencyID} | {symbol: currencySymbol} | {symbol: Native('currencySymbol')} | {symbol: Foreign('currencySymbol')} | {symbol: ForeignAbstract('currencySymbol')} | {multilocation: AssetMultilocationString | AssetMultilocationJson}*/});
289
+
290
+ //Combines the getMaxNative and getMaxForeign transferable amount functions into one, so you don't have to specify whether you want a native or foreign asset.
291
+ await getTransferableAmount({address, node, currency /*- {id: currencyID} | {symbol: currencySymbol} | {symbol: Native('currencySymbol')} | {symbol: Foreign('currencySymbol')} | {symbol: ForeignAbstract('currencySymbol')} | {multilocation: AssetMultilocationString | AssetMultilocationJson}*/});
292
+
293
+ //Get all the information about XCM transfer
294
+ await getTransferInfo({from, to, address, destinationAddress, currency /*- {id: currencyID} | {symbol: currencySymbol} | {symbol: Native('currencySymbol')} | {symbol: Foreign('currencySymbol')} | {symbol: ForeignAbstract('currencySymbol')} | {multilocation: AssetMultilocationString | AssetMultilocationJson}*/, amount, api /* api/ws_url_string optional */})
295
+ ```
296
+
297
+ ## 💻 Tests
298
+ - Run compilation using `pnpm compile`
299
+
300
+ - Run linter using `pnpm lint`
301
+
302
+ - Run unit tests using `pnpm test`
303
+
304
+ - Run end-to-end tests using `pnpm test:e2e`
305
+
306
+ - Run all core tests and checks using `pnpm runAll`
307
+
308
+ XCM SDK can be tested in [Playground](https://github.com/paraspell/xcm-tools/tree/main/apps/playground).
309
+
310
+ ## License
311
+
312
+ Made with 💛 by [ParaSpell✨](https://github.com/paraspell)
313
+
314
+ Published under [MIT License](https://github.com/paraspell/xcm-tools/blob/main/packages/sdk/LICENSE).
315
+
316
+ ## Support
317
+
318
+ <div align="center">
319
+ <p align="center">
320
+ <img width="200" alt="version" src="https://user-images.githubusercontent.com/55763425/211145923-f7ee2a57-3e63-4b7d-9674-2da9db46b2ee.png" />
321
+ <img width="200" alt="version" src="https://github.com/paraspell/xcm-sdk/assets/55763425/9ed74ebe-9b29-4efd-8e3e-7467ac4caed6" />
322
+ </p>
323
+ </div>