def worker(self): self.chain.Pause() BuildAndRun(self.args, wallet=self.wallet, verbose=True) self.chain.Resume() avm_path = self.scPath.replace('.py', '.avm') self.args[0] = avm_path from_addr = None current_height = 0 code = LoadContract(args=self.args) # /scripts/sc.avm 0710 02 True False False if code: script = generate_deploy_script( code.Script, "myTestSmartContract", # name "test", # version "", # author "", # email "", # description code.ContractProperties, code.ReturnTypeBigInteger, code.ParameterList) if script is not None: tx, fee, results, num_ops = test_invoke(script, self.wallet, [], from_addr=from_addr) if tx is not None and results is not None: print("Test deploy invoke successful") print("Deploy Invoke TX GAS cost: %s " % (tx.Gas.value / Fixed8.D)) print("Deploy Invoke TX Fee: %s " % (fee.value / Fixed8.D)) print("-------------------------") while not self.isSynced: self.show_state() time.sleep(1) result = InvokeContract(self.wallet, tx, Fixed8.Zero(), from_addr=from_addr) print("Result: ", result.ToJson(), self.isSynced) print("Result: ", tx.ToJson()) current_height = self.chain.Height + 1 print("Script:", script) # we expect the transaction to be included in the next block: while current_height > self.chain.Height: self.show_state() time.sleep(5) self.twist.stop()
def invokeMethod(self, args): """ Invoke a method of the Smart Contract """ if self.Wallet.IsSynced is False: raise Exception("Wallet is not synced.") tx, fee, results, num_ops = TestInvokeContract(self.Wallet, args) if not tx: raise Exception("TestInvokeContract failed") logger.info("TestInvokeContract done, calling InvokeContract now...") tStart = time.time() sent_tx = InvokeContract(self.Wallet, tx, fee) if sent_tx: logger.info("InvokeContract success, transaction underway: %s" % sent_tx.Hash.ToString()) found = self._wait_for_tx(sent_tx) if found: tResult = time.time() - tStart logger.info("transaction done") logger.info("Tx Latency: %s", tResult) #wait one additional block for the UTXO to be returned self.currentHeight = Blockchain.Default().HeaderHeight while True: if self.Wallet.WalletHeight == self.currentHeight + 1: break return tResult
def invoke_construct(self, operation, args, gas=None, readonly=False, test=False, wait=False): self.invoking = True self.op_output = None arguments = [self.SC_hash, operation, str(args)] print('arguments', arguments) if gas: gas = '--attach-gas=' + str(gas) arguments = [self.SC_hash, operation, str(args), gas] if test: return tx, fee, results, num_ops = TestInvokeContract(self.Wallet, arguments) if tx is not None and results is not None: if readonly: self.invoking = False return results result = InvokeContract(self.Wallet, tx, fee) else: print('Invoke failed') self.quit() self.wait_for_invoke_complete()
def token_crowdsale_register(wallet, args, prompt_passwd=True): token = get_asset_id(wallet, args[0]) args, from_addr = get_from_addr(args) if len(args) < 2: raise Exception("Specify addr to register for crowdsale") register_addr = args[1:] tx, fee, results = token.CrowdsaleRegister(wallet, register_addr) if results[0].GetBigInteger() > 0: print("\n-----------------------------------------------------------") print("[%s] Will register addresses for crowdsale: %s " % (token.symbol, register_addr)) print("Fee: %s " % (fee.value / Fixed8.D)) print("-------------------------------------------------------------\n") if prompt_passwd: passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return return InvokeContract(wallet, tx, fee, from_addr) else: print("Could not register addresses: %s " % str(results[0])) return False
def do_token_transfer(token, wallet, from_address, to_address, amount, prompt_passwd=True, tx_attributes=None): if not tx_attributes: tx_attributes = [] # because we cannot differentiate between a normal and multisig from_addr, and because we want to make # sending NEP5 tokens straight forward even when sending from multisig addresses, we include the script_hash # for verification by default to the transaction attributes. See PR/Issue: https://github.com/CityOfZion/neo-python/pull/491 from_script_hash = binascii.unhexlify(bytes(wallet.ToScriptHash(from_address).ToString2(), 'utf-8')) tx_attributes.append(TransactionAttribute(usage=TransactionAttributeUsage.Script, data=from_script_hash)) tx, fee, results = token.Transfer(wallet, from_address, to_address, amount, tx_attributes=tx_attributes) if tx is not None and results is not None and len(results) > 0: if results[0].GetBigInteger() == 1: print("\n-----------------------------------------------------------") print("Will transfer %s %s from %s to %s" % (string_from_amount(token, amount), token.symbol, from_address, to_address)) print("Transfer fee: %s " % (fee.value / Fixed8.D)) print("-------------------------------------------------------------\n") if prompt_passwd: passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return False return InvokeContract(wallet, tx, fee) print("could not transfer tokens") return False
def do_token_transfer(token, wallet, from_address, to_address, amount, prompt_passwd=True): if from_address is None: print("Please specify --from-addr={addr} to send NEP5 tokens") return False tx, fee, results = token.Transfer(wallet, from_address, to_address, amount) if tx is not None and results is not None and len(results) > 0: if results[0].GetBigInteger() == 1: print("\n-----------------------------------------------------------") print("Will transfer %s %s from %s to %s" % (string_from_amount(token, amount), token.symbol, from_address, to_address)) print("Transfer fee: %s " % (fee.value / Fixed8.D)) print("-------------------------------------------------------------\n") if prompt_passwd: passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return False return InvokeContract(wallet, tx, fee) print("could not transfer tokens") return False
def test_invoke(self, args, expected_result_count, test_only=False, from_addr=None): if args and len(args) > 0: tx, fee, results, num_ops, success = TestInvokeContract(self.wallet, args, from_addr=from_addr) if tx is not None and results is not None and success: print( "\n-------------------------------------------------------------------------------------------------------------------------------------") print("Test invoke successful") print("Total operations: %s" % num_ops) print("Results RAW %s" % results) print("Results %s" % [str(item) for item in results]) print("Invoke TX GAS cost: %s" % (tx.Gas.value / Fixed8.D)) print("Invoke TX fee: %s" % (fee.value / Fixed8.D)) print( "-------------------------------------------------------------------------------------------------------------------------------------\n") if results[0].GetBigInteger() != expected_result_count: self.logger.error("Found invalid result! '%s' but expected '%s'" % (results[0], expected_result_count)) if test_only: return True, True # bl: tx can fail if there are no connected peers, so wait for one self.wait_for_peers() return InvokeContract(self.wallet, tx, fee, from_addr), results[0] else: print("Error testing contract invoke: %s" % args) else: print("Invalid args for test_invoke! %s" % args) return False, False
def invoke_contract(self, tx, fee): if not InvokeContract(self.Wallet, tx, fee): print("Invoke contract failed!") return False print("Invoke successfully!") return True
def token_approve_allowance(wallet, args, prompt_passwd=True): if len(args) != 4: print("please provide a token symbol, from address, to address, and amount") return False token = get_asset_id(wallet, args[0]) approve_from = args[1] approve_to = args[2] amount = amount_from_string(token, args[3]) tx, fee, results = token.Approve(wallet, approve_from, approve_to, amount) if tx is not None and results is not None and len(results) > 0: if results[0].GetBigInteger() == 1: print("\n-----------------------------------------------------------") print("Approve allowance of %s %s from %s to %s" % (string_from_amount(token, amount), token.symbol, approve_from, approve_to)) print("Transfer fee: %s " % (fee.value / Fixed8.D)) print("-------------------------------------------------------------\n") if prompt_passwd: passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return return InvokeContract(wallet, tx, fee) print("could not transfer tokens") return False
def test_invoke_contract(self, args): if not self.Wallet: print("please open a wallet") return if args and len(args) > 0: tx, fee, results, num_ops = TestInvokeContract(self.Wallet, args) if tx is not None and results is not None: print("\n-------------------------------------------------------------------------------------------------------------------------------------") print("Test invoke successful") print("Total operations: %s " % num_ops) print("Results %s " % [str(item) for item in results]) print("Invoke TX gas cost: %s " % (tx.Gas.value / Fixed8.D)) print("Invoke TX Fee: %s " % (fee.value / Fixed8.D)) print("-------------------------------------------------------------------------------------------------------------------------------------\n") print("Enter your password to continue and invoke on the network\n") passwd = prompt("[password]> ", is_password=True) if not self.Wallet.ValidatePassword(passwd): return print("Incorrect password") result = InvokeContract(self.Wallet, tx, fee) return else: print("Error testing contract invoke") return print("please specify a contract to invoke")
def invoke_method(self, method_name, *args): logger.info("invoke_method: method_name=%s, args=%s", method_name, args) logger.info("Block %s / %s" % (str(Blockchain.Default().Height), str(Blockchain.Default().HeaderHeight))) if not self.wallet: raise Exception( "Open a wallet before invoking a smart contract method.") if self.tx_in_progress: raise Exception("Transaction already in progress (%s)" % self.tx_in_progress.Hash.ToString()) time.sleep(3) logger.info("wallet synced. checking if gas is available...") if not self.wallet_has_gas(): logger.error( "Oh now, wallet has no gas! Trying to rebuild the wallet...") raise Exception("Wallet has no gas.") _args = [self.contract_hash, method_name, str(list(args))] logger.info("TestInvokeContract args: %s", _args) tx, fee, results, num_ops = TestInvokeContract(self.wallet, _args) if not tx: raise Exception("TestInvokeContract failed") # logger.info("TestInvoke result: %s", str(results)) logger.info("TestInvoke done, invoking now...") result = InvokeContract(self.wallet, tx, fee)
def test_invoke_contract(self, args): if not self.Wallet: print("Please open a wallet") return args, from_addr = get_from_addr(args) if args and len(args) > 0: # Test invoke contract. tx, fee, results, num_ops = TestInvokeContract(self.Wallet, args, from_addr=from_addr) if tx is not None and results is not None: result_item = results passwd = prompt("[password]> ", is_password=True) if not self.Wallet.ValidatePassword(passwd): return print("Incorrect password") result = InvokeContract(self.Wallet, tx, fee, from_addr=from_addr) return result_item else: print("Error testing contract invoke") return print("Please specify a contract to invoke")
def token_send_blog(wallet, args): if len(args) != 4: print( "please provide a token symbol, from address, to address, and amount sb" ) return False contract = args[0] send_from = args[1] send_to = args[2] amount = 0 # allowance = token_get_allowance(wallet, args[:-1], verbose=False) if True: invoke_args = [ contract, 'output', [ parse_param(send_from, wallet), parse_param(send_to, wallet), parse_param(amount) ] ] print(invoke_args) tx, fee, results, num_ops = TestInvokeContract(wallet, invoke_args, None, True, send_from) if tx is not None and results is not None and len(results) > 0: if results[0].GetBigInteger() == 1: return InvokeContract(wallet, tx, fee) return False
def execute(self, arguments): wallet = PromptData.Wallet if len(arguments) < 4: print("Please specify the required parameters") return False arguments, priority_fee = PromptUtils.get_fee(arguments) token_str = arguments[0] from_addr = arguments[1] to_addr = arguments[2] try: amount = float(arguments[3]) except ValueError: print(f"{arguments[3]} is not a valid amount") 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 try: token = _validate_nep5_args(wallet, token_str, from_addr, to_addr, amount) except ValueError as e: print(str(e)) return False decimal_amount = amount_from_string(token, amount) tx, fee, results = token.Approve(wallet, from_addr, to_addr, decimal_amount) if tx and results: if results[0].GetBigInteger() == 1: print("\n-----------------------------------------------------------") print(f"Approve allowance of {amount} {token.symbol} from {from_addr} to {to_addr}") print(f"Invocation 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}) + Invocation Fee ({fee.value / Fixed8.D}) = {comb_fee.value / Fixed8.D}\n") print("Enter your password to send to the network") passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return False return InvokeContract(wallet, tx, comb_fee) print("Failed to approve tokens. Make sure you are entitled for approving.") return False
def invoke_contract(self, args): if not self._invoke_test_tx: print("Please test your invoke before deploying it with the 'testinvoke {contracthash} *args' command") return result = InvokeContract(self.Wallet, self._invoke_test_tx) self._invoke_test_tx = None return
def load_smart_contract(self, args): if not self.Wallet: print("Please open a wallet") return args, from_addr = get_from_addr(args) print("-------------------------------------->") print(args) print("<<<<<<<<<<<<-------------------------------------->") print(from_addr) print("<<<<<<<<<<<<----------------------+++++---------------->") function_code = LoadContract(args[1:]) if function_code: contract_script = GatherContractDetails(function_code) if contract_script is not None: tx, fee, results, num_ops = test_invoke(contract_script, self.Wallet, [], from_addr=from_addr) if tx is not None and results is not None: print( "\n-------------------------------------------------------------------------------------------------------------------------------------" ) print("Test deploy invoke successful") print("Total operations executed: %s " % num_ops) print("Results:") print([item.GetInterface() for item in results]) print("Deploy Invoke TX GAS cost: %s " % (tx.Gas.value / Fixed8.D)) print("Deploy Invoke TX Fee: %s " % (fee.value / Fixed8.D)) print( "-------------------------------------------------------------------------------------------------------------------------------------\n" ) print( "Enter your password to continue and deploy this contract" ) passwd = prompt("[password]> ", is_password=True) if not self.Wallet.ValidatePassword(passwd): return print("Incorrect password") result = InvokeContract(self.Wallet, tx, Fixed8.Zero(), from_addr=from_addr) return else: print("Test invoke failed") print("TX is %s, results are %s" % (tx, results)) return
def load_smart_contract(self, args): if not self.Wallet: print("please open wallet") return function_code = LoadContract(args[1:]) if function_code: #cname=None #if len(args) > 6: # cname = args[6:][0] #print("name="+args[6:][0]) #contract_script = GatherContractDetails(function_code, self, cname) contract_script = GatherContractDetails(function_code, self) if contract_script is not None: tx, fee, results, num_ops = test_invoke( contract_script, self.Wallet, []) if tx is not None and results is not None: print( "\n-------------------------------------------------------------------------------------------------------------------------------------" ) print("Test deploy invoke successful") print("Total operations executed: %s " % num_ops) print("Results %s " % [str(item) for item in results]) print("Deploy Invoke TX gas cost: %s " % (tx.Gas.value / Fixed8.D)) print("Deploy Invoke TX Fee: %s " % (fee.value / Fixed8.D)) print( "-------------------------------------------------------------------------------------------------------------------------------------\n" ) print( "Enter your password to continue and deploy this contract" ) passwd = 'coz' #passwd = prompt("[password]> ", is_password=True) if not self.Wallet.ValidatePassword(passwd): return print("Incorrect password") result = InvokeContract(self.Wallet, tx, Fixed8.Zero()) if result is False: return None return tx else: print("test ivoke failed") print("tx is, results are %s %s " % (tx, results)) return None
def test_invoke_contract(self, args): if not self.Wallet: print("Please open a wallet") return args, from_addr = get_from_addr(args) args, invoke_attrs = get_tx_attr_from_args(args) args, owners = get_owners_from_params(args) if args and len(args) > 0: tx, fee, results, num_ops = TestInvokeContract( self.Wallet, args, from_addr=from_addr, invoke_attrs=invoke_attrs, owners=owners) if tx is not None and results is not None: parameterized_results = [ ContractParameter.ToParameter(item) for item in results ] print( "\n-------------------------------------------------------------------------------------------------------------------------------------" ) print("Test invoke successful") print("Total operations: %s" % num_ops) print("Results %s" % [item.ToJson() for item in parameterized_results]) print("Invoke TX GAS cost: %s" % (tx.Gas.value / Fixed8.D)) print("Invoke TX fee: %s" % (fee.value / Fixed8.D)) print( "-------------------------------------------------------------------------------------------------------------------------------------\n" ) print( "Enter your password to continue and invoke on the network\n" ) tx.Attributes = invoke_attrs passwd = prompt("[password]> ", is_password=True) if not self.Wallet.ValidatePassword(passwd): return print("Incorrect password") InvokeContract(self.Wallet, tx, fee, from_addr=from_addr, owners=owners) return else: print("Error testing contract invoke") return print("Please specify a contract to invoke")
def execute(self, arguments): wallet = PromptData.Wallet if len(arguments) < 2: print("Please specify the required parameters") return False arguments, priority_fee = PromptUtils.get_fee(arguments) token_str = arguments[0] try: token = PromptUtils.get_token(wallet, token_str) except ValueError as e: print(str(e)) return False register_addr = arguments[1:] addr_list = [] for addr in register_addr: if isValidPublicAddress(addr): addr_list.append(addr) else: print(f"{addr} is not a valid address") 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 = token.CrowdsaleRegister(wallet, addr_list) if tx and results: if results[0].GetBigInteger() > 0: print("\n-----------------------------------------------------------") print("[%s] Will register addresses for crowdsale: %s " % (token.symbol, register_addr)) print("Invocation Fee: %s " % (fee.value / Fixed8.D)) print("-------------------------------------------------------------\n") comb_fee = p_fee + fee if comb_fee != fee: print(f"Priority Fee ({p_fee.value / Fixed8.D}) + Invocation Fee ({fee.value / Fixed8.D}) = {comb_fee.value / Fixed8.D}\n") print("Enter your password to send to the network") passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return False return InvokeContract(wallet, tx, comb_fee) print("Could not register address(es)") return False
def invokeDeploy(self): if self.contract_script: tx, fee, results, num_ops = test_invoke(self.contract_script, self.Wallet, []) print("tx", tx.Hash) print("tx.inputs", tx.inputs) print("fee", fee) InvokeContract(self.Wallet, tx, fee) print('\nDEPLOY', results) print('new_sc:', self.neo_fund_sc) print('deploing Contract...')
def load_smart_contract(self, args): if not self.Wallet: print("Please open a wallet") return function_code = LoadContract(args[1:]) if function_code: contract_script = GatherContractDetails(function_code, self) if contract_script is not None: tx, fee, results, num_ops = test_invoke( contract_script, self.Wallet, []) if tx is not None and results is not None: print( "\n-------------------------------------------------------------------------------------------------------------------------------------" ) print("Test deploy invoke successful") print("Total operations executed: %s " % num_ops) print("Results:") print("\t(Raw) %s" % [str(item) for item in results]) #print("\t(Int) %s" % [item.GetBigInteger() for item in results]) print("\t(Str) %s" % [item.GetString() for item in results]) print("\t(Bool) %s" % [item.GetBoolean() for item in results]) print("Deploy Invoke TX GAS cost: %s " % (tx.Gas.value / Fixed8.D)) print("Deploy Invoke TX Fee: %s " % (fee.value / Fixed8.D)) print( "-------------------------------------------------------------------------------------------------------------------------------------\n" ) print( "Enter your password to continue and deploy this contract" ) passwd = prompt("[password]> ", is_password=True) if not self.Wallet.ValidatePassword(passwd): return print("Incorrect password") result = InvokeContract(self.Wallet, tx, Fixed8.Zero()) return else: print("Test invoke failed") print("TX is %s, results are %s" % (tx, results)) return
def execute(self, arguments): wallet = PromptData.Wallet if len(arguments) != 4: print("Please specify the required parameters") return False token_str = arguments[0] from_addr = arguments[1] to_addr = arguments[2] try: amount = float(arguments[3]) except ValueError: print(f"{arguments[3]} is not a valid amount") return False try: token, tx, fee, results = test_token_send_from(wallet, token_str, from_addr, to_addr, amount) except ValueError as e: # invalid arguments or bad allowance print(str(e)) return False except Exception as e: # we act as the final capturing place print("Something really unexpected happened") logger.error(traceback.format_exc()) return False if tx and results: vm_result = results[0].GetBigInteger() if vm_result == 1: print("\n-----------------------------------------------------------") print("Transfer of %s %s from %s to %s" % ( string_from_amount(token, amount), token.symbol, from_addr, to_addr)) print("Transfer fee: %s " % (fee.value / Fixed8.D)) print("-------------------------------------------------------------\n") passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return False return InvokeContract(wallet, tx, fee) print(f"Could not transfer tokens. Virtual machine returned: {vm_result}") return False print(f"Could not transfer tokens. An unknown error occurred resulting in no Transaction object or VM output.") return False
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, invoke_attrs = PromptUtils.get_tx_attr_from_args(arguments) arguments, owners = PromptUtils.get_owners_from_params(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 tx, fee, results, num_ops, engine_success = TestInvokeContract(wallet, arguments, from_addr=from_addr, invoke_attrs=invoke_attrs, owners=owners) if tx and results: 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") print("Enter your password to continue and invoke on the network\n") tx.Attributes = invoke_attrs passwd = prompt("[password]> ", is_password=True) if not wallet.ValidatePassword(passwd): return print("Incorrect password") return InvokeContract(wallet, tx, fee, from_addr=from_addr, owners=owners) else: print("Error testing contract invoke") return False
def token_send_from(wallet, args, prompt_passwd=True): if len(args) != 4: print( "please provide a token symbol, from address, to address, and amount" ) return False token = get_asset_id(wallet, args[0]) send_from = args[1] send_to = args[2] amount = amount_from_string(token, args[3]) allowance = token_get_allowance(wallet, args[:-1], verbose=False) if allowance and allowance >= amount: tx, fee, results = token.TransferFrom(wallet, send_from, send_to, amount) if tx is not None and results is not None and len(results) > 0: if results[0].GetBigInteger() == 1: if prompt_passwd: print( "\n-----------------------------------------------------------" ) print("Transfer of %s %s from %s to %s" % (string_from_amount(token, amount), token.symbol, send_from, send_to)) print("Transfer fee: %s " % (fee.value / Fixed8.D)) print( "-------------------------------------------------------------\n" ) passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return False return InvokeContract(wallet, tx, fee) print("Requestest transfer from is greater than allowance") return False
def CancelWithdrawalHolds(wallet, contract_hash, require_password=True): wallet.LoadHolds() to_cancel = [] for hold in wallet._holds: if hold.FromAddress == contract_hash: to_cancel.append(hold) if len(to_cancel) < 1: print("No holds to cancel") return sb = ScriptBuilder() for hold in to_cancel: sb.EmitAppCallWithOperationAndData(hold.InputHash, 'cancel_hold', hold.Vin) try: tx, fee, results, num_ops = test_invoke(sb.ToArray(), wallet, []) for i in results: if not i.GetBoolean(): print("Error executing hold cleanup") return if require_password: print( "\n---------------------------------------------------------------" ) print("Will cancel %s holds" % len(to_cancel)) print("FEE IS %s " % fee.ToString()) print("GAS IS %s " % tx.Gas.ToString()) print( "------------------------------------------------------------------\n" ) print("Enter your password to complete this request") passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return result = InvokeContract(wallet, tx, fee) return result except Exception as e: print("could not cancel hold(s): %s " % e)
def CleanupCompletedHolds(wallet, require_password=True): completed = wallet.LoadCompletedHolds() if len(completed) < 1: print("No holds to cleanup") return False sb = ScriptBuilder() for hold in completed: sb.EmitAppCallWithOperationAndData(hold.InputHash, 'cleanup_hold', hold.Vin) try: tx, fee, results, num_ops = test_invoke(sb.ToArray(), wallet, []) for i in results: if not i.GetBoolean(): print("Error executing hold cleanup") return False if require_password: print( "\n---------------------------------------------------------------" ) print("Will cleanup %s holds" % len(completed)) print("FEE IS %s " % fee.ToString()) print("GAS IS %s " % tx.Gas.ToString()) print( "------------------------------------------------------------------\n" ) print("Enter your password to complete this request") passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return result = InvokeContract(wallet, tx, fee) return result except Exception as e: print("could not cancel hold(s): %s " % e) return False
def invokeMethod(self, args): """ Invoke a method of the Smart Contract """ if self.Wallet.IsSynced is False: raise Exception("Wallet is not synced.") tx, fee, results, num_ops = TestInvokeContract(self.Wallet, args) if not tx: raise Exception("TestInvokeContract failed") logger.info("TestInvokeContract done, calling InvokeContract now...") #tStart = time.time() fee = Fixed8.Zero() sent_tx = InvokeContract(self.Wallet, tx, fee) if sent_tx: logger.info("InvokeContract success, transaction underway: %s" %sent_tx.Hash.ToString())
def test_invoke_contract(args): if not Wallet: print("where's the wallet") return if args and len(args) > 0: # Wait one block h = Blockchain.Default().Height while (h == Blockchain.Default().Height): sleep(10) while(Blockchain.Default().Height - Wallet._current_height > 1): print("sleeping whilst it syncs up") print(Blockchain.Default().Height) print(Wallet._current_height) sleep(5) logger.info("here are the args to run") logger.info(args) logger.info(args[1:]) tx, fee, results, num_ops= TestInvokeContract(Wallet, args) print( "\n-------------------------------------------------------------------------------------------------------------------------------------") print("Test invoke successful") print("Total operations: %s" % num_ops) print("Results %s" % [str(item) for item in results]) print("Invoke TX GAS cost: %s" % (tx.Gas.value / Fixed8.D)) print("Invoke TX fee: %s" % (fee.value / Fixed8.D)) print( "-------------------------------------------------------------------------------------------------------------------------------------\n") print("Results %s " % [str(item) for item in results]) print(tx.Gas.value / Fixed8.D) if tx is not None and results is not None: print("Invoking for real") print(Wallet.ToJson()) result = InvokeContract(Wallet, tx, fee) return return
def load_smart_contract(self, args): if not self.Wallet: print("Please open a wallet") return args, from_addr = get_from_addr(args) function_code = LoadContract(args[1:]) if function_code: # Fill contract info and generate bytescript. contract_script = self.GatherContractDetails(function_code) contract_script_json = function_code.ToJson() hash = contract_script_json['hash'] if contract_script is not None: # testing invoke contract. tx, fee, results, num_ops = test_invoke(contract_script, self.Wallet, [], from_addr=from_addr) if tx is not None and results is not None: print( "Enter your password to continue and deploy this contract" ) passwd = prompt("[password]> ", is_password=True) if not self.Wallet.ValidatePassword(passwd): return print("Incorrect password") # Deploy contract to the blockchain. result = InvokeContract(self.Wallet, tx, Fixed8.Zero(), from_addr=from_addr) return hash else: print("Test invoke failed") print("TX is %s, results are %s" % (tx, results)) return
def execute(self, arguments): wallet = PromptData.Wallet if len(arguments) != 4: print("Please specify the required parameters") return False token_str = arguments[0] from_addr = arguments[1] to_addr = arguments[2] amount = arguments[3] try: token = _validate_nep5_args(wallet, token_str, from_addr, to_addr, amount) except ValueError as e: print(str(e)) return False decimal_amount = amount_from_string(token, amount) tx, fee, results = token.Approve(wallet, from_addr, to_addr, decimal_amount) if tx and results: if results[0].GetBigInteger() == 1: print("\n-----------------------------------------------------------") print(f"Approve allowance of {amount} {token.symbol} from {from_addr} to {to_addr}") print(f"Transfer fee: {fee.value / Fixed8.D}") print("-------------------------------------------------------------\n") passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return False return InvokeContract(wallet, tx, fee) print("Failed to approve tokens. Make sure you are entitled for approving.") return False