-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathERC20.ligo
168 lines (139 loc) · 5.28 KB
/
ERC20.ligo
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
type balance is record [
balance : tez;
allowed : map(address, tez);
]
type balances is map(address, balance);
type storageType is record [
owner : address;
name: string;
totalSupply: tez;
currentSupply: tez;
balances: balances;
rate: nat;
];
type actionTransferFrom is record [
addrFrom : address;
addrTo : address;
amount : tez;
]
type actionTransfer is record [
addrTo : address;
amount : tez;
]
type actionBuy is record [
amount : tez;
]
type actionBalanceOf is record [
who : address;
]
type actioaAllowance is record [
addrFrom : address;
addrTo : address;
]
type action is
| TransferFrom of actionTransferFrom
| Transfer of actionTransfer
| Approve of actionTransfer
| Allowance of actioaAllowance
| Buy of actionBuy
| BalanceOf of actionBalanceOf
function buy(const action : actionBuy ; const s : storageType) : (list(operation) * storageType) is
block {
const availableSupply: tez = s.totalSupply - s.currentSupply;
if amount > availableSupply then failwith("Total supply overruned");
else skip;
const balancesMap : balances = s.balances;
const tokensAmount: tez = amount * s.rate;
s.currentSupply := s.currentSupply + tokensAmount;
const balanceFromInfo : balance = case balancesMap[sender] of
| None -> record balance = 0mtz; allowed = ((map end) : map(address, tez)); end
| Some(b) -> get_force(sender, balancesMap)
end;
const allowed: map(address, tez) = balanceFromInfo.allowed;
const balance: tez = balanceFromInfo.balance + tokensAmount;
allowed[sender] := balance;
balancesMap[sender] := record
balance = balance;
allowed = allowed;
end;
s.balances := balancesMap;
} with ((nil: list(operation)) , s)
function transferFrom(const action : actionTransferFrom ; const s : storageType) : (list(operation) * storageType) is
block {
const balancesMap : balances = s.balances;
const balanceFromInfo : balance = case balancesMap[sender] of
| None -> record balance = 0mtz; allowed = ((map end) : map(address, tez)); end
| Some(b) -> get_force(action.addrFrom, s.balances)
end;
const awailableAmount: tez = balanceFromInfo.balance;
if action.amount > awailableAmount then failwith("The amount isn't awailable")
else skip;
const allowedAmont: tez = case balanceFromInfo.allowed[sender] of
| None -> 0mtz
| Some(b) -> get_force(sender, balanceFromInfo.allowed)
end;
if action.amount > allowedAmont then failwith("The amount isn't awailable")
else skip;
const balanceToInfo : balance = case balancesMap[action.addrTo] of
| None -> record balance = 0mtz; allowed = ((map end) : map(address, tez)); end
| Some(b) -> get_force(action.addrTo, s.balances)
end;
const allowed: map(address, tez) = balanceFromInfo.allowed;
allowed[sender] := allowedAmont - action.amount;
allowed[action.addrFrom] := awailableAmount - action.amount;
balancesMap[action.addrFrom] := record
balance = awailableAmount - action.amount;
allowed = allowed;
end;
balancesMap[action.addrFrom] := record
balance = awailableAmount - action.amount;
allowed = balanceFromInfo.allowed;
end;
const allowedTo: map(address, tez) = balanceToInfo.allowed;
allowedTo[action.addrTo] := awailableAmount + action.amount;
balancesMap[action.addrTo] := record
balance = awailableAmount + action.amount;
allowed = balanceToInfo.allowed;
end;
s.balances := balancesMap;
} with ((nil: list(operation)) , s)
function transfer(const action : actionTransfer ; const s : storageType) : (list(operation) * storageType) is
block {
const act: actionTransferFrom = record addrFrom=sender; addrTo=action.addrTo; amount=action.amount end;
} with transferFrom (act, s)
function balanceOf(const action : actionBalanceOf ; const s : storageType) : (list(operation) * storageType) is
block {
skip
} with ((nil: list(operation)) , s)
function allowance(const action : actioaAllowance ; const s : storageType) : (list(operation) * storageType) is
block {
skip
} with ((nil: list(operation)) , s)
function approve(const action : actionTransfer ; const s : storageType) : (list(operation) * storageType) is
block {
const balancesMap : balances = s.balances;
const balanceFromInfo : balance = case balancesMap[sender] of
| None -> record balance = 0mtz; allowed = ((map end) : map(address, tez)); end
| Some(b) -> get_force(sender, balancesMap)
end;
const amount: tez = balanceFromInfo.balance;
if action.amount < amount then failwith("The amount isn't awailable")
else skip;
const allowed: map(address, tez) = balanceFromInfo.allowed;
allowed[action.addrTo] := action.amount;
balancesMap[sender] := record
balance = balanceFromInfo.balance;
allowed = allowed;
end;
s.balances := balancesMap;
} with ((nil: list(operation)) , s)
function main(const action : action; const s : storageType) : (list(operation) * storageType) is
block {skip} with
case action of
| Buy (bt) -> buy (bt, s)
| Transfer (tx) -> transfer (tx, s)
| TransferFrom (tx) -> transferFrom (tx, s)
| Approve (at) -> approve (at, s)
| Allowance (at) -> allowance (at, s)
| BalanceOf (at) -> balanceOf (at, s)
end