I am following the docs to build stateful smart contracts . When trying to to fetch and read global state of the smart contract using the method mentioned in the docs, it is giving me an error.
When looking at the app-details, i see that there is no global-state.
The approval program and clear program are exactly as given in the docs.
Any advice on how to fetch the global state?
The screenshot of error:
def read_global_state(client, addr, app_id):
print("\n--------------------- Global State ---------------------")
results = client.account_info(addr)
apps_created = results['created-apps']
for app in apps_created :
if app['id'] == app_id :
print(f"global_state for app_id {app_id}: ", app['params']['global-state'])
In the global state, can the Value in the key value pair be strings? If yes, is there any limit on the number of strings length of string that is stored in the value?
Seems you correctly declared a global state schema that makes use of 1 integer global variable. You should initialise or assign a value to that integer in order to fetch that global variable.
Did you already assign a key/value pair to that integer?
Hi @cusma ,
The approval teal code does make use of the key/value pair called “counter” and increases it on every NoOp call. So i also made a NoOp Call to the contract thinking that it might initialize it.
I’ve used exactly the same approval code mentioned at the end of the docs under Approval Program Walkthrough.
I’m not sure about the initial assignment for key/value pairs though. Is there any step i am missing out?
Traceback (most recent call last):
File "/path/to/simple_counter_smart_contract/deploy.py", line 96, in <module>
init()
File "/path/to/simple_counter_smart_contract/deploy.py", line 87, in init
read_global_state(
File "/path/to/simple_counter_smart_contract/helpers.py", line 170, in read_global_state
return format_state(app["params"]["global-state"])
KeyError: 'global-state'
My app deploys, however it is unable to retrieve the global-state parameter.
Note: I created a repository that is a version of this tutorial, except I have been moving code into separate modules. You can see my code here