2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
open Client_proto_args
|
|
|
|
open Client_proto_contracts
|
|
|
|
open Client_proto_programs
|
|
|
|
open Client_keys
|
2016-11-14 18:55:24 +04:00
|
|
|
module Ed25519 = Environment.Ed25519
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let check_contract cctxt neu =
|
|
|
|
RawContractAlias.mem cctxt neu >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| true ->
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.error "contract '%s' already exists" neu
|
2016-09-08 21:13:10 +04:00
|
|
|
| false ->
|
|
|
|
Lwt.return ()
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let get_delegate_pkh cctxt = function
|
2016-09-08 21:13:10 +04:00
|
|
|
| None -> Lwt.return None
|
|
|
|
| Some delegate ->
|
|
|
|
Lwt.catch
|
|
|
|
(fun () ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Public_key_hash.find cctxt delegate >>= fun r ->
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return (Some r))
|
|
|
|
(fun _ -> Lwt.return None)
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let get_timestamp cctxt block =
|
|
|
|
Client_node_rpcs.Blocks.timestamp cctxt block >>= fun v ->
|
|
|
|
cctxt.message "%s" (Time.to_notation v)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let list_contracts cctxt block =
|
|
|
|
Client_proto_rpcs.Context.Contract.list cctxt block >>=? fun contracts ->
|
2016-09-08 21:13:10 +04:00
|
|
|
iter_s (fun h ->
|
|
|
|
begin match Contract.is_default h with
|
|
|
|
| Some m -> begin
|
2016-12-03 16:05:02 +04:00
|
|
|
Public_key_hash.rev_find cctxt m >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| None -> Lwt.return ""
|
|
|
|
| Some nm ->
|
2016-12-03 16:05:02 +04:00
|
|
|
RawContractAlias.find_opt cctxt nm >|= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| None -> " (known as " ^ nm ^ ")"
|
|
|
|
| Some _ -> " (known as key:" ^ nm ^ ")"
|
|
|
|
end
|
|
|
|
| None -> begin
|
2016-12-03 16:05:02 +04:00
|
|
|
RawContractAlias.rev_find cctxt h >|= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| None -> ""
|
|
|
|
| Some nm -> " (known as " ^ nm ^ ")"
|
|
|
|
end
|
|
|
|
end >>= fun nm ->
|
|
|
|
let kind = match Contract.is_default h with
|
|
|
|
| Some _ -> " (default)"
|
|
|
|
| None -> "" in
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.message "%s%s%s" (Contract.to_b48check h) kind nm >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
return ())
|
|
|
|
contracts
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let transfer cctxt
|
|
|
|
block ?force
|
2016-09-08 21:13:10 +04:00
|
|
|
~source ~src_pk ~src_sk ~destination ?arg ~amount ~fee () =
|
|
|
|
let open Cli_entries in
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_node_rpcs.Blocks.net cctxt block >>= fun net ->
|
2016-09-08 21:13:10 +04:00
|
|
|
begin match arg with
|
|
|
|
| Some arg ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_proto_programs.parse_data cctxt arg >>= fun arg ->
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return (Some arg)
|
|
|
|
| None -> Lwt.return None
|
|
|
|
end >>= fun parameters ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_proto_rpcs.Context.Contract.counter cctxt block source >>=? fun pcounter ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let counter = Int32.succ pcounter in
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.message "Acquired the source's sequence counter (%ld -> %ld)."
|
2016-11-22 17:23:40 +04:00
|
|
|
pcounter counter >>= fun () ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_proto_rpcs.Helpers.Forge.Manager.transaction cctxt block
|
2016-09-08 21:13:10 +04:00
|
|
|
~net ~source ~sourcePubKey:src_pk ~counter ~amount
|
|
|
|
~destination ?parameters ~fee () >>=? fun bytes ->
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.message "Forged the raw transaction frame." >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let signed_bytes = Ed25519.append_signature src_sk bytes in
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_node_rpcs.inject_operation cctxt ?force ~wait:true signed_bytes >>=? fun oph ->
|
|
|
|
cctxt.answer "Operation successfully injected in the node." >>= fun () ->
|
|
|
|
cctxt.answer "Operation hash is '%a'." Operation_hash.pp oph >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
return ()
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let originate_account cctxt
|
|
|
|
block ?force
|
2016-09-08 21:13:10 +04:00
|
|
|
~source ~src_pk ~src_sk ~manager_pkh ?delegatable ?spendable ?delegate ~balance ~fee () =
|
|
|
|
let open Cli_entries in
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_node_rpcs.Blocks.net cctxt block >>= fun net ->
|
|
|
|
Client_proto_rpcs.Context.Contract.counter cctxt block source >>=? fun pcounter ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let counter = Int32.succ pcounter in
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.message "Acquired the source's sequence counter (%ld -> %ld)."
|
2016-11-22 17:23:40 +04:00
|
|
|
pcounter counter >>= fun () ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_proto_rpcs.Helpers.Forge.Manager.origination cctxt block
|
2016-09-08 21:13:10 +04:00
|
|
|
~net ~source ~sourcePubKey:src_pk ~managerPubKey:manager_pkh
|
|
|
|
~counter ~balance ?spendable
|
|
|
|
?delegatable ?delegatePubKey:delegate ~fee () >>=? fun (contract, bytes) ->
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.message "Forged the raw origination frame." >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let signed_bytes = Ed25519.append_signature src_sk bytes in
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_node_rpcs.inject_operation cctxt ?force ~wait:true signed_bytes >>=? fun oph ->
|
|
|
|
cctxt.message "Operation successfully injected in the node." >>= fun () ->
|
|
|
|
cctxt.message "Operation hash is '%a'." Operation_hash.pp oph >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
return contract
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let originate_contract cctxt
|
2016-09-08 21:13:10 +04:00
|
|
|
block ?force
|
|
|
|
~source ~src_pk ~src_sk ~manager_pkh ~balance ?delegatable ?delegatePubKey
|
|
|
|
~(code:Script.code) ~init ~fee () =
|
|
|
|
let open Cli_entries in
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_proto_programs.parse_data cctxt init >>= fun storage ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let init = Script.{ storage ; storage_type = code.storage_type } in
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_proto_rpcs.Context.Contract.counter cctxt block source >>=? fun pcounter ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let counter = Int32.succ pcounter in
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.message "Acquired the source's sequence counter (%ld -> %ld)."
|
2016-11-22 17:23:40 +04:00
|
|
|
pcounter counter >>= fun () ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_node_rpcs.Blocks.net cctxt block >>= fun net ->
|
|
|
|
Client_proto_rpcs.Helpers.Forge.Manager.origination cctxt block
|
2016-09-08 21:13:10 +04:00
|
|
|
~net ~source ~sourcePubKey:src_pk ~managerPubKey:manager_pkh
|
|
|
|
~counter ~balance ~spendable:!spendable
|
|
|
|
?delegatable ?delegatePubKey
|
|
|
|
~script:(code, init) ~fee () >>=? fun (contract, bytes) ->
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.message "Forged the raw origination frame." >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let signed_bytes = Ed25519.append_signature src_sk bytes in
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_node_rpcs.inject_operation cctxt ?force ~wait:true signed_bytes >>=? fun oph ->
|
|
|
|
cctxt.message "Operation successfully injected in the node." >>= fun () ->
|
|
|
|
cctxt.message "Operation hash is '%a'." Operation_hash.pp oph >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
return contract
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let group =
|
|
|
|
{ Cli_entries.name = "context" ;
|
|
|
|
title = "Block contextual commands (see option -block)" }
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
let commands () =
|
|
|
|
let open Cli_entries in
|
2016-12-03 16:05:02 +04:00
|
|
|
[ command ~group ~desc: "access the timestamp of the block"
|
2016-09-08 21:13:10 +04:00
|
|
|
(fixed [ "get" ; "timestamp" ])
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun cctxt -> get_timestamp cctxt (block ())) ;
|
|
|
|
command ~group ~desc: "lists all non empty contracts of the block"
|
2016-09-08 21:13:10 +04:00
|
|
|
(fixed [ "list" ; "contracts" ])
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun cctxt ->
|
|
|
|
list_contracts cctxt (block ()) >>= fun res ->
|
|
|
|
Client_proto_rpcs.handle_error cctxt res) ;
|
|
|
|
command ~group ~desc: "get the bootstrap keys and bootstrap contract handle"
|
2016-09-08 21:13:10 +04:00
|
|
|
(fixed [ "bootstrap" ])
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun cctxt ->
|
|
|
|
Client_proto_rpcs.Constants.bootstrap cctxt `Genesis >>= fun accounts ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let cpt = ref 0 in
|
|
|
|
Lwt_list.iter_s
|
|
|
|
(fun { Bootstrap.public_key_hash = pkh ;
|
|
|
|
public_key = pk ; secret_key = sk } ->
|
|
|
|
incr cpt ;
|
|
|
|
let name = Printf.sprintf "bootstrap%d" !cpt in
|
2016-12-03 16:05:02 +04:00
|
|
|
Public_key_hash.add cctxt name pkh >>= fun () ->
|
|
|
|
Public_key.add cctxt name pk >>= fun () ->
|
|
|
|
Secret_key.add cctxt name sk >>= fun () ->
|
|
|
|
cctxt.message "Bootstrap keys added under the name '%s'." name)
|
2016-09-08 21:13:10 +04:00
|
|
|
accounts >>= fun () ->
|
|
|
|
Lwt.return_unit) ;
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "get the balance of a contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "get" ; "balance" ]
|
2016-11-22 17:23:40 +04:00
|
|
|
@@ ContractAlias.destination_param ~name:"src" ~desc:"source contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun (_, contract) cctxt ->
|
|
|
|
Client_proto_rpcs.Context.Contract.balance cctxt (block ()) contract
|
|
|
|
>>= Client_proto_rpcs.handle_error cctxt >>= fun amount ->
|
|
|
|
cctxt.answer "%a %s" Tez.pp amount tez_sym);
|
|
|
|
command ~group ~desc: "get the manager of a block"
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "get" ; "manager" ]
|
2016-11-22 17:23:40 +04:00
|
|
|
@@ ContractAlias.destination_param ~name:"src" ~desc:"source contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun (_, contract) cctxt ->
|
|
|
|
Client_proto_rpcs.Context.Contract.manager cctxt (block ()) contract
|
|
|
|
>>= Client_proto_rpcs.handle_error cctxt >>= fun manager ->
|
|
|
|
Public_key_hash.rev_find cctxt manager >>= fun mn ->
|
|
|
|
Public_key_hash.to_source cctxt manager >>= fun m ->
|
|
|
|
cctxt.message "%s (%s)" m
|
2016-11-22 17:23:40 +04:00
|
|
|
(match mn with None -> "unknown" | Some n -> "known as " ^ n));
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "open a new account"
|
2016-09-08 21:13:10 +04:00
|
|
|
~args: ([ fee_arg ; delegate_arg ; force_arg ]
|
|
|
|
@ delegatable_args @ spendable_args)
|
|
|
|
(prefixes [ "originate" ; "account" ]
|
|
|
|
@@ RawContractAlias.fresh_alias_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name: "new" ~desc: "name of the new contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ prefix "for"
|
|
|
|
@@ Public_key_hash.alias_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name: "mgr" ~desc: "manager of the new contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ prefix "transfering"
|
|
|
|
@@ tez_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name: "qty" ~desc: "amount taken from source"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ prefix "from"
|
|
|
|
@@ ContractAlias.alias_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name:"src" ~desc: "name of the source contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun neu (_, manager) balance (_, source) cctxt ->
|
|
|
|
check_contract cctxt neu >>= fun () ->
|
|
|
|
get_delegate_pkh cctxt !delegate >>= fun delegate ->
|
|
|
|
(Client_proto_contracts.get_manager cctxt (block ()) source >>=? fun src_pkh ->
|
|
|
|
Client_keys.get_key cctxt src_pkh >>=? fun (src_name, src_pk, src_sk) ->
|
|
|
|
cctxt.message "Got the source's manager keys (%s)." src_name >>= fun () ->
|
|
|
|
originate_account cctxt (block ()) ~force:!force
|
|
|
|
~source ~src_pk ~src_sk ~manager_pkh:manager ~balance ~fee:!fee
|
|
|
|
~delegatable:!delegatable ~spendable:!spendable ?delegate:delegate
|
|
|
|
()) >>= Client_proto_rpcs.handle_error cctxt >>= fun contract ->
|
|
|
|
RawContractAlias.add cctxt neu contract) ;
|
|
|
|
command ~group ~desc: "open a new scripted account"
|
2016-09-08 21:13:10 +04:00
|
|
|
~args: ([ fee_arg ; delegate_arg ; force_arg ] @
|
|
|
|
delegatable_args @ spendable_args @ [ init_arg ])
|
|
|
|
(prefixes [ "originate" ; "contract" ]
|
|
|
|
@@ RawContractAlias.fresh_alias_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name: "new" ~desc: "name of the new contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ prefix "for"
|
|
|
|
@@ Public_key_hash.alias_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name: "mgr" ~desc: "manager of the new contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ prefix "transfering"
|
|
|
|
@@ tez_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name: "qty" ~desc: "amount taken from source"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ prefix "from"
|
|
|
|
@@ ContractAlias.alias_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name:"src" ~desc: "name of the source contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ prefix "running"
|
|
|
|
@@ Program.source_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name:"prg" ~desc: "script of the account\n\
|
2016-12-03 16:05:02 +04:00
|
|
|
combine with -init if the storage type is non void"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun neu (_, manager) balance (_, source) code cctxt ->
|
|
|
|
check_contract cctxt neu >>= fun () ->
|
|
|
|
get_delegate_pkh cctxt !delegate >>= fun delegate ->
|
|
|
|
(Client_proto_contracts.get_manager cctxt (block ()) source >>=? fun src_pkh ->
|
|
|
|
Client_keys.get_key cctxt src_pkh >>=? fun (src_name, src_pk, src_sk) ->
|
|
|
|
cctxt.message "Got the source's manager keys (%s)." src_name >>= fun () ->
|
|
|
|
originate_contract cctxt (block ()) ~force:!force
|
|
|
|
~source ~src_pk ~src_sk ~manager_pkh:manager ~balance ~fee:!fee
|
|
|
|
~delegatable:!delegatable ?delegatePubKey:delegate ~code ~init:!init
|
|
|
|
()) >>= Client_proto_rpcs.handle_error cctxt >>= fun contract ->
|
|
|
|
RawContractAlias.add cctxt neu contract) ;
|
|
|
|
command ~group ~desc: "transfer tokens"
|
2016-09-08 21:13:10 +04:00
|
|
|
~args: [ fee_arg ; arg_arg ; force_arg ]
|
|
|
|
(prefixes [ "transfer" ]
|
|
|
|
@@ tez_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name: "qty" ~desc: "amount taken from source"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ prefix "from"
|
|
|
|
@@ ContractAlias.alias_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name: "src" ~desc: "name of the source contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ prefix "to"
|
|
|
|
@@ ContractAlias.destination_param
|
2016-11-22 17:23:40 +04:00
|
|
|
~name: "dst" ~desc: "name/literal of the destination contract"
|
2016-09-08 21:13:10 +04:00
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun amount (_, source) (_, destination) cctxt ->
|
|
|
|
(Client_proto_contracts.get_manager cctxt (block ()) source >>=? fun src_pkh ->
|
|
|
|
Client_keys.get_key cctxt src_pkh >>=? fun (src_name, src_pk, src_sk) ->
|
|
|
|
cctxt.message "Got the source's manager keys (%s)." src_name >>= fun () ->
|
|
|
|
transfer cctxt (block ()) ~force:!force
|
|
|
|
~source ~src_pk ~src_sk ~destination ?arg:!arg ~amount ~fee:!fee ()) >>=
|
|
|
|
Client_proto_rpcs.handle_error cctxt)
|
2016-09-08 21:13:10 +04:00
|
|
|
]
|