@moonbeam-network/xcm-builder 1.0.0-dev.249 → 1.0.0-dev.250

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
@@ -85,7 +85,7 @@ type DestinationMultilocation = [
85
85
  /**
86
86
  * 1 - if transaction is going through or to a relay chain
87
87
  */
88
- 1,
88
+ number,
89
89
  ([
90
90
  /**
91
91
  * example '0x00000007DC'
@@ -125,6 +125,7 @@ declare function XcmPrecompile(): {
125
125
  transferAssetsToPara20: (shouldTransferAssetPrecedeFeeAsset?: boolean) => ContractConfigBuilder;
126
126
  transferAssetsToPara32: (shouldTransferAssetPrecedeFeeAsset?: boolean) => ContractConfigBuilder;
127
127
  transferAssetsToRelay: () => ContractConfigBuilder;
128
+ transferAssetsLocation: () => ContractConfigBuilder;
128
129
  transferAssetsUsingTypeAndThenAddress: (shouldTransferAssetPrecedeFeeAsset?: boolean) => ContractConfigBuilder;
129
130
  };
130
131
 
package/build/index.mjs CHANGED
@@ -280,9 +280,130 @@ var TransferType = /* @__PURE__ */ ((TransferType2) => {
280
280
  })(TransferType || {});
281
281
 
282
282
  // src/contract/ContractBuilder.utils.ts
283
- import { EvmParachain } from "@moonbeam-network/xcm-types";
283
+ import {
284
+ EvmParachain
285
+ } from "@moonbeam-network/xcm-types";
284
286
  import { u8aToHex as u8aToHex2 } from "@polkadot/util";
285
287
  import { decodeAddress as decodeAddress2 } from "@polkadot/util-crypto";
288
+
289
+ // src/extrinsic/pallets/polkadotXcm/polkadotXcm.util.ts
290
+ import { Ecosystem } from "@moonbeam-network/xcm-types";
291
+ function getPolkadotXcmExtrinsicArgs({
292
+ assets: assets3,
293
+ destinationAddress,
294
+ destination,
295
+ func,
296
+ parents = 1,
297
+ feeIndex = 0
298
+ }) {
299
+ const version = getExtrinsicArgumentVersion(func);
300
+ return [
301
+ // dest
302
+ {
303
+ [version]: normalizeX1(version, {
304
+ parents,
305
+ interior: {
306
+ X1: {
307
+ Parachain: destination.parachainId
308
+ }
309
+ }
310
+ })
311
+ },
312
+ // beneficiary
313
+ {
314
+ [version]: normalizeX1(version, {
315
+ parents: 0,
316
+ interior: {
317
+ X1: getExtrinsicAccount(destinationAddress)
318
+ }
319
+ })
320
+ },
321
+ // assets
322
+ {
323
+ [version]: assets3
324
+ },
325
+ // feeAssetItem
326
+ feeIndex,
327
+ // weightLimit
328
+ "Unlimited"
329
+ ];
330
+ }
331
+ function isKusamaDestination(destination) {
332
+ return destination.ecosystem === Ecosystem.Kusama || destination.ecosystem === Ecosystem.MoonsamaRelay;
333
+ }
334
+ function isPolkadotDestination(destination) {
335
+ return destination.ecosystem === Ecosystem.Polkadot || destination.ecosystem === Ecosystem.MoonlamaRelay;
336
+ }
337
+ function getGlobalConsensus(destination) {
338
+ if (isKusamaDestination(destination)) {
339
+ return "Kusama";
340
+ }
341
+ if (isPolkadotDestination(destination)) {
342
+ return "Polkadot";
343
+ }
344
+ return {
345
+ ByGenesis: destination.relayGenesisHash
346
+ };
347
+ }
348
+ function getEcosystemTransferExtrinsicArgs({
349
+ assets: assets3,
350
+ destinationAddress,
351
+ destination,
352
+ func,
353
+ feeIndex = 0
354
+ }) {
355
+ const version = getExtrinsicArgumentVersion(func);
356
+ const globalConsensus = getGlobalConsensus(destination);
357
+ return [
358
+ // dest
359
+ {
360
+ [version]: normalizeX1(version, {
361
+ parents: 2,
362
+ interior: {
363
+ X2: [
364
+ { GlobalConsensus: globalConsensus },
365
+ {
366
+ Parachain: destination.parachainId
367
+ }
368
+ ]
369
+ }
370
+ })
371
+ },
372
+ // beneficiary
373
+ {
374
+ [version]: normalizeX1(version, {
375
+ parents: 0,
376
+ interior: {
377
+ X1: getExtrinsicAccount(destinationAddress)
378
+ }
379
+ })
380
+ },
381
+ // assets
382
+ {
383
+ [version]: assets3
384
+ },
385
+ // feeAssetItem
386
+ feeIndex,
387
+ // weightLimit
388
+ "Unlimited"
389
+ ];
390
+ }
391
+ function shouldFeeAssetPrecedeAsset({
392
+ asset,
393
+ fee
394
+ }) {
395
+ const assetIdNumber = Number(asset.getAssetId());
396
+ const feeAssetIdNumber = Number(fee.getAssetId());
397
+ if (Number.isNaN(assetIdNumber) || Number.isNaN(feeAssetIdNumber)) {
398
+ return false;
399
+ }
400
+ return assetIdNumber > feeAssetIdNumber;
401
+ }
402
+
403
+ // src/contract/ContractBuilder.utils.ts
404
+ function encodeParachain(paraId) {
405
+ return `0x00${paraId.toString(16).padStart(8, "0")}`;
406
+ }
286
407
  function getPrecompileDestinationInterior(destination, address) {
287
408
  if (!address) {
288
409
  return [`0x0000000${destination.parachainId.toString(16)}`];
@@ -295,6 +416,15 @@ function getPrecompileDestinationInterior(destination, address) {
295
416
  )}00`;
296
417
  return destination.parachainId ? [`0x0000000${destination.parachainId.toString(16)}`, acc] : [acc];
297
418
  }
419
+ 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]];
427
+ }
298
428
  function getDestinationMultilocation(address, destination) {
299
429
  const interior = getPrecompileDestinationInterior(destination, address);
300
430
  return [1, interior];
@@ -305,6 +435,18 @@ function getDestinationParachainMultilocation(destination) {
305
435
  }
306
436
  return [1, [`0x00${destination.parachainId.toString(16).padStart(8, "0")}`]];
307
437
  }
438
+ function getAssetMultilocation(sourceApi, asset) {
439
+ return [[0, [encodePalletInstance(sourceApi)]], asset.amount];
440
+ }
441
+ function getGlobalConsensusDestination(sourceApi, destination) {
442
+ return [
443
+ 2,
444
+ [
445
+ encodeGlobalConsensus(sourceApi, destination),
446
+ encodeParachain(destination.parachainId)
447
+ ]
448
+ ];
449
+ }
308
450
  function encodeXcmMessageToBytes(xcmMessage, api) {
309
451
  if (!api) {
310
452
  throw new Error("API is required to encode XCM message");
@@ -317,6 +459,26 @@ function encodeXcmMessageToBytes(xcmMessage, api) {
317
459
  throw error;
318
460
  }
319
461
  }
462
+ function encodeGlobalConsensus(api, destination) {
463
+ if (!api) {
464
+ throw new Error("API is required to encode XCM message");
465
+ }
466
+ const globalConsensus = getGlobalConsensus(destination);
467
+ const junctionType = api.registry.createType("XcmV3Junction", {
468
+ GlobalConsensus: globalConsensus
469
+ });
470
+ return junctionType.toHex();
471
+ }
472
+ function encodePalletInstance(api) {
473
+ if (!api) {
474
+ throw new Error("API is required to encode asset");
475
+ }
476
+ const assetType = {
477
+ PalletInstance: 10
478
+ };
479
+ const multiLocationType = api.registry.createType("XcmV3Junction", assetType);
480
+ return multiLocationType.toHex();
481
+ }
320
482
 
321
483
  // src/contract/contracts/XcmPrecompile/XcmPrecompileAbi.ts
322
484
  var XCM_ABI = [
@@ -855,6 +1017,32 @@ function XcmPrecompile() {
855
1017
  });
856
1018
  }
857
1019
  }),
1020
+ 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
+ }
1045
+ }),
858
1046
  transferAssetsUsingTypeAndThenAddress: (shouldTransferAssetPrecedeFeeAsset = false) => ({
859
1047
  build: ({
860
1048
  destinationAddress,
@@ -869,7 +1057,6 @@ function XcmPrecompile() {
869
1057
  fee,
870
1058
  shouldTransferAssetPrecedeFeeAsset
871
1059
  );
872
- const destLocation = getDestinationParachainMultilocation(destination);
873
1060
  const xcmMessage = buildXcmMessage(
874
1061
  assets3,
875
1062
  destinationAddress,
@@ -884,7 +1071,7 @@ function XcmPrecompile() {
884
1071
  address: XCM_PRECOMPILE_ADDRESS,
885
1072
  abi: XCM_ABI,
886
1073
  args: [
887
- destLocation,
1074
+ getDestinationParachainMultilocation(destination),
888
1075
  assets3,
889
1076
  2 /* DestinationReserve */,
890
1077
  feeIndex,
@@ -1571,120 +1758,6 @@ function eqBalances() {
1571
1758
  };
1572
1759
  }
1573
1760
 
1574
- // src/extrinsic/pallets/polkadotXcm/polkadotXcm.util.ts
1575
- import { Ecosystem } from "@moonbeam-network/xcm-types";
1576
- function getPolkadotXcmExtrinsicArgs({
1577
- assets: assets3,
1578
- destinationAddress,
1579
- destination,
1580
- func,
1581
- parents = 1,
1582
- feeIndex = 0
1583
- }) {
1584
- const version = getExtrinsicArgumentVersion(func);
1585
- return [
1586
- // dest
1587
- {
1588
- [version]: normalizeX1(version, {
1589
- parents,
1590
- interior: {
1591
- X1: {
1592
- Parachain: destination.parachainId
1593
- }
1594
- }
1595
- })
1596
- },
1597
- // beneficiary
1598
- {
1599
- [version]: normalizeX1(version, {
1600
- parents: 0,
1601
- interior: {
1602
- X1: getExtrinsicAccount(destinationAddress)
1603
- }
1604
- })
1605
- },
1606
- // assets
1607
- {
1608
- [version]: assets3
1609
- },
1610
- // feeAssetItem
1611
- feeIndex,
1612
- // weightLimit
1613
- "Unlimited"
1614
- ];
1615
- }
1616
- function isKusamaDestination(destination) {
1617
- return destination.ecosystem === Ecosystem.Kusama || destination.ecosystem === Ecosystem.MoonsamaRelay;
1618
- }
1619
- function isPolkadotDestination(destination) {
1620
- return destination.ecosystem === Ecosystem.Polkadot || destination.ecosystem === Ecosystem.MoonlamaRelay;
1621
- }
1622
- function getGlobalConsensus(destination) {
1623
- if (isKusamaDestination(destination)) {
1624
- return "Kusama";
1625
- }
1626
- if (isPolkadotDestination(destination)) {
1627
- return "Polkadot";
1628
- }
1629
- return {
1630
- ByGenesis: destination.relayGenesisHash
1631
- };
1632
- }
1633
- function getEcosystemTransferExtrinsicArgs({
1634
- assets: assets3,
1635
- destinationAddress,
1636
- destination,
1637
- func,
1638
- feeIndex = 0
1639
- }) {
1640
- const version = getExtrinsicArgumentVersion(func);
1641
- const globalConsensus = getGlobalConsensus(destination);
1642
- return [
1643
- // dest
1644
- {
1645
- [version]: normalizeX1(version, {
1646
- parents: 2,
1647
- interior: {
1648
- X2: [
1649
- { GlobalConsensus: globalConsensus },
1650
- {
1651
- Parachain: destination.parachainId
1652
- }
1653
- ]
1654
- }
1655
- })
1656
- },
1657
- // beneficiary
1658
- {
1659
- [version]: normalizeX1(version, {
1660
- parents: 0,
1661
- interior: {
1662
- X1: getExtrinsicAccount(destinationAddress)
1663
- }
1664
- })
1665
- },
1666
- // assets
1667
- {
1668
- [version]: assets3
1669
- },
1670
- // feeAssetItem
1671
- feeIndex,
1672
- // weightLimit
1673
- "Unlimited"
1674
- ];
1675
- }
1676
- function shouldFeeAssetPrecedeAsset({
1677
- asset,
1678
- fee
1679
- }) {
1680
- const assetIdNumber = Number(asset.getAssetId());
1681
- const feeAssetIdNumber = Number(fee.getAssetId());
1682
- if (Number.isNaN(assetIdNumber) || Number.isNaN(feeAssetIdNumber)) {
1683
- return false;
1684
- }
1685
- return assetIdNumber > feeAssetIdNumber;
1686
- }
1687
-
1688
1761
  // src/extrinsic/pallets/polkadotXcm/polkadotXcm.ts
1689
1762
  var pallet2 = "polkadotXcm";
1690
1763
  function polkadotXcm() {