@glowlabs-org/utils 0.2.174 → 0.2.176

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-CBI3L6Cz.js');
3
+ var calculateFarmEfficiency = require('./calculate-farm-efficiency-z9p1HOmd.js');
4
4
  require('decimal.js');
5
5
  var viem = require('viem');
6
6
  require('ethers');
@@ -542,7 +542,16 @@ function assertSigner(maybeSigner) {
542
542
  throw new Error(exports.ForwarderError.SIGNER_NOT_AVAILABLE);
543
543
  }
544
544
  }
545
- function useForwarder(signer, CHAIN_ID, publicClient) {
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) {
546
555
  // Use dynamic addresses based on chain configuration
547
556
  const ADDRESSES = getAddresses(CHAIN_ID);
548
557
  // Framework-agnostic processing flag
@@ -550,7 +559,13 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
550
559
  const setIsProcessing = (value) => {
551
560
  isProcessing = value;
552
561
  };
553
- // 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)
554
569
  function getForwarderContract() {
555
570
  assertSigner(signer);
556
571
  return new ethers.Contract(ADDRESSES.FORWARDER, FORWARDER_ABI, signer);
@@ -673,21 +688,44 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
673
688
  * @param currency The currency to approve
674
689
  */
675
690
  async function approveToken(amount, currency = "USDC") {
676
- assertSigner(signer);
677
691
  try {
678
- const tokenContract = getTokenContract(currency);
679
- if (!tokenContract)
680
- throw new Error(exports.ForwarderError.CONTRACT_NOT_AVAILABLE);
681
692
  setIsProcessing(true);
682
- // Approve only the specific amount needed
683
- const approveTx = await tokenContract.approve(ADDRESSES.FORWARDER, amount);
684
- if (publicClient) {
685
- await waitForViemTransactionWithRetry(publicClient, approveTx.hash, {
686
- timeoutMs: 60000,
687
- pollIntervalMs: 2000,
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,
688
719
  });
720
+ await waitForViemTransactionWithRetry(publicClient, hash);
689
721
  }
690
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);
691
729
  await waitForEthersTransactionWithRetry(signer, approveTx.hash, {
692
730
  timeoutMs: 60000,
693
731
  pollIntervalMs: 2000,
@@ -696,6 +734,9 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
696
734
  return true;
697
735
  }
698
736
  catch (error) {
737
+ if (walletClient && publicClient) {
738
+ throw new Error(parseViemError(error));
739
+ }
699
740
  throw new Error(parseEthersError(error));
700
741
  }
701
742
  finally {
@@ -707,16 +748,9 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
707
748
  * @param params Forward parameters including type, amount, and required fields
708
749
  */
709
750
  async function forwardTokens(params) {
710
- assertSigner(signer);
711
751
  try {
712
- const forwarderContract = getForwarderContract();
713
- if (!forwarderContract)
714
- throw new Error(exports.ForwarderError.CONTRACT_NOT_AVAILABLE);
715
752
  setIsProcessing(true);
716
753
  const { amount, currency = "USDC" } = params;
717
- const tokenContract = getTokenContract(currency);
718
- if (!tokenContract)
719
- throw new Error(exports.ForwarderError.CONTRACT_NOT_AVAILABLE);
720
754
  sentryAddBreadcrumb({
721
755
  category: "forwarder",
722
756
  message: "forwardTokens.start",
@@ -733,50 +767,16 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
733
767
  userAddress: params.userAddress,
734
768
  },
735
769
  });
736
- const owner = await signer.getAddress();
737
- // Construct the appropriate message for this forward type
738
- const message = constructForwardMessage(params);
739
- // Special handling: PayAuditFees can ONLY be USDC, and must call forward()
770
+ // Special handling checks
740
771
  const isAuditFees = params.type === TRANSFER_TYPES.PayAuditFees;
741
772
  if (isAuditFees && currency !== "USDC") {
742
773
  throw new Error("PayAuditFees only supports USDC");
743
774
  }
744
- // CommitKickstarter supports only USDC or USDG (GLW not allowed)
745
775
  const isCommitKickstarter = params.type === TRANSFER_TYPES.CommitKickstarter;
746
776
  if (isCommitKickstarter && currency === "GLW") {
747
777
  throw new Error("CommitKickstarter supports only USDC or USDG");
748
778
  }
749
- // Check allowance and approve if necessary
750
- const allowance = await tokenContract.allowance(owner, ADDRESSES.FORWARDER);
751
- console.log("allowance", allowance.toString());
752
- if (allowance < amount) {
753
- try {
754
- const approveTx = await tokenContract.approve(ADDRESSES.FORWARDER, ethers.MaxUint256);
755
- if (publicClient) {
756
- await waitForViemTransactionWithRetry(publicClient, approveTx.hash, {
757
- timeoutMs: 60000,
758
- pollIntervalMs: 2000,
759
- });
760
- }
761
- else {
762
- await waitForEthersTransactionWithRetry(signer, approveTx.hash, {
763
- timeoutMs: 60000,
764
- pollIntervalMs: 2000,
765
- });
766
- }
767
- }
768
- catch (approveError) {
769
- sentryCaptureException(approveError, {
770
- action: "forwardTokens.approve",
771
- chainId: CHAIN_ID,
772
- spender: ADDRESSES.FORWARDER,
773
- amount: ethers.MaxUint256.toString(),
774
- currency,
775
- });
776
- throw new Error(parseEthersError(approveError) || "Token approval failed");
777
- }
778
- }
779
- // Get the token address based on currency
779
+ // Get token address
780
780
  let tokenAddress;
781
781
  switch (currency) {
782
782
  case "USDC":
@@ -791,62 +791,206 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
791
791
  default:
792
792
  throw new Error(`Unsupported currency for forwarding: ${currency}`);
793
793
  }
794
- // Determine sendToCounterfactualWallet: false for audit fees (paid in USDC directly)
794
+ const message = constructForwardMessage(params);
795
795
  const sendToCounterfactualWallet = !isAuditFees;
796
- // Run a static call first to surface any revert reason (ethers v6)
797
- 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;
798
882
  if (!isAuditFees && currency === "USDC") {
799
- await forwarderContract
800
- .getFunction("swapUSDCAndForwardUSDG")
801
- .staticCall(amount, ADDRESSES.FOUNDATION_WALLET, sendToCounterfactualWallet, message, {
802
- 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,
803
895
  });
804
896
  }
805
897
  else {
806
- await forwarderContract
807
- .getFunction("forward")
808
- .staticCall(tokenAddress, isAuditFees
809
- ? ADDRESSES.AUDIT_FEE_WALLET
810
- : 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
+ });
811
914
  }
812
- }
813
- catch (staticError) {
814
- sentryCaptureException(staticError, {
815
- action: "forwardTokens.staticCall",
816
- chainId: CHAIN_ID,
817
- function: !isAuditFees && currency === "USDC"
818
- ? "swapUSDCAndForwardUSDG"
819
- : "forward",
820
- tokenAddress,
821
- amount: amount.toString(),
822
- currency,
823
- isAuditFees,
824
- });
825
- throw new Error(parseEthersError(staticError));
826
- }
827
- // Execute the forward transaction
828
- let tx;
829
- if (!isAuditFees && currency === "USDC") {
830
- tx = await forwarderContract.getFunction("swapUSDCAndForwardUSDG")(amount, ADDRESSES.FOUNDATION_WALLET, sendToCounterfactualWallet, message);
831
- }
832
- else {
833
- tx = await forwarderContract.getFunction("forward")(tokenAddress, isAuditFees
834
- ? ADDRESSES.AUDIT_FEE_WALLET
835
- : ADDRESSES.FOUNDATION_WALLET, amount, sendToCounterfactualWallet, message);
836
- }
837
- if (publicClient) {
838
- await waitForViemTransactionWithRetry(publicClient, tx.hash, {
839
- timeoutMs: 60000,
840
- pollIntervalMs: 2000,
841
- });
915
+ await waitForViemTransactionWithRetry(publicClient, hash);
916
+ return hash;
842
917
  }
843
918
  else {
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
958
+ .getFunction("forward")
959
+ .staticCall(tokenAddress, isAuditFees
960
+ ? ADDRESSES.AUDIT_FEE_WALLET
961
+ : ADDRESSES.FOUNDATION_WALLET, amount, sendToCounterfactualWallet, message, { from: owner });
962
+ }
963
+ }
964
+ catch (staticError) {
965
+ sentryCaptureException(staticError, {
966
+ action: "forwardTokens.staticCall",
967
+ chainId: CHAIN_ID,
968
+ function: !isAuditFees && currency === "USDC"
969
+ ? "swapUSDCAndForwardUSDG"
970
+ : "forward",
971
+ tokenAddress,
972
+ amount: amount.toString(),
973
+ currency,
974
+ isAuditFees,
975
+ });
976
+ throw new Error(parseEthersError(staticError));
977
+ }
978
+ // Execute transaction
979
+ let tx;
980
+ if (!isAuditFees && currency === "USDC") {
981
+ tx = await forwarderContract.getFunction("swapUSDCAndForwardUSDG")(amount, ADDRESSES.FOUNDATION_WALLET, sendToCounterfactualWallet, message);
982
+ }
983
+ else {
984
+ tx = await forwarderContract.getFunction("forward")(tokenAddress, isAuditFees
985
+ ? ADDRESSES.AUDIT_FEE_WALLET
986
+ : ADDRESSES.FOUNDATION_WALLET, amount, sendToCounterfactualWallet, message);
987
+ }
844
988
  await waitForEthersTransactionWithRetry(signer, tx.hash, {
845
989
  timeoutMs: 60000,
846
990
  pollIntervalMs: 2000,
847
991
  });
992
+ return tx.hash;
848
993
  }
849
- return tx.hash;
850
994
  }
851
995
  catch (txError) {
852
996
  sentryCaptureException(txError, {
@@ -861,6 +1005,9 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
861
1005
  kickstarterId: params.kickstarterId,
862
1006
  userAddress: params.userAddress,
863
1007
  });
1008
+ if (walletClient && publicClient) {
1009
+ throw new Error(parseViemError(txError));
1010
+ }
864
1011
  throw new Error(parseEthersError(txError));
865
1012
  }
866
1013
  finally {
@@ -912,6 +1059,7 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
912
1059
  type: TRANSFER_TYPES.PayProtocolFee,
913
1060
  currency,
914
1061
  applicationId,
1062
+ regionId: undefined, // Fix missing argument
915
1063
  });
