Source file adaptive_issuance_services.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
285
286
287
288
open Alpha_context
type expected_rewards = {
cycle : Cycle.t;
baking_reward_fixed_portion : Tez.t;
baking_reward_bonus_per_slot : Tez.t;
attesting_reward_per_slot : Tez.t;
liquidity_baking_subsidy : Tez.t;
seed_nonce_revelation_tip : Tez.t;
vdf_revelation_tip : Tez.t;
}
let expected_rewards_encoding : expected_rewards Data_encoding.t =
let open Data_encoding in
conv
(fun {
cycle;
baking_reward_fixed_portion;
baking_reward_bonus_per_slot;
attesting_reward_per_slot;
liquidity_baking_subsidy;
seed_nonce_revelation_tip;
vdf_revelation_tip;
} ->
( cycle,
baking_reward_fixed_portion,
baking_reward_bonus_per_slot,
attesting_reward_per_slot,
liquidity_baking_subsidy,
seed_nonce_revelation_tip,
vdf_revelation_tip ))
(fun ( cycle,
baking_reward_fixed_portion,
baking_reward_bonus_per_slot,
attesting_reward_per_slot,
liquidity_baking_subsidy,
seed_nonce_revelation_tip,
vdf_revelation_tip ) ->
{
cycle;
baking_reward_fixed_portion;
baking_reward_bonus_per_slot;
attesting_reward_per_slot;
liquidity_baking_subsidy;
seed_nonce_revelation_tip;
vdf_revelation_tip;
})
(obj7
(req "cycle" Cycle.encoding)
(req "baking_reward_fixed_portion" Tez.encoding)
(req "baking_reward_bonus_per_slot" Tez.encoding)
(req "attesting_reward_per_slot" Tez.encoding)
(req "liquidity_baking_subsidy" Tez.encoding)
(req "seed_nonce_revelation_tip" Tez.encoding)
(req "vdf_revelation_tip" Tez.encoding))
module S = struct
open Data_encoding
let q_encoding =
conv
(fun Q.{num; den} -> (num, den))
(fun (num, den) -> Q.make num den)
(obj2 (req "numerator" n) (req "denominator" n))
let context_path = RPC_path.(open_root / "context")
let path = RPC_path.(context_path / "issuance")
let total_supply =
RPC_service.get_service
~description:"Returns the total supply (in mutez) available on the chain"
~query:RPC_query.empty
~output:Tez.encoding
RPC_path.(context_path / "total_supply")
let total_frozen_stake =
RPC_service.get_service
~description:"Returns the total stake (in mutez) frozen on the chain"
~query:RPC_query.empty
~output:Tez.encoding
RPC_path.(context_path / "total_frozen_stake")
let current_yearly_rate =
RPC_service.get_service
~description:
"Returns the current expected maximum yearly issuance rate (in %)"
~query:RPC_query.empty
~output:(string Plain)
RPC_path.(path / "current_yearly_rate")
let current_yearly_rate_exact =
RPC_service.get_service
~description:
"Returns the current expected maximum yearly issuance rate (exact \
quotient)"
~query:RPC_query.empty
~output:q_encoding
RPC_path.(path / "current_yearly_rate_exact")
let current_issuance_per_minute =
RPC_service.get_service
~description:
"Returns the current expected maximum issuance per minute (in mutez)"
~query:RPC_query.empty
~output:Tez.encoding
RPC_path.(path / "issuance_per_minute")
let launch_cycle =
RPC_service.get_service
~description:
"Returns the cycle at which the launch of the Adaptive Issuance \
feature is set to happen. A result of None means that the feature is \
not yet set to launch."
~query:RPC_query.empty
~output:(Data_encoding.option Cycle.encoding)
RPC_path.(context_path / "adaptive_issuance_launch_cycle")
let expected_issuance =
RPC_service.get_service
~description:
"Returns the expected issued tez for the provided block and the next \
five cycles"
~query:RPC_query.empty
~output:(Data_encoding.list expected_rewards_encoding)
RPC_path.(path / "expected_issuance")
end
let q_to_float_string q =
let offset = 1000 in
let unit = Z.div q.Q.num q.den in
let q = Q.(sub q (unit /// Z.one)) in
let q = Q.(mul q (offset // 1)) in
let dec = Z.div q.num q.den in
let padded_dec_string = Format.asprintf "%03d" (Z.to_int dec) in
Format.asprintf "%a.%s" Z.pp_print unit padded_dec_string
let current_rewards_per_minute ctxt =
let open Lwt_result_syntax in
let base_total_issued_per_minute =
(Constants.issuance_weights ctxt).base_total_issued_per_minute
in
let q_base_total_issued_per_minute =
Tez.to_mutez base_total_issued_per_minute |> Q.of_int64
in
let cycle = (Level.current ctxt).cycle in
let* f = Delegate.Rewards.For_RPC.get_reward_coeff ctxt ~cycle in
let f = Q.mul f q_base_total_issued_per_minute in
return f
let current_yearly_rate_value ~formatter ctxt =
let open Lwt_result_syntax in
let q_min_per_year = Q.of_int 525600 in
let* total_supply = Contract.get_total_supply ctxt in
let q_total_supply = Tez.to_mutez total_supply |> Q.of_int64 in
let* f = current_rewards_per_minute ctxt in
let f = Q.div f q_total_supply in
let f = Q.mul f q_min_per_year in
let f = Q.(mul f (100 // 1)) in
return (formatter f)
let collect_expected_rewards ~ctxt =
let open Lwt_result_syntax in
let open Delegate.Rewards in
let ctxt_cycle = (Level.current ctxt).cycle in
let csts = (Constants.all ctxt).parametric in
let reward_of_cycle cycle =
if Cycle.(cycle = ctxt_cycle) then
return
{
cycle;
baking_reward_fixed_portion = baking_reward_fixed_portion ctxt;
baking_reward_bonus_per_slot = baking_reward_bonus_per_slot ctxt;
attesting_reward_per_slot = attesting_reward_per_slot ctxt;
liquidity_baking_subsidy = liquidity_baking_subsidy ctxt;
seed_nonce_revelation_tip = seed_nonce_revelation_tip ctxt;
vdf_revelation_tip = vdf_revelation_tip ctxt;
}
else
let open Delegate.Rewards.For_RPC in
let* coeff = get_reward_coeff ctxt ~cycle in
return
{
cycle;
baking_reward_fixed_portion =
reward_from_constants
~coeff
csts
~reward_kind:Baking_reward_fixed_portion;
baking_reward_bonus_per_slot =
reward_from_constants
~coeff
csts
~reward_kind:Baking_reward_bonus_per_slot;
attesting_reward_per_slot =
reward_from_constants
~coeff
csts
~reward_kind:Attesting_reward_per_slot;
liquidity_baking_subsidy =
reward_from_constants
~coeff
csts
~reward_kind:Liquidity_baking_subsidy;
seed_nonce_revelation_tip =
reward_from_constants
~coeff
csts
~reward_kind:Seed_nonce_revelation_tip;
vdf_revelation_tip =
reward_from_constants ~coeff csts ~reward_kind:Vdf_revelation_tip;
}
in
let queried_cycles =
Cycle.(ctxt_cycle ---> add ctxt_cycle csts.preserved_cycles)
in
List.map_es reward_of_cycle queried_cycles
let register () =
let open Services_registration in
let open Lwt_result_syntax in
register0 ~chunked:false S.total_supply (fun ctxt () () ->
Contract.get_total_supply ctxt) ;
register0 ~chunked:false S.total_frozen_stake (fun ctxt () () ->
let cycle = (Level.current ctxt).cycle in
Stake_distribution.get_total_frozen_stake ctxt cycle) ;
register0 ~chunked:false S.current_yearly_rate (fun ctxt () () ->
current_yearly_rate_value ~formatter:q_to_float_string ctxt) ;
register0 ~chunked:false S.current_yearly_rate_exact (fun ctxt () () ->
current_yearly_rate_value ~formatter:(fun x -> x) ctxt) ;
register0 ~chunked:false S.current_issuance_per_minute (fun ctxt () () ->
let* f = current_rewards_per_minute ctxt in
return (Tez.of_mutez_exn (Q.to_int64 f))) ;
register0 ~chunked:false S.launch_cycle (fun ctxt () () ->
Adaptive_issuance.launch_cycle ctxt) ;
register0 ~chunked:false S.expected_issuance (fun ctxt () () ->
collect_expected_rewards ~ctxt)
let total_supply ctxt block =
RPC_context.make_call0 S.total_supply ctxt block () ()
let total_frozen_stake ctxt block =
RPC_context.make_call0 S.total_frozen_stake ctxt block () ()
let current_yearly_rate ctxt block =
RPC_context.make_call0 S.current_yearly_rate ctxt block () ()
let current_yearly_rate_exact ctxt block =
RPC_context.make_call0 S.current_yearly_rate_exact ctxt block () ()
let current_issuance_per_minute ctxt block =
RPC_context.make_call0 S.current_issuance_per_minute ctxt block () ()
let launch_cycle ctxt block =
RPC_context.make_call0 S.launch_cycle ctxt block () ()
let expected_issuance ctxt block =
RPC_context.make_call0 S.expected_issuance ctxt block () ()