-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmct-dapp-template.py
140 lines (98 loc) · 3.65 KB
/
mct-dapp-template.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
Master Contract Token dApp Template Contract
============================================
Author: Joe Stewart
Email: hal0x2328@splyse.tech
Date: May 8 2018
This code demonstrates use of MCT receive/send and staked storage functions
Deployment in neo-python:
import contract mct-dapp-template.avm 0710 05 False False
wallet tkn_send MCT {from address} {dApp contract address} {minimum stake}
"""
from boa.interop.Neo.Runtime import GetTrigger, CheckWitness
from boa.interop.Neo.TriggerType import Application, Verification
from boa.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHash
from boa.interop.Neo.App import RegisterAppCall
OWNER = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
# mainnet
#MCT_SCRIPTHASH = b'?\xbc`|\x12\xc2\x87642$\xa4\xb4\xd8\xf5\x13\xa5\xc2|\xa8'
# privatenet
MCT_SCRIPTHASH = b'\x8dKL\x14V4\x17\xc6\x91\x91\xe0\x8b\xe0\xb8m\xdc\xb4\xbc\x86\xc1'
# mainnet
#MCTContract = RegisterAppCall('a87cc2a513f5d8b4a42432343687c2127c60bc3f', 'operation', 'args')
# privatenet
MCTContract = RegisterAppCall('c186bcb4dc6db8e08be09191c6173456144c4b8d', 'operation', 'args')
def Main(operation, args):
trigger = GetTrigger()
if trigger == Verification():
if CheckWitness(OWNER):
return True
return False
elif trigger == Application():
if operation == 'hello':
print('hello world!')
totalcalls = Get('totalCalls')
totalcalls = totalcalls + 1
print(totalcalls)
if Put('totalCalls', totalcalls):
return True
print('staked storage call failed')
return False
if operation == 'ownerWithdraw':
if not CheckWitness(OWNER):
print('only the contract owner can withdraw MCT from the contract')
return False
if len(args) != 1:
print('withdraw amount not specified')
return False
t_amount = args[0]
myhash = GetExecutingScriptHash()
return MCTContract('transfer', [myhash, OWNER, t_amount])
# end of normal invocations, reject any non-MCT invocations
caller = GetCallingScriptHash()
if caller != MCT_SCRIPTHASH:
print('token type not accepted by this contract')
return False
if operation == 'onTokenTransfer':
print('onTokenTransfer() called')
return handle_token_received(caller, args)
return False
def handle_token_received(chash, args):
arglen = len(args)
if arglen < 3:
print('arg length incorrect')
return False
t_from = args[0]
t_to = args[1]
t_amount = args[2]
if arglen == 4:
extra_arg = args[3] # extra argument passed by transfer()
if len(t_from) != 20:
return False
if len(t_to) != 20:
return False
myhash = GetExecutingScriptHash()
if t_to != myhash:
return False
if t_from == OWNER:
# topping up contract token balance, just return True to allow
return True
if extra_arg == 'reject-me':
print('rejecting transfer')
Delete(t_from)
return False
else:
print('received MCT tokens!')
totalsent = Get(t_from)
totalsent = totalsent + t_amount
if Put(t_from, totalsent):
return True
print('staked storage call failed')
return False
# Staked storage appcalls
def Get(key):
return MCTContract('Get', [key])
def Delete(key):
return MCTContract('Delete', [key])
def Put(key, value):
return MCTContract('Put', [key, value])