@stableflow/wallet-evm 3.1.3 → 3.1.4

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/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Csl, OpenAPI, erc20Abi, numberRemoveEndZero, getPrice, ExecTime, Service, SendType, usdtTokens } from '@stableflow/core';
2
- import { LZ_RECEIVE_VALUE, addressToBytes32, getHopMsgFee, USDT0_LEGACY_MESH_TRANSFTER_FEE, quoteSignature, FRAXZERO_MIDDLE_TOKEN_USDC, FRAXZERO_MIDDLE_TOKEN_FRXUSD, USDT0_CONFIG, OFT_ABI } from '@stableflow/bridges';
2
+ import { removeOftDust, LZ_RECEIVE_VALUE, addressToBytes32, getHopMsgFee, USDT0_LEGACY_MESH_TRANSFTER_FEE, quoteSignature, OFT_ABI, FRAXZERO_MIDDLE_TOKEN_USDC, FRAXZERO_MIDDLE_TOKEN_FRXUSD, USDT0_CONFIG } from '@stableflow/bridges';
3
3
  import { evmRpcFallbackProvider, NATIVE_MSG_FEE_BUFFER, buildEndpointV2LzComposePayload } from '@stableflow/utils-evm';
4
4
  import { getDestinationAssociatedTokenAddress } from '@stableflow/utils-solana';
5
5
  import Big from 'big.js';
@@ -304,6 +304,7 @@ var EVMWallet = class {
304
304
  originLayerzero,
305
305
  destinationLayerzero
306
306
  } = params;
