def get_transfer_transaction( # pylint: disable=arguments-differ self, sender_address: Address, destination_address: Address, amount: int, tx_fee: int, tx_nonce: str, **kwargs, ) -> Optional[Any]: """ Submit a transfer transaction to the ledger. :param sender_address: the sender address of the payer. :param destination_address: the destination address of the payee. :param amount: the amount of wealth to be transferred. :param tx_fee: the transaction fee. :param tx_nonce: verifies the authenticity of the tx :return: the transfer transaction """ tx = TokenTxFactory.transfer( FetchaiAddress(sender_address), FetchaiAddress(destination_address), amount, tx_fee, [], # we don't add signer here as we would need the public key for this ) self._api.set_validity_period(tx) return tx
def test_handle_tx_signing_fetchai(self): """Test tx signing for fetchai.""" tx = TokenTxFactory.transfer( FetchaiAddress("v3sZs7gKKz9xmoTo9yzRkfHkjYuX42MzXaq4eVjGHxrX9qu3U"), FetchaiAddress("2bzQNV4TTjMAiKZe85EyLUttoFpHHuksRzUUBYB1brt98pMXKK"), 1, 1, [], ) signing_dialogues = SigningDialogues("agent1") signing_msg = SigningMessage( performative=SigningMessage.Performative.SIGN_TRANSACTION, dialogue_reference=signing_dialogues.new_self_initiated_dialogue_reference(), skill_callback_ids=(str(PublicId("author", "a_skill", "0.1.0")),), skill_callback_info={}, terms=Terms( ledger_id=FETCHAI, sender_address="pk1", counterparty_address="pk2", amount_by_currency_id={"FET": -1}, is_sender_payable_tx_fee=True, quantities_by_good_id={"good_id": 10}, nonce="transaction nonce", ), raw_transaction=RawTransaction(FETCHAI, tx), ) signing_msg.counterparty = "decision_maker" self.decision_maker.message_in_queue.put_nowait(signing_msg) signing_msg_response = self.decision_maker.message_out_queue.get(timeout=2) assert ( signing_msg_response.performative == SigningMessage.Performative.SIGNED_TRANSACTION ) assert signing_msg_response.skill_callback_ids == signing_msg.skill_callback_ids assert type(signing_msg_response.signed_transaction.body) == FetchaiTransaction
def is_transaction_valid( self, tx_digest: str, seller: Address, client: Address, tx_nonce: str, amount: int, ) -> bool: """ Check whether a transaction is valid or not (non-blocking). :param seller: the address of the seller. :param client: the address of the client. :param tx_nonce: the transaction nonce. :param amount: the amount we expect to get from the transaction. :param tx_digest: the transaction digest. :return: True if the random_message is equals to tx['input'] """ is_valid = False tx_contents = self._try_get_transaction(tx_digest) if tx_contents is not None: seller_address = FetchaiAddress(seller) is_valid = (str(tx_contents.from_address) == client and amount == tx_contents.transfers[seller_address] and self.is_transaction_settled(tx_digest=tx_digest)) return is_valid
def __init__(self, private_key_path: Optional[str] = None): """ Instantiate a fetchai crypto object. :param private_key_path: the private key path of the agent """ super().__init__(private_key_path=private_key_path) self._address = str(FetchaiAddress(Identity.from_hex(self.public_key)))
def _try_get_balance(self, address: Address) -> Optional[int]: """Try get the balance.""" try: balance = self._api.tokens.balance(FetchaiAddress(address)) except Exception as e: # pragma: no cover logger.debug("Unable to retrieve balance: {}".format(str(e))) balance = None return balance
def get_address_from_public_key(cls, public_key: str) -> Address: """ Get the address from the public key. :param public_key: the public key :return: str """ identity = Identity.from_hex(public_key) address = str(FetchaiAddress(identity)) return address
def _try_transfer_tokens(self, crypto: Crypto, destination_address: Address, amount: int, tx_fee: int) -> Optional[str]: """Try transfer tokens.""" try: tx_digest = self._api.tokens.transfer( crypto.entity, FetchaiAddress(destination_address), amount, tx_fee) # self._api.sync(tx_digest) except Exception as e: # pragma: no cover logger.debug("Error when attempting transfering tokens: {}".format( str(e))) tx_digest = None return tx_digest
def _try_get_balance(self, address: Address) -> Optional[int]: """Try get the balance.""" return self._api.tokens.balance(FetchaiAddress(address))