Build with GenLayer
Your First Intelligent Contract

Your First Intelligent Contract

Now is the time to utilize all the power of GenLayer!

For blockchain integrity reasons, non-determinism must be contained within special non-deterministic blocks. Such blocks are regular Python functions with no arguments, which can return arbitrary values. However, there are some 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 it works, let's get a webpage as plain HTML, and verify that it has a link to the owner:

example_web_address = 'https://example.org'
def my_non_deterministic_block():
    web_data = gl.get_webpage(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:

  1. It is mandatory to call gl.get_webpage from a function invoked via gl.eq_principle_*, otherwise it will give an error
  2. Type annotations are not required
  3. example_web_address gets captured without the need to specify it explicitly
  4. We are getting the page in plain HTML, because we want text from a link (<a> HTML tag), which is not visible in the text representation
  5. We are using eq_principle_strict_eq because we return a bool (True or False), 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 other eq_principle. More on this topic on the next page

As a Full Contract

# { "Depends": "py-genlayer:test" }
 
from genlayer import *
 
@gl.contract
class Contract:
    had_iana: bool
 
    def __init__(self):
        example_web_address = 'https://example.org'
        def my_non_deterministic_block():
            web_data = gl.get_webpage(example_web_address, mode='html')
            return 'iana' in web_data
        self.had_iana = gl.eq_principle_strict_eq(my_non_deterministic_block)