templates.layout

 1# Data Layout - Example for illustrative purposes only.
 2
 3import smartpy as sp
 4
 5
 6@sp.module
 7def main():
 8    class TestLayout(sp.Contract):
 9        def __init__(self):
10            self.data.a = 1
11            self.data.b = 12
12            sp.cast(self.data, sp.record(a=sp.int, b=sp.nat).layout(("b", "a")))
13            # self.init_entrypoints_layout(((("run_record", "run_record_2"), "run_type_record"), ("run_variant", "run_type_variant")))
14
15        @sp.entrypoint
16        def run_record(self, params):
17            sp.cast(
18                params,
19                sp.record(a=sp.int, b=sp.string, c=sp.bool).layout(("b", ("a", "c"))),
20            )
21
22        # @sp.entrypoint
23        # def run_record_2(self, params):
24        #     sp.cast(params, sp.record(a = sp.int, b = sp.string, c = sp.bool, d = sp.nat).right_comb())
25
26        @sp.entrypoint
27        def run_variant(self, params):
28            sp.cast(
29                params,
30                sp.variant(a=sp.int, b=sp.string, c=sp.bool).layout(("b", ("a", "c"))),
31            )
32
33        @sp.entrypoint
34        def run_type_record(self, params):
35            sp.cast(params, sp.record(a=sp.int, b=sp.string).layout(("b", "a")))
36
37        @sp.entrypoint
38        def run_type_variant(self, params):
39            sp.cast(params, sp.variant(e=sp.int, d=sp.string).layout(("e", "d")))
40
41
42@sp.add_test()
43def test():
44    scenario = sp.test_scenario("Layout", main)
45    scenario.h1("Layout")
46    scenario += main.TestLayout()