@moonbeam-network/xcm-sdk 1.0.0-dev.3 → 1.0.0-dev.300

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 CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2022 PureStake
1
+ Copyright 2024 Moonbeam Foundation
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
package/README.md CHANGED
@@ -1,18 +1,10 @@
1
- ![Moonbeam](https://docs.moonbeam.network/images/builders/interoperability/xcm/sdk/xcm-sdk-banner.png)
1
+ The Moonbeam XCM SDK enables developers to easily transfer assets between chains, either between parachains or between a parachain and the relay chain, within the Polkadot/Kusama ecosystem. With the SDK, you don't need to worry about determining the multilocation of the origin or destination assets or which extrinsics are used on which networks to send XCM transfers.
2
2
 
3
- The Moonbeam XCM SDK enables developers to easily deposit and withdraw assets to Moonbeam/Moonriver from the relay chain and other parachains in the Polkadot/Kusama ecosystem. With the SDK, you don't need to worry about determining the multilocation of the origin or destination assets or which extrinsics are used on which networks to send XCM transfers. To deposit or withdraw assets, you simply define the asset and origin chain you want to deposit from or withdraw back to, along with the sending account's signer, and the amount to send.
3
+ The XCM SDK offers helper functions that provide a very simple interface to execute XCM transfers between chains in the Polkadot/Kusama ecosystem. In addition, the XCM config package allows any parachain project to add their information in a standard way, so they can be immediately supported by the XCM SDK.
4
4
 
5
5
  # Documentation
6
6
 
7
- ## v1 (current)
8
-
9
- - TODO: (coming soon)
10
-
11
- ## v0 (previous)
12
-
13
- - [readme](https://github.com/PureStake/xcm-sdk/tree/v0)
14
- - [usage](https://docs.moonbeam.network/builders/xcm/xcm-sdk/xcm-sdk/)
15
- - [references](https://docs.moonbeam.network/builders/xcm/xcm-sdk/reference/)
7
+ You can find the documentation at [https://moonbeam-foundation.github.io/xcm-sdk/latest/](https://moonbeam-foundation.github.io/xcm-sdk/latest/).
16
8
 
17
9
  # Installation
18
10
 
@@ -23,34 +15,72 @@ npm i @moonbeam-network/xcm-sdk
23
15
  :warning: You need to have peer dependencies of SDK installed as well.
24
16
 
25
17
  ```bash
26
- npm i @polkadot/api @polkadot/api-augment @polkadot/types @polkadot/util @polkadot/util-crypto @polkadot/apps-config ethers
18
+ npm i @polkadot/api @polkadot/util-crypto
27
19
  ```
28
20
 
29
21
  # Usage
30
22
 
31
- - TODO: (coming soon) - in the meantime, see examples below
23
+ The following sections contain basic examples of how to work with the XCM SDK to build transfer data to transfer an asset from one chain to another and how to submit the transfer. For a detailed overview on how to use it, please refer to the [XCM SDK docs](https://moonbeam-foundation.github.io/xcm-sdk/latest/example-usage/xcm).
24
+
25
+ ## Build XCM Transfer Data
26
+
27
+ ```js
28
+ import { Sdk } from '@moonbeam-network/xcm-sdk';
29
+
30
+ const fromPolkadot = async () => {
31
+ const transferData = await Sdk()
32
+ .setAsset(dot)
33
+ .setSource(polkadot)
34
+ .setDestination(moonbeam)
35
+ .setAddresses({
36
+ sourceAddress: pair.address,
37
+ destinationAddress: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
38
+ });
39
+ };
40
+
41
+ fromPolkadot();
42
+ ```
43
+
44
+ ## Transfer
45
+
46
+ ```js
47
+ ...
48
+
49
+ const hash = await data.transfer({
50
+ amount: 1,
51
+ signers: { polkadotSigner: pair }, // Insert the signer, in this case a Substrate key pair
52
+ });
53
+
54
+ ```
32
55
 
33
56
  # Examples
34
57
 
35
- - [sdk](https://github.com/PureStake/xcm-sdk/blob/main/examples/sdk-simple)
58
+ - [sdk](https://github.com/moonbeam-foundation/xcm-sdk/blob/main/examples/sdk-simple)
36
59
 
37
60
  ```bash
38
- git clone git@github.com:PureStake/xcm-sdk.git
61
+ git clone git@github.com:moonbeam-foundation/xcm-sdk.git
39
62
  cd xcm-sdk
40
- npm i
63
+ pnpm install
41
64
  cd examples/sdk-simple
42
65
 
43
66
  # edit index.ts by adding your accounts
44
67
 
45
- npm start
68
+ pnpm run start
46
69
  ```
47
70
 
48
71
  # Contributing
49
72
 
73
+ First fork the repository and clone it.
74
+
75
+ ```bash
76
+ git clone git@github.com:YOUR_GITHUB_USERNAME/xcm-sdk.git
77
+ pnpm install
78
+ ```
79
+
80
+ Optionally, you can install the `pre-commit` hook to run the linter and tests before committing:
81
+
50
82
  ```bash
51
- git clone git@github.com:PureStake/xcm-sdk.git
52
- npm i
53
- npm run dev
83
+ pnpm lefthook install
54
84
  ```
55
85
 
56
86
  # Tests
@@ -58,14 +88,49 @@ npm run dev
58
88
  ## Unit tests
59
89
 
60
90
  ```bash
61
- npm run test
91
+ pnpm run test
62
92
  ```
63
93
 
64
94
  ## Acceptance tests
65
95
 
66
96
  ```bash
67
- cp .env.example .env
68
- # add private key and suri to .env file
97
+ pnpm run test:acc
98
+ ```
99
+
100
+ # Release
101
+
102
+ To create a dev version go to GitHub actions and run `publish dev versions` workflow.
69
103
 
70
- npm run test:acc
104
+ To create a release version run:
105
+
106
+ ```bash
107
+ pnpm run changeset
108
+ ```
109
+
110
+ # Testing the change in the SDK locally
111
+
112
+ Build the project:
113
+
114
+ ```bash
115
+ pnpm run build
116
+ ```
117
+
118
+ Link the SDK:
119
+
120
+ ```bash
121
+ pnpm run clean && pnpm run build && pnpm run link
122
+ ```
123
+
124
+ In your project where you would like to test the changes:
125
+
126
+ ```bash
127
+ pnpm link @moonbeam-network/xcm-types @moonbeam-network/xcm-utils @moonbeam-network/xcm-builder @moonbeam-network/xcm-config @moonbeam-network/xcm-sdk
128
+ ```
129
+
130
+ If you need you can link other packages too.
131
+
132
+ After testing is done, unlink the SDK:
133
+
134
+ ```bash
135
+ pnpm unlink @moonbeam-network/xcm-types @moonbeam-network/xcm-utils @moonbeam-network/xcm-builder @moonbeam-network/xcm-config @moonbeam-network/xcm-sdk
71
136
  ```
@@ -0,0 +1,236 @@
1
+ import { AssetRoute, FeeConfig, ConfigService } from '@moonbeam-network/xcm-config';
2
+ import * as _moonbeam_network_xcm_types from '@moonbeam-network/xcm-types';
3
+ import { AssetAmount, AnyChain, AnyParachain, ChainAsset, EvmChain, EvmParachain, Asset, Ecosystem, AnyAsset } from '@moonbeam-network/xcm-types';
4
+ import { Signer, SubmittableExtrinsic } from '@polkadot/api/types';
5
+ import { IKeyringPair, ISubmittableResult } from '@polkadot/types/types';
6
+ import { WalletClient, PublicClient, HttpTransport, StateOverride, Hash, Address } from 'viem';
7
+ import { ContractConfig, ExtrinsicConfig, BalanceConfigBuilder, FeeConfigBuilder, AssetMinConfigBuilder, EvmQueryConfig, SubstrateQueryConfig, EventMonitoringConfig } from '@moonbeam-network/xcm-builder';
8
+ import { ApiPromise } from '@polkadot/api';
9
+ import { RuntimeDispatchInfo, EventRecord } from '@polkadot/types/interfaces';
10
+ import { HexString } from '@polkadot/util/types';
11
+
12
+ type EvmSigner = WalletClient;
13
+ interface Signers {
14
+ evmSigner?: EvmSigner;
15
+ polkadotSigner?: Signer | IKeyringPair;
16
+ }
17
+ interface TransferData {
18
+ destination: DestinationChainTransferData;
19
+ getEstimate(amount: number | string): AssetAmount;
20
+ max: AssetAmount;
21
+ min: AssetAmount;
22
+ source: SourceChainTransferData;
23
+ transfer(params: TransferParams): Promise<string>;
24
+ }
25
+ interface TransferParams {
26
+ amount: number | string;
27
+ signers: Partial<Signers>;
28
+ statusCallback?: (status: ISubmittableResult) => void;
29
+ onSourceFinalized?: () => void;
30
+ onSourceError?: (error: Error) => void;
31
+ onDestinationFinalized?: () => void;
32
+ onDestinationError?: (error: Error) => void;
33
+ }
34
+ interface SourceChainTransferData extends ChainTransferData {
35
+ destinationFee: AssetAmount;
36
+ destinationFeeBalance: AssetAmount;
37
+ feeBalance: AssetAmount;
38
+ max: AssetAmount;
39
+ }
40
+ interface SovereignAccountBalance {
41
+ feeAssetBalance: bigint | undefined;
42
+ transferAssetBalance: bigint;
43
+ }
44
+ interface DestinationChainTransferData extends ChainTransferData {
45
+ sovereignAccountBalances?: SovereignAccountBalance;
46
+ }
47
+ interface ChainTransferData {
48
+ balance: AssetAmount;
49
+ chain: AnyChain;
50
+ existentialDeposit?: AssetAmount;
51
+ fee: AssetAmount;
52
+ min: AssetAmount;
53
+ }
54
+
55
+ interface GetDestinationDataParams {
56
+ route: AssetRoute;
57
+ destinationAddress: string;
58
+ }
59
+ declare function getDestinationData({ route, destinationAddress, }: GetDestinationDataParams): Promise<DestinationChainTransferData>;
60
+
61
+ interface GetSourceDataParams {
62
+ route: AssetRoute;
63
+ destinationAddress: string;
64
+ destinationFee: AssetAmount;
65
+ sourceAddress: string;
66
+ }
67
+ declare function getSourceData({ route, destinationAddress, destinationFee, sourceAddress, }: GetSourceDataParams): Promise<SourceChainTransferData>;
68
+ interface GetFeeParams {
69
+ balance: AssetAmount;
70
+ feeBalance: AssetAmount;
71
+ contract?: ContractConfig;
72
+ chain: AnyParachain;
73
+ destinationFee: AssetAmount;
74
+ extrinsic?: ExtrinsicConfig;
75
+ feeConfig?: FeeConfig;
76
+ sourceAddress: string;
77
+ }
78
+ declare function getFee({ balance, feeBalance, chain, contract, destinationFee, extrinsic, feeConfig, sourceAddress, }: GetFeeParams): Promise<AssetAmount>;
79
+ interface GetAssetsBalancesParams {
80
+ address: string;
81
+ chain: AnyParachain;
82
+ routes: AssetRoute[];
83
+ evmSigner?: EvmSigner;
84
+ }
85
+ declare function getAssetsBalances({ address, chain, routes, }: GetAssetsBalancesParams): Promise<AssetAmount[]>;
86
+
87
+ interface GetBalancesParams {
88
+ address: string;
89
+ asset: ChainAsset;
90
+ builder: BalanceConfigBuilder;
91
+ chain: AnyChain;
92
+ }
93
+ declare function getBalance({ address, asset, builder, chain, }: GetBalancesParams): Promise<AssetAmount>;
94
+ interface GetMinParams {
95
+ asset: Asset;
96
+ builder?: AssetMinConfigBuilder;
97
+ chain: AnyChain;
98
+ }
99
+ declare function getAssetMin({ asset, builder, chain, }: GetMinParams): Promise<AssetAmount>;
100
+ declare function getMin({ balance, existentialDeposit, fee, min, }: DestinationChainTransferData): AssetAmount;
101
+ interface GetMaxParams {
102
+ balance: AssetAmount;
103
+ existentialDeposit?: AssetAmount;
104
+ fee: AssetAmount;
105
+ min: AssetAmount;
106
+ }
107
+ declare function getMax({ balance, existentialDeposit, fee, min, }: GetMaxParams): AssetAmount;
108
+ interface GetDestinationFeeParams {
109
+ address: string;
110
+ asset: Asset;
111
+ feeAsset: Asset;
112
+ destination: AnyChain;
113
+ fee: number | FeeConfigBuilder;
114
+ source: AnyChain;
115
+ }
116
+ declare function getDestinationFee({ address, asset, destination, fee, feeAsset, source, }: GetDestinationFeeParams): Promise<AssetAmount>;
117
+ interface ConvertToChainDecimalsParams {
118
+ asset: AssetAmount;
119
+ target: ChainAsset;
120
+ }
121
+ declare function convertToChainDecimals({ asset, target, }: ConvertToChainDecimalsParams): AssetAmount;
122
+ declare function getExistentialDeposit(chain: AnyChain): Promise<AssetAmount | undefined>;
123
+ interface GetDestinationFeeBalanceParams {
124
+ balance: AssetAmount;
125
+ feeBalance: AssetAmount;
126
+ route: AssetRoute;
127
+ sourceAddress: string;
128
+ }
129
+ declare function getDestinationFeeBalance({ balance, feeBalance, route, sourceAddress, }: GetDestinationFeeBalanceParams): Promise<AssetAmount>;
130
+ interface GetExtrinsicFeeParams {
131
+ address: string;
132
+ balance: AssetAmount;
133
+ chain: AnyParachain;
134
+ extrinsic: ExtrinsicConfig;
135
+ feeBalance: AssetAmount;
136
+ feeConfig?: FeeConfig;
137
+ }
138
+ declare function getExtrinsicFee({ address, balance, chain, extrinsic, feeBalance, feeConfig, }: GetExtrinsicFeeParams): Promise<AssetAmount>;
139
+ interface GetContractFeeParams {
140
+ address: string;
141
+ balance: AssetAmount;
142
+ chain: EvmChain | EvmParachain;
143
+ contract: ContractConfig;
144
+ destinationFee: AssetAmount;
145
+ feeBalance: AssetAmount;
146
+ feeConfig?: FeeConfig;
147
+ }
148
+ declare function getContractFee({ address, balance, chain, contract, destinationFee, feeBalance, feeConfig, }: GetContractFeeParams): Promise<AssetAmount>;
149
+ interface ValidateSovereignAccountBalancesProps {
150
+ amount: bigint;
151
+ destinationData: DestinationChainTransferData;
152
+ sourceData: SourceChainTransferData;
153
+ }
154
+ declare function validateSovereignAccountBalances({ amount, sourceData, destinationData, }: ValidateSovereignAccountBalancesProps): void;
155
+
156
+ interface SdkOptions {
157
+ configService?: ConfigService;
158
+ ecosystem?: Ecosystem;
159
+ }
160
+ declare function Sdk({ configService, ecosystem }?: SdkOptions): {
161
+ assets: _moonbeam_network_xcm_types.Asset[];
162
+ setAsset(asset: string | AnyAsset): {
163
+ sources: AnyChain[];
164
+ setSource(source: string | AnyChain): {
165
+ destinations: AnyChain[];
166
+ setDestination(destination: string | AnyChain): {
167
+ setAddresses({ sourceAddress, destinationAddress, }: {
168
+ sourceAddress: string;
169
+ destinationAddress: string;
170
+ }): Promise<TransferData>;
171
+ };
172
+ };
173
+ };
174
+ };
175
+ declare function getParachainBalances(chain: AnyParachain, address: string, service?: ConfigService): Promise<AssetAmount[]>;
176
+
177
+ declare class EvmService {
178
+ readonly chain: EvmChain | EvmParachain;
179
+ readonly client: PublicClient<HttpTransport>;
180
+ static create(chain: EvmChain | EvmParachain): EvmService;
181
+ constructor(chain: EvmChain | EvmParachain);
182
+ query(query: EvmQueryConfig): Promise<bigint>;
183
+ read(config: ContractConfig): Promise<unknown>;
184
+ getFee(address: string, contract: ContractConfig, stateOverride?: StateOverride): Promise<bigint>;
185
+ getBalance(address: string, contract: ContractConfig): Promise<bigint>;
186
+ transfer(signer: EvmSigner, contract: ContractConfig): Promise<Hash>;
187
+ }
188
+
189
+ declare class PolkadotService {
190
+ #private;
191
+ readonly api: ApiPromise;
192
+ readonly chain: AnyParachain;
193
+ constructor(api: ApiPromise, chain: AnyParachain);
194
+ static create(chain: AnyParachain): Promise<PolkadotService>;
195
+ static createMulti(chains: AnyParachain[]): Promise<PolkadotService[]>;
196
+ get decimals(): number;
197
+ get existentialDeposit(): AssetAmount;
198
+ query(config: SubstrateQueryConfig): Promise<bigint>;
199
+ getExtrinsic(config: ExtrinsicConfig): SubmittableExtrinsic<'promise', ISubmittableResult>;
200
+ getExtrinsicCallHash(config: ExtrinsicConfig): HexString;
201
+ getPaymentInfo(account: string, config: ExtrinsicConfig): Promise<RuntimeDispatchInfo>;
202
+ getFee(account: string, config: ExtrinsicConfig): Promise<bigint>;
203
+ transfer(account: string, config: ExtrinsicConfig, signer: Signer | IKeyringPair, statusCallback?: (params: ISubmittableResult) => void): Promise<string>;
204
+ }
205
+
206
+ declare function getAllowanceSlot(owner: string, spender: string, allowanceSlot: number): Address;
207
+ declare const MAX_ALLOWANCE_HEX: `0x${string}`;
208
+
209
+ interface ListenToDestinationEventsProps {
210
+ route: AssetRoute;
211
+ monitoringConfig: EventMonitoringConfig;
212
+ messageId?: string;
213
+ onDestinationFinalized?: () => void;
214
+ onDestinationError?: (error: Error) => void;
215
+ }
216
+ declare function listenToDestinationEvents({ route, monitoringConfig, messageId, onDestinationFinalized, onDestinationError, }: ListenToDestinationEventsProps): Promise<void>;
217
+ interface CreateMonitoringCallbackProps {
218
+ sourceAddress: string;
219
+ route: AssetRoute;
220
+ statusCallback?: (status: ISubmittableResult) => void;
221
+ onSourceFinalized?: () => void;
222
+ onSourceError?: (error: Error) => void;
223
+ onDestinationFinalized?: () => void;
224
+ onDestinationError?: (error: Error) => void;
225
+ }
226
+ interface ListenToSourceEventsProps extends CreateMonitoringCallbackProps {
227
+ }
228
+ interface ProcessSourceEventsProps extends ListenToSourceEventsProps {
229
+ events: EventRecord[];
230
+ unsubscribe?: () => void;
231
+ }
232
+ declare function processSourceEvents({ events, sourceAddress, route, onSourceFinalized, onSourceError, onDestinationFinalized, onDestinationError, unsubscribe, }: ProcessSourceEventsProps): void;
233
+ declare function createMonitoringCallback({ sourceAddress, route, statusCallback, onSourceFinalized, onSourceError, onDestinationFinalized, onDestinationError, }: CreateMonitoringCallbackProps): (status: ISubmittableResult) => void;
234
+ declare function listenToSourceEvents({ route, sourceAddress, onSourceFinalized, onSourceError, onDestinationFinalized, onDestinationError, }: ListenToSourceEventsProps): Promise<void>;
235
+
236
+ export { type ChainTransferData, type ConvertToChainDecimalsParams, type DestinationChainTransferData, EvmService, type EvmSigner, type GetAssetsBalancesParams, type GetBalancesParams, type GetContractFeeParams, type GetDestinationDataParams, type GetDestinationFeeBalanceParams, type GetDestinationFeeParams, type GetExtrinsicFeeParams, type GetFeeParams, type GetMaxParams, type GetMinParams, type GetSourceDataParams, MAX_ALLOWANCE_HEX, PolkadotService, Sdk, type SdkOptions, type Signers, type SourceChainTransferData, type SovereignAccountBalance, type TransferData, type TransferParams, convertToChainDecimals, createMonitoringCallback, getAllowanceSlot, getAssetMin, getAssetsBalances, getBalance, getContractFee, getDestinationData, getDestinationFee, getDestinationFeeBalance, getExistentialDeposit, getExtrinsicFee, getFee, getMax, getMin, getParachainBalances, getSourceData, listenToDestinationEvents, listenToSourceEvents, processSourceEvents, validateSovereignAccountBalances };