-
Notifications
You must be signed in to change notification settings - Fork 33
/
broker.liq
53 lines (48 loc) · 1.64 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
type storage = {
state : string;
timeout : timestamp;
pn : tez * tez;
x : address;
a : address;
b : address;
}
let%entry default
(parameter : timestamp)
(storage : storage) =
if storage.state <> "open" then Current.failwith ()
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 op1 = Contract.call storage.x pn0 default () in
let (_, pn1) = storage.pn in
let op2 = Contract.call storage.a pn1 default () in
( [op1; op2], 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 op1 = Contract.call storage.x transfer default () in
(* # We transfer the rest to B *)
let transfer = Current.balance () - 1.00tz in
let op2 = Contract.call storage.b transfer default () in
( [op1; op2], storage )