def spend_by_name(self, account: Account, recipient_name: str, amount: int, payload: str = "", fee: int = defaults.FEE, tx_ttl: int = defaults.TX_TTL): """ Create and execute a spend to name_id transaction """ if utils.is_valid_aens_name(recipient_name): name_id = hashing.name_id(recipient_name) return self.spend(account, name_id, amount, payload, fee, tx_ttl) raise TypeError("Invalid AENS name. Please provide a valid AENS name.")
def __init__(self, domain, client): if not utils.is_valid_aens_name(domain): raise ValueError("Invalid domain ", domain) self.client = client self.domain = domain.lower() self.name_id = hashing.name_id(domain) self.status = NameStatus.UNKNOWN # set after preclaimed: self.preclaimed_block_height = None self.preclaimed_tx_hash = None self.preclaimed_commitment_hash = None self.preclaim_salt = None # set after claimed self.name_ttl = 0 self.pointers = None
def spend(self, account: Account, recipient_id: str, amount, payload: str = "", fee: int = defaults.FEE, tx_ttl: int = defaults.TX_TTL) -> transactions.TxObject: """ Create and execute a spend transaction, automatically retrieve the nonce for the siging account and calculate the absolut ttl. :param account: the account signing the spend transaction (sender) :param recipient_id: the recipient address or name_id :param amount: the amount to spend :param payload: the payload for the transaction :param fee: the fee for the transaction (automatically calculated if not provided) :param tx_ttl: the transaction ttl expressed in relative number of blocks :return: the TxObject of the transaction :raises TypeError: if the recipient_id is not a valid name_id or address """ if utils.is_valid_aens_name(recipient_id): recipient_id = hashing.name_id(recipient_id) elif not utils.is_valid_hash(recipient_id, prefix="ak"): raise TypeError("Invalid recipient_id. Please provide a valid AENS name or account pub_key.") # parse amount and fee amount, fee = utils._amounts_to_aettos(amount, fee) # retrieve the nonce account.nonce = self.get_next_nonce(account.get_address()) if account.nonce == 0 else account.nonce + 1 # retrieve ttl tx_ttl = self.compute_absolute_ttl(tx_ttl) # build the transaction tx = self.tx_builder.tx_spend(account.get_address(), recipient_id, amount, payload, fee, tx_ttl.absolute_ttl, account.nonce) # get the signature tx = self.sign_transaction(account, tx) # post the signed transaction transaction self.broadcast_transaction(tx) return tx
def test_utils_is_valid_name(): # input (hash_str, prefix, expected output) args = [ ('valid.test', True), ('v.test', True), ('isaverylongnamethatidontknow.test', True), ('0123.test', True), ('0alsoGOod.test', True), ('valid.test', True), ('valid.test', True), ('aeternity.com', False), ('aeternity.aet', False), ('om', False), (None, False), (".o.test", False), ("-o.test", False), ] for a in args: got = utils.is_valid_aens_name(a[0]) expected = a[1] assert got == expected
def transfer_funds(self, account: Account, recipient_id: str, percentage: float, payload: str = "", tx_ttl: int = defaults.TX_TTL, fee: int = defaults.FEE, include_fee=True): """ Create and execute a spend transaction """ if utils.is_valid_aens_name(recipient_id): recipient_id = hashing.name_id(recipient_id) elif not utils.is_valid_hash(recipient_id, prefix="ak"): raise TypeError("Invalid recipient_id. Please provide a valid AENS name or account pub_key.") if percentage < 0 or percentage > 1: raise ValueError(f"Percentage should be a number between 0 and 1, got {percentage}") # parse amounts fee = utils.amount_to_aettos(fee) # retrieve the balance account_on_chain = self.get_account_by_pubkey(pubkey=account.get_address()) request_transfer_amount = int(account_on_chain.balance * percentage) # retrieve the nonce account.nonce = account_on_chain.nonce + 1 # retrieve ttl tx_ttl = self.compute_absolute_ttl(tx_ttl) # build the transaction tx = self.tx_builder.tx_spend(account.get_address(), recipient_id, request_transfer_amount, payload, fee, tx_ttl.absolute_ttl, account.nonce) # if the request_transfer_amount should include the fee keep calculating the fee if include_fee: amount = request_transfer_amount while (amount + tx.data.fee) > request_transfer_amount: amount = request_transfer_amount - tx.data.fee tx = self.tx_builder.tx_spend(account.get_address(), recipient_id, amount, payload, fee, tx_ttl.absolute_ttl, account.nonce) # execute the transaction tx = self.sign_transaction(account, tx) # post the transaction self.broadcast_transaction(tx) return tx