From a2031075a641f32e78f3fdc64640304d97652e96 Mon Sep 17 00:00:00 2001 From: Thomas Saunders Date: Wed, 18 Oct 2017 18:34:23 -0500 Subject: [PATCH] add neo-boa dependency and load/build and run functionality --- neo/Prompt/Commands/BuildNRun.py | 93 ++++++++++++++++++++++++++++++++ prompt.py | 13 +++-- requirements.txt | 1 + 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 neo/Prompt/Commands/BuildNRun.py diff --git a/neo/Prompt/Commands/BuildNRun.py b/neo/Prompt/Commands/BuildNRun.py new file mode 100644 index 000000000..89170c646 --- /dev/null +++ b/neo/Prompt/Commands/BuildNRun.py @@ -0,0 +1,93 @@ +from neo.Prompt.Utils import get_arg +from neo.Prompt.Commands.LoadSmartContract import GatherLoadedContractParams +from neo.Prompt.Commands.Invoke import test_deploy_and_invoke +from neo.Fixed8 import Fixed8 +from boa.compiler import Compiler +import binascii +import traceback + +def LoadAndRun(arguments, wallet): + + path = get_arg(arguments) + + try: + + with open(path, 'rb') as f: + + content = f.read() + + try: + content = binascii.unhexlify(content) + except Exception as e: + pass + + script = content + + print("arguments.... %s " % arguments) + DoRun(script, arguments, wallet, path) + + except Exception as e: + print("couldnt load script %s " % e) + + + +def BuildAndRun(arguments, wallet): + path = get_arg(arguments) + + try: + contract_script = Compiler.instance().load_and_save(path) + + newpath = path.replace('.py' ,'.avm') + print("Saved output to %s " % newpath) + + DoRun(contract_script,arguments,wallet, path) + + except Exception as e: + print("couldn compile %s " % e) + + +def DoRun(contract_script, arguments, wallet, path): + + + try: + + test = get_arg(arguments, 1) + + if test is not None and test == 'test': + + if wallet is not None: + + f_args = arguments[2:] + i_args = arguments[5:] + + script = GatherLoadedContractParams(f_args, contract_script) + + tx ,result,total_ops = test_deploy_and_invoke(script, i_args, wallet) + i_args.reverse() + + if tx is not None and result is not None: + print("\n-----------------------------------------------------------") + print("Calling %s with arguments %s " % (path, i_args)) + print("Test deploy invoke successful") + print("Used total of %s operations " % total_ops) + print("Result %s " % result) + print("Invoke TX gas cost: %s " % (tx.Gas.value / Fixed8.D)) + print("-------------------------------------------------------------\n") + return + else: + print("test ivoke failed") + print("tx is, results are %s %s " % (tx, result)) + return + + + + else: + + print("please open a wallet to test built contract") + + + + except Exception as e: + print("could not bulid %s " % e) + traceback.print_stack() + traceback.print_exc() \ No newline at end of file diff --git a/prompt.py b/prompt.py index 354de1071..f56261db0 100644 --- a/prompt.py +++ b/prompt.py @@ -4,20 +4,17 @@ import json import logging import datetime -import time import os from neo.IO.MemoryStream import StreamManager -from neo.Network.NodeLeader import NodeLeader import resource from neo.Core.Blockchain import Blockchain -from neo.Core.TX.Transaction import Transaction,ContractTransaction,TransactionOutput from neo.Implementations.Wallets.peewee.UserWallet import UserWallet from neo.Implementations.Blockchains.LevelDB.LevelDBBlockchain import LevelDBBlockchain -from neo.SmartContract.ContractParameterContext import ContractParametersContext from neo.Wallets.KeyPair import KeyPair from neo.Network.NodeLeader import NodeLeader from neo.Prompt.Commands.Invoke import InvokeContract,TestInvokeContract,test_invoke,test_deploy_and_invoke +from neo.Prompt.Commands.BuildNRun import BuildAndRun,LoadAndRun from neo.Prompt.Commands.LoadSmartContract import LoadContract,GatherContractDetails,ImportContractAddr from neo.Prompt.Commands.Send import construct_and_send from neo.Prompt.Commands.Wallet import DeleteAddress @@ -286,7 +283,11 @@ def do_import(self, arguments): print("please specify something to import") return + def do_build(self, arguments): + BuildAndRun(arguments, self.Wallet) + def do_load_n_run(self, arguments): + LoadAndRun(arguments, self.Wallet) def do_export(self, arguments): item = get_arg(arguments) @@ -713,6 +714,10 @@ def run(self): self.do_create(arguments) elif command == 'open': self.do_open(arguments) + elif command == 'build': + self.do_build(arguments) + elif command == 'load_run': + self.do_load_n_run(arguments) elif command == 'import': self.do_import(arguments) elif command == 'export': diff --git a/requirements.txt b/requirements.txt index 0c8714c5a..e3cc8364e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -29,6 +29,7 @@ incremental==17.5.0 memory-profiler==0.47 mmh3==2.4 mpmath==0.19 +neo-boa==0.1.5 numpy==1.13.1 peewee==2.10.1 plyvel==0.9