Client: lift some commands into functions.

This commit is contained in:
Guillem Rieu 2017-03-15 01:20:25 +01:00 committed by Benjamin Canou
parent 83f2e0dcd3
commit 9c2564391c
4 changed files with 68 additions and 20 deletions

View File

@ -35,7 +35,25 @@ let get_timestamp cctxt block =
let list_contracts cctxt block =
Client_proto_rpcs.Context.Contract.list cctxt block >>=? fun contracts ->
iter_s (fun h ->
map_s (fun h ->
begin match Contract.is_default h with
| Some m -> begin
Public_key_hash.rev_find cctxt m >|= function
| None -> ""
| Some nm -> nm
end
| None -> begin
RawContractAlias.rev_find cctxt h >|= function
| None -> ""
| Some nm -> nm
end
end >>= fun alias ->
return (alias, h, Contract.is_default h))
contracts
let list_contract_labels cctxt block =
Client_proto_rpcs.Context.Contract.list cctxt block >>=? fun contracts ->
map_s (fun h ->
begin match Contract.is_default h with
| Some m -> begin
Public_key_hash.rev_find cctxt m >>= function
@ -54,10 +72,13 @@ let list_contracts cctxt block =
let kind = match Contract.is_default h with
| Some _ -> " (default)"
| None -> "" in
cctxt.message "%s%s%s" (Contract.to_b58check h) kind nm >>= fun () ->
return ())
let h_b58 = Contract.to_b58check h in
return (nm, h_b58, kind))
contracts
let get_balance cctxt block contract =
Client_proto_rpcs.Context.Contract.balance cctxt block contract
let transfer cctxt
block ?force
~source ~src_pk ~src_sk ~destination ?arg ~amount ~fee () =
@ -174,16 +195,19 @@ let commands () =
command ~group ~desc: "lists all non empty contracts of the block"
(fixed [ "list" ; "contracts" ])
(fun cctxt ->
list_contracts cctxt cctxt.config.block >>= fun res ->
Client_proto_rpcs.handle_error cctxt res) ;
list_contract_labels cctxt cctxt.config.block >>= fun res ->
Client_proto_rpcs.handle_error cctxt res >>= fun contracts ->
Lwt_list.iter_s (fun (alias, hash, kind) ->
cctxt.message "%s%s%s" hash kind alias)
contracts) ;
command ~group ~desc: "get the balance of a contract"
(prefixes [ "get" ; "balance" ]
@@ ContractAlias.destination_param ~name:"src" ~desc:"source contract"
@@ stop)
(fun (_, contract) cctxt ->
Client_proto_rpcs.Context.Contract.balance cctxt cctxt.config.block contract
get_balance cctxt cctxt.config.block contract
>>= Client_proto_rpcs.handle_error cctxt >>= fun amount ->
cctxt.answer "%a %s" Tez.pp amount tez_sym);
cctxt.answer "%a %s" Tez.pp amount tez_sym) ;
command ~group ~desc: "get the manager of a block"
(prefixes [ "get" ; "manager" ]
@@ ContractAlias.destination_param ~name:"src" ~desc:"source contract"

View File

@ -7,6 +7,18 @@
(* *)
(**************************************************************************)
val list_contracts:
Client_commands.context ->
Client_proto_rpcs.block ->
(string * Contract.t * Environment.Ed25519.Public_key_hash.t option)
list tzresult Lwt.t
val get_balance:
Client_commands.context ->
Client_proto_rpcs.block ->
Contract.t ->
Tez.t tzresult Lwt.t
val transfer:
Client_commands.context ->
Client_proto_rpcs.block ->

View File

@ -87,6 +87,22 @@ module ContractAlias = struct
end
let list_contracts cctxt =
(* List contracts *)
RawContractAlias.load cctxt >>= fun raw_contracts ->
Lwt_list.map_s (fun (n, v) ->
Lwt.return ("", n, v))
raw_contracts >>= fun contracts ->
Client_keys.Public_key_hash.load cctxt >>= fun keys ->
(* List accounts (default contracts of identities) *)
Lwt_list.map_s (fun (n, v) ->
RawContractAlias.mem cctxt n >>= fun mem ->
let p = if mem then "key:" else "" in
let v' = Contract.default_contract v in
Lwt.return (p, n, v'))
keys >>= fun accounts ->
Lwt.return (contracts @ accounts)
let get_manager cctxt block source =
match Contract.is_default source with
| Some hash -> return hash
@ -147,19 +163,11 @@ let commands () =
command ~group ~desc: "lists all known contracts"
(fixed [ "list" ; "known" ; "contracts" ])
(fun cctxt ->
RawContractAlias.load cctxt >>= fun list ->
Lwt_list.iter_s (fun (n, v) ->
let v = Contract.to_b58check v in
cctxt.message "%s: %s" n v)
list >>= fun () ->
Client_keys.Public_key_hash.load cctxt >>= fun list ->
Lwt_list.iter_s (fun (n, v) ->
RawContractAlias.mem cctxt n >>= fun mem ->
let p = if mem then "key:" else "" in
let v = Contract.to_b58check (Contract.default_contract v) in
cctxt.message "%s%s: %s" p n v)
list >>= fun () ->
Lwt.return ()) ;
list_contracts cctxt >>= fun contracts ->
Lwt_list.iter_s (fun (prefix, alias, contract) ->
cctxt.message "%s%s: %s" prefix alias
(Contract.to_b58check contract))
contracts) ;
command ~group ~desc: "forget all known contracts"
(fixed [ "forget" ; "all" ; "contracts" ])
(fun cctxt ->

View File

@ -32,6 +32,10 @@ module ContractAlias : sig
Contract.t -> string Lwt.t
end
val list_contracts:
Client_commands.context ->
(string * string * Contract.t) list Lwt.t
val get_manager:
Client_commands.context ->
Client_proto_rpcs.block ->