@moonbeam-network/xcm-builder 1.0.0-dev.251 → 1.0.0-dev.253

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
@@ -120,12 +120,18 @@ declare enum TransferType {
120
120
  LocalReserve = 1,
121
121
  DestinationReserve = 2
122
122
  }
123
+ type AssetMultilocation = (bigint | (number | string[])[])[];
123
124
 
124
125
  declare function XcmPrecompile(): {
125
126
  transferAssetsToPara20: (shouldTransferAssetPrecedeFeeAsset?: boolean) => ContractConfigBuilder;
126
127
  transferAssetsToPara32: (shouldTransferAssetPrecedeFeeAsset?: boolean) => ContractConfigBuilder;
127
128
  transferAssetsToRelay: () => ContractConfigBuilder;
128
- transferAssetsLocation: () => ContractConfigBuilder;
129
+ transferAssetsLocation: () => {
130
+ nativeAsset: () => ContractConfigBuilder;
131
+ localErc20: () => ContractConfigBuilder;
132
+ foreignAsset: () => ContractConfigBuilder;
133
+ foreignErc20: () => ContractConfigBuilder;
134
+ };
129
135
  transferAssetsUsingTypeAndThenAddress: (shouldTransferAssetPrecedeFeeAsset?: boolean) => ContractConfigBuilder;
130
136
  };
131
137
 
