def get_asset_attachments(params): to_remove = [] neo_to_attach = None gas_to_attach = None for item in params: if type(item) is str: if '--attach-neo=' in item: to_remove.append(item) try: neo_to_attach = Fixed8.TryParse( int(item.replace('--attach-neo=', ''))) except Exception as e: pass if '--attach-gas=' in item: to_remove.append(item) try: gas_to_attach = Fixed8.FromDecimal( float(item.replace('--attach-gas=', ''))) except Exception as e: pass for item in to_remove: params.remove(item) return params, neo_to_attach, gas_to_attach
def send_neo(wallet, address_from, address_to, amount): assetId = None assetId = Blockchain.Default().SystemShare().Hash scripthash_to = wallet.ToScriptHash(address_to) scripthash_from = wallet.ToScriptHash(address_from) f8amount = Fixed8.TryParse(amount) if f8amount.value % pow(10, 8 - Blockchain.Default().GetAssetState(assetId.ToBytes()).Precision) != 0: raise Exception("incorrect amount precision") fee = Fixed8.Zero() output = TransactionOutput(AssetId=assetId, Value=f8amount, script_hash=scripthash_to) tx = ContractTransaction(outputs=[output]) ttx = wallet.MakeTransaction(tx=tx, change_address=None, fee=fee, from_addr=scripthash_from) if ttx is None: raise Exception("insufficient funds, were funds already moved from multi-sig contract?") context = ContractParametersContext(tx, isMultiSig=True) wallet.Sign(context) if context.Completed: raise Exception("Something went wrong, multi-sig transaction failed") else: print("Transaction initiated") return json.dumps(context.ToJson(), separators=(',', ':'))
def get_asset_amount(amount, assetId): f8amount = Fixed8.TryParse(amount) if f8amount is None: print("invalid amount format") elif f8amount.value % pow( 10, 8 - Blockchain.Default().GetAssetState( assetId.ToBytes()).Precision) != 0: print("incorrect amount precision") return None return f8amount
def do_send(self, arguments): try: if not self.Wallet: print("please open a wallet") return if len(arguments) < 3: print("Not enough arguments") return to_send = get_arg(arguments) address = get_arg(arguments, 1) amount = get_arg(arguments, 2) assetId = None if to_send.lower() == 'neo': assetId = Blockchain.Default().SystemShare().Hash elif to_send.lower() == 'gas': assetId = Blockchain.Default().SystemCoin().Hash elif Blockchain.Default().GetAssetState(to_send): assetId = Blockchain.Default().GetAssetState(to_send).AssetId scripthash = self.Wallet.ToScriptHash(address) if scripthash is None: print("invalid address") return f8amount = Fixed8.TryParse(amount) if f8amount is None: print("invalid amount format") return if f8amount.value % pow( 10, 8 - Blockchain.Default().GetAssetState( assetId.ToBytes()).Precision) != 0: print("incorrect amount precision") return fee = Fixed8.Zero() if get_arg(arguments, 3): fee = Fixed8.TryParse(get_arg(arguments, 3)) output = TransactionOutput(AssetId=assetId, Value=f8amount, script_hash=scripthash) tx = ContractTransaction(outputs=[output]) ttx = self.Wallet.MakeTransaction(tx=tx, change_address=None, fee=fee) if ttx is None: print("insufficient funds") return self._wallet_send_tx = ttx self._num_passwords_req = 1 self._gathered_passwords = [] self._gathering_password = True self._gather_password_action = self.do_send_created_tx except Exception as e: print("could not send: %s " % e) traceback.print_stack() traceback.print_exc()
def construct_and_send(prompter, wallet, arguments): try: if not wallet: print("please open a wallet") return if len(arguments) < 3: print("Not enough arguments") return to_send = get_arg(arguments) address_to = get_arg(arguments, 1) amount = get_arg(arguments, 2) address_from = get_arg(arguments, 3) assetId = None if to_send.lower() == 'neo': assetId = Blockchain.Default().SystemShare().Hash elif to_send.lower() == 'gas': assetId = Blockchain.Default().SystemCoin().Hash elif Blockchain.Default().GetAssetState(to_send): assetId = Blockchain.Default().GetAssetState(to_send).AssetId scripthash = wallet.ToScriptHash(address_to) if scripthash is None: print("invalid address") return f8amount = Fixed8.TryParse(amount) if f8amount is None: print("invalid amount format") return if f8amount.value % pow(10, 8 - Blockchain.Default().GetAssetState(assetId.ToBytes()).Precision) != 0: print("incorrect amount precision") return fee = Fixed8.Zero() if get_arg(arguments, 3): fee = Fixed8.TryParse(get_arg(arguments, 3)) output = TransactionOutput(AssetId=assetId, Value=f8amount, script_hash=scripthash) tx = ContractTransaction(outputs=[output]) ttx = wallet.MakeTransaction(tx=tx, change_address=None, fee=fee) if ttx is None: print("insufficient funds") return passwd = prompt("[Password]> ", completer=prompter.completer, is_password=True, history=prompter.history, get_bottom_toolbar_tokens=prompter.get_bottom_toolbar, style=prompter.token_style) if not wallet.ValidatePassword(passwd): print("incorrect password") return context = ContractParametersContext(tx) wallet.Sign(context) if context.Completed: tx.scripts = context.GetScripts() wallet.SaveTransaction(tx) relayed = NodeLeader.Instance().Relay(tx) if relayed: print("Relayed Tx: %s " % tx.Hash.ToString()) else: print("Could not relay tx %s " % tx.Hash.ToString()) else: print("Could not sign transaction") return except Exception as e: print("could not send: %s " % e) traceback.print_stack() traceback.print_exc()
def construct_and_send(prompter, wallet, arguments, prompt_password=True): try: if not wallet: print("please open a wallet") return False if len(arguments) < 3: print("Not enough arguments") return False arguments, from_address = get_from_addr(arguments) to_send = get_arg(arguments) address_to = get_arg(arguments, 1) amount = get_arg(arguments, 2) assetId = get_asset_id(wallet, to_send) if assetId is None: print("Asset id not found") return False scripthash_to = lookup_addr_str(wallet, address_to) if scripthash_to is None: print("invalid address") return False scripthash_from = None if from_address is not None: scripthash_from = lookup_addr_str(wallet, from_address) # if this is a token, we will use a different # transfer mechanism if type(assetId) is NEP5Token: return do_token_transfer(assetId, wallet, from_address, address_to, amount_from_string(assetId, amount), prompt_passwd=prompt_password) f8amount = Fixed8.TryParse(amount, require_positive=True) if f8amount is None: print("invalid amount format") return False if type(assetId) is UInt256 and f8amount.value % pow( 10, 8 - Blockchain.Default().GetAssetState( assetId.ToBytes()).Precision) != 0: print("incorrect amount precision") return False fee = Fixed8.Zero() output = TransactionOutput(AssetId=assetId, Value=f8amount, script_hash=scripthash_to) tx = ContractTransaction(outputs=[output]) ttx = wallet.MakeTransaction(tx=tx, change_address=None, fee=fee, from_addr=scripthash_from) if ttx is None: print("insufficient funds") return False if prompt_password: passwd = prompt("[Password]> ", is_password=True) if not wallet.ValidatePassword(passwd): print("incorrect password") return False standard_contract = wallet.GetStandardAddress() if scripthash_from is not None: signer_contract = wallet.GetContract(scripthash_from) else: signer_contract = wallet.GetContract(standard_contract) if not signer_contract.IsMultiSigContract: data = standard_contract.Data tx.Attributes = [ TransactionAttribute(usage=TransactionAttributeUsage.Script, data=data) ] context = ContractParametersContext( tx, isMultiSig=signer_contract.IsMultiSigContract) wallet.Sign(context) if context.Completed: tx.scripts = context.GetScripts() wallet.SaveTransaction(tx) # print("will send tx: %s " % json.dumps(tx.ToJson(),indent=4)) relayed = NodeLeader.Instance().Relay(tx) if relayed: print("Relayed Tx: %s " % tx.Hash.ToString()) return tx else: print("Could not relay tx %s " % tx.Hash.ToString()) else: print("Transaction initiated, but the signature is incomplete") print(json.dumps(context.ToJson(), separators=(',', ':'))) return False except Exception as e: print("could not send: %s " % e) traceback.print_stack() traceback.print_exc() return False