@moonbeam-network/mrl 1.0.0-dev.266 → 1.0.0-dev.268

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
@@ -36,6 +36,7 @@ interface MrlOtherFees {
36
36
  }
37
37
  interface SourceTransferData extends SourceChainTransferData {
38
38
  destinationFeeBalance: AssetAmount;
39
+ bridgeChainFeeBalance?: AssetAmount;
39
40
  feeBalance: AssetAmount;
40
41
  max: AssetAmount;
41
42
  otherFees: MrlOtherFees;
package/build/index.mjs CHANGED
@@ -16768,7 +16768,7 @@ import {
16768
16768
  } from "@moonbeam-network/xcm-sdk";
16769
16769
  import {
16770
16770
  AssetAmount as AssetAmount3,
16771
- EvmChain as EvmChain2,
16771
+ EvmChain as EvmChain3,
16772
16772
  EvmParachain as EvmParachain4
16773
16773
  } from "@moonbeam-network/xcm-types";
16774
16774
  import { toBigInt as toBigInt3 } from "@moonbeam-network/xcm-utils";
@@ -16778,21 +16778,28 @@ import Big2 from "big.js";
16778
16778
  import {
16779
16779
  ContractConfig,
16780
16780
  ERC20_ABI,
16781
- GATEWAY_ABI,
16782
- GATEWAY_CONTRACT_ADDRESS
16781
+ GATEWAY_ABI
16783
16782
  } from "@moonbeam-network/xcm-builder";
16784
16783
  import { EvmService as EvmService2 } from "@moonbeam-network/xcm-sdk";
16784
+ import { EvmChain } from "@moonbeam-network/xcm-types";
16785
16785
  import { u8aToHex } from "@polkadot/util";
16786
16786
  import { decodeAddress } from "@polkadot/util-crypto";
16787
16787
  import { encodeFunctionData } from "viem";
16788
16788
  var SnowbridgeService = class _SnowbridgeService {
16789
16789
  chain;
16790
16790
  #evmService;
16791
+ #gatewayAddress;
16791
16792
  static create(chain2) {
16792
16793
  return new _SnowbridgeService(chain2);
16793
16794
  }
16794
16795
  constructor(chain2) {
16796
+ if (!EvmChain.is(chain2) || !chain2.contracts?.Gateway) {
16797
+ throw new Error(
16798
+ "Chain must be an EVMChain with the Gateway contract address configured for Snowbridge operations"
16799
+ );
16800
+ }
16795
16801
  this.chain = chain2;
16802
+ this.#gatewayAddress = chain2.contracts.Gateway;
16796
16803
  this.#evmService = EvmService2.create(chain2);
16797
16804
  }
16798
16805
  async checkAllowance(ownerAddress, tokenAddress, spenderAddress) {
@@ -16822,16 +16829,10 @@ var SnowbridgeService = class _SnowbridgeService {
16822
16829
  const currentAllowance = await this.checkAllowance(
16823
16830
  signer.account.address,
16824
16831
  tokenAddress,
16825
- GATEWAY_CONTRACT_ADDRESS
16832
+ this.#gatewayAddress
16826
16833
  );
16827
- console.log("currentAllowance", currentAllowance);
16828
16834
  if (currentAllowance < amount) {
16829
- await this.approve(
16830
- signer,
16831
- tokenAddress,
16832
- GATEWAY_CONTRACT_ADDRESS,
16833
- amount
16834
- );
16835
+ await this.approve(signer, tokenAddress, this.#gatewayAddress, amount);
16835
16836
  }
16836
16837
  return await this.#evmService.transfer(signer, contract);
16837
16838
  }
@@ -16857,7 +16858,7 @@ var SnowbridgeService = class _SnowbridgeService {
16857
16858
  amount
16858
16859
  ];
16859
16860
  return new ContractConfig({
16860
- address: GATEWAY_CONTRACT_ADDRESS,
16861
+ address: this.#gatewayAddress,
16861
16862
  abi: GATEWAY_ABI,
16862
16863
  args: contractArgs,
16863
16864
  func: "sendToken",
@@ -16889,7 +16890,7 @@ var SnowbridgeService = class _SnowbridgeService {
16889
16890
  const currentAllowance = await this.checkAllowance(
16890
16891
  address,
16891
16892
  tokenAddress,
16892
- GATEWAY_CONTRACT_ADDRESS
16893
+ this.#gatewayAddress
16893
16894
  );
16894
16895
  if (currentAllowance >= amount) {
16895
16896
  return await this.#evmService.getFee(address, contract);
@@ -16904,7 +16905,7 @@ var SnowbridgeService = class _SnowbridgeService {
16904
16905
  const approveData = encodeFunctionData({
16905
16906
  abi: ERC20_ABI,
16906
16907
  functionName: "approve",
16907
- args: [GATEWAY_CONTRACT_ADDRESS, amount]
16908
+ args: [this.#gatewayAddress, amount]
16908
16909
  });
16909
16910
  const approveGas = await this.#evmService.client.estimateGas({
16910
16911
  account: address,
@@ -16923,9 +16924,6 @@ var SnowbridgeService = class _SnowbridgeService {
16923
16924
  value: contract.value
16924
16925
  });
16925
16926
  const gasPrice = await this.#evmService.client.getGasPrice();
16926
- console.log("gasPrice", gasPrice);
16927
- console.log("approveGas", approveGas);
16928
- console.log("sendGas", sendGas);
16929
16927
  return (approveGas + sendGas) * gasPrice;
16930
16928
  } catch (error) {
16931
16929
  console.error("Error estimating approve + send fee:", error);
@@ -17027,7 +17025,7 @@ import {
17027
17025
  } from "@moonbeam-network/xcm-sdk";
17028
17026
  import {
17029
17027
  AssetAmount as AssetAmount2,
17030
- EvmChain,
17028
+ EvmChain as EvmChain2,
17031
17029
  EvmParachain as EvmParachain3
17032
17030
  } from "@moonbeam-network/xcm-types";
17033
17031
  import { toBigInt as toBigInt2 } from "@moonbeam-network/xcm-utils";
@@ -17276,6 +17274,12 @@ async function getSourceData({
17276
17274
  route,
17277
17275
  sourceAddress
17278
17276
  });
17277
+ const bridgeChainFeeBalance = await getBridgeChainFeeBalance({
17278
+ balance,
17279
+ feeBalance,
17280
+ route,
17281
+ sourceAddress
17282
+ });
17279
17283
  const existentialDeposit = await getExistentialDeposit(source);
17280
17284
  const min = await getAssetMin({
17281
17285
  asset,
@@ -17331,7 +17335,7 @@ async function getSourceData({
17331
17335
  chain: source,
17332
17336
  destinationFee,
17333
17337
  destinationFeeBalance,
17334
- // bridgeChainFeeBalance,
17338
+ bridgeChainFeeBalance,
17335
17339
  existentialDeposit,
17336
17340
  fee,
17337
17341
  feeBalance,
@@ -17358,9 +17362,7 @@ async function getFee({
17358
17362
  });
17359
17363
  }
17360
17364
  if (SnowbridgeConfig.is(transfer)) {
17361
- const snowbridge = SnowbridgeService.create(
17362
- chain2
17363
- );
17365
+ const snowbridge = SnowbridgeService.create(chain2);
17364
17366
  const feeAmount = await snowbridge.getFee(sourceAddress, transfer);
17365
17367
  return AssetAmount2.fromChainAsset(chain2.getChainAsset(feeBalance), {
17366
17368
  amount: feeAmount
@@ -17436,6 +17438,33 @@ async function getWormholeFee({
17436
17438
  }
17437
17439
  return;
17438
17440
  }
17441
+ async function getBridgeChainFeeBalance({
17442
+ balance,
17443
+ feeBalance,
17444
+ route,
17445
+ sourceAddress
17446
+ }) {
17447
+ if (!route.source.bridgeChainFee) {
17448
+ return void 0;
17449
+ }
17450
+ if (route.mrl?.bridgeChain.fee.asset.isEqual(balance)) {
17451
+ return balance;
17452
+ }
17453
+ if (route.mrl?.bridgeChain.fee.asset.isEqual(feeBalance)) {
17454
+ return feeBalance;
17455
+ }
17456
+ if (!route.source.bridgeChainFee.balance) {
17457
+ throw new Error(
17458
+ "BalanceBuilder must be defined for source.bridgeChainFee.balance for MrlAssetRoute"
17459
+ );
17460
+ }
17461
+ return getBalance2({
17462
+ address: sourceAddress,
17463
+ asset: route.source.chain.getChainAsset(route.source.bridgeChainFee.asset),
17464
+ builder: route.source.bridgeChainFee.balance,
17465
+ chain: route.source.chain
17466
+ });
17467
+ }
17439
17468
  async function getProtocolFee({
17440
17469
  address,
17441
17470
  asset,
@@ -17456,7 +17485,7 @@ async function getProtocolFee({
17456
17485
  destination,
17457
17486
  source
17458
17487
  });
17459
- if (ContractConfig2.is(config) && EvmChain.is(source)) {
17488
+ if (ContractConfig2.is(config) && EvmChain2.is(source)) {
17460
17489
  const evm = EvmService3.create(source);
17461
17490
  const amount = await evm.read(config);
17462
17491
  if (typeof amount !== "bigint") {
@@ -17496,12 +17525,10 @@ async function getTransferData({
17496
17525
  route,
17497
17526
  destinationAddress
17498
17527
  });
17499
- console.log("destinationData", destinationData);
17500
17528
  const destinationFee = convertToChainDecimals2({
17501
17529
  asset: destinationData.fee,
17502
17530
  target: route.getDestinationFeeAssetOnSource()
17503
17531
  });
17504
- console.log("destinationFee", destinationFee);
17505
17532
  const sourceData = await getSourceData({
17506
17533
  isAutomatic: route.mrl.isAutomaticPossible && isAutomatic,
17507
17534
  route,
@@ -17509,13 +17536,11 @@ async function getTransferData({
17509
17536
  destinationFee,
17510
17537
  sourceAddress
17511
17538
  });
17512
- console.log("sourceData", sourceData);
17513
17539
  const bridgeChainData = await getBridgeChainData({
17514
17540
  route,
17515
17541
  sourceAddress,
17516
17542
  destinationAddress
17517
17543
  });
17518
- console.log("bridgeChainData", bridgeChainData);
17519
17544
  return {
17520
17545
  destination: destinationData,
17521
17546
  getEstimate(amount) {
@@ -17573,7 +17598,7 @@ async function getTransferData({
17573
17598
  sendOnlyRemoteExecution,
17574
17599
  sourceAddress
17575
17600
  });
17576
- if (ContractConfig3.is(transfer) && (EvmChain2.is(source) || EvmParachain4.is(source))) {
17601
+ if (ContractConfig3.is(transfer) && (EvmChain3.is(source) || EvmParachain4.is(source))) {
17577
17602
  if (!evmSigner) {
17578
17603
  throw new Error("EVM Signer must be provided");
17579
17604
  }
@@ -17594,14 +17619,14 @@ async function getTransferData({
17594
17619
  );
17595
17620
  return [hash];
17596
17621
  }
17597
- if (WormholeConfig2.is(transfer) && (EvmChain2.is(source) || EvmParachain4.is(source))) {
17622
+ if (WormholeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain4.is(source))) {
17598
17623
  if (!evmSigner) {
17599
17624
  throw new Error("EVM Signer must be provided");
17600
17625
  }
17601
17626
  const wh = WormholeService.create(source);
17602
17627
  return wh.transfer(evmSigner, transfer);
17603
17628
  }
17604
- if (SnowbridgeConfig2.is(transfer) && (EvmChain2.is(source) || EvmParachain4.is(source))) {
17629
+ if (SnowbridgeConfig2.is(transfer) && (EvmChain3.is(source) || EvmParachain4.is(source))) {
17605
17630
  if (!evmSigner) {
17606
17631
  throw new Error("EVM Signer must be provided");
17607
17632
  }