11import * as algokit from '@algorandfoundation/algokit-utils'
2- import { TransactionSignerAccount } from '@algorandfoundation/algokit-utils/types/account'
3- import { AppDetails } from '@algorandfoundation/algokit-utils/types/app-client'
4- import { useWallet } from '@txnlab/use-wallet'
2+ import { useWallet } from '@txnlab/use-wallet-react'
53import { useSnackbar } from 'notistack'
64import { useEffect , useState } from 'react'
75import { useNavigate } from 'react-router-dom'
@@ -21,7 +19,9 @@ const ViewSavings = () => {
2119 const [ avgDeposit , setAvgDeposit ] = useState < number | null > ( null )
2220 const [ loading , setLoading ] = useState ( true )
2321
24- const { activeAddress, signer } = useWallet ( )
22+ const { activeAccount } = useWallet ( )
23+ const activeAddress = activeAccount ?. address || null
24+
2525 const { enqueueSnackbar } = useSnackbar ( )
2626 const navigate = useNavigate ( )
2727
@@ -32,31 +32,42 @@ const ViewSavings = () => {
3232 token : algodConfig . token ,
3333 } )
3434
35- const deployedAppID = 740800501
36-
37- const getAppDetails = ( ) : AppDetails => ( {
38- resolveBy : 'id' ,
39- sender : { signer, addr : activeAddress ! } as TransactionSignerAccount ,
40- id : deployedAppID ,
41- } )
35+ const deployedAppID = 740800501n
4236
4337 const fetchSavingsData = async ( ) => {
4438 try {
45- const appClient = new SaverClient ( getAppDetails ( ) , algodClient )
46- const localState = await appClient . getLocalState ( activeAddress ! )
47-
48- setGoal ( localState . userGoal ?. asNumber ( ) ?? 0 )
49- setBalance ( localState . userBalance ?. asNumber ( ) ?? 0 )
50- setWithdrawCount ( localState . userWithdrawCount ?. asNumber ( ) ?? 0 )
51- setTotalSaved ( localState . totalSaved ?. asNumber ( ) ?? 0 )
52- setLastGoal ( localState . lastUserGoal ?. asNumber ( ) ?? 0 )
53- setLastWithdrawn ( localState . lastWithdrawn ?. asNumber ( ) ?? 0 )
54- setLastWithdrawTime ( localState . lastWithdrawTime ?. asNumber ( ) ?? 0 )
55- setHighestGoal ( localState . highestGoalAchieved ?. asNumber ( ) ?? 0 )
56- setAvgDeposit ( localState . averageDepositAmount ?. asNumber ( ) ?? 0 )
39+ console . log ( '[fetchSavingsData] Active address:' , activeAddress )
40+
41+ const accountInfo = await algodClient . accountInformation ( activeAddress ! ) . do ( )
42+ console . log ( '[accountInfo]' , accountInfo )
43+
44+ const appLocalState = accountInfo . appsLocalState ?. find ( ( app ) => app . id === deployedAppID )
45+ console . log ( '[appLocalState]' , appLocalState )
46+
47+ const keyValue = appLocalState ?. keyValue || [ ]
48+ console . log ( '[keyValue]' , keyValue )
49+
50+ const getUintValue = ( key : string ) => {
51+ const entry = keyValue . find ( ( kv : any ) => {
52+ const decodedKey = Buffer . from ( kv . key , 'base64' ) . toString ( )
53+ return decodedKey === key
54+ } )
55+ console . log ( `[${ key } ]` , entry )
56+ return entry ? Number ( entry . value . uint ) : 0
57+ }
58+
59+ setGoal ( getUintValue ( 'user_goal' ) )
60+ setBalance ( getUintValue ( 'user_balance' ) )
61+ setWithdrawCount ( getUintValue ( 'user_withdraw_count' ) )
62+ setTotalSaved ( getUintValue ( 'total_saved' ) )
63+ setLastGoal ( getUintValue ( 'last_user_goal' ) )
64+ setLastWithdrawn ( getUintValue ( 'last_withdrawn' ) )
65+ setLastWithdrawTime ( getUintValue ( 'last_withdraw_time' ) )
66+ setHighestGoal ( getUintValue ( 'highest_goal_achieved' ) )
67+ setAvgDeposit ( getUintValue ( 'average_deposit_amount' ) )
5768 } catch ( error ) {
5869 enqueueSnackbar ( 'Error fetching savings data.' , { variant : 'error' } )
59- console . error ( error )
70+ console . error ( '[fetchSavingsData] Error:' , error )
6071 } finally {
6172 setLoading ( false )
6273 }
0 commit comments