@@ -132,29 +132,44 @@ async function main() {
132
132
133
133
// example: APP_READ_STATE
134
134
const appInfo = await algodClient . getApplicationByID ( appId ) . do ( ) ;
135
- const globalState = appInfo . params . globalState [ 0 ] ;
136
- console . log ( `Raw global state - ${ algosdk . stringifyJSON ( globalState ) } ` ) ;
135
+ if ( ! appInfo . params . globalState || appInfo . params . globalState . length === 0 ) {
136
+ throw new Error ( 'Global state not present' ) ;
137
+ }
138
+ const { globalState } = appInfo . params ;
139
+ console . log (
140
+ `Raw global state - ${ globalState . map ( ( kv ) => algosdk . encodeJSON ( kv ) ) } `
141
+ ) ;
137
142
138
- // decode b64 string key with Buffer
139
- const globalKey = algosdk . base64ToString ( globalState . key ) ;
143
+ const globalKey = algosdk . base64ToBytes ( globalState [ 0 ] . key ) ;
140
144
// show global value
141
- const globalValue = globalState . value . bytes ;
145
+ const globalValue = algosdk . base64ToBytes ( globalState [ 0 ] . value . bytes ) ;
142
146
143
- console . log ( `Decoded global state - ${ globalKey } : ${ globalValue } ` ) ;
147
+ console . log (
148
+ `Decoded global state - ${ algosdk . bytesToBase64 ( globalKey ) } : ${ algosdk . bytesToBase64 ( globalValue ) } `
149
+ ) ;
144
150
145
151
const accountAppInfo = await algodClient
146
152
. accountApplicationInformation ( caller . addr , appId )
147
153
. do ( ) ;
154
+ if (
155
+ ! accountAppInfo . appLocalState ||
156
+ ! accountAppInfo . appLocalState . keyValue ||
157
+ accountAppInfo . appLocalState . keyValue . length === 0
158
+ ) {
159
+ throw new Error ( 'Local state values not present' ) ;
160
+ }
161
+ const localState = accountAppInfo . appLocalState . keyValue ;
162
+ console . log (
163
+ `Raw local state - ${ localState . map ( ( kv ) => algosdk . encodeJSON ( kv ) ) } `
164
+ ) ;
148
165
149
- const localState = accountAppInfo . appLocalState . keyValue [ 0 ] ;
150
- console . log ( `Raw local state - ${ algosdk . stringifyJSON ( localState ) } ` ) ;
151
-
152
- // decode b64 string key with Buffer
153
- const localKey = algosdk . base64ToString ( localState . key ) ;
166
+ const localKey = algosdk . base64ToBytes ( localState [ 0 ] . key ) ;
154
167
// get uint value directly
155
- const localValue = localState . value . uint ;
168
+ const localValue = localState [ 0 ] . value . uint ;
156
169
157
- console . log ( `Decoded local state - ${ localKey } : ${ localValue } ` ) ;
170
+ console . log (
171
+ `Decoded local state - ${ algosdk . bytesToBase64 ( localKey ) } : ${ localValue } `
172
+ ) ;
158
173
// example: APP_READ_STATE
159
174
160
175
// example: APP_CLOSEOUT
0 commit comments