@glowlabs-org/utils 0.2.173 → 0.2.175

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.
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var calculateFarmEfficiency = require('./calculate-farm-efficiency-CKxIgfpG.js');
3
+ var calculateFarmEfficiency = require('./calculate-farm-efficiency-psH9YXnF.js');
4
4
  require('decimal.js');
5
5
  var viem = require('viem');
6
6
  require('ethers');
@@ -443,12 +443,20 @@ async function waitForEthersTransactionWithRetry(signer, txHash, options = {}) {
443
443
  }
444
444
  catch (error) {
445
445
  const errorMessage = parseEthersError(error);
446
- consecutiveErrors++;
447
- if (consecutiveErrors >= maxRetries) {
448
- throw new Error(`Transaction failed after ${consecutiveErrors} attempts: ${errorMessage}`);
449
- }
450
- if (enableLogging) {
451
- console.warn(`Error fetching receipt (attempt ${consecutiveErrors}/${maxRetries}), retrying in ${pollIntervalMs}ms...`);
446
+ // Treat not found/receipt missing as retryable without counting towards errors
447
+ const isNotFound = errorMessage.toLowerCase().includes("not found") ||
448
+ errorMessage.toLowerCase().includes("could not be found") ||
449
+ errorMessage.toLowerCase().includes("receipt") ||
450
+ errorMessage.toLowerCase().includes("not confirmed") ||
451
+ errorMessage.toLowerCase().includes("transactionreceiptnotfound");
452
+ if (!isNotFound) {
453
+ consecutiveErrors++;
454
+ if (consecutiveErrors >= maxRetries) {
455
+ throw new Error(`Transaction failed after ${consecutiveErrors} attempts: ${errorMessage}`);
456
+ }
457
+ if (enableLogging) {
458
+ console.warn(`Error fetching receipt (attempt ${consecutiveErrors}/${maxRetries}), retrying in ${pollIntervalMs}ms...`);
459
+ }
452
460
  }
453
461
  }
454
462
  await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
@@ -534,7 +542,16 @@ function assertSigner(maybeSigner) {
534
542
  throw new Error(exports.ForwarderError.SIGNER_NOT_AVAILABLE);
535
543
  }
536
544
  }
