Read address from global state

With smartcontract I have written address to global state with
App.globalPut(global_receiver, Txn.accounts[1]),

Then on the frontend I want to read global variables with JavaScript ussing:
const appInfo = await algodClient.getApplicationByID(APP_ID).do();

I receive an object:

{
“key”: “b3duZXI=”,
“value”: {
“bytes”: “I6Jehut9mkKILqHgKitkQkASAQS3IAQiF11XEvKzt0k=”,
“type”: 1,
“uint”: 0
}
}

I can translate key with base64_decode(“b3duZXI=”) to string: “receiver”
But I dont find the way how to decode value.bytes with type 1 to address string.

I can also read the address with goal within my sandbox:
./sandbox goal app read --global --app-id 5 --guess-format

{
  "owner": {
    "tb": "EORF5BXLPWNEFCBOUHQCUK3EIJABEAIEW4QAIIQXLVLRF4VTW5EQWAA45M",
    "tt": 1
  },
  "receiver": {
    "tb": "XYMH5HAZZVLWKLR3LG7LA2DVTFJMRI4RELRE55UI2YJBWU2F3BC7ZHF5HE",
    "tt": 1
  }
}

The bytes value is base64 encoded bytes, you can convert it to the address you’re looking for by following this section of the docs:

Ben

1 Like

Thank you @Ben. I had Buffer undefined errors when trying to use encoding and decoding functions described in the link you provided.
But finnaly I managed to resolve it.

I am putting my solution how to read base64 address from global state here, if someone will need it later:

      const app_id = 16;
      const accountInfo = await algodClient.getApplicationByID(app_id).do();
      const { key, value } = accountInfo.params["global-state"][1];

      const addressString = algosdk.encodeAddress(
        new Uint8Array(Buffer.from(value.bytes, "base64"))
      );

      console.log(addressString);
1 Like