Source file token.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
type container =
[ `Contract of Contract_repr.t
| `Collected_commitments of Blinded_public_key_hash.t
| `Frozen_deposits of Frozen_staker_repr.t
| `Unstaked_frozen_deposits of Unstaked_frozen_staker_repr.t * Cycle_repr.t
| `Block_fees
| `Frozen_bonds of Contract_repr.t * Bond_id_repr.t ]
type infinite_source =
[ `Invoice
| `Bootstrap
| `Initial_commitments
| `Revelation_rewards
| `Attesting_rewards
| `Baking_rewards
| `Baking_bonuses
| `Minted
| `Liquidity_baking_subsidies
| `Sc_rollup_refutation_rewards ]
type giver = [infinite_source | container]
type infinite_sink =
[ `Storage_fees
| `Double_signing_punishments
| `Lost_attesting_rewards of Signature.Public_key_hash.t * bool * bool
| `Sc_rollup_refutation_punishments
| `Burned ]
type receiver = [infinite_sink | container]
let balance ctxt stored =
let open Lwt_result_syntax in
match stored with
| `Collected_commitments bpkh ->
let+ balance = Commitment_storage.committed_amount ctxt bpkh in
(ctxt, balance)
| `Block_fees -> return (ctxt, Raw_context.get_collected_fees ctxt)
let credit ctxt receiver amount origin =
let open Lwt_result_syntax in
let open Receipt_repr in
let+ ctxt, balance =
match receiver with
| #infinite_sink as infinite_sink ->
let sink =
match infinite_sink with
| `Storage_fees -> Storage_fees
| `Double_signing_punishments -> Double_signing_punishments
| `Lost_attesting_rewards (d, p, r) -> Lost_attesting_rewards (d, p, r)
| `Sc_rollup_refutation_punishments ->
Sc_rollup_refutation_punishments
| `Burned -> Burned
in
let* old_total_supply = Storage.Contract.Total_supply.get ctxt in
let*? new_total_supply = Tez_repr.(old_total_supply -? amount) in
let+ ctxt =
Storage.Contract.Total_supply.update ctxt new_total_supply
in
(ctxt, sink)
| #container as container -> (
match container with
| `Contract receiver ->
let+ ctxt =
Contract_storage.credit_only_call_from_token ctxt receiver amount
in
(ctxt, Contract receiver)
| `Collected_commitments bpkh ->
let+ ctxt =
Commitment_storage.increase_commitment_only_call_from_token
ctxt
bpkh
amount
in
(ctxt, Commitments bpkh)
| `Frozen_deposits staker ->
let+ ctxt =
Stake_storage.add_frozen_stake_only_call_from_token
ctxt
staker
amount
in
(ctxt, Deposits staker)
| `Unstaked_frozen_deposits (staker, cycle) ->
let+ ctxt =
Unstaked_frozen_deposits_storage.credit_only_call_from_token
ctxt
staker
cycle
amount
in
(ctxt, Unstaked_deposits (staker, cycle))
| `Block_fees ->
let*? ctxt =
Raw_context.credit_collected_fees_only_call_from_token ctxt amount
in
return (ctxt, Block_fees)
| `Frozen_bonds (contract, bond_id) ->
let* ctxt =
Contract_storage.credit_bond_only_call_from_token
ctxt
contract
bond_id
amount
in
return (ctxt, Frozen_bonds (contract, bond_id)))
in
(ctxt, item balance (Credited amount) origin)
let spend ctxt giver amount origin =
let open Lwt_result_syntax in
let open Receipt_repr in
let+ ctxt, balance =
match giver with
| #infinite_source as infinite_source ->
let src =
match infinite_source with
| `Bootstrap -> Bootstrap
| `Invoice -> Invoice
| `Initial_commitments -> Initial_commitments
| `Minted -> Minted
| `Liquidity_baking_subsidies -> Liquidity_baking_subsidies
| `Revelation_rewards -> Nonce_revelation_rewards
| `Attesting_rewards -> Attesting_rewards
| `Baking_rewards -> Baking_rewards
| `Baking_bonuses -> Baking_bonuses
| `Sc_rollup_refutation_rewards -> Sc_rollup_refutation_rewards
in
let* old_total_supply = Storage.Contract.Total_supply.get ctxt in
let*? new_total_supply = Tez_repr.(old_total_supply +? amount) in
let+ ctxt =
Storage.Contract.Total_supply.update ctxt new_total_supply
in
(ctxt, src)
| #container as container -> (
match container with
| `Contract giver ->
let+ ctxt =
Contract_storage.spend_only_call_from_token ctxt giver amount
in
(ctxt, Contract giver)
| `Collected_commitments bpkh ->
let+ ctxt =
Commitment_storage.decrease_commitment_only_call_from_token
ctxt
bpkh
amount
in
(ctxt, Commitments bpkh)
| `Frozen_deposits staker ->
let+ ctxt =
Stake_storage.remove_frozen_stake_only_call_from_token
ctxt
staker
amount
in
(ctxt, Deposits staker)
| `Unstaked_frozen_deposits (staker, cycle) ->
let+ ctxt =
Unstaked_frozen_deposits_storage.spend_only_call_from_token
ctxt
staker
cycle
amount
in
(ctxt, Unstaked_deposits (staker, cycle))
| `Block_fees ->
let*? ctxt =
Raw_context.spend_collected_fees_only_call_from_token ctxt amount
in
return (ctxt, Block_fees)
| `Frozen_bonds (contract, bond_id) ->
let* ctxt =
Contract_storage.spend_bond_only_call_from_token
ctxt
contract
bond_id
amount
in
return (ctxt, Frozen_bonds (contract, bond_id)))
in
(ctxt, item balance (Debited amount) origin)
let transfer_n ?(origin = Receipt_repr.Block_application) ctxt givers receiver =
let open Lwt_result_syntax in
let givers = List.filter (fun (_, am) -> Tez_repr.(am <> zero)) givers in
match givers with
| [] ->
return (ctxt, [])
| _ :: _ ->
let* ctxt, amount, debit_logs =
List.fold_left_es
(fun (ctxt, total, debit_logs) (giver, amount) ->
let* ctxt, debit_log = spend ctxt giver amount origin in
let*? total = Tez_repr.(amount +? total) in
return (ctxt, total, debit_log :: debit_logs))
(ctxt, Tez_repr.zero, [])
givers
in
let* ctxt, credit_log = credit ctxt receiver amount origin in
let+ ctxt =
List.fold_left_es
(fun ctxt (giver, _amount) ->
match giver with
| `Contract contract | `Frozen_bonds (contract, _) ->
Contract_storage.ensure_deallocated_if_empty ctxt contract
| #giver -> return ctxt)
ctxt
givers
in
let balance_updates = List.rev (credit_log :: debit_logs) in
(ctxt, balance_updates)
let transfer ?(origin = Receipt_repr.Block_application) ctxt giver receiver
amount =
transfer_n ~origin ctxt [(giver, amount)] receiver
module Internal_for_tests = struct
let allocated ctxt stored =
let open Lwt_result_syntax in
match stored with
| `Contract contract ->
let*! allocated = Contract_storage.allocated ctxt contract in
return (ctxt, allocated)
| `Collected_commitments bpkh ->
let*! allocated = Commitment_storage.exists ctxt bpkh in
return (ctxt, allocated)
| `Frozen_deposits _ | `Unstaked_frozen_deposits _ | `Block_fees ->
return (ctxt, true)
| `Frozen_bonds (contract, bond_id) ->
Contract_storage.bond_allocated ctxt contract bond_id
type container_with_balance =
[ `Contract of Contract_repr.t
| `Collected_commitments of Blinded_public_key_hash.t
| `Block_fees
| `Frozen_bonds of Contract_repr.t * Bond_id_repr.t ]
let balance ctxt (stored : [< container_with_balance]) =
let open Lwt_result_syntax in
match stored with
| (`Collected_commitments _ | `Block_fees) as stored -> balance ctxt stored
| `Contract contract ->
let+ balance = Contract_storage.get_balance ctxt contract in
(ctxt, balance)
| `Frozen_bonds (contract, bond_id) ->
let+ ctxt, balance_opt =
Contract_storage.find_bond ctxt contract bond_id
in
(ctxt, Option.value ~default:Tez_repr.zero balance_opt)
end