@moonbeam-network/xcm-sdk 1.0.0-dev.10 → 1.0.0-dev.101
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 +50 -16
- package/build/index.cjs +1 -1
- package/build/index.cjs.map +1 -1
- package/build/index.d.cts +153 -0
- package/build/index.d.ts +134 -40
- package/build/index.mjs +1 -1
- package/build/index.mjs.map +1 -1
- package/package.json +19 -15
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { SubstrateQueryConfig, ExtrinsicConfig, ContractConfig } from '@moonbeam-network/xcm-builder';
|
|
2
|
+
import { IConfigService, AssetConfig, TransferConfig, FeeAssetConfig, DestinationFeeConfig } from '@moonbeam-network/xcm-config';
|
|
3
|
+
import { AnyParachain, Asset, AssetAmount, ChainAssetId, AnyChain, EvmParachain, Ecosystem } from '@moonbeam-network/xcm-types';
|
|
4
|
+
import { ApiPromise } from '@polkadot/api';
|
|
5
|
+
import { Signer } from '@polkadot/api/types';
|
|
6
|
+
import { IKeyringPair } from '@polkadot/types/types';
|
|
7
|
+
import { WalletClient } from 'viem';
|
|
8
|
+
|
|
9
|
+
declare class PolkadotService {
|
|
10
|
+
#private;
|
|
11
|
+
readonly api: ApiPromise;
|
|
12
|
+
readonly chain: AnyParachain;
|
|
13
|
+
readonly configService: IConfigService;
|
|
14
|
+
constructor(api: ApiPromise, chain: AnyParachain, configService: IConfigService);
|
|
15
|
+
static create(chain: AnyParachain, configService: IConfigService): Promise<PolkadotService>;
|
|
16
|
+
static createMulti(chains: AnyParachain[], configService: IConfigService): Promise<PolkadotService[]>;
|
|
17
|
+
get decimals(): number;
|
|
18
|
+
get asset(): Asset;
|
|
19
|
+
get existentialDeposit(): AssetAmount;
|
|
20
|
+
getAssetMeta(asset: ChainAssetId): Promise<{
|
|
21
|
+
symbol: string;
|
|
22
|
+
decimals: number;
|
|
23
|
+
} | undefined>;
|
|
24
|
+
getAssetDecimalsFromQuery(asset: ChainAssetId): Promise<number | undefined>;
|
|
25
|
+
getAssetDecimals(asset: Asset): Promise<number>;
|
|
26
|
+
query(config: SubstrateQueryConfig): Promise<bigint>;
|
|
27
|
+
getFee(account: string, config: ExtrinsicConfig): Promise<bigint>;
|
|
28
|
+
transfer(account: string, config: ExtrinsicConfig, signer: Signer | IKeyringPair): Promise<string>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type EvmSigner = WalletClient;
|
|
32
|
+
interface Signers {
|
|
33
|
+
evmSigner?: EvmSigner;
|
|
34
|
+
polkadotSigner?: Signer | IKeyringPair;
|
|
35
|
+
}
|
|
36
|
+
interface TransferData {
|
|
37
|
+
destination: DestinationChainTransferData;
|
|
38
|
+
getEstimate(amount: number | string): AssetAmount;
|
|
39
|
+
isSwapPossible: boolean;
|
|
40
|
+
max: AssetAmount;
|
|
41
|
+
min: AssetAmount;
|
|
42
|
+
source: SourceChainTransferData;
|
|
43
|
+
swap(): Promise<TransferData | undefined>;
|
|
44
|
+
transfer(amount: bigint | number | string, signers?: Signers): Promise<string>;
|
|
45
|
+
}
|
|
46
|
+
interface SourceChainTransferData extends ChainTransferData {
|
|
47
|
+
destinationFeeBalance: AssetAmount;
|
|
48
|
+
feeBalance: AssetAmount;
|
|
49
|
+
max: AssetAmount;
|
|
50
|
+
}
|
|
51
|
+
interface DestinationChainTransferData extends ChainTransferData {
|
|
52
|
+
}
|
|
53
|
+
interface ChainTransferData {
|
|
54
|
+
balance: AssetAmount;
|
|
55
|
+
chain: AnyChain;
|
|
56
|
+
existentialDeposit: AssetAmount;
|
|
57
|
+
fee: AssetAmount;
|
|
58
|
+
min: AssetAmount;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface GetBalancesParams {
|
|
62
|
+
address: string;
|
|
63
|
+
asset?: Asset;
|
|
64
|
+
chain: AnyChain;
|
|
65
|
+
config: AssetConfig;
|
|
66
|
+
decimals: number;
|
|
67
|
+
polkadot: PolkadotService;
|
|
68
|
+
}
|
|
69
|
+
type GetDecimalsParams = Omit<GetBalancesParams, 'decimals'> & {
|
|
70
|
+
assetBuiltConfig?: SubstrateQueryConfig | ContractConfig;
|
|
71
|
+
};
|
|
72
|
+
declare function getBalance({ address, chain, config, decimals, polkadot, }: GetBalancesParams): Promise<bigint>;
|
|
73
|
+
declare function getDecimals({ address, asset, config, polkadot, chain, assetBuiltConfig, }: GetDecimalsParams): Promise<number>;
|
|
74
|
+
declare function getMin(config: AssetConfig, polkadot: PolkadotService): Promise<bigint>;
|
|
75
|
+
|
|
76
|
+
interface GetSourceDataParams {
|
|
77
|
+
transferConfig: TransferConfig;
|
|
78
|
+
destinationAddress: string;
|
|
79
|
+
destinationFee: AssetAmount;
|
|
80
|
+
polkadot: PolkadotService;
|
|
81
|
+
sourceAddress: string;
|
|
82
|
+
}
|
|
83
|
+
declare function getSourceData({ transferConfig, destinationAddress, destinationFee, polkadot, sourceAddress, }: GetSourceDataParams): Promise<SourceChainTransferData>;
|
|
84
|
+
interface GetFeeBalanceParams extends Omit<GetBalancesParams, 'config' | 'evmSigner'> {
|
|
85
|
+
balance: bigint;
|
|
86
|
+
feeConfig: FeeAssetConfig | undefined;
|
|
87
|
+
}
|
|
88
|
+
declare function getFeeBalance({ address, balance, chain, decimals, feeConfig, polkadot, }: GetFeeBalanceParams): Promise<bigint>;
|
|
89
|
+
interface GetFeeParams {
|
|
90
|
+
balance: bigint;
|
|
91
|
+
contract?: ContractConfig;
|
|
92
|
+
chain: AnyChain;
|
|
93
|
+
decimals: number;
|
|
94
|
+
extrinsic?: ExtrinsicConfig;
|
|
95
|
+
feeConfig?: FeeAssetConfig;
|
|
96
|
+
destinationFeeConfig?: DestinationFeeConfig;
|
|
97
|
+
destinationFeeBalanceAmount?: AssetAmount;
|
|
98
|
+
polkadot: PolkadotService;
|
|
99
|
+
sourceAddress: string;
|
|
100
|
+
}
|
|
101
|
+
declare function getFee({ balance, chain, contract, decimals, destinationFeeConfig, destinationFeeBalanceAmount, extrinsic, feeConfig, polkadot, sourceAddress, }: GetFeeParams): Promise<bigint>;
|
|
102
|
+
declare function getContractFee({ address, balance, config, decimals, chain, }: {
|
|
103
|
+
address: string;
|
|
104
|
+
balance: bigint;
|
|
105
|
+
config: ContractConfig;
|
|
106
|
+
decimals: number;
|
|
107
|
+
chain: EvmParachain;
|
|
108
|
+
}): Promise<bigint>;
|
|
109
|
+
declare function getExtrinsicFee(balance: bigint, extrinsic: ExtrinsicConfig, polkadot: PolkadotService, sourceAddress: string): Promise<bigint>;
|
|
110
|
+
interface GetMaxParams {
|
|
111
|
+
balanceAmount: AssetAmount;
|
|
112
|
+
existentialDeposit: AssetAmount;
|
|
113
|
+
feeAmount: AssetAmount;
|
|
114
|
+
minAmount: AssetAmount;
|
|
115
|
+
}
|
|
116
|
+
declare function getMax({ balanceAmount, existentialDeposit, feeAmount, minAmount, }: GetMaxParams): AssetAmount;
|
|
117
|
+
interface GetAssetsBalancesParams {
|
|
118
|
+
address: string;
|
|
119
|
+
chain: AnyChain;
|
|
120
|
+
assets: AssetConfig[];
|
|
121
|
+
evmSigner?: EvmSigner;
|
|
122
|
+
polkadot: PolkadotService;
|
|
123
|
+
}
|
|
124
|
+
declare function getAssetsBalances({ address, chain, assets, polkadot, }: GetAssetsBalancesParams): Promise<AssetAmount[]>;
|
|
125
|
+
|
|
126
|
+
interface SdkOptions extends Partial<Signers> {
|
|
127
|
+
configService?: IConfigService;
|
|
128
|
+
}
|
|
129
|
+
declare function Sdk(options?: SdkOptions): {
|
|
130
|
+
assets(ecosystem?: Ecosystem): {
|
|
131
|
+
assets: Asset[];
|
|
132
|
+
asset(keyOrAsset: string | Asset): {
|
|
133
|
+
sourceChains: AnyChain[];
|
|
134
|
+
source(keyOrChain: string | AnyChain): {
|
|
135
|
+
destinationChains: AnyChain[];
|
|
136
|
+
destination(destKeyOrChain: string | AnyChain): {
|
|
137
|
+
accounts(sourceAddress: string, destinationAddress: string): Promise<TransferData>;
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
getTransferData({ destinationAddress, destinationKeyOrChain, evmSigner, keyOrAsset, polkadotSigner, sourceAddress, sourceKeyOrChain, }: SdkTransferParams): Promise<TransferData>;
|
|
143
|
+
};
|
|
144
|
+
declare function getParachainBalances(chain: AnyChain, address: string): Promise<AssetAmount[]>;
|
|
145
|
+
interface SdkTransferParams extends Partial<Signers> {
|
|
146
|
+
destinationAddress: string;
|
|
147
|
+
destinationKeyOrChain: string | AnyChain;
|
|
148
|
+
keyOrAsset: string | Asset;
|
|
149
|
+
sourceAddress: string;
|
|
150
|
+
sourceKeyOrChain: string | AnyChain;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export { type ChainTransferData, type DestinationChainTransferData, type EvmSigner, type GetAssetsBalancesParams, type GetBalancesParams, type GetDecimalsParams, type GetFeeBalanceParams, type GetFeeParams, type GetMaxParams, type GetSourceDataParams, Sdk, type SdkOptions, type SdkTransferParams, type Signers, type SourceChainTransferData, type TransferData, getAssetsBalances, getBalance, getContractFee, getDecimals, getExtrinsicFee, getFee, getFeeBalance, getMax, getMin, getParachainBalances, getSourceData };
|
package/build/index.d.ts
CHANGED
|
@@ -3,27 +3,45 @@ import {
|
|
|
3
3
|
ExtrinsicConfig,
|
|
4
4
|
ContractConfig,
|
|
5
5
|
} from '@moonbeam-network/xcm-builder';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
IConfigService,
|
|
8
|
+
AssetConfig,
|
|
9
|
+
TransferConfig,
|
|
10
|
+
FeeAssetConfig,
|
|
11
|
+
DestinationFeeConfig,
|
|
12
|
+
} from '@moonbeam-network/xcm-config';
|
|
7
13
|
import {
|
|
8
14
|
AnyParachain,
|
|
9
15
|
Asset,
|
|
10
16
|
AssetAmount,
|
|
11
17
|
ChainAssetId,
|
|
12
18
|
AnyChain,
|
|
19
|
+
EvmParachain,
|
|
13
20
|
Ecosystem,
|
|
14
21
|
} from '@moonbeam-network/xcm-types';
|
|
15
|
-
import { Signer as Signer$1 } from 'ethers';
|
|
16
22
|
import { ApiPromise } from '@polkadot/api';
|
|
17
23
|
import { Signer } from '@polkadot/api/types';
|
|
18
24
|
import { IKeyringPair } from '@polkadot/types/types';
|
|
25
|
+
import { WalletClient } from 'viem';
|
|
19
26
|
|
|
20
27
|
declare class PolkadotService {
|
|
21
28
|
#private;
|
|
22
29
|
readonly api: ApiPromise;
|
|
23
30
|
readonly chain: AnyParachain;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
readonly configService: IConfigService;
|
|
32
|
+
constructor(
|
|
33
|
+
api: ApiPromise,
|
|
34
|
+
chain: AnyParachain,
|
|
35
|
+
configService: IConfigService,
|
|
36
|
+
);
|
|
37
|
+
static create(
|
|
38
|
+
chain: AnyParachain,
|
|
39
|
+
configService: IConfigService,
|
|
40
|
+
): Promise<PolkadotService>;
|
|
41
|
+
static createMulti(
|
|
42
|
+
chains: AnyParachain[],
|
|
43
|
+
configService: IConfigService,
|
|
44
|
+
): Promise<PolkadotService[]>;
|
|
27
45
|
get decimals(): number;
|
|
28
46
|
get asset(): Asset;
|
|
29
47
|
get existentialDeposit(): AssetAmount;
|
|
@@ -34,6 +52,7 @@ declare class PolkadotService {
|
|
|
34
52
|
}
|
|
35
53
|
| undefined
|
|
36
54
|
>;
|
|
55
|
+
getAssetDecimalsFromQuery(asset: ChainAssetId): Promise<number | undefined>;
|
|
37
56
|
getAssetDecimals(asset: Asset): Promise<number>;
|
|
38
57
|
query(config: SubstrateQueryConfig): Promise<bigint>;
|
|
39
58
|
getFee(account: string, config: ExtrinsicConfig): Promise<bigint>;
|
|
@@ -44,9 +63,10 @@ declare class PolkadotService {
|
|
|
44
63
|
): Promise<string>;
|
|
45
64
|
}
|
|
46
65
|
|
|
66
|
+
type EvmSigner = WalletClient;
|
|
47
67
|
interface Signers {
|
|
48
|
-
|
|
49
|
-
polkadotSigner
|
|
68
|
+
evmSigner?: EvmSigner;
|
|
69
|
+
polkadotSigner?: Signer | IKeyringPair;
|
|
50
70
|
}
|
|
51
71
|
interface TransferData {
|
|
52
72
|
destination: DestinationChainTransferData;
|
|
@@ -56,9 +76,13 @@ interface TransferData {
|
|
|
56
76
|
min: AssetAmount;
|
|
57
77
|
source: SourceChainTransferData;
|
|
58
78
|
swap(): Promise<TransferData | undefined>;
|
|
59
|
-
transfer(
|
|
79
|
+
transfer(
|
|
80
|
+
amount: bigint | number | string,
|
|
81
|
+
signers?: Signers,
|
|
82
|
+
): Promise<string>;
|
|
60
83
|
}
|
|
61
84
|
interface SourceChainTransferData extends ChainTransferData {
|
|
85
|
+
destinationFeeBalance: AssetAmount;
|
|
62
86
|
feeBalance: AssetAmount;
|
|
63
87
|
max: AssetAmount;
|
|
64
88
|
}
|
|
@@ -71,11 +95,41 @@ interface ChainTransferData {
|
|
|
71
95
|
min: AssetAmount;
|
|
72
96
|
}
|
|
73
97
|
|
|
98
|
+
interface GetBalancesParams {
|
|
99
|
+
address: string;
|
|
100
|
+
asset?: Asset;
|
|
101
|
+
chain: AnyChain;
|
|
102
|
+
config: AssetConfig;
|
|
103
|
+
decimals: number;
|
|
104
|
+
polkadot: PolkadotService;
|
|
105
|
+
}
|
|
106
|
+
type GetDecimalsParams = Omit<GetBalancesParams, 'decimals'> & {
|
|
107
|
+
assetBuiltConfig?: SubstrateQueryConfig | ContractConfig;
|
|
108
|
+
};
|
|
109
|
+
declare function getBalance({
|
|
110
|
+
address,
|
|
111
|
+
chain,
|
|
112
|
+
config,
|
|
113
|
+
decimals,
|
|
114
|
+
polkadot,
|
|
115
|
+
}: GetBalancesParams): Promise<bigint>;
|
|
116
|
+
declare function getDecimals({
|
|
117
|
+
address,
|
|
118
|
+
asset,
|
|
119
|
+
config,
|
|
120
|
+
polkadot,
|
|
121
|
+
chain,
|
|
122
|
+
assetBuiltConfig,
|
|
123
|
+
}: GetDecimalsParams): Promise<number>;
|
|
124
|
+
declare function getMin(
|
|
125
|
+
config: AssetConfig,
|
|
126
|
+
polkadot: PolkadotService,
|
|
127
|
+
): Promise<bigint>;
|
|
128
|
+
|
|
74
129
|
interface GetSourceDataParams {
|
|
75
130
|
transferConfig: TransferConfig;
|
|
76
131
|
destinationAddress: string;
|
|
77
132
|
destinationFee: AssetAmount;
|
|
78
|
-
ethersSigner: Signer$1;
|
|
79
133
|
polkadot: PolkadotService;
|
|
80
134
|
sourceAddress: string;
|
|
81
135
|
}
|
|
@@ -83,46 +137,59 @@ declare function getSourceData({
|
|
|
83
137
|
transferConfig,
|
|
84
138
|
destinationAddress,
|
|
85
139
|
destinationFee,
|
|
86
|
-
ethersSigner,
|
|
87
140
|
polkadot,
|
|
88
141
|
sourceAddress,
|
|
89
142
|
}: GetSourceDataParams): Promise<SourceChainTransferData>;
|
|
90
|
-
interface
|
|
91
|
-
|
|
143
|
+
interface GetFeeBalanceParams
|
|
144
|
+
extends Omit<GetBalancesParams, 'config' | 'evmSigner'> {
|
|
92
145
|
balance: bigint;
|
|
93
|
-
|
|
94
|
-
polkadot: PolkadotService;
|
|
146
|
+
feeConfig: FeeAssetConfig | undefined;
|
|
95
147
|
}
|
|
96
|
-
declare function
|
|
148
|
+
declare function getFeeBalance({
|
|
97
149
|
address,
|
|
98
150
|
balance,
|
|
99
|
-
|
|
151
|
+
chain,
|
|
152
|
+
decimals,
|
|
153
|
+
feeConfig,
|
|
100
154
|
polkadot,
|
|
101
|
-
}:
|
|
155
|
+
}: GetFeeBalanceParams): Promise<bigint>;
|
|
102
156
|
interface GetFeeParams {
|
|
103
157
|
balance: bigint;
|
|
104
158
|
contract?: ContractConfig;
|
|
159
|
+
chain: AnyChain;
|
|
105
160
|
decimals: number;
|
|
106
|
-
ethersSigner?: Signer$1;
|
|
107
161
|
extrinsic?: ExtrinsicConfig;
|
|
162
|
+
feeConfig?: FeeAssetConfig;
|
|
163
|
+
destinationFeeConfig?: DestinationFeeConfig;
|
|
164
|
+
destinationFeeBalanceAmount?: AssetAmount;
|
|
108
165
|
polkadot: PolkadotService;
|
|
109
166
|
sourceAddress: string;
|
|
110
167
|
}
|
|
111
168
|
declare function getFee({
|
|
112
169
|
balance,
|
|
170
|
+
chain,
|
|
113
171
|
contract,
|
|
114
172
|
decimals,
|
|
115
|
-
|
|
173
|
+
destinationFeeConfig,
|
|
174
|
+
destinationFeeBalanceAmount,
|
|
116
175
|
extrinsic,
|
|
176
|
+
feeConfig,
|
|
117
177
|
polkadot,
|
|
118
178
|
sourceAddress,
|
|
119
179
|
}: GetFeeParams): Promise<bigint>;
|
|
120
|
-
declare function getContractFee(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
180
|
+
declare function getContractFee({
|
|
181
|
+
address,
|
|
182
|
+
balance,
|
|
183
|
+
config,
|
|
184
|
+
decimals,
|
|
185
|
+
chain,
|
|
186
|
+
}: {
|
|
187
|
+
address: string;
|
|
188
|
+
balance: bigint;
|
|
189
|
+
config: ContractConfig;
|
|
190
|
+
decimals: number;
|
|
191
|
+
chain: EvmParachain;
|
|
192
|
+
}): Promise<bigint>;
|
|
126
193
|
declare function getExtrinsicFee(
|
|
127
194
|
balance: bigint,
|
|
128
195
|
extrinsic: ExtrinsicConfig,
|
|
@@ -141,8 +208,23 @@ declare function getMax({
|
|
|
141
208
|
feeAmount,
|
|
142
209
|
minAmount,
|
|
143
210
|
}: GetMaxParams): AssetAmount;
|
|
211
|
+
interface GetAssetsBalancesParams {
|
|
212
|
+
address: string;
|
|
213
|
+
chain: AnyChain;
|
|
214
|
+
assets: AssetConfig[];
|
|
215
|
+
evmSigner?: EvmSigner;
|
|
216
|
+
polkadot: PolkadotService;
|
|
217
|
+
}
|
|
218
|
+
declare function getAssetsBalances({
|
|
219
|
+
address,
|
|
220
|
+
chain,
|
|
221
|
+
assets,
|
|
222
|
+
polkadot,
|
|
223
|
+
}: GetAssetsBalancesParams): Promise<AssetAmount[]>;
|
|
144
224
|
|
|
145
|
-
interface SdkOptions extends Partial<Signers> {
|
|
225
|
+
interface SdkOptions extends Partial<Signers> {
|
|
226
|
+
configService?: IConfigService;
|
|
227
|
+
}
|
|
146
228
|
declare function Sdk(options?: SdkOptions): {
|
|
147
229
|
assets(ecosystem?: Ecosystem): {
|
|
148
230
|
assets: Asset[];
|
|
@@ -154,7 +236,6 @@ declare function Sdk(options?: SdkOptions): {
|
|
|
154
236
|
accounts(
|
|
155
237
|
sourceAddress: string,
|
|
156
238
|
destinationAddress: string,
|
|
157
|
-
signers?: Partial<Signers>,
|
|
158
239
|
): Promise<TransferData>;
|
|
159
240
|
};
|
|
160
241
|
};
|
|
@@ -163,13 +244,17 @@ declare function Sdk(options?: SdkOptions): {
|
|
|
163
244
|
getTransferData({
|
|
164
245
|
destinationAddress,
|
|
165
246
|
destinationKeyOrChain,
|
|
166
|
-
|
|
247
|
+
evmSigner,
|
|
167
248
|
keyOrAsset,
|
|
168
249
|
polkadotSigner,
|
|
169
250
|
sourceAddress,
|
|
170
251
|
sourceKeyOrChain,
|
|
171
252
|
}: SdkTransferParams): Promise<TransferData>;
|
|
172
253
|
};
|
|
254
|
+
declare function getParachainBalances(
|
|
255
|
+
chain: AnyChain,
|
|
256
|
+
address: string,
|
|
257
|
+
): Promise<AssetAmount[]>;
|
|
173
258
|
interface SdkTransferParams extends Partial<Signers> {
|
|
174
259
|
destinationAddress: string;
|
|
175
260
|
destinationKeyOrChain: string | AnyChain;
|
|
@@ -179,22 +264,31 @@ interface SdkTransferParams extends Partial<Signers> {
|
|
|
179
264
|
}
|
|
180
265
|
|
|
181
266
|
export {
|
|
182
|
-
ChainTransferData,
|
|
183
|
-
DestinationChainTransferData,
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
267
|
+
type ChainTransferData,
|
|
268
|
+
type DestinationChainTransferData,
|
|
269
|
+
type EvmSigner,
|
|
270
|
+
type GetAssetsBalancesParams,
|
|
271
|
+
type GetBalancesParams,
|
|
272
|
+
type GetDecimalsParams,
|
|
273
|
+
type GetFeeBalanceParams,
|
|
274
|
+
type GetFeeParams,
|
|
275
|
+
type GetMaxParams,
|
|
276
|
+
type GetSourceDataParams,
|
|
188
277
|
Sdk,
|
|
189
|
-
SdkOptions,
|
|
190
|
-
SdkTransferParams,
|
|
191
|
-
Signers,
|
|
192
|
-
SourceChainTransferData,
|
|
193
|
-
TransferData,
|
|
278
|
+
type SdkOptions,
|
|
279
|
+
type SdkTransferParams,
|
|
280
|
+
type Signers,
|
|
281
|
+
type SourceChainTransferData,
|
|
282
|
+
type TransferData,
|
|
283
|
+
getAssetsBalances,
|
|
284
|
+
getBalance,
|
|
194
285
|
getContractFee,
|
|
286
|
+
getDecimals,
|
|
195
287
|
getExtrinsicFee,
|
|
196
288
|
getFee,
|
|
197
|
-
|
|
289
|
+
getFeeBalance,
|
|
198
290
|
getMax,
|
|
291
|
+
getMin,
|
|
292
|
+
getParachainBalances,
|
|
199
293
|
getSourceData,
|
|
200
294
|
};
|
package/build/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{AssetAmount as O}from"@moonbeam-network/xcm-types";import{convertDecimals as V}from"@moonbeam-network/xcm-utils";import _ from"big.js";import{Contract as L}from"ethers";var G=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}];var S=class{address;#t;#e;constructor(t,e){if(!t.address)throw new Error("Contract address is required");this.address=t.address,this.#t=t,this.#e=new L(this.address,G,e)}async getBalance(){return(await this.#e.balanceOf(...this.#t.args)).toBigInt()}async getDecimals(){return await this.#e.decimals()}};import{Contract as J}from"ethers";var q=[{inputs:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transfer",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{components:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"}],internalType:"struct Xtokens.Currency[]",name:"currencies",type:"tuple[]"},{internalType:"uint32",name:"feeItem",type:"uint32"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transferMultiCurrencies",outputs:[],stateMutability:"nonpayable",type:"function"}];var B=class{address="0x0000000000000000000000000000000000000804";#t;#e;#n;constructor(t,e){this.#t=t,this.#n=e,this.#e=new J(this.address,q,e)}async transfer(){return this.#e[this.#t.func](...this.#t.args)}async getFee(t){if(t===0n)return 0n;try{let e=(await this.#e.estimateGas[this.#t.func](...this.#t.args)).toBigInt(),n=await this.getGasPrice();return e*n}catch(e){throw console.error(e),new Error("Can't get a fee. Make sure that you have enough balance!")}}async getGasPrice(){let{gasPrice:t,maxPriorityFeePerGas:e}=await this.#n.getFeeData();return((t==null?void 0:t.toBigInt())||0n)+((e==null?void 0:e.toBigInt())||0n)}};function d(a,t){if(a.module==="Erc20")return new S(a,t);if(a.module==="Xtokens")return new B(a,t);throw new Error(`Contract ${a.module} not found`)}import{CallType as W}from"@moonbeam-network/xcm-builder";import{toBigInt as U}from"@moonbeam-network/xcm-utils";async function D({address:a,config:t,ethersSigner:e,polkadot:n}){let s=t.balance.build({address:a,asset:n.chain.getBalanceAssetId(t.asset)});return s.type===W.Substrate?n.query(s):d(s,e).getBalance()}async function A({address:a,asset:t,config:e,ethersSigner:n,polkadot:s}){let r=e.balance.build({address:a,asset:s.chain.getBalanceAssetId(t||e.asset)});return r.type===W.Substrate?s.getAssetDecimals(t||e.asset):d(r,n).getDecimals()}async function I(a,t){if(a.min)return t.query(a.min.build({asset:t.chain.getMinAssetId(a.asset)}));let e=t.chain.getAssetMin(a.asset);if(e){let n=await t.getAssetDecimals(a.asset);return U(e,n)}return 0n}async function Q({transferConfig:a,destinationAddress:t,destinationFee:e,ethersSigner:n,polkadot:s,sourceAddress:r}){var E,k,F;let{asset:i,destination:m,source:{chain:c,config:o}}=a,f=O.fromAsset(i,{amount:0n,decimals:await A({address:t,config:o,ethersSigner:n,polkadot:s})}),l=(E=o.fee)!=null&&E.asset?O.fromAsset(o.fee.asset,{amount:0n,decimals:await A({address:t,asset:o.fee.asset,config:o,ethersSigner:n,polkadot:s})}):f,u=await D({address:r,config:o,ethersSigner:n,polkadot:s}),p=await Y({address:r,balance:u,config:o,polkadot:s}),b=await I(o,s),C=(k=o.extrinsic)==null?void 0:k.build({address:t,amount:u,asset:c.getAssetId(i),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e),palletInstance:c.getAssetPalletInstance(i),source:c}),P=(F=o.contract)==null?void 0:F.build({address:t,amount:u,asset:c.getAssetId(i),destination:m.chain,fee:e.amount,feeAsset:c.getAssetId(e)}),w=await Z({balance:u,contract:P,decimals:l.decimals,ethersSigner:n,extrinsic:C,polkadot:s,sourceAddress:r}),h=f.copyWith({amount:u}),{existentialDeposit:x}=s,v=l.copyWith({amount:w}),X=l.copyWith({amount:p}),M=f.copyWith({amount:b}),$=nt({balanceAmount:h,existentialDeposit:x,feeAmount:v,minAmount:M});return{balance:h,chain:c,existentialDeposit:x,fee:v,feeBalance:X,max:$,min:M}}async function Y({address:a,balance:t,config:e,polkadot:n}){return e.fee?n.query(e.fee.balance.build({address:a,asset:n.chain.getBalanceAssetId(e.fee.asset)})):t}async function Z({balance:a,contract:t,decimals:e,ethersSigner:n,extrinsic:s,polkadot:r,sourceAddress:i}){if(t){if(!n)throw new Error("Ethers signer must be provided");return tt(a,t,e,n)}if(s)return et(a,s,r,i);throw new Error("Either contract or extrinsic must be provided")}async function tt(a,t,e,n){let r=await d(t,n).getFee(a);return V(r,18,e)}async function et(a,t,e,n){try{return await e.getFee(n,t)}catch(s){if(a)throw s;return 0n}}function nt({balanceAmount:a,existentialDeposit:t,feeAmount:e,minAmount:n}){let s=a.toBig().minus(n.toBig()).minus(a.isSame(t)?t.toBig():_(0)).minus(a.isSame(e)?e.toBig():_(0));return a.copyWith({amount:s.lt(0)?0n:BigInt(s.toFixed())})}import{ConfigBuilder as N}from"@moonbeam-network/xcm-config";import{convertDecimals as ft,toBigInt as z}from"@moonbeam-network/xcm-utils";import y from"big.js";import"@polkadot/api-augment";import{assetsMap as at,darwiniaPangoro as st,eq as rt,equilibriumAlphanet as it,paring as ot}from"@moonbeam-network/xcm-config";import{AssetAmount as ct}from"@moonbeam-network/xcm-types";import{getPolkadotApi as mt}from"@moonbeam-network/xcm-utils";var g=class{api;chain;constructor(t,e){this.api=t,this.chain=e}static async create(t){return new g(await mt(t.ws),t)}static async createMulti(t){return Promise.all(t.map(g.create))}get decimals(){return this.api.registry.chainDecimals.at(0)||12}get asset(){let t=this.api.registry.chainTokens.at(0),e=t==null?void 0:t.toString().toLowerCase();if(e==="token"&&this.chain.key===it.key)return rt;if(e==="oring"&&this.chain.key===st.key)return ot;if(!e)throw new Error("No native symbol key found");let n=at.get(e);if(!n)throw new Error(`No asset found for key "${e}" and symbol "${t}"`);return n}get existentialDeposit(){var s,r;let t=(s=this.api.consts.balances)==null?void 0:s.existentialDeposit,e=(r=this.api.consts.eqBalances)==null?void 0:r.existentialDeposit,n=(t==null?void 0:t.toBigInt())||(e==null?void 0:e.toBigInt())||0n;return ct.fromAsset(this.asset,{amount:n,decimals:this.decimals})}async getAssetMeta(t){var r,i,m,c,o;let e=((r=this.api.query.assets)==null?void 0:r.metadata)||((i=this.api.query.assetRegistry)==null?void 0:i.metadata)||((m=this.api.query.assetRegistry)==null?void 0:m.currencyMetadatas)||((c=this.api.query.assetRegistry)==null?void 0:c.assetMetadatas)||((o=this.api.query.assetRegistry)==null?void 0:o.assetMetadataMap);if(!e)return;let n=await e(t),s="unwrapOrDefault"in n?n.unwrapOrDefault():n;return{decimals:s.decimals.toNumber(),symbol:s.symbol.toString()}}async getAssetDecimals(t){var e;return((e=await this.getAssetMeta(this.chain.getMetadataAssetId(t)))==null?void 0:e.decimals)||this.chain.getAssetDecimals(t)||this.decimals}async query(t){let e=await this.api.query[t.module][t.func](...t.args);return t.transform(e)}async getFee(t,e){let n=this.api.tx[e.module][e.func],s=e.getArgs(n);return(await n(...s).paymentInfo(t,{nonce:-1})).partialFee.toBigInt()}async transfer(t,e,n){let s=this.api.tx[e.module][e.func],r=e.getArgs(s);return(await s(...r).signAndSend(this.#t(n)?t:n,{nonce:-1,signer:this.#t(n)?n:void 0})).toString()}#t(t){return"signPayload"in t}};import{AssetAmount as R}from"@moonbeam-network/xcm-types";import{toBigInt as ut}from"@moonbeam-network/xcm-utils";async function K({transferConfig:a,destinationAddress:t,ethersSigner:e,polkadot:n}){let{asset:s,destination:{chain:r,config:i}}=a,m=R.fromAsset(s,{amount:0n,decimals:await A({address:t,config:i,ethersSigner:e,polkadot:n})}),c=await D({address:t,config:i,ethersSigner:e,polkadot:n}),o=await I(i,n),f=m.copyWith({amount:c}),{existentialDeposit:l}=n,u=await pt({config:a,polkadot:n}),p=m.copyWith({amount:o});return{balance:f,chain:r,existentialDeposit:l,fee:u,min:p}}async function pt({config:a,polkadot:t}){let{amount:e,asset:n}=a.source.config.destinationFee,s=await t.getAssetDecimals(n),r=R.fromAsset(n,{amount:0n,decimals:s});if(Number.isFinite(e))return r.copyWith({amount:ut(e,s)});let i=e.build({api:t.api,asset:t.chain.getAssetId(n)});return r.copyWith({amount:await i.call()})}async function T({destinationAddress:a,ethersSigner:t,polkadotSigner:e,sourceAddress:n,transferConfig:s}){if(!t)throw new Error("Ethers signer must be provided");let[r,i]=await g.createMulti([s.destination.chain,s.source.chain]),m=await K({destinationAddress:a,ethersSigner:t,polkadot:r,transferConfig:s}),c=await dt(i,m.fee),o=await Q({destinationAddress:a,destinationFee:c,ethersSigner:t,polkadot:i,sourceAddress:n,transferConfig:s});return{destination:m,getEstimate(f){let u=y(z(f,o.balance.decimals).toString()).minus(o.balance.isSame(c)?c.toBig():y(0));return o.balance.copyWith({amount:u.lt(0)?0n:BigInt(u.toFixed())})},isSwapPossible:!0,max:o.max,min:lt(m),source:o,async swap(){return T({destinationAddress:n,ethersSigner:t,polkadotSigner:e,sourceAddress:a,transferConfig:{...s,destination:s.source,source:s.destination}})},async transfer(f){var w,h;let l=z(f,o.balance.decimals),{asset:u,source:{chain:p,config:b}}=s,C=(w=b.contract)==null?void 0:w.build({address:a,amount:l,asset:p.getAssetId(u),destination:m.chain,fee:c.amount,feeAsset:p.getAssetId(c)}),P=(h=b.extrinsic)==null?void 0:h.build({address:a,amount:l,asset:p.getAssetId(u),destination:m.chain,fee:c.amount,feeAsset:p.getAssetId(c),palletInstance:p.getAssetPalletInstance(u),source:p});if(C){if(!t)throw new Error("Ethers signer must be provided");return d(C,t).transfer().then(x=>x.hash)}if(P){if(!e)throw new Error("Polkadot signer must be provided");return i.transfer(n,P,e)}throw new Error("Either contract or extrinsic must be provided")}}}function lt({balance:a,existentialDeposit:t,fee:e,min:n}){let s=y(0).plus(a.isSame(e)?e.toBig():y(0)).plus(a.isSame(t)&&a.toBig().lt(t.toBig())?t.toBig():y(0)).plus(a.toBig().lt(n.toBig())?n.toBig():y(0));return a.copyWith({amount:BigInt(s.toFixed())})}async function dt(a,t){let e=await a.getAssetDecimals(t);return t.decimals===e?t:t.copyWith({amount:ft(t.amount,t.decimals,e),decimals:e})}function Pe(a){return{assets(t){let{assets:e,asset:n}=N().assets(t);return{assets:e,asset(s){let{sourceChains:r,source:i}=n(s);return{sourceChains:r,source(m){let{destinationChains:c,destination:o}=i(m);return{destinationChains:c,destination(f){return{async accounts(l,u,p){return T({...a,...p,destinationAddress:u,sourceAddress:l,transferConfig:o(f).build()})}}}}}}}}},async getTransferData({destinationAddress:t,destinationKeyOrChain:e,ethersSigner:n,keyOrAsset:s,polkadotSigner:r,sourceAddress:i,sourceKeyOrChain:m}){return T({destinationAddress:t,ethersSigner:n,polkadotSigner:r,sourceAddress:i,transferConfig:N().assets().asset(s).source(m).destination(e).build()})}}}export{Pe as Sdk,tt as getContractFee,et as getExtrinsicFee,Z as getFee,Y as getFeeBalances,nt as getMax,Q as getSourceData};
|
|
1
|
+
import{CallType as ft}from"@moonbeam-network/xcm-builder";import{AssetAmount as E}from"@moonbeam-network/xcm-types";import{convertDecimals as M,toBigInt as lt,toDecimal as dt}from"@moonbeam-network/xcm-utils";import V from"big.js";import{createPublicClient as rt,http as it}from"viem";var k=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}];var B=class{address;#t;#e;constructor(t,e){if(!t.address)throw new Error("Contract address is required");this.address=t.address,this.#t=t,this.#e=rt({chain:e.getViemChain(),transport:it()})}async getBalance(){let t=this.#t.args[0];return this.#e.readContract({abi:k,address:this.address,args:[t],functionName:"balanceOf"})}async getDecimals(){return this.#e.readContract({abi:k,address:this.address,functionName:"decimals"})}};import{createPublicClient as ot,http as ct}from"viem";var G=[{inputs:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transfer",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{components:[{internalType:"address",name:"currencyAddress",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"}],internalType:"struct Xtokens.Currency[]",name:"currencies",type:"tuple[]"},{internalType:"uint32",name:"feeItem",type:"uint32"},{components:[{internalType:"uint8",name:"parents",type:"uint8"},{internalType:"bytes[]",name:"interior",type:"bytes[]"}],internalType:"struct Xtokens.Multilocation",name:"destination",type:"tuple"},{internalType:"uint64",name:"weight",type:"uint64"}],name:"transferMultiCurrencies",outputs:[],stateMutability:"nonpayable",type:"function"}];var I=class{defaultMoonChainAddress="0x0000000000000000000000000000000000000804";address;#t;#e;constructor(t,e,a){this.#t=t,this.address=a??this.defaultMoonChainAddress,this.#e=ot({chain:e.getViemChain(),transport:ct()})}async transfer(t){let{request:e}=await this.#e.simulateContract({abi:G,account:t.account,address:this.address,args:this.#t.args,functionName:this.#t.func});return await t.writeContract(e)}async getFee(t,e){if(t===0n)return 0n;try{let a=await this.#e.estimateContractGas({abi:G,account:e,address:this.address,args:this.#t.args,functionName:this.#t.func}),n=await this.getGasPrice();return a*n}catch(a){throw console.error(a),new Error("Can't get a fee. Make sure that you have enough balance!")}}async getGasPrice(){return this.#e.getGasPrice()}};function y(s,t){var e;if(!s.isEvmParachain())throw new Error("Chain is not an EVM parachain");if(t.module==="Erc20")return new B(t,s);if(t.module==="Xtokens"){let a="contracts"in s?(e=s==null?void 0:s.contracts)==null?void 0:e.Xtokens:void 0;return new I(t,s,a)}throw new Error(`Contract ${t.module} not found`)}import{CallType as H}from"@moonbeam-network/xcm-builder";import{convertDecimals as mt,toBigInt as ut}from"@moonbeam-network/xcm-utils";async function P({address:s,chain:t,config:e,decimals:a,polkadot:n}){let i=e.balance.build({address:s,asset:n.chain.getBalanceAssetId(e.asset)});if(i.type===H.Substrate){let r=await n.query(i);return t.usesChainDecimals?mt(r,n.decimals,a):r}return await y(t,i).getBalance()}async function A({address:s,asset:t,config:e,polkadot:a,chain:n,assetBuiltConfig:i}){let o=i||e.balance.build({address:s,asset:a.chain.getBalanceAssetId(t||e.asset)});return o.type===H.Substrate?a.getAssetDecimals(t||e.asset):y(n,o).getDecimals()}async function T(s,t){if(s.min)return t.query(s.min.build({asset:t.chain.getMinAssetId(s.asset)}));let e=t.chain.getAssetMin(s.asset);if(e){let a=await t.getAssetDecimals(s.asset);return ut(e,a)}return 0n}async function j({transferConfig:s,destinationAddress:t,destinationFee:e,polkadot:a,sourceAddress:n}){var _,Q,R,X,K,$,z;let{asset:i,destination:o,source:{chain:c,config:r}}=s,m=E.fromAsset(i,{amount:0n,decimals:await A({address:t,chain:c,config:r,polkadot:a})}),u=a.chain.getBalanceAssetId(((_=r==null?void 0:r.fee)==null?void 0:_.asset)||r.asset),l=(Q=r.fee)==null?void 0:Q.balance.build({address:t,asset:u}),d=(R=r.fee)!=null&&R.asset?await A({address:t,asset:r.fee.asset,assetBuiltConfig:l,chain:c,config:r,polkadot:a}):void 0,p=(X=r.fee)!=null&&X.asset?E.fromAsset(r.fee.asset,{amount:0n,decimals:d||0}):m,h=(K=r.destinationFee)!=null&&K.asset?E.fromAsset(r.destinationFee.asset,{amount:0n,decimals:await A({address:t,asset:r.destinationFee.asset,chain:c,config:r,polkadot:a})}):m,f=await P({address:n,chain:c,config:r,decimals:m.decimals,polkadot:a}),w=await L({address:n,balance:f,chain:c,decimals:p.decimals,feeConfig:r.fee,polkadot:a}),x=r.asset.isEqual(r.destinationFee.asset)?f:await L({address:n,balance:f,chain:c,decimals:h.decimals,feeConfig:r.destinationFee,polkadot:a}),S=await T(r,a),v=($=r.extrinsic)==null?void 0:$.build({address:t,amount:f,asset:c.getAssetId(i),destination:o.chain,fee:e.amount,feeAsset:c.getAssetId(e),palletInstance:c.getAssetPalletInstance(i),source:c}),D=(z=r.contract)==null?void 0:z.build({address:t,amount:f,asset:c.getAssetId(i),destination:o.chain,fee:e.amount,feeAsset:c.getAssetId(e)}),g=h.copyWith({amount:x}),at=await pt({balance:f,chain:c,contract:D,decimals:p.decimals,destinationFeeBalanceAmount:g,destinationFeeConfig:r.destinationFee,extrinsic:v,feeConfig:r.fee,polkadot:a,sourceAddress:n}),q=m.copyWith({amount:f}),{existentialDeposit:O}=a,W=p.copyWith({amount:at}),nt=p.copyWith({amount:w}),N=m.copyWith({amount:S}),st=ht({balanceAmount:q,existentialDeposit:O,feeAmount:W,minAmount:N});return{balance:q,chain:c,destinationFeeBalance:g,existentialDeposit:O,fee:W,feeBalance:nt,max:st,min:N}}async function L({address:s,balance:t,chain:e,decimals:a,feeConfig:n,polkadot:i}){if(!n)return t;let o=n.balance.build({address:s,asset:i.chain.getBalanceAssetId(n.asset)});if(o.type===ft.Evm){let r=y(e,o),m=await r.getDecimals(),u=await r.getBalance();return e.usesChainDecimals?M(u,i.decimals,m):u}let c=await i.query(o);return e.usesChainDecimals?M(c,i.decimals,a):c}async function pt({balance:s,chain:t,contract:e,decimals:a,destinationFeeConfig:n,destinationFeeBalanceAmount:i,extrinsic:o,feeConfig:c,polkadot:r,sourceAddress:m}){if(e){if(n&&i&&typeof n.amount=="number"){let u=Number(dt(i.amount,i.decimals));if(u&&n.amount>u)throw new Error(`Can't get a fee, make sure you have ${n==null?void 0:n.amount} ${n==null?void 0:n.asset.originSymbol} in your source balance, needed for destination fees`)}return gt({address:m,balance:s,chain:t,config:e,decimals:a})}if(o){let u=await yt(s,o,r,m),l=At(a,c),d=u+l;return t.usesChainDecimals?M(d,r.decimals,a):d}throw new Error("Either contract or extrinsic must be provided")}async function gt({address:s,balance:t,config:e,decimals:a,chain:n}){let o=await y(n,e).getFee(t,s);return M(o,18,a)}async function yt(s,t,e,a){try{return await e.getFee(a,t)}catch(n){if(s)throw n;return 0n}}function At(s,t){return t!=null&&t.xcmDeliveryFeeAmount?lt(t.xcmDeliveryFeeAmount,s):0n}function ht({balanceAmount:s,existentialDeposit:t,feeAmount:e,minAmount:a}){let n=s.toBig().minus(a.toBig()).minus(s.isSame(t)?t.toBig():V(0)).minus(s.isSame(e)?e.toBig():V(0));return s.copyWith({amount:n.lt(0)?0n:BigInt(n.toFixed())})}async function J({address:s,chain:t,assets:e,polkadot:a}){let n=e.reduce((o,c)=>o.some(r=>r.asset.isEqual(c.asset))?o:[c,...o],[]);return await Promise.all(n.map(async o=>{let c=await A({address:s,asset:o.asset,chain:t,config:o,polkadot:a}),r=await P({address:s,chain:t,config:o,decimals:c,polkadot:a});return E.fromAsset(o.asset,{amount:r,decimals:c})}))}import{ConfigBuilder as tt,ConfigService as et}from"@moonbeam-network/xcm-config";import{convertDecimals as vt,toBigInt as Z}from"@moonbeam-network/xcm-utils";import b from"big.js";import"@polkadot/api-augment";import{eq as Ct,equilibrium as bt}from"@moonbeam-network/xcm-config";import{AssetAmount as Pt}from"@moonbeam-network/xcm-types";import{getPolkadotApi as wt}from"@moonbeam-network/xcm-utils";var C=class s{api;chain;configService;constructor(t,e,a){this.api=t,this.chain=e,this.configService=a}static async create(t,e){return new s(await wt(t.ws),t,e)}static async createMulti(t,e){return Promise.all(t.map(a=>s.create(a,e)))}get decimals(){return this.api.registry.chainDecimals.at(0)||12}get asset(){let t=this.api.registry.chainTokens.at(0),e=t==null?void 0:t.toString().toLowerCase();if(e==="token"&&[bt.key].includes(this.chain.key))return Ct;if(!e)throw new Error("No native symbol key found");let a=this.configService.getAsset(e);if(!a)throw new Error(`No asset found for key "${e}" and symbol "${t}"`);return a}get existentialDeposit(){var n,i;let t=(n=this.api.consts.balances)==null?void 0:n.existentialDeposit,e=(i=this.api.consts.eqBalances)==null?void 0:i.existentialDeposit,a=(t==null?void 0:t.toBigInt())||(e==null?void 0:e.toBigInt())||0n;return Pt.fromAsset(this.asset,{amount:a,decimals:this.decimals})}async getAssetMeta(t){var o,c,r,m,u,l;let e=((o=this.api.query.assets)==null?void 0:o.metadata)||((c=this.api.query.assetRegistry)==null?void 0:c.assets)||((r=this.api.query.assetRegistry)==null?void 0:r.metadata)||((m=this.api.query.assetRegistry)==null?void 0:m.currencyMetadatas)||((u=this.api.query.assetRegistry)==null?void 0:u.assetMetadatas)||((l=this.api.query.assetRegistry)==null?void 0:l.assetMetadataMap);if(!e)return;let a=await e(t),n="unwrapOrDefault"in a?a.unwrapOrDefault():a;return{decimals:("unwrapOrDefault"in n.decimals?n.decimals.unwrapOrDefault():n.decimals).toNumber(),symbol:n.symbol.toString()}}async getAssetDecimalsFromQuery(t){var n;let e=(n=this.api.query.assetsRegistry)==null?void 0:n.assetDecimals;return e?(await e(t)).unwrapOrDefault().toNumber():void 0}async getAssetDecimals(t){var n;let e=this.chain.getMetadataAssetId(t),a=this.chain.getAssetDecimals(t)||await this.getAssetDecimalsFromQuery(e)||((n=await this.getAssetMeta(e))==null?void 0:n.decimals)||this.decimals;if(!a)throw new Error(`No decimals found for asset ${t.originSymbol}`);return a}async query(t){let e=await this.api.query[t.module][t.func](...t.args);return t.transform(e)}async getFee(t,e){let a=this.api.tx[e.module][e.func],n=e.getArgs(a);return(await a(...n).paymentInfo(t,{nonce:-1})).partialFee.toBigInt()}async transfer(t,e,a){let n=this.api.tx[e.module][e.func],i=e.getArgs(n);return(await n(...i).signAndSend(this.#t(a)?t:a,{nonce:-1,signer:this.#t(a)?a:void 0})).toString()}#t(t){return"signPayload"in t}};import{AssetAmount as U}from"@moonbeam-network/xcm-types";import{toBigInt as xt}from"@moonbeam-network/xcm-utils";async function Y({transferConfig:s,destinationAddress:t,polkadot:e}){let{asset:a,destination:{chain:n,config:i}}=s,o=U.fromAsset(a,{amount:0n,decimals:await A({address:t,chain:n,config:i,polkadot:e})}),c=await P({address:t,chain:n,config:i,decimals:o.decimals,polkadot:e}),r=await T(i,e),m=o.copyWith({amount:c}),{existentialDeposit:u}=e,l=await St({address:t,config:s,polkadot:e}),d=o.copyWith({amount:r});return{balance:m,chain:n,existentialDeposit:u,fee:l,min:d}}async function St({config:s,polkadot:t}){let{amount:e,asset:a}=s.source.config.destinationFee,n=await t.getAssetDecimals(a),i=U.fromAsset(a,{amount:0n,decimals:n});if(Number.isFinite(e))return i.copyWith({amount:xt(e,n)});let o=e.build({api:t.api,asset:t.chain.getAssetId(a)});return i.copyWith({amount:await o.call()})}async function F({configService:s,destinationAddress:t,evmSigner:e,polkadotSigner:a,sourceAddress:n,transferConfig:i}){let[o,c]=await C.createMulti([i.destination.chain,i.source.chain],s),r=await Y({destinationAddress:t,polkadot:o,transferConfig:i}),m=await Bt(c,r.fee),u=await j({destinationAddress:t,destinationFee:m,polkadot:c,sourceAddress:n,transferConfig:i});return{destination:r,getEstimate(l){let p=b(Z(l,u.balance.decimals).toString()).minus(u.balance.isSame(m)?m.toBig():b(0));return u.balance.copyWith({amount:p.lt(0)?0n:BigInt(p.toFixed())})},isSwapPossible:!0,max:u.max,min:Dt(r),source:u,async swap(){return F({configService:s,destinationAddress:n,evmSigner:e,polkadotSigner:a,sourceAddress:t,transferConfig:{...i,destination:i.source,source:i.destination}})},async transfer(l,d){var v,D;let p=Z(l,u.balance.decimals),{asset:h,source:{chain:f,config:w}}=i,x=(v=w.contract)==null?void 0:v.build({address:t,amount:p,asset:f.getAssetId(h),destination:r.chain,fee:m.amount,feeAsset:f.getAssetId(m)}),S=(D=w.extrinsic)==null?void 0:D.build({address:t,amount:p,asset:f.getAssetId(h),destination:r.chain,fee:m.amount,feeAsset:f.getAssetId(m),palletInstance:f.getAssetPalletInstance(h),source:f});if(x){let g=e||d.evmSigner;if(!g)throw new Error("EVM Signer must be provided");return y(f,x).transfer(g)}if(S){let g=a||d.polkadotSigner;if(!g)throw new Error("Polkadot signer must be provided");return c.transfer(n,S,g)}throw new Error("Either contract or extrinsic must be provided")}}}function Dt({balance:s,existentialDeposit:t,fee:e,min:a}){let n=b(0).plus(s.isSame(e)?e.toBig():b(0)).plus(s.isSame(t)&&s.toBig().lt(t.toBig())?t.toBig():b(0)).plus(s.toBig().lt(a.toBig())?a.toBig():b(0));return s.copyWith({amount:BigInt(n.toFixed())})}async function Bt(s,t){let e=await s.getAssetDecimals(t);return t.decimals===e?t:t.copyWith({amount:vt(t.amount,t.decimals,e),decimals:e})}function Ue(s){let t=(s==null?void 0:s.configService)??new et;return{assets(e){let{assets:a,asset:n}=tt(t).assets(e);return{assets:a,asset(i){let{sourceChains:o,source:c}=n(i);return{sourceChains:o,source(r){let{destinationChains:m,destination:u}=c(r);return{destinationChains:m,destination(l){return{async accounts(d,p){return F({...s,configService:t,destinationAddress:p,sourceAddress:d,transferConfig:u(l).build()})}}}}}}}}},async getTransferData({destinationAddress:e,destinationKeyOrChain:a,evmSigner:n,keyOrAsset:i,polkadotSigner:o,sourceAddress:c,sourceKeyOrChain:r}){return F({configService:t,destinationAddress:e,evmSigner:n,polkadotSigner:o,sourceAddress:c,transferConfig:tt(t).assets().asset(i).source(r).destination(a).build()})}}}async function Ye(s,t){let e=new et,n=e.getChainConfig(s).getAssetsConfigs(),i=await C.create(s,e);return await J({chain:s,assets:n,address:t,polkadot:i})}export{Ue as Sdk,J as getAssetsBalances,P as getBalance,gt as getContractFee,A as getDecimals,yt as getExtrinsicFee,pt as getFee,L as getFeeBalance,ht as getMax,T as getMin,Ye as getParachainBalances,j as getSourceData};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|