1# Some Syntax - Example for illustrative purposes only.
2
3# This contract here is simply to show a few syntax constructions.
4
5import smartpy as sp
6
7
8@sp.module
9def main():
10 class SyntaxDemo(sp.Contract):
11 def __init__(self, b, s, h, i):
12 self.private.bb = s
13 self.data.b = b
14 self.data.s = s
15 self.data.h = h
16 self.data.i = i
17 self.data.pkh = sp.key_hash("tz1YB12JHVHw9GbN66wyfakGYgdTBvokmXQk")
18 self.data.n = sp.nat(123)
19 self.data.m = {}
20 self.data.l = sp.cast(set(), sp.set[sp.int])
21 self.data.aaa = {1, 2, 3}
22 self.data.abc = [sp.Some(123), None]
23 self.data.abca = {0: sp.Some(123), 1: None}
24 self.data.ddd = range(0, 10)
25 self.data.acb = "toto"
26
27 @sp.entrypoint
28 def comparisons(self):
29 assert self.data.i <= 123
30 assert 2 + self.data.i == 12
31 assert 2 + self.data.i != 1234
32 assert self.data.i + 4 != 123
33 assert self.data.i - 5 < 123
34 assert 7 - self.data.i < 123
35 assert self.data.i > 3
36 assert 4 * self.data.i > 3
37 assert self.data.i * 5 > 3
38 assert self.data.i >= 3
39 assert 3 < self.data.i
40 assert 3 <= self.data.i
41 assert 3000 > self.data.i
42 assert 3000 >= self.data.i
43 assert self.data.b and True
44 if 3 < self.data.i:
45 assert 4 < self.data.i
46
47 @sp.entrypoint
48 def someComputations(self, params):
49 assert self.data.i <= 123
50 self.data.i = self.data.i + params.y
51 self.data.acb = params.x
52 self.data.i = 100
53 self.data.i = self.data.i - 1
54 assert sp.add(sp.nat(4), sp.nat(5)) == 9
55 assert sp.add(sp.int(-4), sp.nat(5)) == 1
56 assert sp.add(sp.nat(5), sp.int(-4)) == 1
57 assert sp.add(sp.int(-4), sp.int(5)) == 1
58 assert sp.mul(sp.nat(4), sp.nat(5)) == 20
59 assert sp.mul(sp.int(-4), sp.nat(5)) == -20
60 assert sp.mul(sp.nat(5), sp.int(-4)) == -20
61 assert sp.mul(sp.int(-4), sp.int(5)) == -20
62
63 @sp.entrypoint
64 def localVariable(self):
65 x = self.data.i
66 x *= 2
67 self.data.i = 10
68 self.data.i = x
69
70 @sp.entrypoint
71 def iterations(self):
72 x = self.data.i
73 x = 0
74 x = 1
75 x = 2
76 x = 3
77 x = 4
78
79 for i in range(0, 5):
80 x = i
81 self.data.i = sp.to_int(self.data.n)
82 self.data.i = 5
83 while self.data.i <= 42:
84 self.data.i += 2
85 if self.data.i <= 123:
86 x = 12
87 self.data.i += x
88 else:
89 x = 5
90 self.data.i = x
91
92 @sp.entrypoint
93 def myMessageName4(self):
94 for x in self.data.m.items():
95 self.data.i += x.key * x.value
96
97 @sp.entrypoint
98 def myMessageName5(self):
99 for x in self.data.m.keys():
100 self.data.i += 2 * x
101
102 @sp.entrypoint
103 def myMessageName6(self, params):
104 sp.cast(params, sp.int) # FIXME This should be inferred.
105 for x in self.data.m.values():
106 self.data.i += 3 * x
107 self.data.aaa.remove(2)
108 self.data.aaa.add(12)
109 self.data.abc.push(sp.Some(16))
110 self.data.abca[0] = sp.Some(16)
111 if self.data.aaa.contains(12):
112 self.data.m[42] = 43
113 self.data.aaa.add(sp.as_nat(-params))
114
115
116# Tests
117@sp.add_test()
118def test():
119 scenario = sp.test_scenario("Syntax", main)
120 # define a contract
121 c1 = main.SyntaxDemo(b=True, s="abc", h=sp.bytes("0x0000ab112233aa"), i=7)
122 # show its representation
123 scenario.h1("Syntax")
124 scenario.h2("Contract")
125 scenario += c1
126 scenario.h2("Message execution")
127 c1.comparisons(_valid=False)
128 scenario.h2("Message execution")
129 c1.someComputations(x="abcd", y=12)
130 scenario.h2("Message execution")
131 c1.localVariable()
132 c1.iterations()
133 scenario.h2("Message execution")
134 c1.myMessageName6(-18)
135 scenario.verify(c1.data.b)
136 scenario.verify(c1.data.i == 55)
137 scenario.show(c1.data)
138 scenario.show(c1.data, html=False)
139 scenario.verify_equal(
140 c1.data,
141 sp.record(
142 acb="abcd",
143 abc=[sp.Some(16), sp.Some(123), None],
144 abca=sp.scenario_utils.vector([sp.Some(16), None]),
145 b=True,
146 h=sp.bytes("0x0000ab112233aa"),
147 i=55,
148 l=sp.set(),
149 n=123,
150 s="abc",
151 aaa=sp.set([1, 3, 12, 18]),
152 m={42: 43},
153 ddd=sp.list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
154 pkh=sp.key_hash("tz1YB12JHVHw9GbN66wyfakGYgdTBvokmXQk"),
155 ),
156 )
157 scenario.simulation(c1)