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
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
| `Frozen_bonds of Contract_repr.t * Bond_id_repr.t ]
type infinite_source =
[ `Invoice
| `Bootstrap
| `Initial_commitments
| `Revelation_rewards
| `Double_signing_evidence_rewards
| `Endorsing_rewards
| `Baking_rewards
| `Baking_bonuses
| `Minted
| `Liquidity_baking_subsidies
| `Tx_rollup_rejection_rewards ]
type source = [infinite_source | container]
type infinite_sink =
[ `Storage_fees
| `Double_signing_punishments
| `Lost_endorsing_rewards of Signature.Public_key_hash.t * bool * bool
| `Tx_rollup_rejection_punishments
| `Burned ]
type sink = [infinite_sink | container]
let allocated ctxt stored =
match stored with
| `Contract contract ->
Contract_storage.allocated ctxt contract >|=? fun allocated ->
(ctxt, allocated)
| `Collected_commitments bpkh ->
Commitment_storage.exists ctxt bpkh >|= ok >|=? fun allocated ->
(ctxt, allocated)
| `Delegate_balance delegate ->
let contract = Contract_repr.implicit_contract delegate in
Contract_storage.allocated ctxt contract >|=? fun allocated ->
(ctxt, allocated)
| `Frozen_deposits delegate ->
let contract = Contract_repr.implicit_contract delegate in
Frozen_deposits_storage.allocated ctxt contract >|= fun allocated ->
ok (ctxt, allocated)
| `Block_fees -> return (ctxt, true)
| `Frozen_bonds (contract, bond_id) ->
Contract_storage.bond_allocated ctxt contract bond_id
let balance ctxt stored =
match stored with
| `Contract contract ->
Contract_storage.get_balance ctxt contract >|=? fun balance ->
(ctxt, balance)
| `Collected_commitments bpkh ->
Commitment_storage.committed_amount ctxt bpkh >|=? fun balance ->
(ctxt, balance)
| `Delegate_balance delegate ->
let contract = Contract_repr.implicit_contract delegate in
Storage.Contract.Spendable_balance.get ctxt contract >|=? fun balance ->
(ctxt, balance)
| `Frozen_deposits delegate ->
let contract = Contract_repr.implicit_contract delegate in
Frozen_deposits_storage.find ctxt contract >|=? fun frozen_deposits ->
let balance =
match frozen_deposits with
| None -> Tez_repr.zero
| Some frozen_deposits -> frozen_deposits.current_amount
in
(ctxt, balance)
| `Block_fees -> return (ctxt, Raw_context.get_collected_fees ctxt)
| `Frozen_bonds (contract, bond_id) ->
Contract_storage.find_bond ctxt contract bond_id
>|=? fun (ctxt, balance_opt) ->
(ctxt, Option.value ~default:Tez_repr.zero balance_opt)
let credit ctxt dest amount origin =
let open Receipt_repr in
(match dest with
| #infinite_sink as infinite_sink ->
let sink =
match infinite_sink with
| `Storage_fees -> Storage_fees
| `Double_signing_punishments -> Double_signing_punishments
| `Lost_endorsing_rewards (d, p, r) -> Lost_endorsing_rewards (d, p, r)
| `Tx_rollup_rejection_punishments -> Tx_rollup_rejection_punishments
| `Burned -> Burned
in
return (ctxt, sink)
| #container as container -> (
match container with
| `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 (ctxt, 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)
| `Frozen_bonds (contract, bond_id) ->
Contract_storage.credit_bond_only_call_from_token
ctxt
contract
bond_id
amount
>>=? fun ctxt -> return (ctxt, Frozen_bonds (contract, bond_id))))
>|=? fun (ctxt, balance) -> (ctxt, (balance, Credited amount, origin))
let spend ctxt src amount origin =
let open Receipt_repr in
(match src 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
| `Double_signing_evidence_rewards -> Double_signing_evidence_rewards
| `Endorsing_rewards -> Endorsing_rewards
| `Baking_rewards -> Baking_rewards
| `Baking_bonuses -> Baking_bonuses
| `Tx_rollup_rejection_rewards -> Tx_rollup_rejection_rewards
in
return (ctxt, src)
| #container as container -> (
match container with
| `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 -> (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 ->
Frozen_deposits_storage.spend_only_call_from_token
ctxt
delegate
amount
>|=? fun ctxt -> (ctxt, Deposits delegate)
| `Block_fees ->
Raw_context.spend_collected_fees_only_call_from_token ctxt amount
>>?= fun ctxt -> return (ctxt, Block_fees)
| `Frozen_bonds (contract, bond_id) ->
Contract_storage.spend_bond_only_call_from_token
ctxt
contract
bond_id
amount
>>=? fun ctxt -> return (ctxt, Frozen_bonds (contract, bond_id))))
>|=? 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) ->
List.fold_left_es
(fun ctxt (source, _amount) ->
match source with
| `Contract contract | `Frozen_bonds (contract, _) ->
Contract_storage.ensure_deallocated_if_empty ctxt contract
| #source -> return ctxt)
ctxt
sources
>|=? fun ctxt ->
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