-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathnew_bank_contract.py
76 lines (63 loc) · 2.08 KB
/
new_bank_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
import serpent
from pyethereum import tester, utils, abi
serpent_code = '''
#Deposit
def deposit():
self.storage[msg.sender] += msg.value
return(1)
#Withdraw the given amount (in wei)
def withdraw(amount):
#Check to ensure enough money in account
if self.storage[msg.sender] < amount:
return(-1)
else:
#If there is enough money, complete with withdraw
self.storage[msg.sender] -= amount
send(0, msg.sender, amount)
return(1)
#Transfer the given amount (in wei) to the destination's public key
def transfer(amount, destination):
#Check to ensure enough money in sender's account
if self.storage[msg.sender] < amount:
return(-1)
else:
#If there is enough money, complete the transfer
self.storage[msg.sender] -= amount
self.storage[destination] += amount
return(1)
#Just return the sender's balance
def balance():
return(self.storage[msg.sender])
'''
public_k1 = utils.privtoaddr(tester.k1)
s = tester.state()
c = s.abi_contract(serpent_code)
o = c.deposit(value=1000, sender=tester.k0)
if o == 1:
print("1000 wei successfully desposited to tester.k0's account")
else:
print("Failed to deposit 1000 wei into tester.k0's account")
o = c.withdraw(1000, sender=tester.k0)
if o == 1:
print("1000 wei successfully withdrawn from tester.k0's account")
else:
print("Failed to witdraw 1000 wei into tester.k0's account")
o = c.withdraw(1000, sender=tester.k1)
if o == 1:
print("1000 wei successfully withdrawn from tester.k1's account")
else:
print("Failed to witdraw 1000 wei into tester.k1's account")
o = c.deposit(value=1000, sender=tester.k0)
if o == 1:
print("1000 wei successfully desposited to tester.k0's account")
else:
print("Failed to deposit 1000 wei into tester.k0's account")
o = c.transfer(500, public_k1, sender=tester.k0)
if o == 1:
print("500 wei successfully transfered from tester.k0's account to tester.k1's account")
else:
print("Failed to transfer 500 wei from tester.k0's account to tester.k1's account")
o = c.balance(sender=tester.k0)
print("tester_k0 has a balance of " + str(o))
o = c.balance(sender=tester.k1)
print("tester_k1 has a balance of " + str(o))