Your First Intelligent Contract
An Intelligent Contract in GenLayer can contain non-deterministic blocks: regular Python functions with no arguments that may return arbitrary values and must be invoked through gl.eq_principle.*. For blockchain integrity reasons, non-determinism must be contained within these special non-deterministic blocks, and the blocks have two important limitations:
- Storage is inaccessible from non-deterministic blocks
- State of the Python interpreter is not passed back to the deterministic code (for instance, you won't see changes in global variables)
Simple Case
To illustrate how non-deterministic blocks work, this example gets a webpage as plain HTML and checks whether the lowercase text iana appears anywhere in it. This is a simple content check: it does not look for a link, and it does not check where a link points or who owns the page:
example_web_address = 'https://example.org'
def my_non_deterministic_block():
web_data = gl.nondet.web.render(example_web_address, mode='html')
return 'iana' in web_data
print(gl.eq_principle.strict_eq(my_non_deterministic_block))Here are a few important parts:
- It is mandatory to call
gl.nondet.web.render(orgl.nondet.web.get) from a function invoked viagl.eq_principle.*, otherwise it will give an error - Type annotations are not required
example_web_addressgets captured without the need to specify it explicitly- We are getting the page in plain HTML, so the check runs on the page's markup and not only on its visible text.
'iana' in web_datais a plain, case-sensitive substring test: it matchesianaanywhere in the returned HTML (visible text, an attribute value such as anhref, and so on). It does not parse<a>elements or validate theirhrefvalues - We are using
gl.eq_principle.strict_eqbecause we return abool(TrueorFalse), so there is no need to run LLMs or other complex computations: validators will agree if they both get the exactly the same result, which makes sense for our case. However, if you wish to return more complex data (such as a text summary), you should use othergl.eq_principlemethods. More on this topic on the next page
As a Full Contract
# { "Depends": "py-genlayer:1jb45aa8ynh2a9c9xn3b7qqh8sm5q93hwfp7jqmwsfhh8jpz09h6" }
from genlayer import *
class Contract(gl.Contract):
had_iana: bool
def __init__(self):
example_web_address = 'https://example.org'
def my_non_deterministic_block():
web_data = gl.nondet.web.render(example_web_address, mode='html')
return 'iana' in web_data
self.had_iana = gl.eq_principle.strict_eq(my_non_deterministic_block)