11import * as algokit from '@algorandfoundation/algokit-utils'
22import { TransactionSignerAccount } from '@algorandfoundation/algokit-utils/types/account'
33import { AppDetails } from '@algorandfoundation/algokit-utils/types/app-client'
4- import { useWallet } from '@txnlab/use-wallet'
4+ import { useWallet } from '@txnlab/use-wallet-react '
55import { useSnackbar } from 'notistack'
66import { useEffect , useState } from 'react'
77import { useNavigate } from 'react-router-dom'
@@ -23,58 +23,76 @@ const StartSaver = ({ openModal, setModalState }: AppCallsInterface) => {
2323
2424 const navigate = useNavigate ( )
2525 const { enqueueSnackbar } = useSnackbar ( )
26- const { signer, activeAddress } = useWallet ( )
26+ const { activeAddress, transactionSigner } = useWallet ( )
27+ const signer = transactionSigner
2728
28- const deployedAppID = 740800501
29+ const deployedAppID = 740800501n
2930 const appAddress = 'NSBKY7G5NNYVF2BKIJL6MAJ4QW6PZSDFMGNQ3EDULAKHBWX2MPK6YZLS2A'
3031 const algodClient = algokit . getAlgoClient ( getAlgodConfigFromViteEnvironment ( ) )
3132
32- const getAppDetails = ( appId : number ) : AppDetails => ( {
33- resolveBy : 'id' ,
34- sender : { signer, addr : activeAddress } as TransactionSignerAccount ,
35- id : appId ,
36- } )
33+ const getAppDetails = ( appId : bigint ) : AppDetails => {
34+ console . log ( '[AppDetails] Active address:' , activeAddress )
35+ return {
36+ resolveBy : 'id' ,
37+ sender : { signer, addr : activeAddress ! } as TransactionSignerAccount ,
38+ id : Number ( appId ) ,
39+ }
40+ }
3741
3842 const showSnackbar = ( message : string , variant : 'success' | 'error' ) => {
3943 enqueueSnackbar ( message , { variant } )
4044 }
4145
4246 const refreshState = async ( ) => {
4347 if ( ! activeAddress ) return
44-
45- const appClient = new SaverClient ( getAppDetails ( deployedAppID ) , algodClient )
4648 try {
47- const local = await appClient . getLocalState ( activeAddress )
49+ const accountInfo = await algodClient . accountInformation ( activeAddress ) . do ( )
50+ const appLocalState = accountInfo . appsLocalState ?. find ( ( app ) => app . id === deployedAppID )
51+ const keyValue = appLocalState ?. keyValue || [ ]
52+
53+ const getUintValue = ( key : string ) => {
54+ const entry = keyValue . find ( ( kv : any ) => {
55+ const decodedKey = Buffer . from ( kv . key , 'base64' ) . toString ( )
56+ return decodedKey === key
57+ } )
58+ return entry ? Number ( entry . value . uint ) : 0
59+ }
60+
4861 setAlreadyOptedIn ( true )
49- setCurrentGoal ( local . userGoal ?. asNumber ( ) || null )
50- setCurrentBalance ( local . userBalance ?. asNumber ( ) || null )
51- } catch {
62+ setCurrentGoal ( getUintValue ( 'user_goal' ) )
63+ setCurrentBalance ( getUintValue ( 'user_balance' ) )
64+ } catch ( err ) {
65+ console . error ( '[refreshState] Failed to get local state:' , err )
5266 setAlreadyOptedIn ( false )
5367 setCurrentGoal ( null )
5468 setCurrentBalance ( null )
5569 }
5670 }
5771
5872 useEffect ( ( ) => {
73+ console . log ( '[useEffect] activeAddress changed:' , activeAddress )
5974 refreshState ( )
6075 } , [ activeAddress ] )
6176
6277 const sendAppCall = async ( ) => {
78+ if ( ! activeAddress ) return showSnackbar ( 'Connect your wallet first.' , 'error' )
6379 setLoading ( true )
6480 try {
6581 const appClient = new SaverClient ( getAppDetails ( deployedAppID ) , algodClient )
6682 const optInResponse = await appClient . optIn
6783 await optInResponse . bare ( )
6884 showSnackbar ( 'Successfully opted into the app!' , 'success' )
6985 await refreshState ( )
70- } catch {
86+ } catch ( err ) {
87+ console . error ( '[sendAppCall] Error:' , err )
7188 showSnackbar ( 'Error during opt-in.' , 'error' )
7289 } finally {
7390 setLoading ( false )
7491 }
7592 }
7693
7794 const sendGoalTxn = async ( ) => {
95+ if ( ! activeAddress ) return showSnackbar ( 'Connect your wallet first.' , 'error' )
7896 setLoading ( true )
7997 try {
8098 const goalAmount = parseInt ( goalInput , 10 )
@@ -87,14 +105,16 @@ const StartSaver = ({ openModal, setModalState }: AppCallsInterface) => {
87105 await appClient . createSaverJar ( { goalAmount : microAlgos } )
88106 showSnackbar ( 'Savings goal set!' , 'success' )
89107 await refreshState ( )
90- } catch {
108+ } catch ( err ) {
109+ console . error ( '[sendGoalTxn] Error:' , err )
91110 showSnackbar ( 'Error setting goal.' , 'error' )
92111 } finally {
93112 setLoading ( false )
94113 }
95114 }
96115
97116 const depositFunds = async ( ) => {
117+ if ( ! activeAddress ) return showSnackbar ( 'Connect your wallet first.' , 'error' )
98118 setLoading ( true )
99119 try {
100120 const amount = parseInt ( depositInput , 10 )
@@ -105,45 +125,47 @@ const StartSaver = ({ openModal, setModalState }: AppCallsInterface) => {
105125 const appClient = new SaverClient ( getAppDetails ( deployedAppID ) , algodClient )
106126 const paymentTxn = await algokit . transferAlgos (
107127 {
108- from : { addr : activeAddress || '' , signer } ,
128+ from : { addr : activeAddress , signer } ,
109129 to : appAddress ,
110130 amount : algokit . algos ( amount ) ,
111131 skipSending : true ,
112132 } ,
113133 algodClient ,
114134 )
115- await appClient . deposit ( { depositTxn : paymentTxn . transaction } , { sender : { addr : activeAddress ! , signer } } )
135+ await appClient . deposit ( { depositTxn : paymentTxn . transaction } , { sender : { addr : activeAddress , signer } } )
116136 showSnackbar ( 'Deposit successful!' , 'success' )
117137 await refreshState ( )
118- } catch {
138+ } catch ( err ) {
139+ console . error ( '[depositFunds] Error:' , err )
119140 showSnackbar ( 'Error during deposit.' , 'error' )
120141 } finally {
121142 setLoading ( false )
122143 }
123144 }
124145
125146 const withdrawFunds = async ( ) => {
147+ if ( ! activeAddress ) return showSnackbar ( 'Connect your wallet first.' , 'error' )
126148 setLoading ( true )
127149 try {
128150 const appClient = new SaverClient ( getAppDetails ( deployedAppID ) , algodClient )
129151 await appClient . withdraw (
130152 { } ,
131153 {
132- sender : { addr : activeAddress || '' , signer } ,
154+ sender : { addr : activeAddress , signer } ,
133155 sendParams : { fee : algokit . microAlgos ( 2000 ) } ,
134156 } ,
135157 )
136158 showSnackbar ( 'Withdrawal complete!' , 'success' )
137159 await refreshState ( )
138- } catch {
160+ } catch ( err ) {
161+ console . error ( '[withdrawFunds] Error:' , err )
139162 showSnackbar ( 'Error during withdrawal.' , 'error' )
140163 } finally {
141164 setLoading ( false )
142165 }
143166 }
144167
145168 const progressPercent = currentGoal && currentBalance ? Math . min ( Math . round ( ( currentBalance / currentGoal ) * 100 ) , 100 ) : 0
146-
147169 const progressColor = progressPercent >= 100 ? 'bg-green-500' : 'bg-blue-400'
148170
149171 return (
0 commit comments