-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathelcaro-contract.py
105 lines (87 loc) · 3.44 KB
/
elcaro-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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
elcaro-contract.py
MIT License
Copyright 2017 Splyse Inc.
"""
from boa.blockchain.vm.System.ExecutionEngine import GetScriptContainer,GetExecutingScriptHash
from boa.blockchain.vm.Neo.Transaction import *
from boa.blockchain.vm.Neo.Runtime import GetTrigger,CheckWitness,Notify,Log
from boa.blockchain.vm.Neo.TriggerType import Application,Verification
from boa.blockchain.vm.Neo.Output import GetScriptHash,GetValue,GetAssetId
from boa.blockchain.vm.Neo.Storage import GetContext,Get,Put
from boa.code.builtins import concat,take
OWNER = b'\x8e\x5b\x17\x79\x3c\xa9\xf5\xd9\x13\x1d\x67\x4d\xfc\x00\x0f\x5a\x65\x58\xa4\x65'
GAS_ASSET_ID = b'\xe7\x2d\x28\x69\x79\xee\x6c\xb1\xb7\xe6\x5d\xfd\xdf\xb2\xe3\x84\x10\x0b\x8d\x14\x8e\x77\x58\xde\x42\xe4\x16\x8b\x71\x79\x2c\x60';
BADPREFIX='price/'
def Main(operation, args):
trigger = GetTrigger()
if trigger == Verification():
return CheckWitness(OWNER)
elif trigger == Application():
context = GetContext()
l = len(args)
if l == 1:
key = args[0]
elif l == 2:
key = args[0]
value = args[1]
else:
Log("Bad invocation argument count")
Log(l)
return False
if operation == 'getvalue':
return Get(context, key)
elif operation == 'putvalue':
prefix = take(key, 6)
if BADPREFIX == prefix:
Log("Hacking attempt!")
return False
if CheckWitness(OWNER):
Log("Owner found, bypassing payment")
Put(context, key, value)
return True
else:
# check if we got paid
tx = GetScriptContainer()
refs = tx.References
if len(refs) < 1:
Log("No payment sent in transaction")
return False
ref = refs[0]
sentAsset = GetAssetId(ref)
if sentAsset == GAS_ASSET_ID:
sender = GetScriptHash(ref)
receiver = GetExecutingScriptHash();
totalGasSent = 0
for output in tx.Outputs:
shash = GetScriptHash(output)
if shash == receiver:
totalGasSent = totalGasSent + output.Value
Log ("Total GAS sent:")
Log (totalGasSent)
pkey = concat('price/', key)
keyprice = Get(context, pkey)
if totalGasSent == keyprice:
Log("Price met, setting value and sending notification")
notification=[sender,key,value]
Notify(notification)
Put(context, key, value)
return True
Log("Price not met!")
return False
return False
elif operation == 'getprice':
key = concat('price/', key)
return Get(context, key)
elif operation == 'putprice':
if CheckWitness(OWNER):
key = concat('price/', key)
Put(context, key, value)
return True
else:
Log("Access denied")
return False
else:
Log("Invalid operation")
return False
return False