forked from OCamlPro/liquidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.liq
59 lines (54 loc) · 1.79 KB
/
broker.liq
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
[%%version 0.14]
type storage = {
state : string;
timeout : timestamp;
pn : tez * tez;
x : (unit, unit) contract;
a : (unit, unit) contract;
b : (unit, unit) contract;
}
let%entry main
(parameter : timestamp)
(storage : storage)
: unit * storage =
if storage.state <> "open" then Current.fail ()
else
if Current.time () < storage.timeout then (* Before timeout *)
(* We compute ((1 + P) + N) tez for keeping the contract alive *)
let (pn0, pn1) = storage.pn in
let cost = 1.00tz + pn0 + pn1 in
let b = Current.balance () in
if cost < b then
(* # Not enough cash, we just accept the transaction
# and leave the global untouched *)
( (), storage )
else
(* # Enough cash, successful ending
# We update the global*)
let storage = storage.state <- "success" in
let (pn0, _) = storage.pn in
let (_result, storage) =
Contract.call storage.x pn0 storage () in
let (_, pn1) = storage.pn in
let (_result, storage) =
Contract.call storage.a pn1 storage () in
( (), storage )
else
(* # After timeout, we refund
# We update the global *)
let (p, _) = storage.pn in
let storage = storage.state <- "timeout" in
(* # We try to transfer the fee to the broker *)
let bal = Current.balance () in
let available = bal - 1.00tz in
let transfer =
if available < p then available
else p
in
let _result, storage =
Contract.call storage.x transfer storage () in
(* # We transfer the rest to B *)
let transfer = Current.balance () - 1.00tz in
let _result, storage =
Contract.call storage.b transfer storage () in
( (), storage )