@glowlabs-org/utils 0.2.174 → 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-CBI3L6Cz.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');
@@ -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,204 @@ 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.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
+ }
844
986
  await waitForEthersTransactionWithRetry(signer, tx.hash, {
845
987
  timeoutMs: 60000,
846
988
  pollIntervalMs: 2000,
847
989
  });
990
+ return tx.hash;
848
991
  }
849
- return tx.hash;
850
992
  }
851
993
  catch (txError) {
852
994
  sentryCaptureException(txError, {
@@ -861,6 +1003,9 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
861
1003
  kickstarterId: params.kickstarterId,
862
1004
  userAddress: params.userAddress,
863
1005
  });
1006
+ if (walletClient && publicClient) {
1007
+ throw new Error(parseViemError(txError));
1008
+ }
864
1009
  throw new Error(parseEthersError(txError));
865
1010
  }
866
1011
  finally {
@@ -912,6 +1057,7 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
912
1057
  type: TRANSFER_TYPES.PayProtocolFee,
913
1058
  currency,
914
1059
  applicationId,
1060
+ regionId: undefined, // Fix missing argument
915
1061
  });
916
1062
  }
917
1063
  /**
@@ -931,7 +1077,10 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
931
1077
  * Forward USDC to mint GCTL and stake to a region
932
1078
  */
933
1079
  async function mintGCTLAndStake(amount, userAddress, regionId, currency = "USDC") {
934
- 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);
935
1084
  // GCTL minting only supports USDC and USDG
936
1085
  if (currency === "GLW") {
937
1086
  throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
@@ -948,7 +1097,8 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
948
1097
  * Forward USDC to mint GCTL (existing functionality, keeping for compatibility)
949
1098
  */
950
1099
  async function mintGCTL(amount, userAddress, currency = "USDC") {
951
- assertSigner(signer);
1100
+ if (!walletClient)
1101
+ assertSigner(signer);
952
1102
  // GCTL minting only supports USDC and USDG
953
1103
  if (currency === "GLW") {
954
1104
  throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
@@ -1132,7 +1282,7 @@ function useForwarder(signer, CHAIN_ID, publicClient) {
1132
1282
  },
1133
1283
  addresses: ADDRESSES,
1134
1284
  // Signer availability
1135
- isSignerAvailable: !!signer,
1285
+ isSignerAvailable: !!signer || !!walletClient,
1136
1286
  };
1137
1287
  }
1138
1288
 
@@ -5371,4 +5521,4 @@ exports.useOffchainFractions = useOffchainFractions;
5371
5521
  exports.useRewardsKernel = useRewardsKernel;
5372
5522
  exports.waitForEthersTransactionWithRetry = waitForEthersTransactionWithRetry;
5373
5523
  exports.waitForViemTransactionWithRetry = waitForViemTransactionWithRetry;
5374
- //# sourceMappingURL=calculate-farm-efficiency-CBI3L6Cz.js.map
5524
+ //# sourceMappingURL=calculate-farm-efficiency-psH9YXnF.js.map