537
- function useForwarder(signer, CHAIN_ID) {
545
+ // Type-guard style helper to ensure a wallet client exists throughout the rest of the function.
546
+ function assertWalletClient$2(maybeWalletClient) {
547
+ if (!maybeWalletClient) {
548
+ throw new Error(exports.ForwarderError.SIGNER_NOT_AVAILABLE);
549
+ }
550
+ if (!maybeWalletClient.account) {
551
+ throw new Error("Wallet client must have an account");
552
+ }
553
+ }
554
+ function useForwarder(signer, CHAIN_ID, publicClient, walletClient) {
538
555
  // Use dynamic addresses based on chain configuration
539
556
  const ADDRESSES = getAddresses(CHAIN_ID);
540
557
  // Framework-agnostic processing flag
@@ -542,7 +559,13 @@ function useForwarder(signer, CHAIN_ID) {
542
559
  const setIsProcessing = (value) => {
543
560
  isProcessing = value;
544
561
  };
545
- // Returns a contract instance for Forwarder
562
+ // Helper to assert public client is available
563
+ function assertPublicClient(maybePublicClient) {
564
+ if (!maybePublicClient) {
565
+ throw new Error("Public client not available");
566
+ }
567
+ }
568
+ // Returns a contract instance for Forwarder (Legacy Ethers)
546
569
  function getForwarderContract() {
547
570
  assertSigner(signer);
548
571
  return new ethers.Contract(ADDRESSES.FORWARDER, FORWARDER_ABI, signer);
@@ -665,21 +688,55 @@ function useForwarder(signer, CHAIN_ID) {
665
688
  * @param currency The currency to approve
666
689
  */
667
690
  async function approveToken(amount, currency = "USDC") {
668
- assertSigner(signer);
669
691
  try {
670
- const tokenContract = getTokenContract(currency);
671
- if (!tokenContract)
672
- throw new Error(exports.ForwarderError.CONTRACT_NOT_AVAILABLE);
673
692
  setIsProcessing(true);
674
- // Approve only the specific amount needed
675
- const approveTx = await tokenContract.approve(ADDRESSES.FORWARDER, amount);
676
- await waitForEthersTransactionWithRetry(signer, approveTx.hash, {
677
- timeoutMs: 60000,
678
- pollIntervalMs: 2000,
679
- });
693
+ // Get token address
694
+ let tokenAddress;
695
+ switch (currency) {
696
+ case "USDC":
697
+ tokenAddress = ADDRESSES.USDC;
698
+ break;
699
+ case "GLW":
700
+ tokenAddress = ADDRESSES.GLW;
701
+ break;
702
+ case "USDG":
703
+ tokenAddress = ADDRESSES.USDG;
704
+ break;
705
+ default:
706
+ throw new Error(`Currency ${currency} not yet supported. Only USDC, GLW, and USDG are currently supported.`);
707
+ }
708
+ // If walletClient and publicClient are available, use Viem (better for mobile)
709
+ if (walletClient && publicClient) {
710
+ assertWalletClient$2(walletClient);
711
+ assertPublicClient(publicClient);
712
+ const hash = await walletClient.writeContract({
713
+ address: tokenAddress,
714
+ abi: ERC20_ABI,
715
+ functionName: "approve",
716
+ args: [ADDRESSES.FORWARDER, amount],
717
+ chain: walletClient.chain,
718
+ account: walletClient.account,
719
+ });
720
+ await waitForViemTransactionWithRetry(publicClient, hash);
721
+ }
722
+ else {
723
+ // Fallback to Ethers
724
+ assertSigner(signer);
725
+ const tokenContract = getTokenContract(currency);
726
+ if (!tokenContract)
727
+ throw new Error(exports.ForwarderError.CONTRACT_NOT_AVAILABLE);
728
+ const approveTx = await tokenContract.approve(ADDRESSES.FORWARDER, amount);
729
+ await waitForEthersTransactionWithRetry(signer, approveTx.hash, {
730
+ timeoutMs: 60000,
731
+ pollIntervalMs: 2000,
732
+ });
733
+ }
680
734
  return true;
681
735
  }
682
736
  catch (error) {
737
+ if (walletClient && publicClient) {
738
+ throw new Error(parseViemError(error));
739
+ }
683
740
  throw new Error(parseEthersError(error));
684
741
  }
685
742
  finally {
@@ -691,16 +748,9 @@ function useForwarder(signer, CHAIN_ID) {
691
748
  * @param params Forward parameters including type, amount, and required fields
692
749
  */
693
750
  async function forwardTokens(params) {
694
- assertSigner(signer);
695
751
  try {
696
- const forwarderContract = getForwarderContract();
697
- if (!forwarderContract)
698
- throw new Error(exports.ForwarderError.CONTRACT_NOT_AVAILABLE);
699
752
  setIsProcessing(true);
700
753
  const { amount, currency = "USDC" } = params;
701
- const tokenContract = getTokenContract(currency);
702
- if (!tokenContract)
703
- throw new Error(exports.ForwarderError.CONTRACT_NOT_AVAILABLE);
704
754
  sentryAddBreadcrumb({
705
755
  category: "forwarder",
706
756
  message: "forwardTokens.start",
@@ -717,42 +767,16 @@ function useForwarder(signer, CHAIN_ID) {
717
767
  userAddress: params.userAddress,
718
768
  },
719
769
  });
720
- const owner = await signer.getAddress();
721
- // Construct the appropriate message for this forward type
722
- const message = constructForwardMessage(params);
723
- // Special handling: PayAuditFees can ONLY be USDC, and must call forward()
770
+ // Special handling checks
724
771
  const isAuditFees = params.type === TRANSFER_TYPES.PayAuditFees;
725
772
  if (isAuditFees && currency !== "USDC") {
726
773
  throw new Error("PayAuditFees only supports USDC");
727
774
  }
728
- // CommitKickstarter supports only USDC or USDG (GLW not allowed)
729
775
  const isCommitKickstarter = params.type === TRANSFER_TYPES.CommitKickstarter;
730
776
  if (isCommitKickstarter && currency === "GLW") {
731
777
  throw new Error("CommitKickstarter supports only USDC or USDG");
732
778
  }
733
- // Check allowance and approve if necessary
734
- const allowance = await tokenContract.allowance(owner, ADDRESSES.FORWARDER);
735
- console.log("allowance", allowance.toString());
736
- if (allowance < amount) {
737
- try {
738
- const approveTx = await tokenContract.approve(ADDRESSES.FORWARDER, ethers.MaxUint256);
739
- await waitForEthersTransactionWithRetry(signer, approveTx.hash, {
740
- timeoutMs: 60000,
741
- pollIntervalMs: 2000,
742
- });
743
- }
744
- catch (approveError) {
745
- sentryCaptureException(approveError, {
746
- action: "forwardTokens.approve",
747
- chainId: CHAIN_ID,
748
- spender: ADDRESSES.FORWARDER,
749
- amount: ethers.MaxUint256.toString(),
750
- currency,
751
- });
752
- throw new Error(parseEthersError(approveError) || "Token approval failed");
753
- }
754
- }
755
- // Get the token address based on currency
779
+ // Get token address
756
780
  let tokenAddress;
757
781
  switch (currency) {
758
782
  case "USDC":
@@ -767,54 +791,204 @@ function useForwarder(signer, CHAIN_ID) {
767
791
  default:
768
792
  throw new Error(`Unsupported currency for forwarding: ${currency}`);
769
793
  }
770
- // Determine sendToCounterfactualWallet: false for audit fees (paid in USDC directly)
794
+ const message = constructForwardMessage(params);
771
795
  const sendToCounterfactualWallet = !isAuditFees;
772
- // Run a static call first to surface any revert reason (ethers v6)
773
- try {
796
+ // Handle transaction based on available client
797
+ if (walletClient && publicClient) {
798
+ // --- VIEM IMPLEMENTATION ---
799
+ assertWalletClient$2(walletClient);
800
+ assertPublicClient(publicClient);
801
+ const owner = walletClient.account.address;
802
+ // Check allowance
803
+ const allowance = (await publicClient.readContract({
804
+ address: tokenAddress,
805
+ abi: ERC20_ABI,
806
+ functionName: "allowance",
807
+ args: [owner, ADDRESSES.FORWARDER],
808
+ }));
809
+ if (allowance < amount) {
810
+ try {
811
+ const approveHash = await walletClient.writeContract({
812
+ address: tokenAddress,
813
+ abi: ERC20_ABI,
814
+ functionName: "approve",
815
+ args: [ADDRESSES.FORWARDER, ethers.MaxUint256],
816
+ chain: walletClient.chain,
817
+ account: walletClient.account,
818
+ });
819
+ await waitForViemTransactionWithRetry(publicClient, approveHash);
820
+ }
821
+ catch (approveError) {
822
+ sentryCaptureException(approveError, {
823
+ action: "forwardTokens.approve",
824
+ chainId: CHAIN_ID,
825
+ spender: ADDRESSES.FORWARDER,
826
+ amount: ethers.MaxUint256.toString(),
827
+ currency,
828
+ });
829
+ throw new Error(parseViemError(approveError) || "Token approval failed");
830
+ }
831
+ }
832
+ // Simulate
833
+ try {
834
+ if (!isAuditFees && currency === "USDC") {
835
+ await publicClient.simulateContract({
836
+ address: ADDRESSES.FORWARDER,
837
+ abi: FORWARDER_ABI,
838
+ functionName: "swapUSDCAndForwardUSDG",
839
+ args: [
840
+ amount,
841
+ ADDRESSES.FOUNDATION_WALLET,
842
+ sendToCounterfactualWallet,
843
+ message,
844
+ ],
845
+ account: walletClient.account,
846
+ });
847
+ }
848
+ else {
849
+ await publicClient.simulateContract({
850
+ address: ADDRESSES.FORWARDER,
851
+ abi: FORWARDER_ABI,
852
+ functionName: "forward",
853
+ args: [
854
+ tokenAddress,
855
+ (isAuditFees
856
+ ? ADDRESSES.AUDIT_FEE_WALLET
857
+ : ADDRESSES.FOUNDATION_WALLET),
858
+ amount,
859
+ sendToCounterfactualWallet,
860
+ message,
861
+ ],
862
+ account: walletClient.account,
863
+ });
864
+ }
865
+ }
866
+ catch (staticError) {
867
+ sentryCaptureException(staticError, {
868
+ action: "forwardTokens.simulate",
869
+ chainId: CHAIN_ID,
870
+ function: !isAuditFees && currency === "USDC"
871
+ ? "swapUSDCAndForwardUSDG"
872
+ : "forward",
873
+ tokenAddress,
874
+ amount: amount.toString(),
875
+ currency,
876
+ isAuditFees,
877
+ });
878
+ throw new Error(parseViemError(staticError));
879
+ }
880
+ // Write
881
+ let hash;
774
882
  if (!isAuditFees && currency === "USDC") {
775
- await forwarderContract
776
- .getFunction("swapUSDCAndForwardUSDG")
777
- .staticCall(amount, ADDRESSES.FOUNDATION_WALLET, sendToCounterfactualWallet, message, {
778
- from: owner,
883
+ hash = await walletClient.writeContract({
884
+ address: ADDRESSES.FORWARDER,
885
+ abi: FORWARDER_ABI,
886
+ functionName: "swapUSDCAndForwardUSDG",
887
+ args: [
888
+ amount,
889
+ ADDRESSES.FOUNDATION_WALLET,
890
+ sendToCounterfactualWallet,
891
+ message,
892
+ ],
893
+ chain: walletClient.chain,
894
+ account: walletClient.account,
779
895
  });
780
896
  }
781
897
  else {
782
- await forwarderContract
783
- .getFunction("forward")
784
- .staticCall(tokenAddress, isAuditFees
785
- ? ADDRESSES.AUDIT_FEE_WALLET
786
- : ADDRESSES.FOUNDATION_WALLET, amount, sendToCounterfactualWallet, message, { from: owner });
898
+ hash = await walletClient.writeContract({
899
+ address: ADDRESSES.FORWARDER,
900
+ abi: FORWARDER_ABI,
901
+ functionName: "forward",
902
+ args: [
903
+ tokenAddress,
904
+ (isAuditFees
905
+ ? ADDRESSES.AUDIT_FEE_WALLET
906
+ : ADDRESSES.FOUNDATION_WALLET),
907
+ amount,
908
+ sendToCounterfactualWallet,
909
+ message,
910
+ ],
911
+ chain: walletClient.chain,
912
+ account: walletClient.account,
913
+ });
787
914
  }
788
- }
789
- catch (staticError) {
790
- sentryCaptureException(staticError, {
791
- action: "forwardTokens.staticCall",
792
- chainId: CHAIN_ID,
793
- function: !isAuditFees && currency === "USDC"
794
- ? "swapUSDCAndForwardUSDG"
795
- : "forward",
796
- tokenAddress,
797
- amount: amount.toString(),
798
- currency,
799
- isAuditFees,
800
- });
801
- throw new Error(parseEthersError(staticError));
802
- }
803
- // Execute the forward transaction
804
- let tx;
805
- if (!isAuditFees && currency === "USDC") {
806
- tx = await forwarderContract.getFunction("swapUSDCAndForwardUSDG")(amount, ADDRESSES.FOUNDATION_WALLET, sendToCounterfactualWallet, message);
915
+ await waitForViemTransactionWithRetry(publicClient, hash);
916
+ return hash;
807
917
  }
808
918
  else {
809
- tx = await forwarderContract.getFunction("forward")(tokenAddress, isAuditFees
810
- ? ADDRESSES.AUDIT_FEE_WALLET
811
- : ADDRESSES.FOUNDATION_WALLET, amount, sendToCounterfactualWallet, message);
919
+ // --- ETHERS IMPLEMENTATION (Legacy) ---
920
+ assertSigner(signer);
921
+ const forwarderContract = getForwarderContract();
922
+ if (!forwarderContract)
923
+ throw new Error(exports.ForwarderError.CONTRACT_NOT_AVAILABLE);
924
+ const tokenContract = getTokenContract(currency);
925
+ if (!tokenContract)
926
+ throw new Error(exports.ForwarderError.CONTRACT_NOT_AVAILABLE);
927
+ const owner = await signer.getAddress();
928
+ // Check allowance
929
+ const allowance = await tokenContract.allowance(owner, ADDRESSES.FORWARDER);
930
+ if (allowance < amount) {
931
+ try {
932
+ const approveTx = await tokenContract.approve(ADDRESSES.FORWARDER, ethers.MaxUint256);
933
+ await waitForEthersTransactionWithRetry(signer, approveTx.hash, {
934
+ timeoutMs: 60000,
935
+ pollIntervalMs: 2000,
936
+ });
937
+ }
938
+ catch (approveError) {
939
+ sentryCaptureException(approveError, {
940
+ action: "forwardTokens.approve",
941
+ chainId: CHAIN_ID,
942
+ spender: ADDRESSES.FORWARDER,
943
+ amount: ethers.MaxUint256.toString(),
944
+ currency,
945
+ });
946
+ throw new Error(parseEthersError(approveError) || "Token approval failed");
947
+ }
948
+ }
949
+ // Determine function and call static
950
+ try {
951
+ if (!isAuditFees && currency === "USDC") {
952
+ await forwarderContract
953
+ .getFunction("swapUSDCAndForwardUSDG")
954
+ .staticCall(amount, ADDRESSES.FOUNDATION_WALLET, sendToCounterfactualWallet, message, { from: owner });
955
+ }
956
+ else {
957
+ await forwarderContract.getFunction("forward").staticCall(tokenAddress, isAuditFees
958
+ ? ADDRESSES.AUDIT_FEE_WALLET
959
+ : ADDRESSES.FOUNDATION_WALLET, amount, sendToCounterfactualWallet, message, { from: owner });
960
+ }
961
+ }
962
+ catch (staticError) {
963
+ sentryCaptureException(staticError, {
964
+ action: "forwardTokens.staticCall",
965
+ chainId: CHAIN_ID,
966
+ function: !isAuditFees && currency === "USDC"
967
+ ? "swapUSDCAndForwardUSDG"
968
+ : "forward",
969
+ tokenAddress,
970
+ amount: amount.toString(),
971
+ currency,
972
+ isAuditFees,
973
+ });
974
+ throw new Error(parseEthersError(staticError));
975
+ }
976
+ // Execute transaction
977
+ let tx;
978
+ if (!isAuditFees && currency === "USDC") {
979
+ tx = await forwarderContract.getFunction("swapUSDCAndForwardUSDG")(amount, ADDRESSES.FOUNDATION_WALLET, sendToCounterfactualWallet, message);
980
+ }
981
+ else {
982
+ tx = await forwarderContract.getFunction("forward")(tokenAddress, isAuditFees
983
+ ? ADDRESSES.AUDIT_FEE_WALLET
984
+ : ADDRESSES.FOUNDATION_WALLET, amount, sendToCounterfactualWallet, message);
985
+ }
986
+ await waitForEthersTransactionWithRetry(signer, tx.hash, {
987
+ timeoutMs: 60000,
988
+ pollIntervalMs: 2000,
989
+ });
990
+ return tx.hash;
812
991
  }
813
- await waitForEthersTransactionWithRetry(signer, tx.hash, {
814
- timeoutMs: 60000,
815
- pollIntervalMs: 2000,
816
- });
817
- return tx.hash;
818
992
  }
819
993
  catch (txError) {
820
994
  sentryCaptureException(txError, {
@@ -829,6 +1003,9 @@ function useForwarder(signer, CHAIN_ID) {
829
1003
  kickstarterId: params.kickstarterId,
830
1004
  userAddress: params.userAddress,
831
1005
  });
1006
+ if (walletClient && publicClient) {
1007
+ throw new Error(parseViemError(txError));
1008
+ }
832
1009
  throw new Error(parseEthersError(txError));
833
1010
  }
834
1011
  finally {
@@ -880,6 +1057,7 @@ function useForwarder(signer, CHAIN_ID) {
880
1057
  type: TRANSFER_TYPES.PayProtocolFee,
881
1058
  currency,
882
1059
  applicationId,
1060
+ regionId: undefined, // Fix missing argument
883
1061
  });
884
1062
  }
885
1063
  /**
@@ -899,7 +1077,10 @@ function useForwarder(signer, CHAIN_ID) {
899
1077
  * Forward USDC to mint GCTL and stake to a region
900
1078
  */
901
1079
  async function mintGCTLAndStake(amount, userAddress, regionId, currency = "USDC") {
902
- assertSigner(signer);
1080
+ // If using WalletClient, we don't strictly need a signer here anymore for forwardTokens
1081
+ // But other methods might still rely on it.
1082
+ if (!walletClient)
1083
+ assertSigner(signer);
903
1084
  // GCTL minting only supports USDC and USDG
904
1085
  if (currency === "GLW") {
905
1086
  throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
@@ -916,7 +1097,8 @@ function useForwarder(signer, CHAIN_ID) {
916
1097
  * Forward USDC to mint GCTL (existing functionality, keeping for compatibility)
917
1098
  */
918
1099
  async function mintGCTL(amount, userAddress, currency = "USDC") {
919
- assertSigner(signer);
1100
+ if (!walletClient)
1101
+ assertSigner(signer);
920
1102
  // GCTL minting only supports USDC and USDG
921
1103
  if (currency === "GLW") {
922
1104
  throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
@@ -1048,10 +1230,18 @@ function useForwarder(signer, CHAIN_ID) {
1048
1230
  setIsProcessing(true);
1049
1231
  // Try to call mint function (common for test tokens)
1050
1232
  const tx = await usdcContract.mint(recipient, amount);
1051
- await waitForEthersTransactionWithRetry(signer, tx.hash, {
1052
- timeoutMs: 60000,
1053
- pollIntervalMs: 2000,
1054
- });
1233
+ if (publicClient) {
1234
+ await waitForViemTransactionWithRetry(publicClient, tx.hash, {
1235
+ timeoutMs: 60000,
1236
+ pollIntervalMs: 2000,
1237
+ });
1238
+ }
1239
+ else {
1240
+ await waitForEthersTransactionWithRetry(signer, tx.hash, {
1241
+ timeoutMs: 60000,
1242
+ pollIntervalMs: 2000,
1243
+ });
1244
+ }
1055
1245
  return tx.hash;
1056
1246
  }
1057
1247
  catch (error) {
@@ -1092,7 +1282,7 @@ function useForwarder(signer, CHAIN_ID) {
1092
1282
  },
1093
1283
  addresses: ADDRESSES,
1094
1284
  // Signer availability
1095
- isSignerAvailable: !!signer,
1285
+ isSignerAvailable: !!signer || !!walletClient,
1096
1286
  };
1097
1287
  }
1098
1288
 
@@ -5331,4 +5521,4 @@ exports.useOffchainFractions = useOffchainFractions;
5331
5521
  exports.useRewardsKernel = useRewardsKernel;
5332
5522
  exports.waitForEthersTransactionWithRetry = waitForEthersTransactionWithRetry;
5333
5523
  exports.waitForViemTransactionWithRetry = waitForViemTransactionWithRetry;
5334
- //# sourceMappingURL=calculate-farm-efficiency-CKxIgfpG.js.map
5524
+ //# sourceMappingURL=calculate-farm-efficiency-psH9YXnF.js.map