123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openProtocolopenAlpha_contextopenClient_proto_argsletgroup={Tezos_clic.name="tokens";title="Commands for managing FA1.2-compatible smart contracts";}letalias_param=Client_proto_contracts.Contract_alias.destination_paramletimplicit_alias_param=Client_keys.Public_key_hash.source_paramlettoken_contract_param()=Client_proto_contracts.Originated_contract_alias.destination_param~name:"contract"~desc:"name or address of the FA1.2-compatible contract"letfrom_implicit_param()=implicit_alias_param~name:"from"~desc:"name or address of the sender"letfrom_param()=alias_param~name:"from"~desc:"name or address of the sender"letto_param()=alias_param~name:"to"~desc:"name or address of the receiver"letamount_param()=Tezos_clic.param~name:"amount"~desc:"number of tokens"(Tezos_clic.parameter(fun(cctxt:#Client_context.full)s->tryletv=Z.of_stringsinassert(Compare.Z.(v>=Z.zero));Lwt_result_syntax.returnvwith_->cctxt#error"invalid amount (must be a non-negative number)"))lettez_amount_arg=tez_arg~default:"0"~parameter:"tez-amount"~doc:"amount in \xEA\x9C\xA9"letimplicit_as_arg=Client_keys.Public_key_hash.source_arg~long:"as"~doc:"name or address of the caller of the contract"()letpayer_arg=Client_keys.Public_key_hash.source_arg~long:"payer"~doc:"name of the payer (i.e. SOURCE) contract for the transaction"()letcallback_entrypoint_arg=Tezos_clic.arg~doc:"Entrypoint the view should use to callback to"~long:"callback-entrypoint"~placeholder:"name"string_parameterletcontract_call_options=Tezos_clic.args9tez_amount_argfee_argClient_proto_context_commands.dry_run_switchClient_proto_context_commands.verbose_signing_switchgas_limit_argstorage_limit_argcounter_argno_print_source_flagfee_parameter_argsletcontract_view_options=Tezos_clic.args10callback_entrypoint_argtez_amount_argfee_argClient_proto_context_commands.dry_run_switchClient_proto_context_commands.verbose_signing_switchgas_limit_argstorage_limit_argcounter_argno_print_source_flagfee_parameter_argsletview_options=Tezos_clic.args3run_gas_limit_argpayer_arg(unparsing_mode_arg~default:"Readable")letdummy_callback=Contract.ImplicitSignature.Public_key_hash.zeroletget_contract_caller_keys(cctxt:#Client_context.full)source=letopenLwt_result_syntaxinlet*_,caller_pk,caller_sk=Client_keys.get_keycctxtsourceinreturn(source,caller_pk,caller_sk)letcommands_ro():#Protocol_client_context.fullTezos_clic.commandlist=Tezos_clic.[command~group~desc:"Check that a contract is FA1.2-compatible."no_options(prefixes["check";"contract"]@@token_contract_param()@@prefixes["implements";"fa1.2"]@@stop)(fun()contract(cctxt:#Protocol_client_context.full)->letopenLwt_result_syntaxinlet*_=Client_proto_fa12.contract_has_fa12_interfacecctxt~chain:cctxt#chain~block:cctxt#block~contract()inFormat.printf"Contract %a has an FA1.2 interface.\n%!"Contract_hash.ppcontract;return_unit);command~group~desc:"Ask for an address's balance offchain"view_options(prefixes["from";"fa1.2";"contract"]@@token_contract_param()@@prefixes["get";"balance";"for"]@@alias_param~name:"from"~desc:"name or address of the account to lookup (also the source \
contract)"@@stop)(fun(gas,payer,unparsing_mode)contractaddr(cctxt:#Protocol_client_context.full)->letopenLwt_syntaxinletaction=Client_proto_fa12.Get_balance(addr,(dummy_callback,None))inlet*res=Client_proto_fa12.run_view_actioncctxt~chain:cctxt#chain~block:cctxt#block~contract~action~sender:(Someaddr)~gas~payer~unparsing_mode()inClient_proto_programs.print_view_resultcctxtres);command~group~desc:"Ask for an address's allowance offchain"view_options(prefixes["from";"fa1.2";"contract"]@@token_contract_param()@@prefixes["get";"allowance";"on"]@@alias_param~name:"owner"~desc:"name or address of the account giving the allowance"@@prefix"as"@@alias_param~name:"operator"~desc:"name or address of the account receiving the allowance"@@stop)(fun(gas,payer,unparsing_mode)contractsenderdestination(cctxt:#Protocol_client_context.full)->letopenLwt_syntaxinletaction=Client_proto_fa12.Get_allowance(sender,destination,(dummy_callback,None))inlet*res=Client_proto_fa12.run_view_actioncctxt~chain:cctxt#chain~block:cctxt#block~contract~action~sender:(Somesender)~gas~payer~unparsing_mode()inClient_proto_programs.print_view_resultcctxtres);command~group~desc:"Ask for the contract's total token supply offchain"view_options(prefixes["from";"fa1.2";"contract"]@@token_contract_param()@@prefixes["get";"total";"supply"]@@stop)(fun(gas,payer,unparsing_mode)contract(cctxt:#Protocol_client_context.full)->letopenLwt_syntaxinletaction=Client_proto_fa12.Get_total_supply(dummy_callback,None)inlet*res=Client_proto_fa12.run_view_actioncctxt~chain:cctxt#chain~block:cctxt#block~sender:None~contract~action~gas~payer~unparsing_mode()inClient_proto_programs.print_view_resultcctxtres);command~group~desc:"Ask for an address's balance using a callback contract"contract_view_options(prefixes["from";"fa1.2";"contract"]@@token_contract_param()@@prefixes["get";"balance";"for"]@@implicit_alias_param~name:"from"~desc:"name or address of the account to lookup (also the source \
contract)"@@prefixes["callback";"on"]@@alias_param~name:"callback"~desc:"name or address of the callback contract"@@stop)(fun(callback_entrypoint,tez_amount,fee,dry_run,verbose_signing,gas_limit,storage_limit,counter,no_print_source,fee_parameter)contractaddrcallback(cctxt:#Protocol_client_context.full)->letopenLwt_result_syntaxinlet*source,src_pk,src_sk=get_contract_caller_keyscctxtaddrinletaction=Client_proto_fa12.Get_balance(Implicitaddr,(callback,callback_entrypoint))inlet*!errors=Client_proto_fa12.call_contractcctxt~chain:cctxt#chain~block:cctxt#block~contract~action?confirmations:cctxt#confirmations~dry_run~verbose_signing?fee~source~src_pk~src_sk~tez_amount?gas_limit?storage_limit?counter~fee_parameter()inlet*!_=Client_proto_context_commands.report_michelson_errors~no_print_source~msg:"transfer simulation failed"cctxterrorsinreturn_unit);command~group~desc:"Ask for an address's allowance using a callback contract"contract_view_options(prefixes["from";"fa1.2";"contract"]@@token_contract_param()@@prefixes["get";"allowance";"on"]@@implicit_alias_param~name:"from"~desc:"name or address of the account giving the allowance"@@prefix"as"@@alias_param~name:"to"~desc:"name or address of the account receiving the allowance"@@prefixes["callback";"on"]@@alias_param~name:"callback"~desc:"name or address of the callback contract"@@stop)(fun(callback_entrypoint,tez_amount,fee,dry_run,verbose_signing,gas_limit,storage_limit,counter,no_print_source,fee_parameter)contractsrcdstcallback(cctxt:#Protocol_client_context.full)->letopenLwt_result_syntaxinlet*source,src_pk,src_sk=get_contract_caller_keyscctxtsrcinletaction=Client_proto_fa12.Get_allowance(Implicitsrc,dst,(callback,callback_entrypoint))inlet*!errors=Client_proto_fa12.call_contractcctxt~chain:cctxt#chain~block:cctxt#block~contract~action?confirmations:cctxt#confirmations~dry_run~verbose_signing?fee~source~src_pk~src_sk~tez_amount?gas_limit?storage_limit?counter~fee_parameter()inlet*!_=Client_proto_context_commands.report_michelson_errors~no_print_source~msg:"transfer simulation failed"cctxterrorsinreturn_unit);command~group~desc:"Ask for a contract's total token supply using a callback contract"contract_view_options(prefixes["from";"fa1.2";"contract"]@@token_contract_param()@@prefixes["get";"total";"supply";"as"]@@implicit_alias_param~name:"from"~desc:"name or address of the source account"@@prefixes["callback";"on"]@@alias_param~name:"callback"~desc:"name or address of the callback contract"@@stop)(fun(callback_entrypoint,tez_amount,fee,dry_run,verbose_signing,gas_limit,storage_limit,counter,no_print_source,fee_parameter)contractaddrcallback(cctxt:#Protocol_client_context.full)->letopenLwt_result_syntaxinlet*source,src_pk,src_sk=get_contract_caller_keyscctxtaddrinletaction=Client_proto_fa12.Get_total_supply(callback,callback_entrypoint)inlet*!errors=Client_proto_fa12.call_contractcctxt~chain:cctxt#chain~block:cctxt#block~contract~action?confirmations:cctxt#confirmations~dry_run~verbose_signing?fee~source~src_pk~src_sk~tez_amount?gas_limit?storage_limit?counter~fee_parameter()inlet*!_=Client_proto_context_commands.report_michelson_errors~no_print_source~msg:"transfer simulation failed"cctxterrorsinreturn_unit);]letcommands_rw():#Protocol_client_context.fullTezos_clic.commandlist=letopenClient_proto_argsinTezos_clic.[command~group~desc:"Transfer tokens between two given accounts"(Tezos_clic.args10implicit_as_argtez_amount_argfee_argClient_proto_context_commands.dry_run_switchClient_proto_context_commands.verbose_signing_switchgas_limit_argstorage_limit_argcounter_argno_print_source_flagfee_parameter_args)(prefixes["from";"fa1.2";"contract"]@@token_contract_param()@@prefix"transfer"@@amount_param()@@prefix"from"@@from_implicit_param()@@prefix"to"@@to_param()@@stop)(fun(as_address,tez_amount,fee,dry_run,verbose_signing,gas_limit,storage_limit,counter,no_print_source,fee_parameter)contractamountsrcdst(cctxt:#Protocol_client_context.full)->letopenLwt_result_syntaxinletcaller=Option.value~default:srcas_addressinlet*source,caller_pk,caller_sk=get_contract_caller_keyscctxtcallerinletaction=Client_proto_fa12.Transfer(Implicitsrc,dst,amount)inlet*!errors=Client_proto_fa12.call_contractcctxt~chain:cctxt#chain~block:cctxt#block~contract~action?confirmations:cctxt#confirmations~dry_run~verbose_signing?fee~source~src_pk:caller_pk~src_sk:caller_sk~tez_amount?gas_limit?storage_limit?counter~fee_parameter()inlet*!_=Client_proto_context_commands.report_michelson_errors~no_print_source~msg:"transfer simulation failed"cctxterrorsinreturn_unit);command~group~desc:"Allow account to transfer an amount of token"contract_call_options(prefixes["from";"fa1.2";"contract"]@@token_contract_param()@@prefix"as"@@implicit_alias_param~name:"as"~desc:"name or address of the sender"@@prefix"approve"@@amount_param()@@prefix"from"@@alias_param~name:"from"~desc:"name or address to approve withdrawal"@@stop)(fun(tez_amount,fee,dry_run,verbose_signing,gas_limit,storage_limit,counter,no_print_source,fee_parameter)contractsourceamountdst(cctxt:#Protocol_client_context.full)->letopenLwt_result_syntaxinlet*source,src_pk,src_sk=get_contract_caller_keyscctxtsourceinletaction=Client_proto_fa12.Approve(dst,amount)inlet*!errors=Client_proto_fa12.call_contractcctxt~chain:cctxt#chain~block:cctxt#block~contract~action?confirmations:cctxt#confirmations~dry_run~verbose_signing?fee~source~src_pk~src_sk~tez_amount?gas_limit?storage_limit?counter~fee_parameter()inlet*!_=Client_proto_context_commands.report_michelson_errors~no_print_source~msg:"transfer simulation failed"cctxterrorsinreturn_unit);command~group~desc:"Execute multiple token transfers from a single source account. If \
one of the token transfers fails, none of them are executed."(args9default_fee_argimplicit_as_argClient_proto_context_commands.dry_run_switchClient_proto_context_commands.verbose_signing_switchdefault_gas_limit_argdefault_storage_limit_argcounter_argno_print_source_flagfee_parameter_args)(prefixes["multiple";"fa1.2";"transfers";"from"]@@implicit_alias_param~name:"src"~desc:"name or address of the source of the transfers"@@prefix"using"@@json_encoded_param~name:"transfers"~desc:(Format.sprintf"List of token transfers to inject from the source contract \
in JSON format (as a file or string). The JSON must be an \
array of objects of the form: '[ {\"token_contract\": \
address or alias, \"destination\": address or alias, \
\"amount\": non-negative integer (, <field>: <val> ...) } \
(, ...) ]', where an optional <field> can either be \
\"tez-amount\", \"fee\", \"gas-limit\" or \
\"storage-limit\". The complete schema can be inspected via \
`tezos-codec describe %s.fa1.2.token_transfer json schema`."Protocol.name)~pp_error:(funjsonfmtexn->match(json,exn)with|`Alj,Data_encoding.Json.Cannot_destruct([`Indexn],exn)->Format.fprintffmt"Invalid transfer at index %i: %a %a"n(funppf->Data_encoding.Json.print_errorppf)exn(Format.pp_print_optionData_encoding.Json.pp)(List.nth_optljn)|_,(Data_encoding.Json.Cannot_destruct_asexn)->Format.fprintffmt"Invalid transfer file: %a %a"(funppf->Data_encoding.Json.print_errorppf)exnData_encoding.Json.ppjson|_,exn->raiseexn(* this case can't happen because only `Cannot_destruct` error are
given to this pp *))(Data_encoding.listClient_proto_fa12.token_transfer_encoding)@@stop)(fun(fee,as_address,dry_run,verbose_signing,gas_limit,storage_limit,counter,no_print_source,fee_parameter)srcoperationscctxt->letopenLwt_result_syntaxinletcaller=Option.value~default:srcas_addressinmatchoperationswith|[]->cctxt#error"Empty operation list"|operations->let*source,src_pk,src_sk=get_contract_caller_keyscctxtcallerinlet*!errors=Client_proto_fa12.inject_token_transfer_batchcctxt~chain:cctxt#chain~block:cctxt#block?confirmations:cctxt#confirmations~dry_run~verbose_signing~sender:(Implicitsrc)~source~src_pk~src_sk~token_transfers:operations~fee_parameter?counter?default_fee:fee?default_gas_limit:gas_limit?default_storage_limit:storage_limit()inlet*!_=Client_proto_context_commands.report_michelson_errors~no_print_source~msg:"multiple transfers simulation failed"cctxterrorsinreturn_unit);]letcommands()=commands_ro()@commands_rw()