templates.welcome

 1import smartpy as sp
 2
 3# This is the SmartPy editor.
 4# You can experiment with SmartPy by loading a template.
 5# (in the Commands menu above this editor)
 6#
 7# A typical SmartPy program has the following form:
 8
 9
10# A SmartPy module
11@sp.module
12def main():
13    # A class of contracts
14    class MyContract(sp.Contract):
15        def __init__(self, myParameter1, myParameter2):
16            self.data.myParameter1 = myParameter1
17            self.data.myParameter2 = myParameter2
18
19        # An entrypoint, i.e., a message receiver
20        # (contracts react to messages)
21        @sp.entrypoint
22        def myEntryPoint(self, params):
23            assert self.data.myParameter1 <= 123
24            self.data.myParameter1 += params
25
26
27# Tests
28@sp.add_test()
29def test():
30    # We define a test scenario, together with some outputs and checks
31    # The scenario takes the module as a parameter
32    scenario = sp.test_scenario("Welcome", main)
33    scenario.h1("Welcome")
34
35    # We first define a contract and add it to the scenario
36    c1 = main.MyContract(12, 123)
37    scenario += c1
38
39    # And call some of its entrypoints
40    c1.myEntryPoint(12)
41    c1.myEntryPoint(13)
42    c1.myEntryPoint(14)
43    c1.myEntryPoint(50)
44    c1.myEntryPoint(50)
45    c1.myEntryPoint(50, _valid=False)  # this is expected to fail
46    # Finally, we check its final storage
47    scenario.verify(c1.data.myParameter1 == 151)
48
49    # We can define another contract using the current state of c1
50    c2 = main.MyContract(1, c1.data.myParameter1)
51    scenario += c2
52    scenario.verify(c2.data.myParameter2 == 151)