2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2017-11-14 03:36:14 +04:00
|
|
|
(* Copyright (c) 2014 - 2017. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
open Tezos_micheline
|
2016-09-08 21:13:10 +04:00
|
|
|
open Client_proto_contracts
|
|
|
|
open Client_keys
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let get_balance (rpc : #Client_rpcs.rpc_sig) block contract =
|
|
|
|
Client_proto_rpcs.Context.Contract.balance rpc block contract
|
2017-03-15 04:20:25 +04:00
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let get_storage (rpc : #Client_rpcs.rpc_sig) block contract =
|
|
|
|
Client_proto_rpcs.Context.Contract.storage rpc block contract
|
2017-07-24 17:57:03 +04:00
|
|
|
|
2017-04-20 10:49:14 +04:00
|
|
|
let rec find_predecessor rpc_config h n =
|
|
|
|
if n <= 0 then
|
|
|
|
return (`Hash h)
|
|
|
|
else
|
|
|
|
Client_node_rpcs.Blocks.predecessor rpc_config (`Hash h) >>=? fun h ->
|
|
|
|
find_predecessor rpc_config h (n-1)
|
|
|
|
|
|
|
|
let get_branch rpc_config block branch =
|
2017-11-27 09:13:12 +04:00
|
|
|
let branch = Option.unopt ~default:0 branch in (* TODO export parameter *)
|
2017-11-20 06:37:06 +04:00
|
|
|
let block = Client_rpcs.last_baked_block block in
|
2017-04-20 10:49:14 +04:00
|
|
|
begin
|
|
|
|
match block with
|
|
|
|
| `Head n -> return (`Head (n+branch))
|
|
|
|
| `Test_head n -> return (`Test_head (n+branch))
|
|
|
|
| `Hash h -> find_predecessor rpc_config h branch
|
|
|
|
| `Genesis -> return `Genesis
|
|
|
|
end >>=? fun block ->
|
|
|
|
Client_node_rpcs.Blocks.info rpc_config block >>=? fun { net_id ; hash } ->
|
|
|
|
return (net_id, hash)
|
|
|
|
|
2017-11-04 03:16:05 +04:00
|
|
|
let parse_expression arg =
|
|
|
|
Lwt.return
|
|
|
|
(Micheline_parser.no_parsing_error
|
|
|
|
(Michelson_v1_parser.parse_expression arg))
|
|
|
|
|
2017-04-06 00:33:46 +04:00
|
|
|
let transfer rpc_config
|
2017-04-20 10:49:14 +04:00
|
|
|
block ?force ?branch
|
2016-09-08 21:13:10 +04:00
|
|
|
~source ~src_pk ~src_sk ~destination ?arg ~amount ~fee () =
|
2017-04-20 10:49:14 +04:00
|
|
|
get_branch rpc_config block branch >>=? fun (net_id, branch) ->
|
2016-09-08 21:13:10 +04:00
|
|
|
begin match arg with
|
|
|
|
| Some arg ->
|
2017-11-04 03:16:05 +04:00
|
|
|
parse_expression arg >>=? fun { expanded = arg } ->
|
2017-11-02 21:57:17 +04:00
|
|
|
return (Some arg)
|
2017-04-05 12:22:41 +04:00
|
|
|
| None -> return None
|
|
|
|
end >>=? fun parameters ->
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_rpcs.Context.Contract.counter
|
|
|
|
rpc_config block source >>=? fun pcounter ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let counter = Int32.succ pcounter in
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_rpcs.Helpers.Forge.Manager.transaction
|
|
|
|
rpc_config block
|
2017-11-14 05:41:37 +04:00
|
|
|
~branch ~source ~sourcePubKey:src_pk ~counter ~amount
|
2016-09-08 21:13:10 +04:00
|
|
|
~destination ?parameters ~fee () >>=? fun bytes ->
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_node_rpcs.Blocks.predecessor rpc_config block >>=? fun predecessor ->
|
2017-02-16 22:01:35 +04:00
|
|
|
let signature = Ed25519.sign src_sk bytes in
|
2017-11-27 09:13:12 +04:00
|
|
|
let signed_bytes = Ed25519.Signature.concat bytes signature in
|
2017-02-16 22:01:35 +04:00
|
|
|
let oph = Operation_hash.hash_bytes [ signed_bytes ] in
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_rpcs.Helpers.apply_operation rpc_config block
|
2017-02-16 22:01:35 +04:00
|
|
|
predecessor oph bytes (Some signature) >>=? fun contracts ->
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_node_rpcs.inject_operation
|
2017-11-14 05:41:37 +04:00
|
|
|
rpc_config ?force ~net_id signed_bytes >>=? fun injected_oph ->
|
2017-02-16 22:01:35 +04:00
|
|
|
assert (Operation_hash.equal oph injected_oph) ;
|
2017-04-06 00:33:46 +04:00
|
|
|
return (oph, contracts)
|
2017-02-16 22:01:35 +04:00
|
|
|
|
2017-11-14 05:41:37 +04:00
|
|
|
let originate rpc_config ?force ?net_id ~block ?signature bytes =
|
2017-02-28 05:48:51 +04:00
|
|
|
let signed_bytes =
|
|
|
|
match signature with
|
|
|
|
| None -> bytes
|
2017-11-27 09:13:12 +04:00
|
|
|
| Some signature -> Ed25519.Signature.concat bytes signature in
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_node_rpcs.Blocks.predecessor rpc_config block >>=? fun predecessor ->
|
2017-02-16 22:01:35 +04:00
|
|
|
let oph = Operation_hash.hash_bytes [ signed_bytes ] in
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_rpcs.Helpers.apply_operation rpc_config block
|
2017-02-28 05:48:51 +04:00
|
|
|
predecessor oph bytes signature >>=? function
|
2017-02-16 22:01:35 +04:00
|
|
|
| [ contract ] ->
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_node_rpcs.inject_operation
|
2017-11-14 05:41:37 +04:00
|
|
|
rpc_config ?force ?net_id signed_bytes >>=? fun injected_oph ->
|
2017-02-16 22:01:35 +04:00
|
|
|
assert (Operation_hash.equal oph injected_oph) ;
|
2017-04-06 00:33:46 +04:00
|
|
|
return (oph, contract)
|
2017-02-16 22:01:35 +04:00
|
|
|
| contracts ->
|
2017-04-06 00:33:46 +04:00
|
|
|
failwith
|
|
|
|
"The origination introduced %d contracts instead of one."
|
|
|
|
(List.length contracts)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let operation_submitted_message (cctxt : #Client_commands.logger) ?(force=false) ?(contracts = []) oph =
|
|
|
|
begin
|
|
|
|
if not force then
|
|
|
|
cctxt#message "Operation successfully injected in the node."
|
|
|
|
else
|
|
|
|
Lwt.return_unit
|
|
|
|
end >>= fun () ->
|
|
|
|
cctxt#message "Operation hash is '%a'." Operation_hash.pp oph >>= fun () ->
|
|
|
|
Lwt_list.iter_s
|
|
|
|
(fun c ->
|
|
|
|
cctxt#message
|
|
|
|
"New contract %a originated from a smart contract."
|
|
|
|
Contract.pp c)
|
|
|
|
contracts >>= return
|
|
|
|
|
|
|
|
let originate_account ?(force=false) ?branch
|
2017-04-06 00:33:46 +04:00
|
|
|
~source ~src_pk ~src_sk ~manager_pkh
|
2017-11-07 20:38:11 +04:00
|
|
|
?delegatable ?delegate ~balance ~fee block rpc_config () =
|
2017-04-20 10:49:14 +04:00
|
|
|
get_branch rpc_config block branch >>=? fun (net_id, branch) ->
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_rpcs.Context.Contract.counter
|
|
|
|
rpc_config block source >>=? fun pcounter ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let counter = Int32.succ pcounter in
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_rpcs.Helpers.Forge.Manager.origination rpc_config block
|
2017-11-14 05:41:37 +04:00
|
|
|
~branch ~source ~sourcePubKey:src_pk ~managerPubKey:manager_pkh
|
2017-11-07 20:38:11 +04:00
|
|
|
~counter ~balance ~spendable:true
|
2017-02-16 22:01:35 +04:00
|
|
|
?delegatable ?delegatePubKey:delegate ~fee () >>=? fun bytes ->
|
2017-02-28 05:48:51 +04:00
|
|
|
let signature = Ed25519.sign src_sk bytes in
|
2017-11-07 20:38:11 +04:00
|
|
|
originate rpc_config ~force ~block ~net_id ~signature bytes
|
2017-02-28 05:48:51 +04:00
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let faucet ?force ?branch ~manager_pkh block rpc_config () =
|
2017-04-20 10:49:14 +04:00
|
|
|
get_branch rpc_config block branch >>=? fun (net_id, branch) ->
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_rpcs.Helpers.Forge.Anonymous.faucet
|
2017-11-14 05:41:37 +04:00
|
|
|
rpc_config block ~branch ~id:manager_pkh () >>=? fun bytes ->
|
|
|
|
originate rpc_config ?force ~net_id ~block bytes
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-06 00:33:46 +04:00
|
|
|
let delegate_contract rpc_config
|
2017-04-20 10:49:14 +04:00
|
|
|
block ?force ?branch
|
2017-03-21 18:19:35 +04:00
|
|
|
~source ?src_pk ~manager_sk
|
|
|
|
~fee delegate_opt =
|
2017-04-20 10:49:14 +04:00
|
|
|
get_branch rpc_config block branch >>=? fun (net_id, branch) ->
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_rpcs.Context.Contract.counter
|
|
|
|
rpc_config block source >>=? fun pcounter ->
|
2017-03-21 18:19:35 +04:00
|
|
|
let counter = Int32.succ pcounter in
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_rpcs.Helpers.Forge.Manager.delegation rpc_config block
|
2017-11-14 05:41:37 +04:00
|
|
|
~branch ~source ?sourcePubKey:src_pk ~counter ~fee delegate_opt
|
2017-03-21 18:19:35 +04:00
|
|
|
>>=? fun bytes ->
|
2017-11-27 09:13:12 +04:00
|
|
|
let signature = Ed25519.sign manager_sk bytes in
|
|
|
|
let signed_bytes = Ed25519.Signature.concat bytes signature in
|
2017-03-21 18:19:35 +04:00
|
|
|
let oph = Operation_hash.hash_bytes [ signed_bytes ] in
|
2017-05-19 07:14:14 +04:00
|
|
|
Client_node_rpcs.inject_operation
|
2017-11-14 05:41:37 +04:00
|
|
|
rpc_config ?force ~net_id signed_bytes >>=? fun injected_oph ->
|
2017-05-19 07:14:14 +04:00
|
|
|
assert (Operation_hash.equal oph injected_oph) ;
|
|
|
|
return oph
|
2017-03-21 18:19:35 +04:00
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let list_contract_labels (cctxt : Client_commands.full_context) block =
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_rpcs.Context.Contract.list
|
2017-11-07 20:38:11 +04:00
|
|
|
cctxt block >>=? fun contracts ->
|
2017-04-06 00:33:46 +04:00
|
|
|
map_s (fun h ->
|
|
|
|
begin match Contract.is_default h with
|
|
|
|
| Some m -> begin
|
|
|
|
Public_key_hash.rev_find cctxt m >>=? function
|
|
|
|
| None -> return ""
|
|
|
|
| Some nm ->
|
|
|
|
RawContractAlias.find_opt cctxt nm >>=? function
|
|
|
|
| None -> return (" (known as " ^ nm ^ ")")
|
|
|
|
| Some _ -> return (" (known as key:" ^ nm ^ ")")
|
|
|
|
end
|
|
|
|
| None -> begin
|
|
|
|
RawContractAlias.rev_find cctxt h >>=? function
|
|
|
|
| None -> return ""
|
|
|
|
| Some nm -> return (" (known as " ^ nm ^ ")")
|
|
|
|
end
|
|
|
|
end >>=? fun nm ->
|
|
|
|
let kind = match Contract.is_default h with
|
|
|
|
| Some _ -> " (default)"
|
|
|
|
| None -> "" in
|
|
|
|
let h_b58 = Contract.to_b58check h in
|
|
|
|
return (nm, h_b58, kind))
|
|
|
|
contracts
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let message_added_contract (cctxt : Client_commands.full_context) name =
|
|
|
|
cctxt#message "Contract memorized as %s." name
|
2017-04-06 00:33:46 +04:00
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let get_manager (cctxt : Client_commands.full_context) block source =
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_proto_contracts.get_manager
|
2017-11-07 20:38:11 +04:00
|
|
|
cctxt block source >>=? fun src_pkh ->
|
2017-04-06 00:33:46 +04:00
|
|
|
Client_keys.get_key cctxt src_pkh >>=? fun (src_name, src_pk, src_sk) ->
|
|
|
|
return (src_name, src_pkh, src_pk, src_sk)
|
2017-02-27 21:24:26 +04:00
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let dictate rpc_config block command seckey =
|
2017-11-20 06:37:06 +04:00
|
|
|
let block = Client_rpcs.last_baked_block block in
|
2017-04-20 10:49:14 +04:00
|
|
|
Client_node_rpcs.Blocks.info
|
|
|
|
rpc_config block >>=? fun { net_id ; hash = branch } ->
|
2017-05-19 07:14:14 +04:00
|
|
|
Client_proto_rpcs.Helpers.Forge.Dictator.operation
|
2017-11-14 05:41:37 +04:00
|
|
|
rpc_config block ~branch command >>=? fun bytes ->
|
2017-05-19 07:14:14 +04:00
|
|
|
let signature = Ed25519.sign seckey bytes in
|
2017-11-27 09:13:12 +04:00
|
|
|
let signed_bytes = Ed25519.Signature.concat bytes signature in
|
2017-05-19 07:14:14 +04:00
|
|
|
let oph = Operation_hash.hash_bytes [ signed_bytes ] in
|
|
|
|
Client_node_rpcs.inject_operation
|
2017-11-07 20:38:11 +04:00
|
|
|
rpc_config ~net_id signed_bytes >>=? fun injected_oph ->
|
2017-05-19 07:14:14 +04:00
|
|
|
assert (Operation_hash.equal oph injected_oph) ;
|
|
|
|
return oph
|
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let set_delegate (cctxt : #Client_rpcs.rpc_sig) block ~fee contract ~src_pk ~manager_sk opt_delegate =
|
|
|
|
delegate_contract
|
|
|
|
cctxt block ~source:contract
|
|
|
|
~src_pk ~manager_sk ~fee opt_delegate
|
|
|
|
|
|
|
|
let source_to_keys (wallet : #Client_commands.full_context) block source =
|
|
|
|
get_manager wallet block source >>=? fun (_src_name, _src_pkh, src_pk, src_sk) ->
|
|
|
|
return (src_pk, src_sk)
|
|
|
|
|
|
|
|
let save_contract ~force cctxt alias_name contract =
|
|
|
|
RawContractAlias.add ~force cctxt alias_name contract >>=? fun () ->
|
|
|
|
message_added_contract cctxt alias_name >>= fun () ->
|
|
|
|
return ()
|
|
|
|
|
|
|
|
let originate_contract
|
|
|
|
~fee
|
|
|
|
~delegate
|
|
|
|
?(force=false)
|
|
|
|
?(delegatable=true)
|
|
|
|
?(spendable=false)
|
|
|
|
~initial_storage
|
|
|
|
~manager
|
|
|
|
~balance
|
|
|
|
~source
|
|
|
|
~src_pk
|
|
|
|
~src_sk
|
|
|
|
~code
|
|
|
|
(cctxt : Client_commands.full_context) =
|
|
|
|
Lwt.return (Michelson_v1_parser.parse_expression initial_storage) >>= fun result ->
|
|
|
|
Lwt.return (Micheline_parser.no_parsing_error result) >>=?
|
|
|
|
fun { Michelson_v1_parser.expanded = storage } ->
|
|
|
|
let block = cctxt#block in
|
|
|
|
Client_proto_rpcs.Context.Contract.counter
|
|
|
|
cctxt block source >>=? fun pcounter ->
|
|
|
|
let counter = Int32.succ pcounter in
|
|
|
|
get_branch cctxt block None >>=? fun (_net_id, branch) ->
|
|
|
|
Client_proto_rpcs.Helpers.Forge.Manager.origination cctxt block
|
|
|
|
~branch ~source ~sourcePubKey:src_pk ~managerPubKey:manager
|
|
|
|
~counter ~balance ~spendable:spendable
|
|
|
|
~delegatable ?delegatePubKey:delegate
|
|
|
|
~script:{ code ; storage } ~fee () >>=? fun bytes ->
|
|
|
|
let signature = Ed25519.sign src_sk bytes in
|
|
|
|
originate cctxt ~force ~block ~signature bytes
|