@moonbeam-network/xcm-builder 4.4.7 → 4.5.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/build/index.d.ts CHANGED
@@ -1733,7 +1733,7 @@ declare enum Provider {
1733
1733
  }
1734
1734
  type MrlTransferConfig = ContractConfig | ExtrinsicConfig | WormholeConfig | SnowbridgeConfig;
1735
1735
  type MrlConfigBuilder = ConfigBuilder<MrlTransferConfig, MrlBuilderParams> & {
1736
- provider?: Provider;
1736
+ provider: Provider;
1737
1737
  };
1738
1738
  type MrlExecuteConfigBuilder = ConfigBuilder<ContractConfig, MrlExecuteBuilderParams>;
1739
1739
  interface MrlBuilderParams extends BuilderParams<AnyChain> {
@@ -1796,7 +1796,12 @@ declare function snowbridge(): {
1796
1796
  };
1797
1797
 
1798
1798
  declare function Batch(): {
1799
- transferAssetsAndMessage: () => MrlConfigBuilder;
1799
+ /**
1800
+ * Transfers assets and XCM message using XTokens contract for multi-currency transfer.
1801
+ * Uses parents: 1 for multilocation derivation.
1802
+ */
1803
+ transferAssetsAndMessageViaXtokens: () => MrlConfigBuilder;
1804
+ transferAssetsAndMessageViaXcmPrecompile: () => MrlConfigBuilder;
1800
1805
  };
1801
1806
 
1802
1807
  declare function Gmp(): {
@@ -1823,7 +1828,7 @@ declare function ethereumXcm(): {
1823
1828
  };
1824
1829
 
1825
1830
  declare function polkadotXcm(): {
1826
- send: () => MrlConfigBuilder;
1831
+ send: (transferAssetsPallet?: "polkadotXcm" | "xTokens") => MrlConfigBuilder;
1827
1832
  };
1828
1833
 
1829
1834
  declare function extrinsic(): {
package/build/index.mjs CHANGED
@@ -5178,7 +5178,7 @@ var CROSS_CHAIN_FEE = 100000000000000000n;
5178
5178
  function polkadotXcm3() {
5179
5179
  const provider = "wormhole" /* Wormhole */;
5180
5180
  return {
5181
- send: () => ({
5181
+ send: (transferAssetsPallet) => ({
5182
5182
  provider,
5183
5183
  build: ({
5184
5184
  asset,
@@ -5207,9 +5207,10 @@ function polkadotXcm3() {
5207
5207
  const { address20: computedOriginAccount } = getMultilocationDerivedAddresses({
5208
5208
  address: sourceAddress,
5209
5209
  paraId: source.parachainId,
5210
- isParents: true
5210
+ parents: 1
5211
5211
  });
5212
5212
  const assetTransferTxs = getAssetTransferTxs({
5213
+ transferAssetsPallet,
5213
5214
  asset,
5214
5215
  computedOriginAccount,
5215
5216
  destination,
@@ -5248,7 +5249,28 @@ function polkadotXcm3() {
5248
5249
  })
5249
5250
  };
5250
5251
  }
5252
+ function getDestinationMultilocation2(source, bridgeChain) {
5253
+ const isDifferentEcosystem = source.ecosystem !== bridgeChain.ecosystem;
5254
+ if (isDifferentEcosystem) {
5255
+ return {
5256
+ parents: 2,
5257
+ interior: {
5258
+ X2: [
5259
+ { GlobalConsensus: getGlobalConsensus(bridgeChain) },
5260
+ { Parachain: bridgeChain.parachainId }
5261
+ ]
5262
+ }
5263
+ };
5264
+ }
5265
+ return {
5266
+ parents: 1,
5267
+ interior: {
5268
+ X1: { Parachain: bridgeChain.parachainId }
5269
+ }
5270
+ };
5271
+ }
5251
5272
  function buildSendExtrinsic({
5273
+ source,
5252
5274
  computedOriginAccount,
5253
5275
  moonAsset,
5254
5276
  bridgeChain,
@@ -5261,12 +5283,10 @@ function buildSendExtrinsic({
5261
5283
  const version = getExtrinsicArgumentVersion(sourceApi.tx.polkadotXcm.send);
5262
5284
  return sourceApi.tx.polkadotXcm.send(
5263
5285
  {
5264
- [version]: normalizeX1(version, {
5265
- parents: 1,
5266
- interior: {
5267
- X1: { Parachain: bridgeChain.parachainId }
5268
- }
5269
- })
5286
+ [version]: normalizeX1(
5287
+ version,
5288
+ getDestinationMultilocation2(source, bridgeChain)
5289
+ )
5270
5290
  },
5271
5291
  {
5272
5292
  [version]: [
@@ -5342,6 +5362,48 @@ function buildSendExtrinsic({
5342
5362
  );
5343
5363
  }
5344
5364
  function getAssetTransferTxs({
5365
+ transferAssetsPallet = "xTokens",
5366
+ ...params
5367
+ }) {
5368
+ if (transferAssetsPallet === "xTokens") {
5369
+ return getAssetTransferTxsFromXtokens(params);
5370
+ }
5371
+ if (transferAssetsPallet === "polkadotXcm") {
5372
+ return getAssetTransferTxsForPolkadotXcm(params);
5373
+ }
5374
+ throw new Error(
5375
+ "Invalid transferAssetsPallet for polkadotXcm().send() function"
5376
+ );
5377
+ }
5378
+ function getAssetTransferTxsForPolkadotXcm({
5379
+ asset,
5380
+ computedOriginAccount,
5381
+ moonApi,
5382
+ moonAsset,
5383
+ bridgeChain,
5384
+ source,
5385
+ sourceAddress,
5386
+ sourceApi
5387
+ }) {
5388
+ const { transferAssets } = sourceApi.tx.polkadotXcm;
5389
+ const polkadotXcmBuilder = ExtrinsicBuilder().polkadotXcm().transferAssetsToEcosystem().X4();
5390
+ const transferAssetsTx = transferAssets(
5391
+ ...polkadotXcmBuilder.build({
5392
+ asset,
5393
+ destination: bridgeChain,
5394
+ destinationAddress: computedOriginAccount,
5395
+ destinationApi: moonApi,
5396
+ fee: AssetAmount.fromChainAsset(source.getChainAsset(moonAsset), {
5397
+ amount: CROSS_CHAIN_FEE + BUY_EXECUTION_FEE
5398
+ }),
5399
+ source,
5400
+ sourceAddress,
5401
+ sourceApi
5402
+ }).getArgs(transferAssets)
5403
+ );
5404
+ return [transferAssetsTx];
5405
+ }
5406
+ function getAssetTransferTxsFromXtokens({
5345
5407
  asset,
5346
5408
  computedOriginAccount,
5347
5409
  fee,
@@ -5352,9 +5414,9 @@ function getAssetTransferTxs({
5352
5414
  sourceAddress,
5353
5415
  sourceApi
5354
5416
  }) {
5355
- const { transfer, transferMulticurrencies } = sourceApi.tx.xTokens;
5356
5417
  const transferBuilder = ExtrinsicBuilder().xTokens().transfer();
5357
5418
  const transferMulticurrenciesBuilder = ExtrinsicBuilder().xTokens().transferMultiCurrencies();
5419
+ const { transfer, transferMulticurrencies } = sourceApi.tx.xTokens;
5358
5420
  if (asset.isSame(fee)) {
5359
5421
  const assetTransferTx = transfer(
5360
5422
  ...transferBuilder.build({
@@ -5405,8 +5467,8 @@ function getAssetTransferTxs({
5405
5467
  return [multiCurrenciesTransferTx];
5406
5468
  }
5407
5469
 
5408
- // src/mrl/providers/wormhole/contract/Batch/abi/peaq/PeaqBatchContractAbi.ts
5409
- var PEAQ_BATCH_ABI = [
5470
+ // src/mrl/providers/wormhole/contract/Batch/abi/BatchContractAbi.ts
5471
+ var BATCH_ABI = [
5410
5472
  {
5411
5473
  anonymous: false,
5412
5474
  inputs: [
@@ -5519,8 +5581,8 @@ var PEAQ_BATCH_ABI = [
5519
5581
  }
5520
5582
  ];
5521
5583
 
5522
- // src/mrl/providers/wormhole/contract/Batch/abi/peaq/PeaqXcmUtilsContractAbi.ts
5523
- var PEAQ_XCM_UTILS_ABI = [
5584
+ // src/mrl/providers/wormhole/contract/Batch/abi/XcmUtilsContractAbi.ts
5585
+ var XCM_UTILS_ABI = [
5524
5586
  {
5525
5587
  inputs: [
5526
5588
  {
@@ -5621,8 +5683,8 @@ var PEAQ_XCM_UTILS_ABI = [
5621
5683
  }
5622
5684
  ];
5623
5685
 
5624
- // src/mrl/providers/wormhole/contract/Batch/abi/peaq/PeaqXtokensContractAbi.ts
5625
- var PEAQ_XTOKENS_ABI = [
5686
+ // src/mrl/providers/wormhole/contract/Batch/abi/XtokensContractAbi.ts
5687
+ var XTOKENS_ABI2 = [
5626
5688
  {
5627
5689
  inputs: [
5628
5690
  {
@@ -5938,9 +6000,9 @@ var PEAQ_XTOKENS_ABI = [
5938
6000
  // src/mrl/providers/wormhole/contract/Batch/abi/abi.helpers.ts
5939
6001
  function getAbisForChain(_) {
5940
6002
  return {
5941
- BatchAbi: PEAQ_BATCH_ABI,
5942
- XcmUtilsAbi: PEAQ_XCM_UTILS_ABI,
5943
- XtokensAbi: PEAQ_XTOKENS_ABI
6003
+ BatchAbi: BATCH_ABI,
6004
+ XcmUtilsAbi: XCM_UTILS_ABI,
6005
+ XtokensAbi: XTOKENS_ABI2
5944
6006
  };
5945
6007
  }
5946
6008
 
@@ -5949,22 +6011,14 @@ var module = "Batch";
5949
6011
  function Batch() {
5950
6012
  const provider = "wormhole" /* Wormhole */;
5951
6013
  return {
5952
- transferAssetsAndMessage: () => ({
6014
+ /**
6015
+ * Transfers assets and XCM message using XTokens contract for multi-currency transfer.
6016
+ * Uses parents: 1 for multilocation derivation.
6017
+ */
6018
+ transferAssetsAndMessageViaXtokens: () => ({
5953
6019
  provider,
5954
- build: ({
5955
- asset,
5956
- destination,
5957
- destinationAddress,
5958
- fee,
5959
- isAutomatic,
5960
- moonAsset,
5961
- bridgeChain,
5962
- moonApi,
5963
- source,
5964
- sourceAddress,
5965
- sourceApi,
5966
- transact
5967
- }) => {
6020
+ build: (params) => {
6021
+ const { source, sourceAddress, sourceApi } = params;
5968
6022
  if (!EvmParachain5.is(source)) {
5969
6023
  throw new Error("Source chain needs to be an EVMParachain");
5970
6024
  }
@@ -5977,44 +6031,53 @@ function Batch() {
5977
6031
  );
5978
6032
  }
5979
6033
  const subMappedAddress = evmToAddress3(sourceAddress);
5980
- const { BatchAbi, XcmUtilsAbi, XtokensAbi } = getAbisForChain(source);
5981
6034
  const { address20: computedOriginAccount } = getMultilocationDerivedAddresses2({
5982
6035
  address: subMappedAddress,
5983
6036
  paraId: source.parachainId,
5984
- isParents: true
6037
+ parents: 1
5985
6038
  });
5986
- const send = buildSendExtrinsic({
5987
- asset,
5988
- destination,
5989
- destinationAddress,
6039
+ const encodedXcmMessage = buildXcmMessage2({
6040
+ asset: params.asset,
6041
+ destination: params.destination,
6042
+ destinationAddress: params.destinationAddress,
5990
6043
  computedOriginAccount,
5991
- fee,
5992
- isAutomatic,
5993
- moonAsset,
5994
- bridgeChain,
5995
- moonApi,
6044
+ fee: params.fee,
6045
+ isAutomatic: params.isAutomatic,
6046
+ moonAsset: params.moonAsset,
6047
+ bridgeChain: params.bridgeChain,
6048
+ moonApi: params.moonApi,
5996
6049
  source,
5997
6050
  sourceAddress,
5998
6051
  sourceApi,
5999
- transact
6052
+ transact: params.transact
6000
6053
  });
6001
- const encodedXcmMessage = send.args[1].toHex();
6002
- const { destinationParachain, destinationParachainAndAddress } = getDestinationInHex(bridgeChain, computedOriginAccount);
6054
+ const { BatchAbi, XcmUtilsAbi, XtokensAbi } = getAbisForChain(source);
6055
+ const { destinationParachainAndAddress } = getDestinationInHex(
6056
+ params.bridgeChain,
6057
+ computedOriginAccount
6058
+ );
6003
6059
  const { currencies, feeItem } = getCurrencies({
6004
6060
  source,
6005
- moonAsset,
6006
- asset
6061
+ moonAsset: params.moonAsset,
6062
+ asset: params.asset
6007
6063
  });
6008
- const weight = maxUint64;
6009
6064
  const multiTransferTxData = encodeFunctionData2({
6010
6065
  abi: XtokensAbi,
6011
6066
  functionName: "transferMultiCurrencies",
6012
- args: [currencies, feeItem, destinationParachainAndAddress, weight]
6067
+ args: [
6068
+ currencies,
6069
+ feeItem,
6070
+ destinationParachainAndAddress,
6071
+ maxUint64
6072
+ ]
6013
6073
  });
6014
- const xcmSendTxData = encodeFunctionData2({
6074
+ const xcmSendTxData = buildXcmSendTxData({
6015
6075
  abi: XcmUtilsAbi,
6016
- functionName: "xcmSend",
6017
- args: [destinationParachain, encodedXcmMessage]
6076
+ destination: getDestinationInHex(
6077
+ params.bridgeChain,
6078
+ computedOriginAccount
6079
+ ).destinationParachain,
6080
+ encodedXcmMessage
6018
6081
  });
6019
6082
  return new ContractConfig({
6020
6083
  address: source.contracts.Batch,
@@ -6029,9 +6092,96 @@ function Batch() {
6029
6092
  module
6030
6093
  });
6031
6094
  }
6095
+ }),
6096
+ transferAssetsAndMessageViaXcmPrecompile: () => ({
6097
+ provider,
6098
+ build: (params) => {
6099
+ const { source, sourceAddress, sourceApi } = params;
6100
+ if (!EvmParachain5.is(source)) {
6101
+ throw new Error("Source chain needs to be an EVMParachain");
6102
+ }
6103
+ if (!sourceApi) {
6104
+ throw new Error("Source API needs to be defined");
6105
+ }
6106
+ if (!params.bridgeChainFee) {
6107
+ throw new Error("Bridge chain fee is required");
6108
+ }
6109
+ if (!source.contracts?.XcmUtils || !source.contracts?.Batch || !source.contracts?.XcmPrecompile) {
6110
+ throw new Error(
6111
+ "Source chain needs to have the XcmUtils, Batch and XcmPrecompile contract addresses configured"
6112
+ );
6113
+ }
6114
+ const { address20: computedOriginAccount } = getMultilocationDerivedAddresses2({
6115
+ address: sourceAddress,
6116
+ paraId: source.parachainId,
6117
+ parents: 2
6118
+ // this function is only used for global consensus currently
6119
+ });
6120
+ const encodedXcmMessage = buildXcmMessage2({
6121
+ asset: params.asset,
6122
+ destination: params.destination,
6123
+ destinationAddress: params.destinationAddress,
6124
+ computedOriginAccount,
6125
+ fee: params.fee,
6126
+ isAutomatic: params.isAutomatic,
6127
+ moonAsset: params.moonAsset,
6128
+ bridgeChain: params.bridgeChain,
6129
+ moonApi: params.moonApi,
6130
+ source,
6131
+ sourceAddress,
6132
+ sourceApi,
6133
+ transact: params.transact
6134
+ });
6135
+ const { BatchAbi, XcmUtilsAbi } = getAbisForChain(source);
6136
+ const transferAssetsCall = ContractBuilder().XcmPrecompile().transferAssetsLocation().foreignErc20().build({
6137
+ sourceApi,
6138
+ asset: params.asset,
6139
+ fee: params.bridgeChainFee,
6140
+ destination: params.bridgeChain,
6141
+ destinationAddress: computedOriginAccount,
6142
+ source,
6143
+ sourceAddress
6144
+ });
6145
+ const transferAssetsCallData = transferAssetsCall.encodeFunctionData();
6146
+ const globalConsensusDestination = getGlobalConsensusDestination(
6147
+ sourceApi,
6148
+ params.bridgeChain
6149
+ );
6150
+ const xcmSendTxData = buildXcmSendTxData({
6151
+ abi: XcmUtilsAbi,
6152
+ destination: {
6153
+ parents: globalConsensusDestination[0],
6154
+ interior: globalConsensusDestination[1]
6155
+ },
6156
+ encodedXcmMessage
6157
+ });
6158
+ return new ContractConfig({
6159
+ address: source.contracts.Batch,
6160
+ abi: BatchAbi,
6161
+ args: [
6162
+ [source.contracts.XcmPrecompile, source.contracts.XcmUtils],
6163
+ [],
6164
+ [transferAssetsCallData, xcmSendTxData],
6165
+ []
6166
+ ],
6167
+ func: "batchAll",
6168
+ module
6169
+ });
6170
+ }
6032
6171
  })
6033
6172
  };
6034
6173
  }
6174
+ function buildXcmMessage2(params) {
6175
+ const send = buildSendExtrinsic(params);
6176
+ return send.args[1].toHex();
6177
+ }
6178
+ function buildXcmSendTxData(params) {
6179
+ return encodeFunctionData2({
6180
+ abi: params.abi,
6181
+ functionName: "xcmSend",
6182
+ args: [params.destination, params.encodedXcmMessage]
6183
+ });
6184
+ }
6035
6185
  function getDestinationInHex(bridgeChain, computedOriginAccount) {
6036
6186
  const destinationParachain = {
6037
6187
  parents: 1,
@@ -6163,28 +6313,28 @@ function wormhole() {
6163
6313
  destinationAddress,
6164
6314
  isAutomatic,
6165
6315
  moonApi,
6166
- bridgeChain: moonChain,
6316
+ bridgeChain,
6167
6317
  source,
6168
6318
  sourceAddress
6169
6319
  }) => {
6170
6320
  const isSourceParachain = Parachain3.is(source);
6171
- const isDestinationMoonChain = destination.isEqual(moonChain);
6321
+ const isDestinationMoonChain = destination.isEqual(bridgeChain);
6172
6322
  const isDestinationEvmChain = EvmChain4.is(destination);
6173
6323
  const isNativeAsset = asset.isSame(
6174
- isDestinationEvmChain ? moonChain.nativeAsset : source.nativeAsset
6324
+ isDestinationEvmChain ? bridgeChain.nativeAsset : source.nativeAsset
6175
6325
  );
6176
- const tokenAddress = isNativeAsset ? "native" : isDestinationEvmChain ? moonChain.getChainAsset(asset).address : asset.address;
6326
+ const tokenAddress = isNativeAsset ? "native" : isDestinationEvmChain ? bridgeChain.getChainAsset(asset).address : asset.address;
6177
6327
  const { address20: computedOriginAccount } = getMultilocationDerivedAddresses3({
6178
6328
  address: sourceAddress,
6179
6329
  paraId: isSourceParachain ? source.parachainId : void 0,
6180
- isParents: true
6330
+ parents: 1
6181
6331
  });
6182
6332
  if (!tokenAddress) {
6183
6333
  throw new Error(`Asset ${asset.key} has no address`);
6184
6334
  }
6185
6335
  const wh = wormholeFactory(source);
6186
- const whSource = isDestinationEvmChain ? wh.getChain(moonChain.getWormholeName()) : wh.getChain(source.getWormholeName());
6187
- const whDestination = isDestinationEvmChain ? wh.getChain(destination.getWormholeName()) : wh.getChain(moonChain.getWormholeName());
6336
+ const whSource = isDestinationEvmChain ? wh.getChain(bridgeChain.getWormholeName()) : wh.getChain(source.getWormholeName());
6337
+ const whDestination = isDestinationEvmChain ? wh.getChain(destination.getWormholeName()) : wh.getChain(bridgeChain.getWormholeName());
6188
6338
  const whAsset = Wormhole2.tokenId(whSource.chain, tokenAddress);
6189
6339
  const whSourceAddress = Wormhole2.chainAddress(
6190
6340
  whSource.chain,
@@ -6201,7 +6351,12 @@ function wormhole() {
6201
6351
  from: whSourceAddress,
6202
6352
  to: whDestinationAddress,
6203
6353
  protocol: isAutomatic ? "AutomaticTokenBridge" /* AutomaticTokenBridge */ : "TokenBridge" /* TokenBridge */,
6204
- payload: isDestinationMoonChain || isDestinationEvmChain ? void 0 : getPayload({ destination, destinationAddress, moonApi })
6354
+ payload: isDestinationMoonChain || isDestinationEvmChain ? void 0 : getPayload({
6355
+ destination,
6356
+ destinationAddress,
6357
+ moonApi,
6358
+ bridgeChain
6359
+ })
6205
6360
  },
6206
6361
  func: "tokenTransfer"
6207
6362
  });
@@ -6212,16 +6367,56 @@ function wormhole() {
6212
6367
  function getPayload({
6213
6368
  moonApi,
6214
6369
  destination,
6215
- destinationAddress
6370
+ destinationAddress,
6371
+ bridgeChain
6372
+ }) {
6373
+ const destinationMultilocation = getDestinationMultilocation3({
6374
+ destination,
6375
+ destinationAddress,
6376
+ moonApi,
6377
+ bridgeChain
6378
+ });
6379
+ const action = moonApi.createType("XcmRoutingUserAction", {
6380
+ destination: destinationMultilocation
6381
+ });
6382
+ const versioned = moonApi.createType("VersionedUserAction", {
6383
+ V1: action
6384
+ });
6385
+ return versioned.toU8a();
6386
+ }
6387
+ function getDestinationMultilocation3({
6388
+ moonApi,
6389
+ destination,
6390
+ destinationAddress,
6391
+ bridgeChain
6216
6392
  }) {
6217
6393
  if (!EvmParachain7.isAnyParachain(destination)) {
6218
6394
  throw new Error(
6219
6395
  `Destination ${destination.name} is not a Parachain or EvmParachain`
6220
6396
  );
6221
6397
  }
6222
- const isEvmDestination = EvmParachain7.is(destination);
6223
6398
  const version = getExtrinsicArgumentVersion(moonApi.tx.polkadotXcm.send);
6224
- const multilocation = moonApi.createType("XcmVersionedLocation", {
6399
+ const isDifferentEcosystem = destination.ecosystem !== bridgeChain.ecosystem;
6400
+ const isEvmDestination = EvmParachain7.is(destination);
6401
+ if (isDifferentEcosystem) {
6402
+ return moonApi.createType("XcmVersionedLocation", {
6403
+ [version]: {
6404
+ parents: 2,
6405
+ interior: {
6406
+ X3: [
6407
+ {
6408
+ GlobalConsensus: getGlobalConsensus(destination)
6409
+ },
6410
+ {
6411
+ Parachain: destination.parachainId
6412
+ },
6413
+ getExtrinsicAccount(destinationAddress)
6414
+ ]
6415
+ }
6416
+ }
6417
+ });
6418
+ }
6419
+ return moonApi.createType("XcmVersionedLocation", {
6225
6420
  [version]: {
6226
6421
  parents: 1,
6227
6422
  interior: {
@@ -6236,13 +6431,6 @@ function getPayload({
6236
6431
  }
6237
6432
  }
6238
6433
  });
6239
- const action = moonApi.createType("XcmRoutingUserAction", {
6240
- destination: multilocation
6241
- });
6242
- const versioned = moonApi.createType("VersionedUserAction", {
6243
- V1: action
6244
- });
6245
- return versioned.toU8a();
6246
6434
  }
6247
6435
 
6248
6436
  // src/mrl/providers/wormhole/contract/TokenBridge/TokenBridgeAbi.ts