@@ -2,6 +2,12 @@ import { useState, useEffect, useCallback } from 'react';
22import { useContractIntegration } from './useContractIntegration' ;
33import { useBloxsStore } from '../stores/useBloxsStore' ;
44import { usePools } from './usePools' ;
5+ import { useUserProfileStore } from '../stores/useUserProfileStore' ;
6+ import { useSettingsStore } from '../stores/useSettingsStore' ;
7+ import { ethers } from 'ethers' ;
8+ import { getChainConfigByName } from '../contracts/config' ;
9+ import { REWARD_ENGINE_ABI } from '../contracts/abis' ;
10+ import { peerIdToBytes32 } from '../utils/peerIdConversion' ;
511
612export interface ClaimableRewardsState {
713 unclaimedMining : string ;
@@ -18,6 +24,10 @@ export const useClaimableTokens = () => {
1824 const { contractService, isReady, connectedAccount } = useContractIntegration ( ) ;
1925 const currentBloxPeerId = useBloxsStore ( ( state ) => state . currentBloxPeerId ) ;
2026 const { userPoolId } = usePools ( ) ;
27+ const manualSignatureWalletAddress = useUserProfileStore (
28+ ( state ) => state . manualSignatureWalletAddress
29+ ) ;
30+ const selectedChain = useSettingsStore ( ( state ) => state . selectedChain ) ;
2131
2232 const [ state , setState ] = useState < ClaimableRewardsState > ( {
2333 unclaimedMining : '0' ,
@@ -30,17 +40,22 @@ export const useClaimableTokens = () => {
3040 canClaim : false ,
3141 } ) ;
3242
43+ // Determine effective account (MetaMask or manual signature)
44+ const effectiveAccount = connectedAccount || manualSignatureWalletAddress ;
45+ const useReadOnlyService = ! isReady && ! ! manualSignatureWalletAddress ;
46+
3347 const fetchClaimableTokens = useCallback ( async ( ) => {
3448 console . log ( '🚀 useClaimableTokens: fetchClaimableTokens called' ) ;
3549 console . log ( '🔍 useClaimableTokens: Dependencies check:' , {
3650 contractService : ! ! contractService ,
3751 isReady,
3852 currentBloxPeerId,
39- connectedAccount,
40- userPoolId
53+ effectiveAccount,
54+ userPoolId,
55+ useReadOnlyService,
4156 } ) ;
4257
43- if ( ! contractService || ! isReady || ! currentBloxPeerId || ! connectedAccount || ! userPoolId ) {
58+ if ( ! currentBloxPeerId || ! effectiveAccount || ! userPoolId ) {
4459 console . log ( '⚠️ useClaimableTokens: Missing dependencies, resetting state to zeros' ) ;
4560 setState ( prev => ( {
4661 ...prev ,
@@ -60,34 +75,70 @@ export const useClaimableTokens = () => {
6075 setState ( prev => ( { ...prev , loading : true , error : null } ) ) ;
6176
6277 try {
63- console . log ( '📞 useClaimableTokens: Calling getUnclaimedRewards with params:' , {
64- account : connectedAccount ,
65- peerId : currentBloxPeerId ,
66- poolId : userPoolId
67- } ) ;
68-
69- // Get unclaimed rewards
70- const unclaimedRewards = await contractService . getUnclaimedRewards (
71- connectedAccount ,
72- currentBloxPeerId ,
73- userPoolId
74- ) ;
78+ let unclaimedRewards : any ;
79+ let claimedInfo : any ;
80+
81+ if ( contractService && isReady ) {
82+ // Use contractService when MetaMask is connected
83+ console . log ( '📞 useClaimableTokens: Using contractService (MetaMask connected)' ) ;
84+
85+ unclaimedRewards = await contractService . getUnclaimedRewards (
86+ effectiveAccount ,
87+ currentBloxPeerId ,
88+ userPoolId
89+ ) ;
90+
91+ claimedInfo = await contractService . getClaimedRewardsInfo (
92+ effectiveAccount ,
93+ currentBloxPeerId ,
94+ userPoolId
95+ ) ;
96+ } else if ( useReadOnlyService ) {
97+ // Use RPC provider when MetaMask is not connected
98+ console . log ( '📞 useClaimableTokens: Using RPC provider (manual signature fallback)' ) ;
99+
100+ const chainConfig = getChainConfigByName ( selectedChain ) ;
101+ const readOnlyProvider = new ethers . providers . JsonRpcProvider ( chainConfig . rpcUrl ) ;
102+ const rewardContract = new ethers . Contract (
103+ chainConfig . contracts . rewardEngine ,
104+ REWARD_ENGINE_ABI ,
105+ readOnlyProvider
106+ ) ;
107+
108+ const peerIdBytes32 = await peerIdToBytes32 ( currentBloxPeerId ) ;
109+
110+ // Get unclaimed rewards
111+ const [ unclaimedMining , unclaimedStorage ] = await rewardContract . getUnclaimedRewards (
112+ effectiveAccount ,
113+ peerIdBytes32 ,
114+ userPoolId
115+ ) ;
116+
117+ unclaimedRewards = {
118+ unclaimedMining : ethers . utils . formatEther ( unclaimedMining ) ,
119+ unclaimedStorage : ethers . utils . formatEther ( unclaimedStorage ) ,
120+ totalUnclaimed : ethers . utils . formatEther ( unclaimedMining . add ( unclaimedStorage ) ) ,
121+ } ;
122+
123+ // Get claimed rewards info
124+ const [ lastClaimedTimestamp ] = await rewardContract . getClaimedRewardsInfo (
125+ effectiveAccount ,
126+ peerIdBytes32 ,
127+ userPoolId
128+ ) ;
129+
130+ const now = Math . floor ( Date . now ( ) / 1000 ) ;
131+ const timeSinceLastClaim = Math . max ( 0 , now - lastClaimedTimestamp . toNumber ( ) ) ;
132+
133+ claimedInfo = {
134+ lastClaimedTimestamp : lastClaimedTimestamp . toNumber ( ) ,
135+ timeSinceLastClaim,
136+ } ;
137+ } else {
138+ throw new Error ( 'No service available for fetching rewards' ) ;
139+ }
75140
76141 console . log ( '📥 useClaimableTokens: getUnclaimedRewards response:' , unclaimedRewards ) ;
77-
78- console . log ( '📞 useClaimableTokens: Calling getClaimedRewardsInfo with params:' , {
79- account : connectedAccount ,
80- peerId : currentBloxPeerId ,
81- poolId : userPoolId
82- } ) ;
83-
84- // Get claimed rewards info
85- const claimedInfo = await contractService . getClaimedRewardsInfo (
86- connectedAccount ,
87- currentBloxPeerId ,
88- userPoolId
89- ) ;
90-
91142 console . log ( '📥 useClaimableTokens: getClaimedRewardsInfo response:' , claimedInfo ) ;
92143
93144 const canClaim = parseFloat ( unclaimedRewards . totalUnclaimed ) > 0 ;
@@ -137,7 +188,7 @@ export const useClaimableTokens = () => {
137188 canClaim : false ,
138189 } ) ;
139190 }
140- } , [ contractService , isReady , currentBloxPeerId , connectedAccount , userPoolId ] ) ;
191+ } , [ contractService , isReady , currentBloxPeerId , effectiveAccount , userPoolId , useReadOnlyService , selectedChain ] ) ;
141192
142193 const claimTokens = useCallback ( async ( ) => {
143194 if ( ! contractService || ! isReady || ! currentBloxPeerId || ! userPoolId || ! state . canClaim ) {
0 commit comments