-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.ligo
168 lines (141 loc) · 5.58 KB
/
token.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 actionConvertToTez is record [
dex : address;
amount : nat;
]
type actionBuy is record [
amount : tez;
]
type actionBuyTez is record [
amount : nat;
]
type actionDex is
| BuyTez of actionBuyTez
| BuyToken of actionBuyTez
type action is
| TransferFrom of actionTransferFrom
| Transfer of actionTransfer
| Approve of actionTransfer
| Buy of actionBuy
| ConvertToTez of actionConvertToTez
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 convertToTez(const action : actionConvertToTez ; const s : storageType) : (list(operation) * storageType) is
block {
const act: actionTransferFrom = record addrFrom=sender; addrTo=action.dex; amount=action.amount*1mtz end;
const params: actionDex = BuyTez(record amount=action.amount; end);
const contract : contract(actionDex) = get_contract(action.dex);
const payment : operation = transaction(params, 0mtz, contract);
const operations : list(operation) = list payment end;
const transferOPeration: (list(operation) * storageType) = transferFrom (act, s);
} with (operations, transferOPeration.1)
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)
| ConvertToTez (at) -> convertToTez (at, s)
end