How I can convert value of Global State to human-readable

Hi everybody,

I wanna push data (string type) to store in Global State of Smart Contract.

This my TEAL code:

#pragma version 2
byte "myKey"
txna ApplicationArgs 0
app_global_put
int 1
return

“appArgs” is required Uint8Array so I convert my string data and then create the transaction.

let appArgs = [];  
appArgs[0] = stringToUint8Array('This is my data');
let txn = algosdk.makeApplicationCreateTxn(recoveredAccount.addr, suggestedParams, 0,
            approvalProgByteArray, clearProgByteArray, localInts, localBytes,
            globalInts, globalBytes, appArgs);

I used this function to read Global State data

async function readGlobalState(client, account, index){
    let accountInfoResponse = await client.accountInformation(account.addr).do();
    for (let i = 0; i < accountInfoResponse['created-apps'].length; i++) { 
        if (accountInfoResponse['created-apps'][i].id == index) {
            console.log("Application's global state:");
            for (let n = 0; n < accountInfoResponse['created-apps'][i]['params']['global-state'].length; n++) {
                console.log(accountInfoResponse['created-apps'][i]['params']['global-state'][n]);
            }
        }
    }
}

I got the result and I tried to convert it to human-readable but I can’t.

{
  key: 'bXlLZXk=',
  value: {
    bytes: 'AAAAAAANCgo=',
    type: 1,
    uint: 0
  }
}

Could someone help me, please?
Thanks!

Can you try something similar to:

console.log("Converted String:",Buffer.from(transactionResponse['global-state-delta'][0].key, "base64").toString());

In that example, I am just converting the key but you should be able to do the value as well. Also note that I am only doing the first global.

The problem is that ‘AAAAAAANCgo=’ doesn’t correspont to the base64 of ‘This is my data’.
The base64 form of ‘This is my data’ is ‘77+5VGhpcyBpcyBteSBkYXRh’

I got this to work using:

async function readGlobalState(client, account, index){
    let accountInfoResponse = await client.accountInformation(account.addr).do();
    for (let i = 0; i < accountInfoResponse['created-apps'].length; i++) { 
        if (accountInfoResponse['created-apps'][i].id == index) {
            console.log("Application's global state:");
            for (let n = 0; n < accountInfoResponse['created-apps'][i]['params']['global-state'].length; n++) {
                const gs = accountInfoResponse['created-apps'][i]['params']['global-state'][n]
                console.log("Key: " + Buffer.from(gs.key, "base64").toString());
                console.log("Value: " + Buffer.from(gs.value.bytes, "base64").toString());
            }
        }
    }

and I set it using this code:

    let appArgs = [];
    const enc = new TextEncoder(); // always utf-8
    appArgs[0] = enc.encode("This is my data");

Thank Jason. This solution work for me.