-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontract.py
83 lines (68 loc) Β· 2.63 KB
/
contract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import smartpy as sp
class SocialBlock(sp.Contract):
def __init__(self):
self.add_flag("initial-cast")
self.init(
causes=sp.big_map(
tkey=sp.TNat,
tvalue=sp.TRecord(
owner=sp.TAddress,
cause_title=sp.TString,
balance=sp.TMutez
)
),
fundings=sp.big_map(
tkey=sp.TRecord(
cause_id=sp.TNat,
funder=sp.TAddress
),
tvalue=sp.TMutez
)
)
@sp.entry_point
def create_cause(self, cause_id, title):
""" Creates a new cause. """
# Verify if the cause_id doesn't exists
sp.verify(~ self.data.causes.contains(cause_id))
self.data.causes[cause_id] = sp.record(owner=sp.sender, cause_title=title , balance=sp.mutez(0))
@sp.entry_point
def fund_cause(self, cause_id):
""" Funds a specific cause """
# Verify if the cause_id exists
sp.verify(self.data.causes.contains(cause_id))
self.data.fundings[sp.record(cause_id=cause_id, funder=sp.sender)] = sp.amount
self.data.causes[cause_id].balance += sp.amount
@sp.entry_point
def withdraw_funds(self, cause_id):
""" It allows the owner of a cause to transfer withdraw the funds. """
# Verify if the cause_id exists
sp.verify(self.data.causes.contains(cause_id))
# Verify if the sender/reciever is the owner of the cause
sp.verify(self.data.causes[cause_id].owner == sp.sender)
# Transfer the collected funds
sp.send(self.data.causes[cause_id].owner, self.data.causes[cause_id].balance)
# Reset the amount as it's withdrawn now.
self.data.causes[cause_id].balance = sp.mutez(0)
@sp.add_test("Social Block")
def test():
""" Test for our smart contract. """
scenario = sp.test_scenario()
social = SocialBlock()
owner = sp.test_account("Owner")
funder1 = sp.test_account("Funder1")
funder2 = sp.test_account("Funder2")
scenario += social
scenario += social.create_cause(cause_id=1, title="Test").run(sender=owner)
# Let's fund the cause
scenario += social.fund_cause(1).run(sender=funder1, amount=sp.mutez(1000000))
scenario += social.fund_cause(1).run(sender=funder2, amount=sp.mutez(2000000))
# Let's verify the amount
scenario.verify(social.data.causes[1].balance == sp.mutez(3000000))
# Should raise an error
scenario += social.create_cause(cause_id=1, title="Another Test").run(sender=owner, valid=False)
# Withdrawing funds
# Should raise error
scenario += social.withdraw_funds(1).run(sender=funder1, valid=False)
# scenario += social.create_cause(cause_id=2, title="Test").run(sender=owner)
# With correct owner
scenario += social.withdraw_funds(1).run(sender=owner)