99from aeternity import __version__
1010
1111from aeternity .node import NodeClient , Config
12- from aeternity .transactions import TxSigner
13- # from aeternity.oracle import Oracle, OracleQuery, NoOracleResponse
12+ from aeternity .transactions import TxSigner , TxBuilder
13+ from aeternity .identifiers import NETWORK_ID_MAINNET
1414from . import utils , signing , aens , defaults , exceptions
15- from aeternity .contract import Contract
15+ from aeternity .contract import CompilerClient
1616
1717from datetime import datetime , timezone
1818
@@ -164,7 +164,7 @@ def _print_error(err, title="error", exit_code=0):
164164]
165165
166166_sign_options = [
167- click .option ('--network-id' , default = None , help = "The network id to use when signing a transaction" )
167+ click .option ('--network-id' , default = NETWORK_ID_MAINNET , help = "The network id to use when signing a transaction" , show_default = True )
168168]
169169
170170_transaction_options = [
@@ -338,19 +338,20 @@ def account_balance(keystore_name, password, force, wait, json_):
338338@click .argument ('keystore_name' , required = True )
339339@click .argument ('recipient_id' , required = True )
340340@click .argument ('amount' , required = True , type = int )
341+ @click .option ('--payload' , default = "" , help = "Spend transaction payload" )
341342@global_options
342343@account_options
343344@online_options
344345@transaction_options
345346@sign_options
346- def account_spend (keystore_name , recipient_id , amount , fee , ttl , nonce , password , network_id , force , wait , json_ ):
347+ def account_spend (keystore_name , recipient_id , amount , payload , fee , ttl , nonce , password , network_id , force , wait , json_ ):
347348 try :
348349 set_global_options (json_ , force , wait )
349350 account , keystore_path = _account (keystore_name , password = password )
350351 account .nonce = nonce
351352 if not utils .is_valid_hash (recipient_id , prefix = "ak" ):
352353 raise ValueError ("Invalid recipient address" )
353- tx = _node_cli (network_id = network_id ).spend (account , recipient_id , amount , tx_ttl = ttl , fee = fee )
354+ tx = _node_cli (network_id = network_id ).spend (account , recipient_id , amount , tx_ttl = ttl , fee = fee , payload = payload )
354355 _print_object (tx , title = 'spend transaction' )
355356 except Exception as e :
356357 _print_error (e , exit_code = 1 )
@@ -420,8 +421,7 @@ def tx_broadcast(signed_transaction, force, wait, json_):
420421def tx_spend (sender_id , recipient_id , amount , ttl , fee , nonce , payload , json_ ):
421422 try :
422423 set_global_options (json_ )
423- cli = _node_cli ()
424- tx = cli .tx_builder .tx_spend (sender_id , recipient_id , amount , payload , fee , ttl , nonce )
424+ tx = TxBuilder ().tx_spend (sender_id , recipient_id , amount , payload , fee , ttl , nonce )
425425 # print the results
426426 _print_object (tx , title = 'spend tx' )
427427 except Exception as e :
@@ -584,34 +584,101 @@ def name_transfer(keystore_name, domain, address, ttl, fee, nonce, password, net
584584#
585585#
586586
587- @click .group ('contract' , help = 'Deploy and execute contracts on the chain' )
588- def contract ():
587+ @cli .group (help = "Interact with Æternity smart contract compiler" )
588+ def compiler ():
589589 pass
590590
591591
592- @contract .command (help = "Compile a contract" )
592+ @compiler .command ('compile' , help = "Compile a contract" )
593+ @click .option ('--compiler-url' , '-c' , default = 'http://localhost:3080' , envvar = 'COMPILER_URL' , help = 'Aeternity compiler url' , metavar = 'URL' )
593594@click .argument ("contract_file" )
594- def contract_compile (contract_file ):
595+ @global_options
596+ def contract_compile (contract_file , compiler_url , json_ ):
595597 try :
598+ set_global_options (json_ , False , False )
596599 with open (contract_file ) as fp :
597600 code = fp .read ()
598- c = _node_cli (). Contract ( Contract . SOPHIA )
601+ c = CompilerClient ( compiler_url )
599602 result = c .compile (code )
600- _print_object ({"bytecode" , result }, title = "contract" )
603+ if click .confirm (f'Save contract bytecode to file ({ contract_file } .bin) ?' , default = True , show_default = True ):
604+ with open (f"{ contract_file } .bin" , "w" ) as fp :
605+ fp .write (result .bytecode )
606+ _print_object (result , title = "contract" )
607+ except Exception as e :
608+ _print_error (e , exit_code = 1 )
609+
610+
611+ @compiler .command ('aci' , help = "Get the aci of a contract" )
612+ @click .option ('--compiler-url' , '-c' , default = 'http://localhost:3080' , envvar = 'COMPILER_URL' , help = 'Aeternity compiler url' , metavar = 'URL' )
613+ @click .argument ("contract_file" )
614+ @global_options
615+ def contract_aci (contract_file , compiler_url , json_ ):
616+ try :
617+ set_global_options (json_ , False , False )
618+ with open (contract_file ) as fp :
619+ code = fp .read ()
620+ c = CompilerClient (compiler_url )
621+ result = c .aci (code )
622+ _print_object (result , title = "contract" )
623+ except Exception as e :
624+ _print_error (e , exit_code = 1 )
625+
626+
627+ @compiler .command ('encode-calldata' , help = "Enocode the calldata to invoke a contract" )
628+ @click .option ('--compiler-url' , '-c' , default = 'http://localhost:3080' , envvar = 'COMPILER_URL' , help = 'Aeternity compiler url' , metavar = 'URL' )
629+ @click .argument ("contract_file" )
630+ @click .argument ("function_name" )
631+ @click .option ("--arguments" , default = None , help = "Argument of the function if any, comma separated" )
632+ @global_options
633+ def contract_encode_calldata (contract_file , function_name , arguments , compiler_url , json_ ):
634+ try :
635+ set_global_options (json_ , False , False )
636+ with open (contract_file ) as fp :
637+ code = fp .read ()
638+ c = CompilerClient (compiler_url )
639+ arguments = [] if arguments is None else arguments .split ("," )
640+ result = c .encode_calldata (code , function_name , arguments = arguments )
641+ _print_object (result , title = "contract" )
642+ # except Exception as e:
643+ # _print_error(e, exit_code=1)
644+ finally :
645+ pass
646+
647+
648+ @compiler .command ('decode-data' , help = "Decode the data retrieve from a contract" )
649+ @click .option ('--compiler-url' , '-c' , default = 'http://localhost:3080' , envvar = 'COMPILER_URL' , help = 'Aeternity compiler url' , metavar = 'URL' )
650+ @click .argument ("sophia_type" )
651+ @click .argument ("encoded_data" )
652+ @global_options
653+ def contract_decode_data (contract_file , encoded_data , sophia_type , compiler_url , json_ ):
654+ try :
655+ set_global_options (json_ , False , False )
656+ c = CompilerClient (compiler_url )
657+ result = c .decode_data (sophia_type , encoded_data )
658+ _print_object (result , title = "contract" )
601659 except Exception as e :
602660 _print_error (e , exit_code = 1 )
603661
604662
605- @contract .command ('deploy' , help = 'Deploy a contract on the chain' )
663+ @cli .group (help = 'Deploy and execute contracts on the chain' )
664+ def contracts ():
665+ pass
666+
667+
668+ @contracts .command ('deploy' , help = 'Deploy a contract on the chain' )
606669@click .argument ('keystore_name' , required = True )
607- @click .argument ("contract_file" , required = True )
670+ @click .argument ("bytecode_file" , required = True )
671+ @click .option ("--init-calldata" , default = defaults .CONTRACT_INIT_CALLDATA , help = "The calldata for the init function" , show_default = True )
608672@click .option ("--gas" , default = defaults .CONTRACT_GAS , help = 'Amount of gas to deploy the contract' , show_default = True )
673+ @click .option ("--amount" , default = defaults .CONTRACT_AMOUNT , help = 'Amount of tokens to transfer to the contract' , show_default = True )
674+ @click .option ("--gas-price" , default = defaults .CONTRACT_GAS_PRICE , help = 'The gas price used to execute the contract init function' , show_default = True )
675+ @click .option ("--deposit" , default = defaults .CONTRACT_AMOUNT , help = 'A initial deposit to the contract' , show_default = True )
609676@global_options
610677@account_options
611678@online_options
612679@transaction_options
613680@sign_options
614- def contract_deploy (keystore_name , contract_file , gas , password , network_id , force , wait , json_ ):
681+ def contract_deploy (keystore_name , bytecode_file , init_calldata , gas , gas_price , amount , deposit , password , ttl , fee , nonce , network_id , force , wait , json_ ):
615682 """
616683 Deploy a contract to the chain and create a deploy descriptor
617684 with the contract informations that can be use to invoke the contract
@@ -621,36 +688,22 @@ def contract_deploy(keystore_name, contract_file, gas, password, network_id, for
621688 source file. Multiple deploy of the same contract file will generate different
622689 deploy descriptor
623690 """
691+ print ("Not yet implemented" )
692+ return
624693 try :
625- with open (contract_file ) as fp :
694+ with open (bytecode_file ) as fp :
626695 set_global_options (json_ , force , wait )
627696 account , _ = _account (keystore_name , password = password )
628- code = fp .read ()
629- contract = _node_cli (network_id = network_id ).Contract (code )
630- tx = contract .tx_create (account , gas = gas )
631- # save the contract data
632- contract_data = {
633- 'source' : contract .source_code ,
634- 'bytecode' : contract .bytecode ,
635- 'id' : contract .id ,
636- 'transaction' : tx .tx_hash ,
637- 'owner' : account .get_address (),
638- 'created_at' : datetime .now ().isoformat ('T' )
639- }
640- # write the contract data to a file
641- deploy_descriptor = f"{ contract_file } .deploy.{ contract .id [3 :]} .json"
642- with open (deploy_descriptor , 'w' ) as fw :
643- json .dump (contract_data , fw , indent = 2 )
644- _print_object ({
645- "Contract id" : contract .id ,
646- "Transaction hash" : tx .tx_hash ,
647- "Deploy descriptor" : deploy_descriptor ,
648- }, title = "contract" )
697+ bytecode = fp .read ()
698+ contract = _node_cli (network_id = network_id ).Contract ()
699+ tx = contract .create (account , bytecode , init_calldata = init_calldata , gas = gas , amount = amount ,
700+ gas_price = gas_price , deposit = deposit , tx_ttl = ttl , fee = fee )
701+ _print_object (tx , title = "contract create" )
649702 except Exception as e :
650703 _print_error (e , exit_code = 1 )
651704
652705
653- @contract .command ('call' , help = 'Execute a function of the contract' )
706+ @contracts .command ('call' , help = 'Execute a function of the contract' )
654707@click .argument ('keystore_name' , required = True )
655708@click .argument ("deploy_descriptor" , required = True )
656709@click .argument ("function" , required = True )
@@ -663,6 +716,8 @@ def contract_deploy(keystore_name, contract_file, gas, password, network_id, for
663716@transaction_options
664717@sign_options
665718def contract_call (keystore_name , deploy_descriptor , function , params , return_type , gas , password , network_id , force , wait , json_ ):
719+ print ("Not yet implemented" )
720+ return
666721 try :
667722 with open (deploy_descriptor ) as fp :
668723 contract = json .load (fp )
@@ -674,16 +729,23 @@ def contract_call(keystore_name, deploy_descriptor, function, params, return_typ
674729 account , _ = _account (keystore_name , password = password )
675730
676731 contract = _node_cli (network_id = network_id ).Contract (source , bytecode = bytecode , address = address )
677- tx , result = contract .tx_call (account , function , params , gas = gas )
732+ tx = contract .tx_call (account , function , params , gas = gas )
678733 _print_object (tx , "contract call" )
679- if result .return_type == 'ok' :
680- value , remote_type = contract .decode_data (result .return_value , return_type )
681- _print_object ({
682- 'Return value' : value ,
683- 'Return remote type' : remote_type ,
684- })
685-
686- pass
734+ except Exception as e :
735+ _print_error (e , exit_code = 1 )
736+
737+
738+ @contracts .command ('call-info' , help = 'Retrieve the result of a contract call if any' )
739+ @click .argument ('tx_hash' , required = True )
740+ @global_options
741+ @online_options
742+ def contract_call_info (tx_hash , force , wait , json_ ):
743+ print ("Not yet implemented" )
744+ return
745+ try :
746+ contract = _node_cli ().Contract ()
747+ call_object = contract .get_call_object (tx_hash )
748+ _print_object (call_object , "contract call object" )
687749 except Exception as e :
688750 _print_error (e , exit_code = 1 )
689751
0 commit comments