@@ -753,4 +759,4 @@ declare function MrlBuilder(): {
753
759
 
754
760
  declare const BATCH_CONTRACT_ADDRESS = "0x0000000000000000000000000000000000000808";
755
761
 
756
- export { type AssetAddressFormat, AssetMinBuilder, type AssetMinConfigBuilder, type AssetMinConfigBuilderParams, BATCH_CONTRACT_ABI, BATCH_CONTRACT_ADDRESS, BalanceBuilder, type BalanceBuilderParams, type BalanceConfigBuilder, type BuilderParams, type ConfigBuilder, ContractBuilder, ContractConfig, type ContractConfigBuilder, type ContractConfigConstructorParams, type DestinationMultilocation, ERC20_ABI, type EquilibriumSystemBalanceData, type EventMonitoringConfig, type EvmFunctionArgs, EvmQueryConfig, type EvmQueryConfigParams, type EvmQueryFunctions, ExtrinsicBuilder, ExtrinsicConfig, type ExtrinsicConfigBuilder, type ExtrinsicConfigConstructorParams, FeeBuilder, type FeeConfigBuilder, type FeeConfigBuilderParams, type GetVersionedAssetId, MonitoringBuilder, type MonitoringBuilderConfig, type MoonbeamRuntimeXcmConfigAssetType, MrlBuilder, type MrlBuilderParams, type MrlConfigBuilder, type MrlExecuteBuilderParams, type MrlExecuteConfigBuilder, type PalletBalancesAccountDataOld, type Parents, Protocols, type QueryConfigConstructorParams, SubstrateCallConfig, type SubstrateCallConfigConstructorParams, SubstrateQueryConfig, type TokensPalletAccountData, type Transact, TransferType, WormholeConfig, type WormholeConfigConstructorParams, type WormholeFunctionArgs, type WormholeTransferFunctions, type XcmPaymentFeeProps, XcmVersion, calculateSystemAccountBalance, evm, substrate, wormhole, wormholeFactory };
762
+ export { type AssetAddressFormat, AssetMinBuilder, type AssetMinConfigBuilder, type AssetMinConfigBuilderParams, type AssetMultilocation, BATCH_CONTRACT_ABI, BATCH_CONTRACT_ADDRESS, BalanceBuilder, type BalanceBuilderParams, type BalanceConfigBuilder, type BuilderParams, type ConfigBuilder, ContractBuilder, ContractConfig, type ContractConfigBuilder, type ContractConfigConstructorParams, type DestinationMultilocation, ERC20_ABI, type EquilibriumSystemBalanceData, type EventMonitoringConfig, type EvmFunctionArgs, EvmQueryConfig, type EvmQueryConfigParams, type EvmQueryFunctions, ExtrinsicBuilder, ExtrinsicConfig, type ExtrinsicConfigBuilder, type ExtrinsicConfigConstructorParams, FeeBuilder, type FeeConfigBuilder, type FeeConfigBuilderParams, type GetVersionedAssetId, MonitoringBuilder, type MonitoringBuilderConfig, type MoonbeamRuntimeXcmConfigAssetType, MrlBuilder, type MrlBuilderParams, type MrlConfigBuilder, type MrlExecuteBuilderParams, type MrlExecuteConfigBuilder, type PalletBalancesAccountDataOld, type Parents, Protocols, type QueryConfigConstructorParams, SubstrateCallConfig, type SubstrateCallConfigConstructorParams, SubstrateQueryConfig, type TokensPalletAccountData, type Transact, TransferType, WormholeConfig, type WormholeConfigConstructorParams, type WormholeFunctionArgs, type WormholeTransferFunctions, type XcmPaymentFeeProps, XcmVersion, calculateSystemAccountBalance, evm, substrate, wormhole, wormholeFactory };
package/build/index.mjs CHANGED
@@ -280,9 +280,7 @@ var TransferType = /* @__PURE__ */ ((TransferType2) => {
280
280
  })(TransferType || {});
281
281
 
282
282
  // src/contract/ContractBuilder.utils.ts
283
- import {
284
- EvmParachain
285
- } from "@moonbeam-network/xcm-types";
283
+ import { EvmParachain } from "@moonbeam-network/xcm-types";
286
284
  import { u8aToHex as u8aToHex2 } from "@polkadot/util";
287
285
  import { decodeAddress as decodeAddress2 } from "@polkadot/util-crypto";
288
286
 
@@ -401,29 +399,15 @@ function shouldFeeAssetPrecedeAsset({
401
399
  }
402
400
 
403
401
  // src/contract/ContractBuilder.utils.ts
404
- function encodeParachain(paraId) {
405
- return `0x00${paraId.toString(16).padStart(8, "0")}`;
406
- }
407
402
  function getPrecompileDestinationInterior(destination, address) {
408
403
  if (!address) {
409
- return [`0x0000000${destination.parachainId.toString(16)}`];
404
+ return [encodeParachain(destination.parachainId)];
410
405
  }
411
- const accountType = EvmParachain.is(destination) ? "03" : "01";
412
- const acc = `0x${accountType}${u8aToHex2(
413
- decodeAddress2(address),
414
- -1,
415
- false
416
- )}00`;
417
- return destination.parachainId ? [`0x0000000${destination.parachainId.toString(16)}`, acc] : [acc];
406
+ const acc = encodeAddress(destination, address);
407
+ return destination.parachainId ? [encodeParachain(destination.parachainId), acc] : [acc];
418
408
  }
419
409
  function getBeneficiaryMultilocation(address, destination) {
420
- const accountType = EvmParachain.is(destination) ? "03" : "01";
421
- const acc = `0x${accountType}${u8aToHex2(
422
- decodeAddress2(address),
423
- -1,
424
- false
425
- )}00`;
426
- return [0, [acc]];
410
+ return [0, [encodeAddress(destination, address)]];
427
411
  }
428
412
  function getDestinationMultilocation(address, destination) {
429
413
  const interior = getPrecompileDestinationInterior(destination, address);
@@ -433,10 +417,7 @@ function getDestinationParachainMultilocation(destination) {
433
417
  if (destination.isRelay) {
434
418
  return [1, []];
435
419
  }
436
- return [1, [`0x00${destination.parachainId.toString(16).padStart(8, "0")}`]];
437
- }
438
- function getAssetMultilocation(sourceApi, asset) {
439
- return [[0, [encodePalletInstance(sourceApi)]], asset.amount];
420
+ return [1, [encodeParachain(destination.parachainId)]];
440
421
  }
441
422
  function getGlobalConsensusDestination(sourceApi, destination) {
442
423
  return [
@@ -447,6 +428,76 @@ function getGlobalConsensusDestination(sourceApi, destination) {
447
428
  ]
448
429
  ];
449
430
  }
431
+ function getPalletInstanceMultilocation({
432
+ sourceApi,
433
+ asset
434
+ }) {
435
+ return [
436
+ [0, [encodePalletInstance(sourceApi, asset.getAssetPalletInstance())]],
437
+ asset.amount
438
+ ];
439
+ }
440
+ function getAssetAddressMultilocation({
441
+ sourceApi,
442
+ asset,
443
+ destination
444
+ }) {
445
+ if (!asset.address) {
446
+ throw new Error(`Asset address is required for ${asset.key}`);
447
+ }
448
+ return [
449
+ [
450
+ 0,
451
+ [
452
+ encodePalletInstance(sourceApi, asset.getAssetPalletInstance()),
453
+ encodeAddress(destination, asset.address)
454
+ ]
455
+ ],
456
+ asset.amount
457
+ ];
458
+ }
459
+ function getGlobalConsensusAssetMultilocation({
460
+ sourceApi,
461
+ asset,
462
+ destination
463
+ }) {
464
+ const assetInDestination = destination.getChainAsset(asset);
465
+ return [
466
+ [
467
+ 2,
468
+ [
469
+ encodeGlobalConsensus(sourceApi, destination),
470
+ encodeParachain(destination.parachainId),
471
+ encodePalletInstance(
472
+ sourceApi,
473
+ assetInDestination.getAssetPalletInstance()
474
+ )
475
+ ]
476
+ ],
477
+ asset.amount
478
+ ];
479
+ }
480
+ function getAddressGlobalConsensusAssetMultilocation({
481
+ sourceApi,
482
+ asset,
483
+ destination
484
+ }) {
485
+ if (!asset.address) {
486
+ throw new Error(`Asset address is required for ${asset.key}`);
487
+ }
488
+ return [
489
+ [
490
+ 2,
491
+ [
492
+ encodeGlobalConsensus(sourceApi, destination),
493
+ encodeParachain(destination.parachainId),
494
+ encodePalletInstance(sourceApi, asset.getAssetPalletInstance()),
495
+ encodeAddress(destination, asset.address)
496
+ ]
497
+ ],
498
+ asset.amount
499
+ ];
500
+ }
450
501
  function encodeXcmMessageToBytes(xcmMessage, api) {
451
502
  if (!api) {
452
503
  throw new Error("API is required to encode XCM message");
@@ -459,25 +510,34 @@ function encodeXcmMessageToBytes(xcmMessage, api) {
459
510
  throw error;
460
511
  }
461
512
  }
513
+ function encodeParachain(paraId) {
514
+ return `0x00${paraId.toString(16).padStart(8, "0")}`;
515
+ }
462
516
  function encodeGlobalConsensus(api, destination) {
463
- if (!api) {
464
- throw new Error("API is required to encode XCM message");
465
- }
466
517
  const globalConsensus = getGlobalConsensus(destination);
467
- const junctionType = api.registry.createType("XcmV3Junction", {
518
+ return encodeXcmJunction(api, {
468
519
  GlobalConsensus: globalConsensus
469
520
  });
470
- return junctionType.toHex();
471
521
  }
472
- function encodePalletInstance(api) {
522
+ function encodePalletInstance(api, palletInstance) {
523
+ return encodeXcmJunction(api, {
524
+ PalletInstance: palletInstance
525
+ });
526
+ }
527
+ function encodeXcmJunction(api, junction) {
473
528
  if (!api) {
474
- throw new Error("API is required to encode asset");
529
+ throw new Error("API is required to encode XCM junction");
475
530
  }
476
- const assetType = {
477
- PalletInstance: 10
478
- };
479
- const multiLocationType = api.registry.createType("XcmV3Junction", assetType);
480
- return multiLocationType.toHex();
531
+ const junctionType = api.createType("XcmV3Junction", junction);
532
+ return junctionType.toHex();
533
+ }
534
+ function encodeAddress(destination, address) {
535
+ const accountType = EvmParachain.is(destination) ? "03" : "01";
536
+ return `0x${accountType}${u8aToHex2(
537
+ decodeAddress2(address),
538
+ -1,
539
+ false
540
+ )}00`;
481
541
  }
482
542
 
483
543
  // src/contract/contracts/XcmPrecompile/XcmPrecompileAbi.ts
@@ -1018,30 +1078,43 @@ function XcmPrecompile() {
1018
1078
  }
1019
1079
  }),
1020
1080
  transferAssetsLocation: () => ({
1021
- // TODO
1022
- build: ({ destinationAddress, asset, destination, sourceApi }) => {
1023
- const destinationMultilocation = getGlobalConsensusDestination(
1024
- sourceApi,
1025
- destination
1026
- );
1027
- console.log("destinationMultilocation", destinationMultilocation);
1028
- console.log(
1029
- "assetMultilocation",
1030
- getAssetMultilocation(sourceApi, asset)
1031
- );
1032
- return new ContractConfig({
1033
- address: XCM_PRECOMPILE_ADDRESS,
1034
- abi: XCM_ABI,
1035
- args: [
1036
- getGlobalConsensusDestination(sourceApi, destination),
1037
- getBeneficiaryMultilocation(destinationAddress, destination),
1038
- [getAssetMultilocation(sourceApi, asset)],
1039
- 0
1040
- ],
1041
- func: "transferAssetsLocation",
1042
- module: "Xcm"
1043
- });
1044
- }
1081
+ nativeAsset: () => ({
1082
+ build: (params) => {
1083
+ return buildTransferAssetsLocation({
1084
+ ...params,
1085
+ assetsMultilocations: [getPalletInstanceMultilocation(params)]
1086
+ });
1087
+ }
1088
+ }),
1089
+ localErc20: () => ({
1090
+ build: (params) => {
1091
+ return buildTransferAssetsLocation({
1092
+ ...params,
1093
+ assetsMultilocations: [getAssetAddressMultilocation(params)]
1094
+ });
1095
+ }
1096
+ }),
1097
+ foreignAsset: () => ({
1098
+ build: (params) => {
1099
+ return buildTransferAssetsLocation({
1100
+ ...params,
1101
+ assetsMultilocations: [
1102
+ getGlobalConsensusAssetMultilocation(params)
1103
+ ]
1104
+ });
1105
+ }
1106
+ }),
1107
+ foreignErc20: () => ({
1108
+ build: (params) => {
1109
+ return buildTransferAssetsLocation({
1110
+ ...params,
1111
+ assetsMultilocations: [
1112
+ getGlobalConsensusAssetMultilocation(params),
1113
+ getAddressGlobalConsensusAssetMultilocation(params)
1114
+ ]
1115
+ });
1116
+ }
1117
+ })
1045
1118
  }),
1046
1119
  transferAssetsUsingTypeAndThenAddress: (shouldTransferAssetPrecedeFeeAsset = false) => ({
1047
1120
  build: ({
@@ -1123,6 +1196,26 @@ function buildXcmMessage(assets3, destinationAddress, sourceApi) {
1123
1196
  [xcmVersion]: [instruction]
1124
1197
  };
1125
1198
  }
1199
+ function buildTransferAssetsLocation({
1200
+ assetsMultilocations,
1201
+ feeAssetItem = 0,
1202
+ destinationAddress,
1203
+ destination,
1204
+ sourceApi
1205
+ }) {
1206
+ return new ContractConfig({
1207
+ address: XCM_PRECOMPILE_ADDRESS,
1208
+ abi: XCM_ABI,
1209
+ args: [
1210
+ getGlobalConsensusDestination(sourceApi, destination),
1211
+ getBeneficiaryMultilocation(destinationAddress, destination),
1212
+ assetsMultilocations,
1213
+ feeAssetItem
1214
+ ],
1215
+ func: "transferAssetsLocation",
1216
+ module: "Xcm"
1217
+ });
1218
+ }
1126
1219
 
1127
1220
  // src/contract/contracts/Xtokens/Xtokens.ts
1128
1221
  import { formatAssetIdToERC20 } from "@moonbeam-network/xcm-utils";