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
type container =
[ `Contract of Contract_repr.t
| `Collected_commitments of Blinded_public_key_hash.t
| `Delegate_balance of Signature.Public_key_hash.t
| `Frozen_deposits of Signature.Public_key_hash.t
| `Block_fees
| `Legacy_deposits of Signature.Public_key_hash.t * Cycle_repr.t
| `Legacy_fees of Signature.Public_key_hash.t * Cycle_repr.t
| `Legacy_rewards of Signature.Public_key_hash.t * Cycle_repr.t ]
type source =
[ `Invoice
| `Bootstrap
| `Initial_commitments
| `Revelation_rewards
| `Double_signing_evidence_rewards
| `Endorsing_rewards
| `Baking_rewards
| `Baking_bonuses
| `Minted
| `Liquidity_baking_subsidies
| container ]
type sink =
[ `Storage_fees
| `Double_signing_punishments
| `Lost_endorsing_rewards of Signature.Public_key_hash.t * bool * bool
| `Burned
| container ]
let allocated ctxt stored =
match stored with
| `Contract contract -> Contract_storage.allocated ctxt contract
| `Collected_commitments bpkh -> Commitment_storage.exists ctxt bpkh >|= ok
| `Delegate_balance delegate ->
let contract = Contract_repr.implicit_contract delegate in
Contract_storage.allocated ctxt contract
| `Frozen_deposits delegate ->
let contract = Contract_repr.implicit_contract delegate in
Frozen_deposits_storage.allocated ctxt contract >|= ok
| `Block_fees -> return_true
| `Legacy_deposits (delegate, cycle) ->
let contract = Contract_repr.implicit_contract delegate in
Storage.Contract.Legacy_frozen_deposits.mem (ctxt, contract) cycle >|= ok
| `Legacy_fees (delegate, cycle) ->
let contract = Contract_repr.implicit_contract delegate in
Storage.Contract.Legacy_frozen_fees.mem (ctxt, contract) cycle >|= ok
| `Legacy_rewards (delegate, cycle) ->
let contract = Contract_repr.implicit_contract delegate in
Storage.Contract.Legacy_frozen_rewards.mem (ctxt, contract) cycle >|= ok
let balance ctxt stored =
match stored with
| `Contract contract -> Contract_storage.get_balance ctxt contract
| `Collected_commitments bpkh -> Commitment_storage.committed_amount ctxt bpkh
| `Delegate_balance delegate ->
let contract = Contract_repr.implicit_contract delegate in
Storage.Contract.Balance.get ctxt contract
| `Frozen_deposits delegate -> (
let contract = Contract_repr.implicit_contract delegate in
Frozen_deposits_storage.find ctxt contract >|=? fun frozen_deposits ->
match frozen_deposits with
| None -> Tez_repr.zero
| Some frozen_deposits -> frozen_deposits.current_amount)
| `Block_fees -> return (Raw_context.get_collected_fees ctxt)
| `Legacy_deposits (delegate, cycle) ->
let contract = Contract_repr.implicit_contract delegate in
Storage.Contract.Legacy_frozen_deposits.find (ctxt, contract) cycle
>|=? Option.value ~default:Tez_repr.zero
| `Legacy_fees (delegate, cycle) ->
let contract = Contract_repr.implicit_contract delegate in
Storage.Contract.Legacy_frozen_fees.find (ctxt, contract) cycle
>|=? Option.value ~default:Tez_repr.zero
| `Legacy_rewards (delegate, cycle) ->
let contract = Contract_repr.implicit_contract delegate in
Storage.Contract.Legacy_frozen_rewards.find (ctxt, contract) cycle
>|=? Option.value ~default:Tez_repr.zero
let credit ctxt dest amount origin =
let open Receipt_repr in
(match dest with
| `Storage_fees -> return (ctxt, Storage_fees)
| `Double_signing_punishments -> return (ctxt, Double_signing_punishments)
| `Lost_endorsing_rewards (d, p, r) ->
return (ctxt, Lost_endorsing_rewards (d, p, r))
| `Burned -> return (ctxt, Burned)
| `Contract dest ->
Contract_storage.credit_only_call_from_token ctxt dest amount
>|=? fun ctxt -> (ctxt, Contract dest)
| `Collected_commitments bpkh ->
Commitment_storage.increase_commitment_only_call_from_token
ctxt
bpkh
amount
>|=? fun ctxt -> (ctxt, Commitments bpkh)
| `Delegate_balance delegate ->
let contract = Contract_repr.implicit_contract delegate in
Contract_storage.increase_balance_only_call_from_token
ctxt
contract
amount
>|=? fun ctxt -> (ctxt, Contract contract)
| `Frozen_deposits delegate as dest ->
allocated ctxt dest >>=? fun allocated ->
(if not allocated then Frozen_deposits_storage.init ctxt delegate
else return ctxt)
>>=? fun ctxt ->
Frozen_deposits_storage.credit_only_call_from_token ctxt delegate amount
>|=? fun ctxt -> (ctxt, Deposits delegate)
| `Block_fees ->
Raw_context.credit_collected_fees_only_call_from_token ctxt amount
>>?= fun ctxt -> return (ctxt, Block_fees)
| `Legacy_deposits (delegate, cycle) as dest ->
let contract = Contract_repr.implicit_contract delegate in
balance ctxt dest >>=? fun old_amount ->
Tez_repr.(old_amount +? amount) >>?= fun new_amount ->
Storage.Contract.Legacy_frozen_deposits.add
(ctxt, contract)
cycle
new_amount
>>= fun ctxt -> return (ctxt, Legacy_deposits (delegate, cycle))
| `Legacy_fees (delegate, cycle) as dest ->
let contract = Contract_repr.implicit_contract delegate in
balance ctxt dest >>=? fun old_amount ->
Tez_repr.(old_amount +? amount) >>?= fun new_amount ->
Storage.Contract.Legacy_frozen_fees.add (ctxt, contract) cycle new_amount
>>= fun ctxt -> return (ctxt, Legacy_fees (delegate, cycle))
| `Legacy_rewards (delegate, cycle) as dest ->
let contract = Contract_repr.implicit_contract delegate in
balance ctxt dest >>=? fun old_amount ->
Tez_repr.(old_amount +? amount) >>?= fun new_amount ->
Storage.Contract.Legacy_frozen_rewards.add
(ctxt, contract)
cycle
new_amount
>>= fun ctxt -> return (ctxt, Legacy_rewards (delegate, cycle)))
>|=? fun (ctxt, balance) -> (ctxt, (balance, Credited amount, origin))
let spend ctxt src amount origin =
let open Receipt_repr in
(match src with
| `Bootstrap -> return (ctxt, Bootstrap)
| `Invoice -> return (ctxt, Invoice)
| `Initial_commitments -> return (ctxt, Initial_commitments)
| `Minted -> return (ctxt, Minted)
| `Liquidity_baking_subsidies -> return (ctxt, Liquidity_baking_subsidies)
| `Revelation_rewards -> return (ctxt, Nonce_revelation_rewards)
| `Double_signing_evidence_rewards ->
return (ctxt, Double_signing_evidence_rewards)
| `Endorsing_rewards -> return (ctxt, Endorsing_rewards)
| `Baking_rewards -> return (ctxt, Baking_rewards)
| `Baking_bonuses -> return (ctxt, Baking_bonuses)
| `Contract src ->
Contract_storage.spend_only_call_from_token ctxt src amount
>|=? fun ctxt -> (ctxt, Contract src)
| `Collected_commitments bpkh ->
Commitment_storage.decrease_commitment_only_call_from_token
ctxt
bpkh
amount
>>=? fun ctxt -> return (ctxt, Commitments bpkh)
| `Delegate_balance delegate ->
let contract = Contract_repr.implicit_contract delegate in
Contract_storage.decrease_balance_only_call_from_token
ctxt
contract
amount
>|=? fun ctxt -> (ctxt, Contract contract)
| `Frozen_deposits delegate ->
(if Tez_repr.(amount = zero) then return ctxt
else
Frozen_deposits_storage.spend_only_call_from_token ctxt delegate amount)
>>=? fun ctxt -> return (ctxt, Deposits delegate)
| `Block_fees ->
Raw_context.spend_collected_fees_only_call_from_token ctxt amount
>>?= fun ctxt -> return (ctxt, Block_fees)
| `Legacy_deposits (delegate, cycle) as src ->
balance ctxt src >>=? fun old_amount ->
Tez_repr.(old_amount -? amount) >>?= fun new_amount ->
let contract = Contract_repr.implicit_contract delegate in
(if Tez_repr.(new_amount = zero) then
Storage.Contract.Legacy_frozen_deposits.remove (ctxt, contract) cycle
else
Storage.Contract.Legacy_frozen_deposits.add
(ctxt, contract)
cycle
new_amount)
>>= fun ctxt -> return (ctxt, Legacy_deposits (delegate, cycle))
| `Legacy_fees (delegate, cycle) as src ->
balance ctxt src >>=? fun old_amount ->
Tez_repr.(old_amount -? amount) >>?= fun new_amount ->
let contract = Contract_repr.implicit_contract delegate in
(if Tez_repr.(new_amount = zero) then
Storage.Contract.Legacy_frozen_fees.remove (ctxt, contract) cycle
else
Storage.Contract.Legacy_frozen_fees.add
(ctxt, contract)
cycle
new_amount)
>>= fun ctxt -> return (ctxt, Legacy_fees (delegate, cycle))
| `Legacy_rewards (delegate, cycle) as src ->
balance ctxt src >>=? fun old_amount ->
Tez_repr.(old_amount -? amount) >>?= fun new_amount ->
let contract = Contract_repr.implicit_contract delegate in
(if Tez_repr.(new_amount = zero) then
Storage.Contract.Legacy_frozen_rewards.remove (ctxt, contract) cycle
else
Storage.Contract.Legacy_frozen_rewards.add
(ctxt, contract)
cycle
new_amount)
>>= fun ctxt -> return (ctxt, Legacy_rewards (delegate, cycle)))
>|=? fun (ctxt, balance) -> (ctxt, (balance, Debited amount, origin))
let transfer_n ?(origin = Receipt_repr.Block_application) ctxt src dest =
let sources = List.filter (fun (_, am) -> Tez_repr.(am <> zero)) src in
match sources with
| [] ->
return (ctxt, [])
| _ :: _ ->
List.fold_left_es
(fun (ctxt, total, debit_logs) (source, amount) ->
spend ctxt source amount origin >>=? fun (ctxt, debit_log) ->
Tez_repr.(amount +? total) >>?= fun total ->
return (ctxt, total, debit_log :: debit_logs))
(ctxt, Tez_repr.zero, [])
sources
>>=? fun (ctxt, amount, debit_logs) ->
credit ctxt dest amount origin >|=? fun (ctxt, credit_log) ->
let balance_updates = List.rev (credit_log :: debit_logs) in
(ctxt, balance_updates)
let transfer ?(origin = Receipt_repr.Block_application) ctxt src dest amount =
transfer_n ~origin ctxt [(src, amount)] dest