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 = let list_contracts cctxt block =
Client_proto_rpcs.Context.Contract.list cctxt block >>=? fun contracts -> 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 begin match Contract.is_default h with
| Some m -> begin | Some m -> begin
Public_key_hash.rev_find cctxt m >>= function 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 let kind = match Contract.is_default h with
| Some _ -> " (default)" | Some _ -> " (default)"
| None -> "" in | None -> "" in
cctxt.message "%s%s%s" (Contract.to_b58check h) kind nm >>= fun () -> let h_b58 = Contract.to_b58check h in
return ()) return (nm, h_b58, kind))
contracts contracts
let get_balance cctxt block contract =
Client_proto_rpcs.Context.Contract.balance cctxt block contract
let transfer cctxt let transfer cctxt
block ?force block ?force
~source ~src_pk ~src_sk ~destination ?arg ~amount ~fee () = ~source ~src_pk ~src_sk ~destination ?arg ~amount ~fee () =
@ -174,14 +195,17 @@ let commands () =
command ~group ~desc: "lists all non empty contracts of the block" command ~group ~desc: "lists all non empty contracts of the block"
(fixed [ "list" ; "contracts" ]) (fixed [ "list" ; "contracts" ])
(fun cctxt -> (fun cctxt ->
list_contracts cctxt cctxt.config.block >>= fun res -> list_contract_labels cctxt cctxt.config.block >>= fun res ->
Client_proto_rpcs.handle_error cctxt 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" command ~group ~desc: "get the balance of a contract"
(prefixes [ "get" ; "balance" ] (prefixes [ "get" ; "balance" ]
@@ ContractAlias.destination_param ~name:"src" ~desc:"source contract" @@ ContractAlias.destination_param ~name:"src" ~desc:"source contract"
@@ stop) @@ stop)
(fun (_, contract) cctxt -> (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 -> >>= 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" command ~group ~desc: "get the manager of a block"

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: val transfer:
Client_commands.context -> Client_commands.context ->
Client_proto_rpcs.block -> Client_proto_rpcs.block ->

View File

@ -87,6 +87,22 @@ module ContractAlias = struct
end 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 = let get_manager cctxt block source =
match Contract.is_default source with match Contract.is_default source with
| Some hash -> return hash | Some hash -> return hash
@ -147,19 +163,11 @@ let commands () =
command ~group ~desc: "lists all known contracts" command ~group ~desc: "lists all known contracts"
(fixed [ "list" ; "known" ; "contracts" ]) (fixed [ "list" ; "known" ; "contracts" ])
(fun cctxt -> (fun cctxt ->
RawContractAlias.load cctxt >>= fun list -> list_contracts cctxt >>= fun contracts ->
Lwt_list.iter_s (fun (n, v) -> Lwt_list.iter_s (fun (prefix, alias, contract) ->
let v = Contract.to_b58check v in cctxt.message "%s%s: %s" prefix alias
cctxt.message "%s: %s" n v) (Contract.to_b58check contract))
list >>= fun () -> contracts) ;
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 ()) ;
command ~group ~desc: "forget all known contracts" command ~group ~desc: "forget all known contracts"
(fixed [ "forget" ; "all" ; "contracts" ]) (fixed [ "forget" ; "all" ; "contracts" ])
(fun cctxt -> (fun cctxt ->

View File

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