Source file liquidity_baking_migration.ml
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
(** This module is used to originate contracts for liquidity baking during
protocol stitching: a CPMM (constant product market making) contract and a
liquidity token FA1.2 contract, with the storage of each containing the
other's address.
The CPMM's storage contains a token address, which corresponds to tzBTC when
originated on mainnet and a reference FA1.2 contract when originated for
testing.
The test FA1.2 contract uses the same script as the liquidity token. Its
manager is initialized to the first bootstrap account. Before originating it,
we make sure we are not on mainnet by both checking for the existence of the
tzBTC contract and that the level is sufficiently low.
The Michelson and Ligo code, as well as Coq proofs, for the CPMM and
liquidity token contracts are available here:
https://gitlab.com/dexter2tz/dexter2tz/-/tree/liquidity_baking
All contracts were generated from Ligo at revision
4d10d07ca05abe0f8a5fb97d15267bf5d339d9f4 and converted to OCaml using
`tezos-client convert`.
*)
open Michelson_v1_primitives
open Micheline
let null_address =
Bytes.of_string
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
let mainnet_tzBTC_address = "KT1PWx2mnDueood7fEmfbBDKx1D9BAnnXitn"
(** If token_pool, xtz_pool, or lqt_total are ever zero the CPMM will be
permanently broken. Therefore, we initialize it with the null address
registered as a liquidity provider with 1 satoshi tzBTC and 100 mutez
(roughly the current exchange rate). *)
let cpmm_init_storage ~token_address ~lqt_address =
Script_repr.lazy_expr
(Micheline.strip_locations
(Prim
( 0,
D_Pair,
[ Int (1, Z.one);
Int (2, Z.of_int 100);
Int (3, Z.of_int 100);
String (4, token_address);
String (5, lqt_address) ],
[] )))
let lqt_init_storage cpmm_address =
Script_repr.lazy_expr
(Micheline.strip_locations
(Prim
( 0,
D_Pair,
[ Seq
( 1,
[ Prim
( 2,
D_Elt,
[Bytes (3, null_address); Int (4, Z.of_int 100)],
[] ) ] );
Seq (5, []);
String (6, cpmm_address);
Int (7, Z.of_int 100) ],
[] )))
let test_fa12_init_storage manager =
Script_repr.lazy_expr
(Micheline.strip_locations
(Prim
( 0,
D_Pair,
[ Seq (1, []);
Seq (2, []);
String (3, manager);
Int (4, Z.of_int 10_000) ],
[] )))
let originate ctxt address ~balance script =
Contract_storage.raw_originate ctxt address ~balance ~script ~delegate:None
>>=? fun ctxt ->
Fees_storage.record_paid_storage_space_subsidy ctxt address
>>=? fun (ctxt, size, paid_storage_size_diff) ->
let result : Migration_repr.origination_result =
{
balance_updates =
Receipt_repr.[(Contract address, Credited balance, Protocol_migration)];
originated_contracts = [address];
storage_size = size;
paid_storage_size_diff;
}
in
return (ctxt, result)
let originate_test_fa12 ~typecheck ctxt admin =
Contract_storage.fresh_contract_from_current_nonce ctxt
>>?= fun (ctxt, fa12_address) ->
let script =
Script_repr.
{
code = Script_repr.lazy_expr Liquidity_baking_lqt.script;
storage =
test_fa12_init_storage (Signature.Public_key_hash.to_b58check admin);
}
in
typecheck ctxt script
>>=? fun (script, ctxt) ->
originate
ctxt
fa12_address
~balance:(Tez_repr.of_mutez_exn 1_000_000L)
script
>|=? fun (ctxt, origination_result) ->
(ctxt, fa12_address, [origination_result])
let first_bootstrap_account =
Signature.Public_key.hash
(Signature.Public_key.of_b58check_exn
"edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav")
let check_tzBTC ~typecheck current_level ctxt f =
Contract_repr.of_b58check mainnet_tzBTC_address
>>?= fun tzBTC ->
Contract_storage.exists ctxt tzBTC
>>=? function
| true ->
f ctxt tzBTC []
| false ->
if Compare.Int32.(current_level < 1_437_862l) then
originate_test_fa12 typecheck ctxt first_bootstrap_account
>>=? fun (ctxt, token_address, token_result) ->
f ctxt token_address token_result
else
return (ctxt, [])
let init ctxt ~typecheck =
let nonce =
Operation_hash.hash_bytes [Bytes.of_string "Drip, drip, drip."]
in
let ctxt = Raw_context.init_origination_nonce ctxt nonce in
Storage.Liquidity_baking.Escape_ema.init ctxt 0l
>>=? fun ctxt ->
let current_level =
Raw_level_repr.to_int32 (Level_storage.current ctxt).level
in
Contract_storage.fresh_contract_from_current_nonce ctxt
>>?= fun (ctxt, cpmm_address) ->
Contract_storage.fresh_contract_from_current_nonce ctxt
>>?= fun (ctxt, lqt_address) ->
Storage.Liquidity_baking.Cpmm_address.init ctxt cpmm_address
>>=? fun ctxt ->
check_tzBTC
~typecheck
current_level
ctxt
(fun ctxt token_address token_result ->
let cpmm_script =
Script_repr.
{
code = Script_repr.lazy_expr Liquidity_baking_cpmm.script;
storage =
cpmm_init_storage
~token_address:(Contract_repr.to_b58check token_address)
~lqt_address:(Contract_repr.to_b58check lqt_address);
}
in
typecheck ctxt cpmm_script
>>=? fun (cpmm_script, ctxt) ->
let lqt_script =
Script_repr.
{
code = Script_repr.lazy_expr Liquidity_baking_lqt.script;
storage = lqt_init_storage (Contract_repr.to_b58check cpmm_address);
}
in
typecheck ctxt lqt_script
>>=? fun (lqt_script, ctxt) ->
originate
ctxt
cpmm_address
~balance:(Tez_repr.of_mutez_exn 100L)
cpmm_script
>>=? fun (ctxt, cpmm_result) ->
originate ctxt lqt_address ~balance:Tez_repr.zero lqt_script
>|=? fun (ctxt, lqt_result) ->
let ctxt = Raw_context.unset_origination_nonce ctxt in
(ctxt, [cpmm_result; lqt_result] @ token_result))