-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
82 lines (58 loc) · 1.79 KB
/
main.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
import json
from compiler import Compiler
from vm import VM
from compiler_backend import CompilerBackend
source = """
@dataclass
class Storage:
balances: Dict[address, int]
total_supply: int
admin: address
@dataclass
class MintParam:
to: address
amount: int
@dataclass
class TransferParam:
to: address
amount: int
def require(arg: bool) -> int:
_ = 0
if arg:
_ = 0
else:
raise "Error"
return 0
class Contract:
def deploy():
return Storage({}, 0, "tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb")
def mint(param: MintParam) -> Storage:
_ = require(self.sender == self.storage.admin)
self.storage.total_supply = self.storage.total_supply + param.amount
balances = self.storage.balances
if param.to in balances:
balances[param.to] = balances[param.to] + param.amount
else:
balances[param.to] = param.amount
self.storage.balances = balances
return self.storage
def transfer(param: TransferParam) -> Storage:
_ = require(self.sender == self.storage.admin)
_ = require(param.amount > 0)
balances = self.storage.balances
sender_balance = balances[self.sender]
_ = require(sender_balance >= param.amount)
balances[self.sender] = sender_balance - param.amount
if param.to in balances:
balances[param.to] = balances[param.to] + param.amount
else:
balances[param.to] = param.amount
self.storage.balances = balances
return self.storage
"""
vm = VM(isDebug=False)
c = Compiler(source, isDebug=False)
instructions = c._compile(c.ast)
micheline = CompilerBackend().compile_contract(c.contract)
with open("my_contract.json", "w+") as f:
f.write(json.dumps(micheline))