-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathEscrowLIGO.v
126 lines (103 loc) · 4.17 KB
/
EscrowLIGO.v
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
(** * Extraction of Escrow to CameLIGO and liquidity *)
From MetaCoq.Template Require Import All.
From ConCert.Embedding.Extraction Require Import SimpleBlockchainExt.
From ConCert.Execution Require Import Blockchain.
From ConCert.Execution Require Import ResultMonad.
From ConCert.Execution Require Monad.
From ConCert.Examples.Escrow Require Import Escrow.
From ConCert.Extraction Require Import Common.
From ConCert.Extraction Require CameLIGOPretty.
From ConCert.Extraction Require CameLIGOExtract.
From Coq Require Import String.
From Coq Require Import ZArith_base.
Local Open Scope string_scope.
Definition escrow_init_wrapper (cctx : ContractCallContext)
(s : Setup * Chain)
: result State Error :=
Escrow.init (snd s) cctx (fst s).
Definition ligo_init (s : Address * Setup * nat)
: result State Error :=
let seller := s.1.1 in
let setup := s.1.2 in
let curr_slot := s.2 in
let buyer := setup_buyer setup in
if (buyer =? seller)%address then Err default_error
else Ok {| last_action := curr_slot;
next_step := buyer_commit;
seller := seller;
buyer := buyer;
seller_withdrawable := 0;
buyer_withdrawable := 0 |}.
Lemma escrow_init_eq_ligo_init cctx s :
(* The contract should be deployed with non-zero even amount *)
(ctx_amount cctx =? 0 = false)%Z ->
Z.even (ctx_amount cctx) ->
escrow_init_wrapper cctx s = ligo_init (cctx.(ctx_from),s.1, s.2.(current_slot)).
Proof.
intros Hamount Heven.
unfold escrow_init_wrapper,init, ligo_init. cbn.
rewrite Hamount.
now destruct (_ =? _)%address; destruct (Z.even _).
Qed.
Definition escrow_receive (c : Chain)
(cctx : ContractCallContext)
(s : State)
(msg : option Msg)
: result (list ActionBody * State) Error :=
match Escrow.receive c cctx s msg with
| Ok (s, acts) => Ok (acts, s)
| Err e => Err e
end.
Module EscrowCameLIGOExtraction.
Import CameLIGOExtract.
Import CameLIGOPretty.
#[local]
Existing Instance PrintConfShortNames.PrintWithShortNames.
(** A translation table of constructors and some constants. The corresponding definitions will be extracted and renamed. *)
Definition TT_rename_ligo : list (string * string) :=
[ ("true", "true")
; ("false", "false")
; ("tt", "()")
].
Definition TT_remap_ligo : list (kername * string) :=
[ remap <%% subAmountOption %%> "subTez" ].
Definition ESCROW_MODULE_LIGO : CameLIGOMod Msg ContractCallContext _ State ActionBody Error :=
{| (* a name for the definition with the extracted code *)
lmd_module_name := "cameligo_escrow" ;
(* definitions of operations on numbers, call context, etc. *)
lmd_prelude := CameLIGOPrelude;
(* initial storage *)
lmd_init := ligo_init ;
(* no extra operations in [init] are required *)
lmd_init_prelude := "";
(* the main functionality *)
lmd_receive := escrow_receive ;
lmd_receive_prelude := "";
(* code for the entry point *)
lmd_entry_point :=
printMain "escrow_receive" "msg" "state"
|}.
Definition to_inline : list kername :=
[ <%% @ConCert.Execution.ResultMonad.Monad_result %%>
; <%% @Monad.bind %%>
; <%% @Monad.ret %%>
; <%% @Monad.lift %%>
; <%% bool_rect %%>
; <%% bool_rec %%>
; <%% option_map %%>
; <%% @Extras.with_default %%>
; <%% @setter_from_getter_State_last_action %%>
; <%% @setter_from_getter_State_next_step %%>
; <%% @setter_from_getter_State_seller %%>
; <%% @setter_from_getter_State_buyer %%>
; <%% @setter_from_getter_State_seller_withdrawable %%>
; <%% @setter_from_getter_State_buyer_withdrawable %%>
; <%% default_error %%>
].
Time MetaCoq Run
(CameLIGO_prepare_extraction to_inline TT_remap_ligo
TT_rename_ligo [] "cctx_instance" ESCROW_MODULE_LIGO).
Time Definition cameLIGO_escrow := Eval vm_compute in cameligo_escrow_prepared.
Redirect "cameligo-extract/EscrowExtract.mligo"
MetaCoq Run (tmMsg (String.of_string cameLIGO_escrow)).
End EscrowCameLIGOExtraction.