307
+ const amountLdClean = removeOftDust(amountWei || 0, fromToken.decimals);
307
308
  const result = {
308
309
  needApprove: false,
309
310
  approveSpender: originLayerzeroAddress,
@@ -320,8 +321,12 @@ var EVMWallet = class {
320
321
  estimateSourceGasUsd: "0",
321
322
  estimateTime: 0,
322
323
  // seconds - dynamically calculated using LayerZero formula
323
- outputAmount: numberRemoveEndZero(Big(amountWei || 0).div(10 ** params.fromToken.decimals).toFixed(params.fromToken.decimals, 0))
324
+ outputAmount: numberRemoveEndZero(Big(amountLdClean || 0).div(10 ** params.fromToken.decimals).toFixed(params.fromToken.decimals, 0))
324
325
  };
326
+ if (BigInt(amountLdClean) === 0n) {
327
+ result.errMsg = "Amount below minimum cross-chain unit";
328
+ return result;
329
+ }
325
330
  const execTime = new ExecTime({ type: `Usdt0 EVM ${fromToken.chainName}->${toToken.chainName}`, logStyle: "stone-100", isDebug: OpenAPI.DEBUG });
326
331
  const provider = evmRpcFallbackProvider(fromToken);
327
332
  const oftContract = new ethers.Contract(originLayerzeroAddress, abi, this.signer);
@@ -348,7 +353,7 @@ var EVMWallet = class {
348
353
  const sendParam = {
349
354
  dstEid,
350
355
  to: addressToBytes32(toToken.chainType, recipient),
351
- amountLD: amountWei,
356
+ amountLD: amountLdClean,
352
357
  minAmountLD: 0n,
353
358
  extraOptions: unMultiHopExtraOptions,
354
359
  composeMsg: "0x",
@@ -384,21 +389,22 @@ var EVMWallet = class {
384
389
  );
385
390
  }
386
391
  if (isOriginLegacy || isDestinationLegacy) {
387
- result.fees.legacyMeshFeeUsd = numberRemoveEndZero(Big(amountWei || 0).div(10 ** params.fromToken.decimals).times(USDT0_LEGACY_MESH_TRANSFTER_FEE).toFixed(params.fromToken.decimals));
388
- result.outputAmount = numberRemoveEndZero(Big(Big(amountWei || 0).div(10 ** params.fromToken.decimals)).minus(result.fees.legacyMeshFeeUsd || 0).toFixed(params.fromToken.decimals, 0));
392
+ result.fees.legacyMeshFeeUsd = numberRemoveEndZero(Big(amountLdClean || 0).div(10 ** params.fromToken.decimals).times(USDT0_LEGACY_MESH_TRANSFTER_FEE).toFixed(params.fromToken.decimals));
393
+ result.outputAmount = numberRemoveEndZero(Big(Big(amountLdClean || 0).div(10 ** params.fromToken.decimals)).minus(result.fees.legacyMeshFeeUsd || 0).toFixed(params.fromToken.decimals, 0));
389
394
  }
390
395
  if (!dry) {
391
396
  execTime.breakpoint();
392
397
  const oftData = await oftContractRead.quoteOFT.staticCall(sendParam);
393
398
  const [, , oftReceipt] = oftData;
394
- sendParam.minAmountLD = Big(amountWei).times(Big(1).minus(Big(slippageTolerance || 0).div(100))).toFixed(0);
399
+ sendParam.amountLD = oftReceipt[0].toString();
400
+ sendParam.minAmountLD = Big(sendParam.amountLD).times(Big(1).minus(Big(slippageTolerance || 0).div(100))).toFixed(0);
395
401
  result.outputAmount = numberRemoveEndZero(Big(oftReceipt[1].toString()).div(10 ** params.toToken.decimals).toFixed(params.toToken.decimals, 0));
396
402
  execTime.log("quoteOFT.staticCall");
397
403
  }
398
404
  this.csl("EVMWallet quoteOFT", "red-600", "result.outputAmount: %o", result.outputAmount);
399
405
  this.csl("EVMWallet quoteOFT", "red-600", "slippageTolerance: %o", slippageTolerance + "%");
400
- this.csl("EVMWallet quoteOFT", "red-600", "Minimum received amount: %o", Big(amountWei).div(10 ** fromToken.decimals).times(Big(1).minus(Big(slippageTolerance || 0).div(100))).toFixed(6, 0));
401
- if (Big(result.outputAmount).lt(Big(amountWei).div(10 ** fromToken.decimals).times(Big(1).minus(Big(slippageTolerance || 0).div(100))))) {
406
+ this.csl("EVMWallet quoteOFT", "red-600", "Minimum received amount: %o", Big(amountLdClean).div(10 ** fromToken.decimals).times(Big(1).minus(Big(slippageTolerance || 0).div(100))).toFixed(6, 0));
407
+ if (Big(result.outputAmount).lt(Big(amountLdClean).div(10 ** fromToken.decimals).times(Big(1).minus(Big(slippageTolerance || 0).div(100))))) {
402
408
  result.errMsg = "Slippage limit exceeded";
403
409
  return result;
404
410
  }
@@ -413,7 +419,7 @@ var EVMWallet = class {
413
419
  contractAddress: fromToken.contractAddress,
414
420
  spender: originLayerzeroAddress,
415
421
  address: refundTo,
416
- amountWei,
422
+ amountWei: amountLdClean,
417
423
  provider
418
424
  })
419
425
  );
@@ -878,6 +884,18 @@ var EVMWallet = class {
878
884
  eid: dstEid
879
885
  } = destinationLayerzero;
880
886
  const isFromEthereum = fromToken.chainId === 1;
887
+ const oftAddress = isFromEthereum ? lockbox : fromToken.contractAddress;
888
+ const provider = evmRpcFallbackProvider(fromToken);
889
+ let amountLdClean = removeOftDust(amountWei || 0, fromToken.decimals);
890
+ if (!dry) {
891
+ try {
892
+ const oftContractRead = new ethers.Contract(oftAddress, OFT_ABI, provider);
893
+ amountLdClean = (await oftContractRead.removeDust.staticCall(amountWei)).toString();
894
+ } catch (error) {
895
+ this.csl("EVM quoteFraxZero", "yellow-500", "removeDust failed, use local: %o", error);
896
+ amountLdClean = removeOftDust(amountWei || 0, fromToken.decimals);
897
+ }
898
+ }
881
899
  const result = {
882
900
  needApprove: false,
883
901
  approveSpender: remoteHop,
@@ -891,21 +909,24 @@ var EVMWallet = class {
891
909
  totalEstimateSourceGas: 0n,
892
910
  estimateSourceGasUsd: 0,
893
911
  estimateTime: 0,
894
- outputAmount: numberRemoveEndZero(Big(amountWei || 0).div(10 ** params.fromToken.decimals).toFixed(params.fromToken.decimals, 0))
912
+ outputAmount: numberRemoveEndZero(Big(amountLdClean || 0).div(10 ** params.fromToken.decimals).toFixed(params.fromToken.decimals, 0))
895
913
  };
896
- const provider = evmRpcFallbackProvider(fromToken);
914
+ if (BigInt(amountLdClean) === 0n) {
915
+ result.errMsg = "Amount below minimum cross-chain unit";
916
+ return result;
917
+ }
897
918
  const execTime = new ExecTime({ type: `FraxZero EVM ${fromToken.chainName}->${toToken.chainName}`, logStyle: "stone-500", isDebug: OpenAPI.DEBUG });
898
919
  const remoteHopContract = new ethers.Contract(remoteHop, abi, this.signer);
899
920
  const remoteHopContractRead = new ethers.Contract(remoteHop, abi, provider);
900
921
  const sendParams = [
901
922
  // _oft
902
- isFromEthereum ? lockbox : fromToken.contractAddress,
923
+ oftAddress,
903
924
  // _dstEid
904
925
  dstEid,
905
926
  // _to
906
927
  addressToBytes32(toToken.chainType, recipient),
907
- // _amountLD
908
- amountWei
928
+ // _amountLD (OFT sharedDecimals dust removed)
929
+ amountLdClean
909
930
  ];
910
931
  execTime.breakpoint();
911
932
  const mergedCalls = [
@@ -914,7 +935,7 @@ var EVMWallet = class {
914
935
  contractAddress: fromToken.contractAddress,
915
936
  spender: remoteHop,
916
937
  address: refundTo,
917
- amountWei,
938
+ amountWei: amountLdClean,
918
939
  provider
919
940
  }),
920
941
  remoteHopContractRead.quote.staticCall(...sendParams)
@@ -932,6 +953,7 @@ var EVMWallet = class {
932
953
  result.fees.nativeFeeUsd = numberRemoveEndZero(Big(nativeFeeUsd).toFixed(20));
933
954
  result.fees.lzTokenFeeUsd = numberRemoveEndZero(Big(msgFee[1]?.toString() || 0).div(10 ** fromToken.decimals).toFixed(20));
934
955
  this.csl("EVM quoteFraxZero", "blue-700", "msgFee: %o", msgFee);
956
+ this.csl("EVM quoteFraxZero", "blue-700", "amountLdClean: %o", amountLdClean);
935
957
  execTime.breakpoint();
936
958
  const finalSendParam = [...sendParams, { value: nativeMsgFee }];
937
959
  const ett = await this.estimateTransaction({