@moonbeam-network/mrl 1.0.0-dev.285 → 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,6 +17274,9 @@ async function getBridgeChainGasLimit(params) {
17265
17274
  return gasEstimation * 110n / 100n;
17266
17275
  }
17267
17276
  function getAmountForTransferSimulation(balance, protocolFee) {
17277
+ if (!protocolFee || !balance.isSame(protocolFee)) {
17278
+ return balance;
17279
+ }
17268
17280
  return balance.copyWith({
17269
17281
  amount: balance.amount - protocolFee.amount > 0 ? balance.amount - protocolFee.amount : 0n
17270
17282
  });
@@ -17276,7 +17288,8 @@ async function getSourceData({
17276
17288
  route,
17277
17289
  destinationAddress,
17278
17290
  destinationFee,
17279
- sourceAddress
17291
+ sourceAddress,
17292
+ bridgeChainData
17280
17293
  }) {
17281
17294
  if (!route.mrl) {
17282
17295
  throw new Error(
@@ -17285,6 +17298,7 @@ async function getSourceData({
17285
17298
  }
17286
17299
  const source = route.source.chain;
17287
17300
  const destination = route.destination.chain;
17301
+ const bridgeChain = route.mrl.bridgeChain.chain;
17288
17302
  const asset = source.getChainAsset(route.source.asset);
17289
17303
  const feeAsset = route.source.fee ? source.getChainAsset(route.source.fee.asset) : asset;
17290
17304
  const balance = await getBalance2({
@@ -17324,11 +17338,14 @@ async function getSourceData({
17324
17338
  feeAsset,
17325
17339
  balance,
17326
17340
  protocolFee: route.source.protocolFee,
17327
- address: destinationAddress
17341
+ address: destinationAddress,
17342
+ bridgeChain,
17343
+ bridgeChainFee: bridgeChainData.fee
17328
17344
  });
17329
17345
  const transfer = await buildTransfer({
17330
17346
  asset: getAmountForTransferSimulation(balance, protocolFee),
17331
17347
  protocolFee,
17348
+ bridgeChainFee: bridgeChainData.fee,
17332
17349
  destinationAddress,
17333
17350
  feeAsset: feeBalance,
17334
17351
  isAutomatic,
@@ -17344,7 +17361,13 @@ async function getSourceData({
17344
17361
  feeConfig: route.source.fee,
17345
17362
  sourceAddress
17346
17363
  });
17347
- const relayerFee = await getRelayerFee({
17364
+ const max = getMax({
17365
+ balance,
17366
+ existentialDeposit,
17367
+ fee,
17368
+ min
17369
+ });
17370
+ const extraFees = await getExtraFees({
17348
17371
  chain: source,
17349
17372
  transfer,
17350
17373
  asset: balance,
@@ -17352,13 +17375,9 @@ async function getSourceData({
17352
17375
  isAutomatic,
17353
17376
  destinationAddress,
17354
17377
  route,
17355
- sourceAddress
17356
- });
17357
- const max = getMax({
17358
- balance,
17359
- existentialDeposit,
17360
- fee,
17361
- min
17378
+ sourceAddress,
17379
+ bridgeChainFee: bridgeChainData.fee,
17380
+ protocolFee
17362
17381
  });
17363
17382
  return {
17364
17383
  balance,
@@ -17371,10 +17390,7 @@ async function getSourceData({
17371
17390
  feeBalance,
17372
17391
  max,
17373
17392
  min,
17374
- otherFees: {
17375
- protocol: protocolFee,
17376
- relayer: relayerFee?.amount ? relayerFee : void 0
17377
- }
17393
+ extraFees
17378
17394
  };
17379
17395
  }
17380
17396
  async function getFee({
@@ -17436,7 +17452,8 @@ async function getRelayerFee({
17436
17452
  isAutomatic,
17437
17453
  route,
17438
17454
  sourceAddress,
17439
- transfer
17455
+ transfer,
17456
+ bridgeChainFee
17440
17457
  }) {
17441
17458
  if (route.mrl.transfer.provider === Provider2.Snowbridge || SnowbridgeConfig.is(transfer)) {
17442
17459
  return void 0;
@@ -17451,7 +17468,8 @@ async function getRelayerFee({
17451
17468
  feeAsset,
17452
17469
  isAutomatic,
17453
17470
  route,
17454
- sourceAddress
17471
+ sourceAddress,
17472
+ bridgeChainFee
17455
17473
  });
17456
17474
  const wormholeConfig = MrlBuilder3().wormhole().wormhole().tokenTransfer().build(builderParams);
17457
17475
  return getWormholeFee({ asset, chain: chain2, config: wormholeConfig });
@@ -17503,24 +17521,30 @@ async function getBridgeChainFeeBalance({
17503
17521
  async function getProtocolFee({
17504
17522
  address,
17505
17523
  asset,
17506
- feeAsset,
17507
17524
  balance,
17508
17525
  protocolFee,
17509
17526
  destination,
17510
- source
17527
+ source,
17528
+ bridgeChain,
17529
+ bridgeChainFee
17511
17530
  }) {
17512
- if (typeof protocolFee === "number") {
17513
- return AssetAmount2.fromChainAsset(feeAsset, {
17514
- 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
17515
17538
  });
17516
17539
  }
17517
- const config = protocolFee?.build({
17540
+ const config = protocolFee?.amount?.build({
17518
17541
  address,
17519
17542
  asset,
17520
- feeAsset,
17543
+ feeAsset: protocolFeeAsset,
17521
17544
  balance,
17522
17545
  destination,
17523
- source
17546
+ source,
17547
+ bridgeChainFee
17524
17548
  });
17525
17549
  if (ContractConfig2.is(config) && EvmChain2.is(source)) {
17526
17550
  const evm = EvmService3.create(source);
@@ -17530,21 +17554,58 @@ async function getProtocolFee({
17530
17554
  `Error getting bridge fee: expected bigint from contract call, but received ${typeof amount}. `
17531
17555
  );
17532
17556
  }
17533
- return AssetAmount2.fromChainAsset(feeAsset, {
17557
+ return AssetAmount2.fromChainAsset(protocolFeeAsset, {
17534
17558
  amount
17535
17559
  });
17536
17560
  }
17537
- if (SubstrateQueryConfig.is(config) && EvmParachain3.isAnyParachain(source)) {
17538
- const polkadot = await PolkadotService2.create(source);
17561
+ if (SubstrateQueryConfig.is(config)) {
17562
+ const polkadot = await PolkadotService2.create(bridgeChain);
17539
17563
  const amount = await polkadot.query(config);
17540
- return AssetAmount2.fromChainAsset(feeAsset, {
17564
+ return AssetAmount2.fromChainAsset(protocolFeeAsset, {
17541
17565
  amount
17542
17566
  });
17543
17567
  }
17544
- return AssetAmount2.fromChainAsset(feeAsset, {
17568
+ return AssetAmount2.fromChainAsset(protocolFeeAsset, {
17545
17569
  amount: 0n
17546
17570
  });
17547
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
+ }
17548
17609
 
17549
17610
  // src/getTransferData/getTransferData.ts
17550
17611
  async function getTransferData({
@@ -17566,17 +17627,18 @@ async function getTransferData({
17566
17627
  asset: destinationData.fee,
17567
17628
  target: route.getDestinationFeeAssetOnSource()
17568
17629
  });
17630
+ const bridgeChainData = await getBridgeChainData({
17631
+ route,
17632
+ sourceAddress,
17633
+ destinationAddress
17634
+ });
17569
17635
  const sourceData = await getSourceData({
17570
17636
  isAutomatic: route.mrl.isAutomaticPossible && isAutomatic,
17571
17637
  route,
17572
17638
  destinationAddress,
17573
17639
  destinationFee,
17574
- sourceAddress
17575
- });
17576
- const bridgeChainData = await getBridgeChainData({
17577
- route,
17578
17640
  sourceAddress,
17579
- destinationAddress
17641
+ bridgeChainData
17580
17642
  });
17581
17643
  return {
17582
17644
  destination: destinationData,
@@ -17592,7 +17654,7 @@ async function getTransferData({
17592
17654
  });
17593
17655
  const result = bigAmount.minus(
17594
17656
  isSameAssetPayingDestinationFee ? destinationFee.toBig() : Big2(0)
17595
- ).minus(fee).minus(sourceData.otherFees?.relayer?.toBig() || Big2(0));
17657
+ ).minus(fee).minus(sourceData.extraFees.remote?.fee.toBig() || Big2(0));
17596
17658
  return sourceData.balance.copyWith({
17597
17659
  amount: result.lt(0) ? 0n : BigInt(result.toFixed())
17598
17660
  });
@@ -17627,7 +17689,8 @@ async function getTransferData({
17627
17689
  );
17628
17690
  const transfer = await buildTransfer({
17629
17691
  asset,
17630
- protocolFee: sourceData.otherFees?.protocol,
17692
+ protocolFee: sourceData.extraFees.local?.fee,
17693
+ bridgeChainFee: bridgeChainData.fee,
17631
17694
  destinationAddress,
17632
17695
  feeAsset,
17633
17696
  isAutomatic: isAutomatic2,
@@ -17635,7 +17698,7 @@ async function getTransferData({
17635
17698
  sendOnlyRemoteExecution,
17636
17699
  sourceAddress
17637
17700
  });
17638
- if (ContractConfig3.is(transfer) && (EvmChain3.is(source) || EvmParachain4.is(source))) {
17701
+ if (ContractConfig3.is(transfer) && (EvmChain3.is(source) || EvmParachain3.is(source))) {
17639
17702
  if (!evmSigner) {
17640
17703
  throw new Error("EVM Signer must be provided");
17641
17704
  }
@@ -17643,7 +17706,7 @@ async function getTransferData({
17643
17706
  const hash = await evm.transfer(evmSigner, transfer);
17644
17707
  return [hash];
17645
17708
  }
17646
- if (ExtrinsicConfig.is(transfer) && EvmParachain4.isAnyParachain(source)) {
17709
+ if (ExtrinsicConfig.is(transfer) && EvmParachain3.isAnyParachain(source)) {
17647
17710
  if (!polkadotSigner) {
17648
17711
  throw new Error("Polkadot signer must be provided");
17649
17712
  }
@@ -17656,14 +17719,14 @@ async function getTransferData({
17656
17719
  );
17657
17720
  return [hash];
17658
17721
  }
17659
- if (WormholeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain4.is(source))) {
17722
+ if (WormholeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain3.is(source))) {
17660
17723
  if (!evmSigner) {
17661
17724
  throw new Error("EVM Signer must be provided");
17662
17725
  }
17663
17726
  const wh = WormholeService.create(source);
17664
17727
  return wh.transfer(evmSigner, transfer);
17665
17728
  }
17666
- if (SnowbridgeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain4.is(source))) {
17729
+ if (SnowbridgeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain3.is(source))) {
17667
17730
  if (!evmSigner) {
17668
17731
  throw new Error("EVM Signer must be provided");
17669
17732
  }