@moonbeam-network/mrl 4.2.5 → 4.3.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.mjs CHANGED
@@ -16798,7 +16798,7 @@ import {
16798
16798
  import {
16799
16799
  AssetAmount as AssetAmount3,
16800
16800
  EvmChain as EvmChain3,
16801
- EvmParachain as EvmParachain3
16801
+ EvmParachain as EvmParachain2
16802
16802
  } from "@moonbeam-network/xcm-types";
16803
16803
  import { toBigInt as toBigInt3 } from "@moonbeam-network/xcm-utils";
16804
16804
  import Big2 from "big.js";
@@ -16809,7 +16809,11 @@ import {
16809
16809
  ERC20_ABI,
16810
16810
  GATEWAY_ABI
16811
16811
  } from "@moonbeam-network/xcm-builder";
16812
- import { EvmService as EvmService2 } from "@moonbeam-network/xcm-sdk";
16812
+ import {
16813
+ EvmService as EvmService2,
16814
+ getAllowanceSlot,
16815
+ MAX_ALLOWANCE_HEX
16816
+ } from "@moonbeam-network/xcm-sdk";
16813
16817
  import { EvmChain } from "@moonbeam-network/xcm-types";
16814
16818
  import { isEthAddress } from "@moonbeam-network/xcm-utils";
16815
16819
  import { u8aToHex } from "@polkadot/util";
@@ -16908,7 +16912,7 @@ var SnowbridgeService = class _SnowbridgeService {
16908
16912
  });
16909
16913
  return hash;
16910
16914
  }
