def DoRun(contract_script, arguments, wallet, path, verbose=True, from_addr=None, min_fee=DEFAULT_MIN_FEE): 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[6:] script = GatherLoadedContractParams(f_args, contract_script) tx, result, total_ops, engine = test_deploy_and_invoke( script, i_args, wallet, from_addr, min_fee) i_args.reverse() return_type_results = [] try: rtype = ContractParameterType.FromString(f_args[1]) for r in result: cp = ContractParameter.AsParameterType(rtype, r) return_type_results.append(cp.ToJson()) except Exception as e: logger.error( 'Could not convert result to ContractParameter: %s ' % e) if tx is not None and result is not None: if verbose: 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 " % return_type_results) print("Invoke TX gas cost: %s " % (tx.Gas.value / Fixed8.D)) print( "-------------------------------------------------------------\n" ) return tx, result, total_ops, engine else: if verbose: print("Test invoke failed") print("tx is, results are %s %s " % (tx, result)) else: print("please open a wallet to test built contract") return None, None, None, None
def DoRun(contract_script, arguments, wallet, path, verbose=True, from_addr=None, min_fee=DEFAULT_MIN_FEE, invocation_test_mode=True, debug_map=None, invoke_attrs=None, owners=None): if not wallet: print("Please open a wallet to test build contract") return None, None, None, None f_args = arguments[1:] i_args = arguments[6:] try: script = GatherLoadedContractParams(f_args, contract_script) except Exception: raise TypeError tx, result, total_ops, engine = test_deploy_and_invoke( script, i_args, wallet, from_addr, min_fee, invocation_test_mode, debug_map=debug_map, invoke_attrs=invoke_attrs, owners=owners) i_args.reverse() return_type_results = [] try: rtype = ContractParameterType.FromString(f_args[4]) for r in result: cp = ContractParameter.AsParameterType(rtype, r) return_type_results.append(cp.ToJson()) except Exception: raise TypeError if tx and result: if verbose: print( "\n-----------------------------------------------------------" ) print("Calling %s with arguments %s " % (path, [item for item in reversed(engine.invocation_args)])) print("Test deploy invoke successful") print("Used total of %s operations " % total_ops) print("Result %s " % return_type_results) print("Invoke TX gas cost: %s " % (tx.Gas.value / Fixed8.D)) print( "-------------------------------------------------------------\n" ) return tx, result, total_ops, engine else: if verbose: print("Test invoke failed") print(f"tx is {tx}, results are {result}") return tx, result, None, None
def test_to_parameter(self): stack_item = Integer(BigInteger(14)) cp1 = ContractParameter.AsParameterType(ContractParameterType.Integer, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Integer', 'value': 14}) cp1 = ContractParameter.AsParameterType(ContractParameterType.Boolean, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Boolean', 'value': True}) cp1 = ContractParameter.AsParameterType( ContractParameterType.ByteArray, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'ByteArray', 'value': '0e'}) with self.assertRaises(Exception) as ctx: cp1 = ContractParameter.AsParameterType( ContractParameterType.Array, stack_item) cp1 = ContractParameter.AsParameterType(ContractParameterType.String, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'String', 'value': '14'}) cp1 = ContractParameter.AsParameterType( ContractParameterType.InteropInterface, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'InteropInterface'}) stack_item = Boolean(False) cp1 = ContractParameter.AsParameterType(ContractParameterType.Integer, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Integer', 'value': 0}) cp1 = ContractParameter.AsParameterType(ContractParameterType.Boolean, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Boolean', 'value': False}) cp1 = ContractParameter.AsParameterType( ContractParameterType.ByteArray, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'ByteArray', 'value': '00'}) with self.assertRaises(Exception) as ctx: cp1 = ContractParameter.AsParameterType( ContractParameterType.Array, stack_item) cp1 = ContractParameter.AsParameterType(ContractParameterType.String, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'String', 'value': 'False'}) cp1 = ContractParameter.AsParameterType( ContractParameterType.InteropInterface, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'InteropInterface'}) stack_item = ByteArray(bytearray(b'\xe0\x02')) cp1 = ContractParameter.AsParameterType(ContractParameterType.Integer, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Integer', 'value': 736}) cp1 = ContractParameter.AsParameterType(ContractParameterType.Boolean, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'Boolean', 'value': True}) cp1 = ContractParameter.AsParameterType( ContractParameterType.ByteArray, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'ByteArray', 'value': 'e002'}) with self.assertRaises(Exception) as ctx: cp1 = ContractParameter.AsParameterType( ContractParameterType.Array, stack_item) cp1 = ContractParameter.AsParameterType(ContractParameterType.String, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'String', 'value': 'e002'}) cp1 = ContractParameter.AsParameterType( ContractParameterType.InteropInterface, stack_item) self.assertEqual(cp1.ToJson(), {'type': 'InteropInterface'})
def execute(self, arguments): wallet = PromptData.Wallet if not wallet: print("Please open a wallet") return False arguments, from_addr = PromptUtils.get_from_addr(arguments) arguments, priority_fee = PromptUtils.get_fee(arguments) arguments, invoke_attrs = PromptUtils.get_tx_attr_from_args(arguments) arguments, owners = PromptUtils.get_owners_from_params(arguments) arguments, return_type = PromptUtils.get_return_type_from_args(arguments) if len(arguments) < 1: print("Please specify the required parameters") return False hash_string = arguments[0] try: script_hash = UInt160.ParseString(hash_string) except Exception: # because UInt160 throws a generic exception. Should be fixed in the future print("Invalid script hash") return False p_fee = Fixed8.Zero() if priority_fee is not None: p_fee = priority_fee if p_fee is False: logger.debug("invalid fee") return False tx, fee, results, num_ops, engine_success = TestInvokeContract(wallet, arguments, from_addr=from_addr, invoke_attrs=invoke_attrs, owners=owners) if tx and results: if return_type is not None: try: parameterized_results = [ContractParameter.AsParameterType(ContractParameterType.FromString(return_type), item).ToJson() for item in results] except ValueError: logger.debug("invalid return type") return False else: parameterized_results = [ContractParameter.ToParameter(item).ToJson() for item in results] print( "\n-------------------------------------------------------------------------------------------------------------------------------------") print("Test invoke successful") print(f"Total operations: {num_ops}") print(f"Results {str(parameterized_results)}") print(f"Invoke TX GAS cost: {tx.Gas.value / Fixed8.D}") print(f"Invoke TX fee: {fee.value / Fixed8.D}") print( "-------------------------------------------------------------------------------------------------------------------------------------\n") comb_fee = p_fee + fee if comb_fee != fee: print(f"Priority Fee ({p_fee.value / Fixed8.D}) + Invoke TX Fee ({fee.value / Fixed8.D}) = {comb_fee.value / Fixed8.D}\n") print("Enter your password to send this invocation to the network") tx.Attributes = invoke_attrs try: passwd = prompt("[password]> ", is_password=True) except KeyboardInterrupt: print("Invocation cancelled") return False if not wallet.ValidatePassword(passwd): return print("Incorrect password") return InvokeContract(wallet, tx, comb_fee, from_addr=from_addr, owners=owners) else: print("Error testing contract invoke") return False