1import smartpy as sp
2
3
4@sp.module
5def main():
6 class B(sp.Contract):
7 def __init__(self):
8 self.data.n = 0
9
10 @sp.entrypoint
11 def b(self):
12 self.data.n += 1
13 assert self.data.n < 2, ">= 2"
14
15 @sp.entrypoint
16 def fail(self):
17 raise (
18 "This is a very long error message with a looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong word."
19 )
20
21 @sp.entrypoint
22 def do_nothing(self):
23 pass
24
25 class A(sp.Contract):
26 def __init__(self):
27 self.data.x = None
28 self.data.y = None
29 self.data.created = sp.cast(None, sp.option[sp.address])
30 self.data.z = ""
31 self.data.m = {
32 sp.record(a="1", b="2"): sp.record(c="3", d=4),
33 sp.record(a="2", b="3"): sp.record(c="a", d=42),
34 }
35
36 @sp.entrypoint
37 def call_b(self, address):
38 contract = sp.contract(sp.unit, address, entrypoint="b")
39 sp.transfer((), sp.balance, contract.unwrap_some())
40
41 @sp.entrypoint
42 def emit(self, name):
43 self.data.z += ", " + name
44 sp.emit(name)
45 sp.emit(name, with_type=False)
46 sp.emit(name, tag="mytag")
47 sp.emit(sp.record(name=name, b="XYZ"), tag="mytag2")
48 sp.emit(sp.record(name=name, b="XYZ"), tag="mytag2", with_type=False)
49
50 @sp.entrypoint
51 def set_delegate(self, delegator):
52 sp.set_delegate(delegator)
53 sp.set_delegate(None)
54 sp.set_delegate(delegator)
55
56 @sp.entrypoint
57 def create_contract(self, delegator, amount, data):
58 self.data.created = sp.Some(sp.create_contract(B, delegator, amount, data))
59
60 @sp.entrypoint
61 def withdraw(self, amount):
62 sp.send(sp.sender, amount)
63 contract = sp.contract(sp.string, sp.sender, entrypoint="default")
64 sp.transfer("Test", sp.tez(0), contract.unwrap_some())
65
66
67if "templates" not in __name__:
68
69 @sp.add_test()
70 def test():
71 baker = sp.test_account("baker")
72 baker2 = sp.test_account("baker2")
73 voting_powers = {
74 baker.public_key_hash: 42,
75 baker2.public_key_hash: 10,
76 }
77 alice = sp.test_account("alice")
78 sc = sp.test_scenario("MyContract", main)
79 sc.h1("h1")
80 sc.h2("h2")
81 sc.h3("h3")
82 sc.h4("h4")
83 sc.p("p")
84
85 sc.h2("Origination A")
86 c1 = main.A()
87 c1.set_initial_balance(sp.tez(5))
88 c1.data.z = "Hello"
89 sc += c1
90 sc.verify(c1.data.z == "Hello")
91
92 sc.h2("Origination B")
93 c2 = main.B()
94 sc += c2
95
96 sc.h2("Call b")
97 c1.call_b(c2.address, _amount=sp.tez(5))
98 c1.call_b(c2.address, _amount=sp.tez(1), _valid=False)
99
100 sc.h2("Set delegate")
101 c1.set_delegate(sp.Some(baker.public_key_hash), _voting_powers=voting_powers)
102
103 sc.h2("Show values")
104 sc.show("test")
105 sc.show(c1.data)
106 sc.show(alice)
107
108 sc.h2("Emit")
109 c1.emit("alice", _sender=alice, _now=sp.timestamp(500), _voting_powers={})
110
111 sc.h2("Transfer to tz address")
112 c1.withdraw(sp.tez(7), _sender=alice, _amount=sp.tez(8), _now=sp.timestamp(0))
113
114 sc.h2("Fail")
115 c2.fail(
116 _valid=False,
117 _exception="This is a very long error message with a looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong word.",
118 )
119
120 sc.h2("Create contract")
121 c1.create_contract(
122 sp.record(delegator=None, amount=sp.tez(8), data=sp.record(n=5)),
123 _sender=alice,
124 _amount=sp.tez(8),
125 )
126 c1.create_contract(
127 sp.record(
128 delegator=sp.Some(baker.public_key_hash),
129 amount=sp.tez(0),
130 data=sp.record(n=0),
131 ),
132 _sender=alice,
133 _amount=sp.tez(0),
134 )
135
136 sc.h2("Detailed context")
137 c2.do_nothing(
138 _sender=c1.address,
139 _source=alice,
140 _amount=sp.mutez(500),
141 _voting_powers=voting_powers,
142 _now=sp.timestamp(1000),
143 _level=42,
144 _chain_id=sp.chain_id_cst("0x9caecab9"),
145 )
146
147 sc.h2("Scenario verify")
148 sc.verify(c2.data.n == 1)
149 # sc.show("The following should fail:")
150 # sc.verify(c2.data.n == 2)
151 # sc.show("The following should fail:")
152 # sc.verify("Expected to fail" == "True")
153
154 x = sp.variant.Circle(0)
155 sc.compute(x.unwrap_variant.Circle())