Alpha: added rpc 'proto/delegate/<id>/frozen_balances'

This commit is contained in:
Marco Stronati 2018-04-12 15:28:03 +02:00 committed by Grégoire Henry
parent 3ad1ea073e
commit a491083586
5 changed files with 73 additions and 5 deletions

View File

@ -550,6 +550,15 @@ module Delegate : sig
val frozen_balance: val frozen_balance:
context -> public_key_hash -> Tez.t tzresult Lwt.t context -> public_key_hash -> Tez.t tzresult Lwt.t
type frozen_balances = {
deposit : Tez.t ;
fees : Tez.t ;
rewards : Tez.t ;
}
val frozen_balances:
context -> public_key_hash -> frozen_balances tzresult Lwt.t
val full_balance: val full_balance:
context -> public_key_hash -> Tez.t tzresult Lwt.t context -> public_key_hash -> Tez.t tzresult Lwt.t

View File

@ -322,5 +322,37 @@ module Endorser = struct
end end
module S = struct
let frozen_balances_encoding =
let open Delegate in
Data_encoding.(
conv
(fun { deposit ; fees ; rewards } ->
(deposit, fees, rewards))
(fun (deposit, fees, rewards) ->
{ deposit ; fees ; rewards }
)
(obj3
(req "deposit" Tez.encoding)
(req "fees" Tez.encoding)
(req "rewards" Tez.encoding)))
let frozen_balances =
RPC_service.post_service
~description: "Returns the amount of frozen tokens associated to a given key."
~query: RPC_query.empty
~input: Data_encoding.empty
~output: frozen_balances_encoding
RPC_path.(open_root / "delegate" /: Ed25519.Public_key_hash.rpc_arg / "frozen_balances")
end
let () =
let open Services_registration in
register1 S.frozen_balances begin fun ctxt pkh () () ->
Delegate.frozen_balances ctxt pkh
end
let frozen_balances ctxt block pkh =
RPC_context.make_call1 S.frozen_balances ctxt block pkh () ()
let baking_rights = Baker.I.baking_rights let baking_rights = Baker.I.baking_rights
let endorsement_rights = Endorser.I.endorsement_rights let endorsement_rights = Endorser.I.endorsement_rights

View File

@ -9,6 +9,9 @@
open Alpha_context open Alpha_context
val frozen_balances : 'a #RPC_context.simple -> 'a -> Ed25519.Public_key_hash.t ->
Delegate.frozen_balances shell_tzresult Lwt.t
module Baker : sig module Baker : sig
val rights: val rights:

View File

@ -359,25 +359,38 @@ let has_frozen_balance ctxt delegate cycle =
get_frozen_rewards ctxt contract cycle >>=? fun rewards -> get_frozen_rewards ctxt contract cycle >>=? fun rewards ->
return Tez_repr.(rewards <> zero) return Tez_repr.(rewards <> zero)
let frozen_balance ctxt delegate = type frozen_balances = {
deposit : Tez_repr.t ;
fees : Tez_repr.t ;
rewards : Tez_repr.t ;
}
let frozen_balances ctxt delegate =
let contract = Contract_repr.implicit_contract delegate in let contract = Contract_repr.implicit_contract delegate in
let balance = Ok Tez_repr.zero in let balance = Ok Tez_repr.zero in
Storage.Contract.Frozen_deposits.fold Storage.Contract.Frozen_deposits.fold
(ctxt, contract) ~init:balance (ctxt, contract) ~init:balance
~f:(fun _cycle amount acc -> ~f:(fun _cycle amount acc ->
Lwt.return acc >>=? fun acc -> Lwt.return acc >>=? fun acc ->
Lwt.return (Tez_repr.(acc +? amount))) >>= fun balance -> Lwt.return (Tez_repr.(acc +? amount))) >>=? fun deposit ->
Storage.Contract.Frozen_fees.fold Storage.Contract.Frozen_fees.fold
(ctxt, contract) ~init:balance (ctxt, contract) ~init:balance
~f:(fun _cycle amount acc -> ~f:(fun _cycle amount acc ->
Lwt.return acc >>=? fun acc -> Lwt.return acc >>=? fun acc ->
Lwt.return (Tez_repr.(acc +? amount))) >>= fun balance -> Lwt.return (Tez_repr.(acc +? amount))) >>=? fun fees ->
Storage.Contract.Frozen_rewards.fold Storage.Contract.Frozen_rewards.fold
(ctxt, contract) ~init:balance (ctxt, contract) ~init:balance
~f:(fun _cycle amount acc -> ~f:(fun _cycle amount acc ->
Lwt.return acc >>=? fun acc -> Lwt.return acc >>=? fun acc ->
Lwt.return (Tez_repr.(acc +? amount))) >>= fun balance -> Lwt.return (Tez_repr.(acc +? amount))) >>=? fun rewards ->
Lwt.return balance return { deposit ; fees ; rewards }
let frozen_balance ctxt delegate =
frozen_balances ctxt delegate >>=? fun { deposit ; fees ; rewards } ->
Error_monad.fold_left_s (fun amount sum -> Lwt.return Tez_repr.(amount +? sum))
Tez_repr.zero
[deposit; fees; rewards] >>=
Lwt.return
let full_balance ctxt delegate = let full_balance ctxt delegate =
let contract = Contract_repr.implicit_contract delegate in let contract = Contract_repr.implicit_contract delegate in

View File

@ -90,6 +90,17 @@ val frozen_balance:
Raw_context.t -> Signature.Public_key_hash.t -> Raw_context.t -> Signature.Public_key_hash.t ->
Tez_repr.t tzresult Lwt.t Tez_repr.t tzresult Lwt.t
type frozen_balances = {
deposit : Tez_repr.t ;
fees : Tez_repr.t ;
rewards : Tez_repr.t ;
}
(** Returns the amount of frozen deposit, fees and rewards associated to a given key. *)
val frozen_balances:
Raw_context.t -> Ed25519.Public_key_hash.t ->
frozen_balances tzresult Lwt.t
(** Returns the full 'balance' of the implicit contract associated to (** Returns the full 'balance' of the implicit contract associated to
a given key, i.e. the sum of the spendable balance and of the a given key, i.e. the sum of the spendable balance and of the
frozen balance. *) frozen balance. *)