Skip to content
On this page

Offchain views and signatures โ€‹

Offchain views โ€‹

Another way to deal with offchain with is to use

TZComet. :::

To deal with offchain views in scenario, you can create a contract with a method that takes the storage of the platform and the offchain view params as an argument and reproduce the offchain view computation.

Example:

python
import smartpy as sp
gp = sp.io.import_template("state_channel_games/game_platform.py")

class TestView(sp.Contract):
    def __init__(self, c, f):
        self.c = c
        self.f = f.f
        self.init(result = sp.none)

    @sp.entrypoint
    def compute(self, data, params):
        self.c.data = data
        b = sp.bind_block()
        with b:
            self.f(self.c, params)
        self.data.result = sp.some(b.value)

if "templates" not in __name__:
    sc = sp.test_scenario()
    sc.table_of_contents()
    platform_addr = sp.address("KT1V8pmoHLq6obLb3q7D6XfordUA5G1K7xkb")
    c1 = gp.GamePlatform(admins = sp.set([admin.address]), self_addr = platform_addr)
    c1.address = platform_addr

    sc.h2("Offchain new_game")
    offchain_new_game = TestView(c1, c1.offchain_new_game)
    sc += offchain_new_game

    sc.h2("Offchain play")
    offchain_play = TestView(c1, c1.offchain_play)
    sc += offchain_play

    constants = #...
    new_game_signatures = # ...

    offchain_new_game.compute(sp.record(data = c1.data, params = sp.record(constants = constants, params = sp.pack(sp.unit), signatures = new_game_signatures))).run(sender = player1.address)
    game = sc.compute(offchain_new_game.data.result.open_some())

Signatures โ€‹

All signatures can be done in scenario by putting the secret key of the signer.

That's useful for tests.

A complete example is available here: https://legacy.smartpy.io/ide?template=state_channel_games/tests/test_game_platform_testnet.py