Fetching global state via PythonSDK


  1. 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:

Code to read app global state

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'])
  1. 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?

Hi @boomerang, I just paste here a snippet I wrote some time ago.

Try it and see if it works as example for your code.

app_id = 0  #FIXME

app = indexer_client.applications(app_id)

app_global_state = app['application']['params']['global-state']

print(json.dumps(app_global_state, indent=4))

HI @cusma , Im till getting the same error.

Here’s the screenshot of the entire application object, the global-state isn’t getting formed.

Just to be sure that there is not compilation issues for the teal code, this is the code for compilation right?

def compile_program(client, source_file):
	print("\n------------------- Compiler Program -------------------")
	source_code = open(source_file, 'r').read()
	compile_response = client.compile(source_code)
	program_str = compile_response['result']
	program = base64.b64decode(program_str)
	return program

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?