Fetching global state via PythonSDK

Hi,

  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?
1 Like

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))
2 Likes

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?

1 Like

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?

I am experiencing the same problem:

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

I also created an issue here

I’ve answered directly in the issue:

In short, the issue is the same as @cusma mentioned above: Fetching global state via PythonSDK - #4 by cusma
The TEAL code is not assigning any global state, so the global state key is not present.

For the sake of completeness, here is my full answer:


The issue is that your approval code is actually doing nothing as it just returns Int(1):

You need to return program instead.

Since the code does nothing, it actually does not create anything in the global state, in which case the "global-state" key is not present.

You can easily confirm it using the sandbox (assuming your app id is 1):

$ ./sandbox goal app read --global --app-id 1
null%

Thanks for the clarification @fabrice. I previously misunderstood the purpose of the return statement