async def test_get_balance(ledger_id, address, config, ledger_apis_connection: Connection): """Test get balance.""" import aea # noqa # to load registries ledger_api_dialogues = LedgerApiDialogues(address) request, ledger_api_dialogue = ledger_api_dialogues.create( counterparty=str(ledger_apis_connection.connection_id), performative=LedgerApiMessage.Performative.GET_BALANCE, ledger_id=ledger_id, address=address, ) envelope = Envelope( to=request.to, sender=request.sender, protocol_id=request.protocol_id, message=request, ) await ledger_apis_connection.send(envelope) await asyncio.sleep(0.01) response = await ledger_apis_connection.receive() assert response is not None assert type(response.message) == LedgerApiMessage response_msg = cast(LedgerApiMessage, response.message) response_dialogue = ledger_api_dialogues.update(response_msg) assert response_dialogue == ledger_api_dialogue assert response_msg.performative == LedgerApiMessage.Performative.BALANCE actual_balance_amount = response_msg.balance expected_balance_amount = make_ledger_api(ledger_id, **config).get_balance(address) assert actual_balance_amount == expected_balance_amount
def get_transfer_transaction( cls, identifier: str, sender_address: str, destination_address: str, amount: int, tx_fee: int, tx_nonce: str, **kwargs, ) -> Optional[Any]: """ Get a transaction to transfer from self to destination. :param identifier: the identifier of the ledger :param sender_address: the address of the sender :param destination_address: the address of the receiver :param amount: the amount :param tx_nonce: verifies the authenticity of the tx :param tx_fee: the tx fee :return: tx """ assert ( identifier in ledger_apis_registry.supported_ids ), "Not a registered ledger api identifier." api = make_ledger_api(identifier, **cls.ledger_api_configs[identifier]) tx = api.get_transfer_transaction( sender_address, destination_address, amount, tx_fee, tx_nonce, **kwargs, ) return tx
def _check_validity(self) -> None: """ Checks validity of record. Specificyally: - if ledger_id is valid - if agent signed the message - if message is correctly formatted """ if self.message != self._get_message(self.representative_public_key): raise ValueError("Invalid message.") # pragma: no cover ledger_api = make_ledger_api(self.ledger_id) public_keys = ledger_api.recover_public_keys_from_message( self.message, self.signature) if len(public_keys) == 0: raise ValueError("Malformed signature!") # pragma: no cover public_key: Optional[str] = None for public_key_ in public_keys: address = ledger_api.get_address_from_public_key(public_key_) if address == self.address: public_key = public_key_ break if public_key is None: raise ValueError( "Invalid signature for provided representative_public_key and agent address!" ) self._public_key = public_key
def get_api(cls, identifier: str) -> LedgerApi: """Get the ledger API.""" assert ( identifier in ledger_apis_registry.supported_ids ), "Not a registered ledger api identifier." api = make_ledger_api(identifier, **cls.ledger_api_configs[identifier]) return api
async def test_get_state( ledger_id, address, ledger_apis_connection: Connection, update_default_ethereum_ledger_api, ethereum_testnet_config, ganache, ): """Test get state.""" import aea # noqa # to load registries if ledger_id == FETCHAI: config = FETCHAI_TESTNET_CONFIG else: config = ethereum_testnet_config if "ethereum" in ledger_id: callable_name = "getBlock" else: callable_name = "blocks" args = ("latest", ) kwargs = Kwargs({}) ledger_api_dialogues = LedgerApiDialogues(address) request, ledger_api_dialogue = ledger_api_dialogues.create( counterparty=str(ledger_apis_connection.connection_id), performative=LedgerApiMessage.Performative.GET_STATE, ledger_id=ledger_id, callable=callable_name, args=args, kwargs=kwargs, ) envelope = Envelope( to=request.to, sender=request.sender, message=request, ) await ledger_apis_connection.send(envelope) await asyncio.sleep(0.01) response = await ledger_apis_connection.receive() assert response is not None assert type(response.message) == LedgerApiMessage response_msg = cast(LedgerApiMessage, response.message) response_dialogue = ledger_api_dialogues.update(response_msg) assert response_dialogue == ledger_api_dialogue assert (response_msg.performative == LedgerApiMessage.Performative.STATE ), response_msg actual_block = response_msg.state.body expected_block = make_ledger_api(ledger_id, **config).get_state(callable_name, *args) assert actual_block == expected_block
def get_balance(cls, identifier: str, address: str) -> Optional[int]: """ Get the token balance. :param identifier: the identifier of the ledger :param address: the address to check for :return: the token balance """ assert ( identifier in ledger_apis_registry.supported_ids ), "Not a registered ledger api identifier." api = make_ledger_api(identifier, **cls.ledger_api_configs[identifier]) balance = api.get_balance(address) return balance
def get_transaction(cls, identifier: str, tx_digest: str) -> Optional[Any]: """ Get the transaction for a transaction digest. :param identifier: the identifier of the ledger :param tx_digest: the digest associated to the transaction. :return: the tx, if present """ assert ( identifier in ledger_apis_registry.supported_ids ), "Not a registered ledger api identifier." api = make_ledger_api(identifier, **cls.ledger_api_configs[identifier]) tx = api.get_transaction(tx_digest) return tx
def send_signed_transaction(cls, identifier: str, tx_signed: Any) -> Optional[str]: """ Send a signed transaction and wait for confirmation. :param identifier: the identifier of the ledger :param tx_signed: the signed transaction :return: the tx_digest, if present """ assert ( identifier in ledger_apis_registry.supported_ids ), "Not a registered ledger api identifier." api = make_ledger_api(identifier, **cls.ledger_api_configs[identifier]) tx_digest = api.send_signed_transaction(tx_signed) return tx_digest
def __init__( self, ledger_api_configs: Dict[str, Dict[str, Union[str, int]]], default_ledger_id: str, ): """ Instantiate a wallet object. :param ledger_api_configs: the ledger api configs. :param default_ledger_id: the default ledger id. """ apis = {} # type: Dict[str, LedgerApi] for identifier, config in ledger_api_configs.items(): api = make_ledger_api(identifier, **config) apis[identifier] = api self._apis = apis self._configs = ledger_api_configs self._default_ledger_id = default_ledger_id
async def test_get_balance(ledger_id, address, config, ledger_apis_connection: Connection): """Test get balance.""" import aea # noqa # to load registries ledger_api_dialogues = LedgerApiDialogues(address) request = LedgerApiMessage( performative=LedgerApiMessage.Performative.GET_BALANCE, dialogue_reference=ledger_api_dialogues. new_self_initiated_dialogue_reference(), ledger_id=ledger_id, address=address, ) request.counterparty = str(ledger_apis_connection.connection_id) ledger_api_dialogue = ledger_api_dialogues.update(request) assert ledger_api_dialogue is not None envelope = Envelope( to=str(ledger_apis_connection.connection_id), sender=address, protocol_id=request.protocol_id, message=request, ) await ledger_apis_connection.send(envelope) await asyncio.sleep(0.01) response = await ledger_apis_connection.receive() assert response is not None assert type(response.message) == LedgerApiMessage response_msg_orig = cast(LedgerApiMessage, response.message) response_msg = copy.copy(response_msg_orig) response_msg.is_incoming = True response_msg.counterparty = response_msg_orig.sender response_dialogue = ledger_api_dialogues.update(response_msg) assert response_dialogue == ledger_api_dialogue assert response_msg.performative == LedgerApiMessage.Performative.BALANCE actual_balance_amount = response_msg.balance expected_balance_amount = make_ledger_api(ledger_id, **config).get_balance(address) assert actual_balance_amount == expected_balance_amount
def test_make_ledger_api_fetchai_positive(): """Test make_crypto for fetchai.""" ledger_api = make_ledger_api("fetchai", **{"network": "testnet"}) assert isinstance(ledger_api, FetchAIApi)
def test_make_ledger_api_cosmos_positive(): """Test make_crypto for fetchai.""" ledger_api = make_ledger_api(COSMOS, **{"network": "testnet"}) assert isinstance(ledger_api, CosmosApi)