-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdex.ligo
99 lines (85 loc) · 2.88 KB
/
dex.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
type storageTypeDex is record [
totalTezos: nat;
totalTokens: nat;
token: address;
];
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 action is
| TransferFrom of actionTransferFrom
| Transfer of actionTransfer
| Approve of actionTransfer
| Buy of actionBuy
| ConvertToTez of actionConvertToTez
type actionDex is
| BuyTez of actionBuyTez
| BuyToken of actionBuyTez
function buyTez(const action : actionBuyTez ; const s : storageTypeDex) : (list(operation) * storageTypeDex) is
block {
if sender =/= s.token then failwith("Permition denaed");
else skip;
const availableTez: nat = s.totalTezos;
const tezAmount: nat = action.amount * s.totalTokens / ( s.totalTezos + action.amount);
const totalTezos : int = s.totalTezos - tezAmount;
const totalTokens : nat = s.totalTokens + tezAmount;
if tezAmount >= availableTez then failwith("Not enough tez");
else skip;
s.totalTezos := abs(totalTezos);
s.totalTokens := totalTokens;
const contract : contract(unit) = get_contract(source);
const payment : operation = transaction(unit, tezAmount * 1mtz, contract);
const operations : list(operation) = list payment end;
} with (operations, s)
function buyToken(const action : actionBuyTez ; const s : storageTypeDex) : (list(operation) * storageTypeDex) is
block {
if amount =/= action.amount*1mtz then failwith("Not enough tez");
else skip;
const availableTokens: nat = s.totalTokens;
const tokenAmount: nat = action.amount * s.totalTezos / ( s.totalTokens + action.amount);
const totalTezos : nat = s.totalTezos + tokenAmount;
const totalTokens : int = s.totalTokens - tokenAmount;
if tokenAmount >= availableTokens then failwith("Not enough tez");
else skip;
const params: action = Transfer(record addrTo=sender; amount=tokenAmount*1mtz; end);
s.totalTezos := totalTezos;
s.totalTokens := abs(totalTokens);
const contract : contract(action) = get_contract(s.token);
const payment : operation = transaction(params, 0mtz, contract);
const operations : list(operation) = list payment end;
} with (operations , s)
function main(const action : actionDex; const s : storageTypeDex) : (list(operation) * storageTypeDex) is
block {skip} with
case action of
| BuyTez (bt) -> buyTez (bt, s)
| BuyToken (bt) -> buyToken (bt, s)
end