16911
- async getFee(address, transfer) {
16915
+ async getFee(address, transfer, allowanceSlot) {
16912
16916
  const { args } = transfer;
16913
16917
  const { tokenAddress, amount, requiresApproval } = args;
16914
16918
  const contract = this.buildContractConfig(transfer);
@@ -16923,11 +16927,20 @@ var SnowbridgeService = class _SnowbridgeService {
16923
16927
  if (currentAllowance >= amount) {
16924
16928
  return await this.#evmService.getFee(address, contract);
16925
16929
  }
16926
- return await this.estimateApproveAndSendFee(address, transfer);
16930
+ return await this.estimateApproveAndSendFee(
16931
+ address,
16932
+ transfer,
16933
+ allowanceSlot
16934
+ );
16927
16935
  }
16928
- async estimateApproveAndSendFee(address, transfer) {
16936
+ async estimateApproveAndSendFee(address, transfer, allowanceSlotNumber) {
16929
16937
  const { args } = transfer;
16930
16938
  const { tokenAddress, amount } = args;
16939
+ if (!allowanceSlotNumber) {
16940
+ throw new Error(
16941
+ "Allowance slot is required to calculate the fee for approve and send"
16942
+ );
16943
+ }
16931
16944
  const contract = this.buildContractConfig(transfer);
16932
16945
  try {
16933
16946
  const approveData = encodeFunctionData({
@@ -16940,19 +16953,24 @@ var SnowbridgeService = class _SnowbridgeService {
16940
16953
  to: tokenAddress,
16941
16954
  data: approveData
16942
16955
  });
16943
- const sendData = encodeFunctionData({
16944
- abi: contract.abi,
16945
- functionName: contract.func,
16946
- args: contract.args
16947
- });
16948
- const sendGas = await this.#evmService.client.estimateGas({
16949
- account: address,
16950
- to: contract.address,
16951
- data: sendData,
16952
- value: contract.value
16953
- });
16956
+ const allowanceSlot = getAllowanceSlot(
16957
+ address,
16958
+ this.#gatewayAddress,
16959
+ allowanceSlotNumber
16960
+ );
16961
+ const sendFee = await this.#evmService.getFee(address, contract, [
16962
+ {
16963
+ address: tokenAddress,
16964
+ stateDiff: [
16965
+ {
16966
+ slot: allowanceSlot,
16967
+ value: MAX_ALLOWANCE_HEX
16968
+ }
16969
+ ]
16970
+ }
16971
+ ]);
16954
16972
  const gasPrice = await this.#evmService.client.getGasPrice();
16955
- return (approveGas + sendGas) * gasPrice;
16973
+ return approveGas * gasPrice + sendFee;
16956
16974
  } catch (error) {
16957
16975
  console.error("Error estimating approve + send fee:", error);
16958
16976
  return 0n;
@@ -16962,7 +16980,7 @@ var SnowbridgeService = class _SnowbridgeService {
16962
16980
 
16963
16981
  // src/getTransferData/getBridgeChainData.ts
16964
16982
  import { getBalance, getDestinationFee } from "@moonbeam-network/xcm-sdk";
16965
- import { EvmParachain, Parachain } from "@moonbeam-network/xcm-types";
16983
+ import { Parachain } from "@moonbeam-network/xcm-types";
16966
16984
  import {
16967
16985
  getMultilocationDerivedAddresses,
16968
16986
  isEthAddress as isEthAddress2
@@ -17022,17 +17040,17 @@ function getBridgeChainAddress({
17022
17040
  const bridgeChain = route.mrl.bridgeChain.chain;
17023
17041
  const isDestinationBridgeChain = bridgeChain.isEqual(destination);
17024
17042
  const isSourceBridgeChain = bridgeChain.isEqual(source);
17043
+ const isDifferentEcosystem = source.ecosystem !== bridgeChain.ecosystem;
17025
17044
  let bridgeChainAddress = isDestinationBridgeChain ? destinationAddress : sourceAddress;
17026
17045
  if (Parachain.isExactly(bridgeChain) && isEthAddress2(bridgeChainAddress)) {
17027
17046
  bridgeChainAddress = evmToAddress(bridgeChainAddress);
17028
17047
  return bridgeChainAddress;
17029
17048
  }
17030
17049
  if (Parachain.is(source) && !isSourceBridgeChain) {
17031
- const isSourceEvmSigner = EvmParachain.is(source) && source.isEvmSigner;
17032
17050
  const { address20: computedOriginAccount } = getMultilocationDerivedAddresses({
17033
- address: isSourceEvmSigner ? evmToAddress(sourceAddress) : sourceAddress,
17051
+ address: sourceAddress,
17034
17052
  paraId: source.parachainId,
17035
- isParents: true
17053
+ parents: isDifferentEcosystem ? 2 : 1
17036
17054
  });
17037
17055
  bridgeChainAddress = computedOriginAccount;
17038
17056
  }
@@ -17074,7 +17092,8 @@ import {
17074
17092
  } from "@moonbeam-network/xcm-builder";
17075
17093
  import {
17076
17094
  moonbaseAlpha,
17077
- moonbeam
17095
+ moonbeam,
17096
+ moonriver
17078
17097
  } from "@moonbeam-network/xcm-config";
17079
17098
  import {
17080
17099
  convertToChainDecimals,
@@ -17082,7 +17101,7 @@ import {
17082
17101
  getMin,
17083
17102
  PolkadotService
17084
17103
  } from "@moonbeam-network/xcm-sdk";
17085
- import { AssetAmount, EvmParachain as EvmParachain2 } from "@moonbeam-network/xcm-types";
17104
+ import { AssetAmount, EvmParachain } from "@moonbeam-network/xcm-types";
17086
17105
  import {
17087
17106
  getMultilocationDerivedAddresses as getMultilocationDerivedAddresses2,
17088
17107
  getPolkadotApi
@@ -17095,6 +17114,7 @@ import {
17095
17114
  } from "viem";
17096
17115
  var MOON_CHAIN_AUTOMATIC_GAS_ESTIMATION = {
17097
17116
  [moonbeam.key]: 1273110n,
17117
+ [moonriver.key]: 1273110n,
17098
17118
  [moonbaseAlpha.key]: 1470417n
17099
17119
  };
17100
17120
  function getBridgeChainFeeValueOnSource({
@@ -17102,7 +17122,7 @@ function getBridgeChainFeeValueOnSource({
17102
17122
  bridgeChainData,
17103
17123
  sourceData
17104
17124
  }) {
17105
- const isSourceParachain = EvmParachain2.isAnyParachain(sourceData.chain);
17125
+ const isSourceParachain = EvmParachain.isAnyParachain(sourceData.chain);
17106
17126
  const isDestinationBridgeChain = destinationData.chain.isEqual(
17107
17127
  bridgeChainData.chain
17108
17128
  );
@@ -17137,7 +17157,7 @@ function getMrlMin({
17137
17157
  });
17138
17158
  }
17139
17159
  function requiresTransact(route) {
17140
- return route.mrl?.transfer.provider === Provider.Wormhole && EvmParachain2.isAnyParachain(route.source.chain);
17160
+ return route.mrl?.transfer.provider === Provider.Wormhole && EvmParachain.isAnyParachain(route.source.chain);
17141
17161
  }
17142
17162
  async function buildTransfer(params) {
17143
17163
  const { route } = params;
@@ -17177,8 +17197,8 @@ async function getMrlBuilderParams({
17177
17197
  const destination = route.destination.chain;
17178
17198
  const bridgeChain = route.mrl.bridgeChain.chain;
17179
17199
  const [sourceApi, destinationApi, bridgeApi] = await Promise.all([
17180
- EvmParachain2.isAnyParachain(source) ? getPolkadotApi(source.ws) : void 0,
17181
- EvmParachain2.isAnyParachain(destination) ? getPolkadotApi(destination.ws) : void 0,
17200
+ EvmParachain.isAnyParachain(source) ? getPolkadotApi(source.ws) : void 0,
17201
+ EvmParachain.isAnyParachain(destination) ? getPolkadotApi(destination.ws) : void 0,
17182
17202
  getPolkadotApi(bridgeChain.ws)
17183
17203
  ]);
17184
17204
  return {
@@ -17203,13 +17223,14 @@ async function getTransact(params) {
17203
17223
  const { sourceAddress, source, bridgeChain } = params;
17204
17224
  const polkadot = await PolkadotService.create(bridgeChain);
17205
17225
  const bridgeChainGasLimit = await getBridgeChainGasLimit(params);
17206
- if (!EvmParachain2.isAnyParachain(source)) {
17226
+ if (!EvmParachain.isAnyParachain(source)) {
17207
17227
  throw new Error("Source chain must be Parachain or EvmParachain");
17208
17228
  }
17229
+ const isDifferentEcosystem = source.ecosystem !== bridgeChain.ecosystem;
17209
17230
  const { address20 } = getMultilocationDerivedAddresses2({
17210
17231
  address: sourceAddress,
17211
17232
  paraId: source.parachainId,
17212
- isParents: true
17233
+ parents: isDifferentEcosystem ? 2 : 1
17213
17234
  });
17214
17235
  const extrinsic = MrlBuilder2().wormhole().extrinsic().ethereumXcm().transact().build({ ...params, bridgeChainGasLimit });
17215
17236
  const { weight } = await polkadot.getPaymentInfo(address20, extrinsic);
@@ -17223,10 +17244,10 @@ async function getTransact(params) {
17223
17244
  }
17224
17245
  async function getBridgeChainGasLimit(params) {
17225
17246
  const { asset, isAutomatic, bridgeChain, source, sourceAddress } = params;
17226
- if (!EvmParachain2.isAnyParachain(source)) {
17247
+ if (!EvmParachain.isAnyParachain(source)) {
17227
17248
  throw new Error("Source chain must be Parachain or EvmParachain");
17228
17249
  }
17229
- if (!EvmParachain2.is(bridgeChain)) {
17250
+ if (!EvmParachain.is(bridgeChain)) {
17230
17251
  throw new Error("Bridge chain must be an EvmParachain");
17231
17252
  }
17232
17253
  const client = createPublicClient2({
@@ -17236,7 +17257,7 @@ async function getBridgeChainGasLimit(params) {
17236
17257
  const { address20 } = getMultilocationDerivedAddresses2({
17237
17258
  address: sourceAddress,
17238
17259
  paraId: source.parachainId,
17239
- isParents: true
17260
+ parents: 1
17240
17261
  });
17241
17262
  if (isAutomatic) {
17242
17263
  return MOON_CHAIN_AUTOMATIC_GAS_ESTIMATION[bridgeChain.key] * 110n / 100n;
@@ -17434,7 +17455,12 @@ async function getFee({
17434
17455
  if (SnowbridgeConfig.is(transfer)) {
17435
17456
  const snowbridge = SnowbridgeService.create(chain2);
17436
17457
  try {
17437
- const feeAmount = await snowbridge.getFee(sourceAddress, transfer);
17458
+ const allowanceSlot = balance.ids?.allowanceSlot;
17459
+ const feeAmount = await snowbridge.getFee(
17460
+ sourceAddress,
17461
+ transfer,
17462
+ allowanceSlot
17463
+ );
17438
17464
  return AssetAmount2.fromChainAsset(chain2.getChainAsset(feeBalance), {
17439
17465
  amount: feeAmount
17440
17466
  });
@@ -17722,7 +17748,7 @@ async function getTransferData({
17722
17748
  sendOnlyRemoteExecution,
17723
17749
  sourceAddress
17724
17750
  });
17725
- if (ContractConfig3.is(transfer) && (EvmChain3.is(source) || EvmParachain3.is(source))) {
17751
+ if (ContractConfig3.is(transfer) && (EvmChain3.is(source) || EvmParachain2.is(source))) {
17726
17752
  if (!evmSigner) {
17727
17753
  throw new Error("EVM Signer must be provided");
17728
17754
  }
@@ -17730,7 +17756,7 @@ async function getTransferData({
17730
17756
  const hash = await evm.transfer(evmSigner, transfer);
17731
17757
  return [hash];
17732
17758
  }
17733
- if (ExtrinsicConfig.is(transfer) && EvmParachain3.isAnyParachain(source)) {
17759
+ if (ExtrinsicConfig.is(transfer) && EvmParachain2.isAnyParachain(source)) {
17734
17760
  if (!polkadotSigner) {
17735
17761
  throw new Error("Polkadot signer must be provided");
17736
17762
  }
@@ -17743,14 +17769,14 @@ async function getTransferData({
17743
17769
  );
17744
17770
  return [hash];
17745
17771
  }
17746
- if (WormholeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain3.is(source))) {
17772
+ if (WormholeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain2.is(source))) {
17747
17773
  if (!evmSigner) {
17748
17774
  throw new Error("EVM Signer must be provided");
17749
17775
  }
17750
17776
  const wh = WormholeService.create(source);
17751
17777
  return wh.transfer(evmSigner, transfer);
17752
17778
  }
17753
- if (SnowbridgeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain3.is(source))) {
17779
+ if (SnowbridgeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain2.is(source))) {
17754
17780
  if (!evmSigner) {
17755
17781
  throw new Error("EVM Signer must be provided");
17756
17782
  }