916
1064
  }
917
1065
  /**
@@ -931,7 +1079,10 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
931
1079
  * Forward USDC to mint GCTL and stake to a region
932
1080
  */
933
1081
  async function mintGCTLAndStake(amount, userAddress, regionId, currency = "USDC") {
934
- assertSigner(signer);
1082
+ // If using WalletClient, we don't strictly need a signer here anymore for forwardTokens
1083
+ // But other methods might still rely on it.
1084
+ if (!walletClient)
1085
+ assertSigner(signer);
935
1086
  // GCTL minting only supports USDC and USDG
936
1087
  if (currency === "GLW") {
937
1088
  throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
@@ -948,7 +1099,8 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
948
1099
  * Forward USDC to mint GCTL (existing functionality, keeping for compatibility)
949
1100
  */
950
1101
  async function mintGCTL(amount, userAddress, currency = "USDC") {
951
- assertSigner(signer);
1102
+ if (!walletClient)
1103
+ assertSigner(signer);
952
1104
  // GCTL minting only supports USDC and USDG
953
1105
  if (currency === "GLW") {
954
1106
  throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
@@ -1132,7 +1284,7 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
1132
1284
  },
1133
1285
  addresses: ADDRESSES,
1134
1286
  // Signer availability
1135
- isSignerAvailable: !!signer,
1287
+ isSignerAvailable: !!signer || !!walletClient,
1136
1288
  };
1137
1289
  }
1138
1290
 
@@ -1864,6 +2016,17 @@ function useOffchainFractions(walletClient, publicClient, CHAIN_ID) {
1864
2016
  account: walletClient.account,
1865
2017
  });
