@moonbeam-network/mrl 1.0.0-dev.286 → 1.0.0-dev.287

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
@@ -28,18 +28,22 @@ interface TransferParams {
28
28
  statusCallback?: (status: ISubmittableResult) => void;
29
29
  sendOnlyRemoteExecution?: boolean;
30
30
  }
31
- interface MrlOtherFees {
32
- /** Protocol bridge fee (e.g., Snowbridge) - deducted from transfer amount */
33
- protocol?: AssetAmount;
34
- /** Relayer service fee for automatic execution - only applies when isAutomatic=true */
35
- relayer?: AssetAmount;
31
+ interface FeeWithBalance {
32
+ fee: AssetAmount;
33
+ balance: AssetAmount;
34
+ }
35
+ interface MrlExtraFees {
36
+ /** Deducted from source balance */
37
+ local?: FeeWithBalance;
38
+ /** Deducted from transfer amount*/
39
+ remote?: FeeWithBalance;
36
40
  }
37
41
  interface SourceTransferData extends SourceChainTransferData {
38
42
  destinationFeeBalance: AssetAmount;
39
43
  bridgeChainFeeBalance?: AssetAmount;
40
44
  feeBalance: AssetAmount;
41
45
  max: AssetAmount;
42
- otherFees: MrlOtherFees;
46
+ extraFees: MrlExtraFees;
43
47
  }
44
48
  interface DestinationTransferData extends ChainTransferData {
45
49
  }
@@ -88,4 +92,4 @@ declare function Mrl(options?: MrlOptions): {
88
92
  getExecuteTransferData({ txId, chain }: WormholeExecuteTransferParams): Promise<ExecuteTransferData>;
89
93
  };
90
94
 
91
- export { type BridgeChainTransferData, type ChainTransferData, type DestinationTransferData, type ExecuteTransferData, Mrl, type MrlOptions, type MrlOtherFees, type Signers, type SourceTransferData, type TransferData, type TransferParams };
95
+ export { type BridgeChainTransferData, type ChainTransferData, type DestinationTransferData, type ExecuteTransferData, type FeeWithBalance, Mrl, type MrlExtraFees, type MrlOptions, type Signers, type SourceTransferData, type TransferData, type TransferParams };
package/build/index.mjs CHANGED
@@ -16795,7 +16795,7 @@ import {
16795
16795
  import {
16796
16796
  AssetAmount as AssetAmount3,
16797
16797
  EvmChain as EvmChain3,
16798
- EvmParachain as EvmParachain4
16798
+ EvmParachain as EvmParachain3
16799
16799
  } from "@moonbeam-network/xcm-types";
16800
16800
  import { toBigInt as toBigInt3 } from "@moonbeam-network/xcm-utils";
16801
16801
  import Big2 from "big.js";
@@ -16808,6 +16808,7 @@ import {
16808
16808
  } from "@moonbeam-network/xcm-builder";
16809
16809
  import { EvmService as EvmService2 } from "@moonbeam-network/xcm-sdk";
16810
16810
  import { EvmChain } from "@moonbeam-network/xcm-types";
16811
+ import { isEthAddress } from "@moonbeam-network/xcm-utils";
16811
16812
  import { u8aToHex } from "@polkadot/util";
16812
16813
  import { decodeAddress } from "@polkadot/util-crypto";
16813
16814
  import { encodeFunctionData } from "viem";
@@ -16869,16 +16870,16 @@ var SnowbridgeService = class _SnowbridgeService {
16869
16870
  destinationAddress,
16870
16871
  destinationParaId,
16871
16872
  amount,
16873
+ bridgeChainFee,
16872
16874
  value
16873
16875
  } = args;
16876
+ const isEthereumDestination = isEthAddress(destinationAddress);
16877
+ const destination = isEthereumDestination ? { kind: 2, data: destinationAddress } : { kind: 1, data: u8aToHex(decodeAddress(destinationAddress)) };
16874
16878
  const contractArgs = [
16875
16879
  tokenAddress,
16876
16880
  destinationParaId,
16877
- {
16878
- kind: 1,
16879
- data: u8aToHex(decodeAddress(destinationAddress))
16880
- },
16881
- 0n,
16881
+ destination,
16882
+ bridgeChainFee,
16882
16883
  amount
16883
16884
  ];
16884
16885
  return new ContractConfig({
@@ -16959,7 +16960,10 @@ var SnowbridgeService = class _SnowbridgeService {
16959
16960
  // src/getTransferData/getBridgeChainData.ts
16960
16961
  import { getBalance, getDestinationFee } from "@moonbeam-network/xcm-sdk";
16961
16962
  import { EvmParachain, Parachain } from "@moonbeam-network/xcm-types";
16962
- import { getMultilocationDerivedAddresses } from "@moonbeam-network/xcm-utils";
16963
+ import {
16964
+ getMultilocationDerivedAddresses,
16965
+ isEthAddress as isEthAddress2
16966
+ } from "@moonbeam-network/xcm-utils";
16963
16967
  import { evmToAddress } from "@polkadot/util-crypto";
16964
16968
  async function getBridgeChainData({
16965
16969
  route,
@@ -17016,6 +17020,10 @@ function getBridgeChainAddress({
17016
17020
  const isDestinationBridgeChain = bridgeChain.isEqual(destination);
17017
17021
  const isSourceBridgeChain = bridgeChain.isEqual(source);
17018
17022
  let bridgeChainAddress = isDestinationBridgeChain ? destinationAddress : sourceAddress;
17023
+ if (Parachain.isExactly(bridgeChain) && isEthAddress2(bridgeChainAddress)) {
17024
+ bridgeChainAddress = evmToAddress(bridgeChainAddress);
17025
+ return bridgeChainAddress;
17026
+ }
17019
17027
  if (Parachain.is(source) && !isSourceBridgeChain) {
17020
17028
  const isSourceEvmSigner = EvmParachain.is(source) && source.isEvmSigner;
17021
17029
  const { address20: computedOriginAccount } = getMultilocationDerivedAddresses({
@@ -17050,8 +17058,7 @@ import {
17050
17058
  } from "@moonbeam-network/xcm-sdk";
17051
17059
  import {
17052
17060
  AssetAmount as AssetAmount2,
17053
- EvmChain as EvmChain2,
17054
- EvmParachain as EvmParachain3
17061
+ EvmChain as EvmChain2
17055
17062
  } from "@moonbeam-network/xcm-types";
17056
17063
  import { toBigInt as toBigInt2 } from "@moonbeam-network/xcm-utils";
17057
17064
 
@@ -17121,7 +17128,7 @@ function getMrlMin({
17121
17128
  bridgeChainData,
17122
17129
  sourceData
17123
17130
  });
17124
- const relayerFee = sourceData.otherFees?.relayer?.amount ? sourceData.otherFees.relayer.toBig() : Big(0);
17131
+ const relayerFee = sourceData.extraFees?.remote?.fee.amount ? sourceData.extraFees.remote?.fee.toBig() : Big(0);
17125
17132
  return min.copyWith({
17126
17133
  amount: BigInt(min.toBig().add(bridgeChainFee).add(relayerFee).toFixed())
17127
17134
  });
@@ -17150,6 +17157,7 @@ async function buildTransfer(params) {
17150
17157
  async function getMrlBuilderParams({
17151
17158
  asset,
17152
17159
  protocolFee,
17160
+ bridgeChainFee,
17153
17161
  destinationAddress,
17154
17162
  feeAsset,
17155
17163
  isAutomatic,
@@ -17173,6 +17181,7 @@ async function getMrlBuilderParams({
17173
17181
  return {
17174
17182
  asset,
17175
17183
  protocolFee,
17184
+ bridgeChainFee,
17176
17185
  destination,
17177
17186
  destinationAddress,
17178
17187
  destinationApi,
@@ -17265,7 +17274,7 @@ async function getBridgeChainGasLimit(params) {
17265
17274
  return gasEstimation * 110n / 100n;
17266
17275
  }
17267
17276
  function getAmountForTransferSimulation(balance, protocolFee) {
17268
- if (!balance.isSame(protocolFee)) {
17277
+ if (!protocolFee || !balance.isSame(protocolFee)) {
17269
17278
  return balance;
17270
17279
  }
17271
17280
  return balance.copyWith({
@@ -17279,7 +17288,8 @@ async function getSourceData({
17279
17288
  route,
17280
17289
  destinationAddress,
17281
17290
  destinationFee,
17282
- sourceAddress
17291
+ sourceAddress,
17292
+ bridgeChainData
17283
17293
  }) {
17284
17294
  if (!route.mrl) {
17285
17295
  throw new Error(
@@ -17288,6 +17298,7 @@ async function getSourceData({
17288
17298
  }
17289
17299
  const source = route.source.chain;
17290
17300
  const destination = route.destination.chain;
17301
+ const bridgeChain = route.mrl.bridgeChain.chain;
17291
17302
  const asset = source.getChainAsset(route.source.asset);
17292
17303
  const feeAsset = route.source.fee ? source.getChainAsset(route.source.fee.asset) : asset;
17293
17304
  const balance = await getBalance2({
@@ -17327,11 +17338,14 @@ async function getSourceData({
17327
17338
  feeAsset,
17328
17339
  balance,
17329
17340
  protocolFee: route.source.protocolFee,
17330
- address: destinationAddress
17341
+ address: destinationAddress,
17342
+ bridgeChain,
17343
+ bridgeChainFee: bridgeChainData.fee
17331
17344
  });
17332
17345
  const transfer = await buildTransfer({
17333
17346
  asset: getAmountForTransferSimulation(balance, protocolFee),
17334
17347
  protocolFee,
17348
+ bridgeChainFee: bridgeChainData.fee,
17335
17349
  destinationAddress,
17336
17350
  feeAsset: feeBalance,
17337
17351
  isAutomatic,
@@ -17347,7 +17361,13 @@ async function getSourceData({
17347
17361
  feeConfig: route.source.fee,
17348
17362
  sourceAddress
17349
17363
  });
17350
- const relayerFee = await getRelayerFee({
17364
+ const max = getMax({
17365
+ balance,
17366
+ existentialDeposit,
17367
+ fee,
17368
+ min
17369
+ });
17370
+ const extraFees = await getExtraFees({
17351
17371
  chain: source,
17352
17372
  transfer,
17353
17373
  asset: balance,
@@ -17355,13 +17375,9 @@ async function getSourceData({
17355
17375
  isAutomatic,
17356
17376
  destinationAddress,
17357
17377
  route,
17358
- sourceAddress
17359
- });
17360
- const max = getMax({
17361
- balance,
17362
- existentialDeposit,
17363
- fee,
17364
- min
17378
+ sourceAddress,
17379
+ bridgeChainFee: bridgeChainData.fee,
17380
+ protocolFee
17365
17381
  });
17366
17382
  return {
17367
17383
  balance,
@@ -17374,10 +17390,7 @@ async function getSourceData({
17374
17390
  feeBalance,
17375
17391
  max,
17376
17392
  min,
17377
- otherFees: {
17378
- protocol: protocolFee,
17379
- relayer: relayerFee?.amount ? relayerFee : void 0
17380
- }
17393
+ extraFees
17381
17394
  };
17382
17395
  }
17383
17396
  async function getFee({
@@ -17439,7 +17452,8 @@ async function getRelayerFee({
17439
17452
  isAutomatic,
17440
17453
  route,
17441
17454
  sourceAddress,
17442
- transfer
17455
+ transfer,
17456
+ bridgeChainFee
17443
17457
  }) {
17444
17458
  if (route.mrl.transfer.provider === Provider2.Snowbridge || SnowbridgeConfig.is(transfer)) {
17445
17459
  return void 0;
@@ -17454,7 +17468,8 @@ async function getRelayerFee({
17454
17468
  feeAsset,
17455
17469
  isAutomatic,
17456
17470
  route,
17457
- sourceAddress
17471
+ sourceAddress,
17472
+ bridgeChainFee
17458
17473
  });
17459
17474
  const wormholeConfig = MrlBuilder3().wormhole().wormhole().tokenTransfer().build(builderParams);
17460
17475
  return getWormholeFee({ asset, chain: chain2, config: wormholeConfig });
@@ -17506,24 +17521,30 @@ async function getBridgeChainFeeBalance({
17506
17521
  async function getProtocolFee({
17507
17522
  address,
17508
17523
  asset,
17509
- feeAsset,
17510
17524
  balance,
17511
17525
  protocolFee,
17512
17526
  destination,
17513
- source
17527
+ source,
17528
+ bridgeChain,
17529
+ bridgeChainFee
17514
17530
  }) {
17515
- if (typeof protocolFee === "number") {
17516
- return AssetAmount2.fromChainAsset(feeAsset, {
17517
- amount: protocolFee
17531
+ if (!protocolFee) {
17532
+ return void 0;
17533
+ }
17534
+ const protocolFeeAsset = source.getChainAsset(protocolFee.asset);
17535
+ if (typeof protocolFee.amount === "number") {
17536
+ return AssetAmount2.fromChainAsset(protocolFeeAsset, {
17537
+ amount: protocolFee.amount
17518
17538
  });
17519
17539
  }
17520
- const config = protocolFee?.build({
17540
+ const config = protocolFee?.amount?.build({
17521
17541
  address,
17522
17542
  asset,
17523
- feeAsset,
17543
+ feeAsset: protocolFeeAsset,
17524
17544
  balance,
17525
17545
  destination,
17526
- source
17546
+ source,
17547
+ bridgeChainFee
17527
17548
  });
17528
17549
  if (ContractConfig2.is(config) && EvmChain2.is(source)) {
17529
17550
  const evm = EvmService3.create(source);
@@ -17533,21 +17554,58 @@ async function getProtocolFee({
17533
17554
  `Error getting bridge fee: expected bigint from contract call, but received ${typeof amount}. `
17534
17555
  );
17535
17556
  }
17536
- return AssetAmount2.fromChainAsset(feeAsset, {
17557
+ return AssetAmount2.fromChainAsset(protocolFeeAsset, {
17537
17558
  amount
17538
17559
  });
17539
17560
  }
17540
- if (SubstrateQueryConfig.is(config) && EvmParachain3.isAnyParachain(source)) {
17541
- const polkadot = await PolkadotService2.create(source);
17561
+ if (SubstrateQueryConfig.is(config)) {
17562
+ const polkadot = await PolkadotService2.create(bridgeChain);
17542
17563
  const amount = await polkadot.query(config);
17543
- return AssetAmount2.fromChainAsset(feeAsset, {
17564
+ return AssetAmount2.fromChainAsset(protocolFeeAsset, {
17544
17565
  amount
17545
17566
  });
17546
17567
  }
17547
- return AssetAmount2.fromChainAsset(feeAsset, {
17568
+ return AssetAmount2.fromChainAsset(protocolFeeAsset, {
17548
17569
  amount: 0n
17549
17570
  });
17550
17571
  }
17572
+ async function getExtraFees({
17573
+ asset,
17574
+ chain: chain2,
17575
+ destinationAddress,
17576
+ feeAsset,
17577
+ isAutomatic,
17578
+ route,
17579
+ sourceAddress,
17580
+ transfer,
17581
+ bridgeChainFee,
17582
+ protocolFee
17583
+ }) {
17584
+ const relayerFee = await getRelayerFee({
17585
+ chain: chain2,
17586
+ transfer,
17587
+ asset,
17588
+ feeAsset,
17589
+ isAutomatic,
17590
+ destinationAddress,
17591
+ route,
17592
+ sourceAddress,
17593
+ bridgeChainFee
17594
+ });
17595
+ const protocolFeeConfig = route.source.protocolFee;
17596
+ const protocolFeeBalance = protocolFeeConfig ? await getBalance2({
17597
+ address: sourceAddress,
17598
+ asset: chain2.getChainAsset(protocolFeeConfig.asset),
17599
+ builder: protocolFeeConfig.balance,
17600
+ chain: chain2
17601
+ }) : void 0;
17602
+ const localFee = protocolFee && protocolFeeBalance ? { fee: protocolFee, balance: protocolFeeBalance } : void 0;
17603
+ const remoteFee = relayerFee ? { fee: relayerFee, balance: feeAsset } : void 0;
17604
+ return {
17605
+ local: localFee,
17606
+ remote: remoteFee
17607
+ };
17608
+ }
17551
17609
 
17552
17610
  // src/getTransferData/getTransferData.ts
17553
17611
  async function getTransferData({
@@ -17569,17 +17627,18 @@ async function getTransferData({
17569
17627
  asset: destinationData.fee,
17570
17628
  target: route.getDestinationFeeAssetOnSource()
17571
17629
  });
17630
+ const bridgeChainData = await getBridgeChainData({
17631
+ route,
17632
+ sourceAddress,
17633
+ destinationAddress
17634
+ });
17572
17635
  const sourceData = await getSourceData({
17573
17636
  isAutomatic: route.mrl.isAutomaticPossible && isAutomatic,
17574
17637
  route,
17575
17638
  destinationAddress,
17576
17639
  destinationFee,
17577
- sourceAddress
17578
- });
17579
- const bridgeChainData = await getBridgeChainData({
17580
- route,
17581
17640
  sourceAddress,
17582
- destinationAddress
17641
+ bridgeChainData
17583
17642
  });
17584
17643
  return {
17585
17644
  destination: destinationData,
@@ -17595,7 +17654,7 @@ async function getTransferData({
17595
17654
  });
17596
17655
  const result = bigAmount.minus(
17597
17656
  isSameAssetPayingDestinationFee ? destinationFee.toBig() : Big2(0)
17598
- ).minus(fee).minus(sourceData.otherFees?.relayer?.toBig() || Big2(0));
17657
+ ).minus(fee).minus(sourceData.extraFees.remote?.fee.toBig() || Big2(0));
17599
17658
  return sourceData.balance.copyWith({
17600
17659
  amount: result.lt(0) ? 0n : BigInt(result.toFixed())
17601
17660
  });
@@ -17630,7 +17689,8 @@ async function getTransferData({
17630
17689
  );
17631
17690
  const transfer = await buildTransfer({
17632
17691
  asset,
17633
- protocolFee: sourceData.otherFees?.protocol,
17692
+ protocolFee: sourceData.extraFees.local?.fee,
17693
+ bridgeChainFee: bridgeChainData.fee,
17634
17694
  destinationAddress,
17635
17695
  feeAsset,
17636
17696
  isAutomatic: isAutomatic2,
@@ -17638,7 +17698,7 @@ async function getTransferData({
17638
17698
  sendOnlyRemoteExecution,
17639
17699
  sourceAddress
17640
17700
  });
17641
- if (ContractConfig3.is(transfer) && (EvmChain3.is(source) || EvmParachain4.is(source))) {
17701
+ if (ContractConfig3.is(transfer) && (EvmChain3.is(source) || EvmParachain3.is(source))) {
17642
17702
  if (!evmSigner) {
17643
17703
  throw new Error("EVM Signer must be provided");
17644
17704
  }
@@ -17646,7 +17706,7 @@ async function getTransferData({
17646
17706
  const hash = await evm.transfer(evmSigner, transfer);
17647
17707
  return [hash];
17648
17708
  }
17649
- if (ExtrinsicConfig.is(transfer) && EvmParachain4.isAnyParachain(source)) {
17709
+ if (ExtrinsicConfig.is(transfer) && EvmParachain3.isAnyParachain(source)) {
17650
17710
  if (!polkadotSigner) {
17651
17711
  throw new Error("Polkadot signer must be provided");
17652
17712
  }
@@ -17659,14 +17719,14 @@ async function getTransferData({
17659
17719
  );
17660
17720
  return [hash];
17661
17721
  }
17662
- if (WormholeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain4.is(source))) {
17722
+ if (WormholeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain3.is(source))) {
17663
17723
  if (!evmSigner) {
17664
17724
  throw new Error("EVM Signer must be provided");
17665
17725
  }
17666
17726
  const wh = WormholeService.create(source);
17667
17727
  return wh.transfer(evmSigner, transfer);
17668
17728
  }
17669
- if (SnowbridgeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain4.is(source))) {
17729
+ if (SnowbridgeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain3.is(source))) {
17670
17730
  if (!evmSigner) {
17671
17731
  throw new Error("EVM Signer must be provided");
17672
17732
  }