1866
2018
  await waitForViemTransactionWithRetry(publicClient, approveHash);
2019
+ // Verify allowance is reflected before proceeding (RPC lag mitigation)
2020
+ const maxAllowanceChecks = 5;
2021
+ const allowanceCheckDelayMs = 500;
2022
+ for (let i = 0; i < maxAllowanceChecks; i++) {
2023
+ const updatedAllowance = await checkTokenAllowance(owner, fractionData.token);
2024
+ if (updatedAllowance >= requiredAmount)
2025
+ break;
2026
+ if (i < maxAllowanceChecks - 1) {
2027
+ await new Promise((r) => setTimeout(r, allowanceCheckDelayMs));
2028
+ }
2029
+ }
1867
2030
  }
1868
2031
  // Run a simulation first to surface any revert reason
1869
2032
  try {
@@ -5371,4 +5534,4 @@ exports.useOffchainFractions = useOffchainFractions;
5371
5534
  exports.useRewardsKernel = useRewardsKernel;
5372
5535
  exports.waitForEthersTransactionWithRetry = waitForEthersTransactionWithRetry;
5373
5536
  exports.waitForViemTransactionWithRetry = waitForViemTransactionWithRetry;
5374
- //# sourceMappingURL=calculate-farm-efficiency-CBI3L6Cz.js.map
5537
+ //# sourceMappingURL=calculate-farm-efficiency-z9p1HOmd.js.map