def test_format_default(): """Test if default CosmosSDK transaction is correctly formated.""" account = FetchAICrypto() cc2 = FetchAICrypto() cosmos_api = FetchAIApi(**FETCHAI_TESTNET_CONFIG) amount = 10000 transfer_transaction = cosmos_api.get_transfer_transaction( sender_address=account.address, destination_address=cc2.address, amount=amount, tx_fee=1000, tx_nonce="something", ) signed_transaction = cc2.sign_transaction(transfer_transaction) assert "tx" in signed_transaction assert "signatures" in signed_transaction["tx"] assert len(signed_transaction["tx"]["signatures"]) == 1 assert "pub_key" in signed_transaction["tx"]["signatures"][0] assert "value" in signed_transaction["tx"]["signatures"][0]["pub_key"] base64_pbk = signed_transaction["tx"]["signatures"][0]["pub_key"]["value"] assert "signature" in signed_transaction["tx"]["signatures"][0] signature = signed_transaction["tx"]["signatures"][0]["signature"] default_formated_transaction = cc2.format_default_transaction( transfer_transaction, signature, base64_pbk) # Compare default formatted transaction with signed transaction assert signed_transaction == default_formated_transaction
def test_construct_sign_and_submit_transfer_transaction(): """Test the construction, signing and submitting of a transfer transaction.""" account = FetchAICrypto() balance = get_wealth(account.address) assert balance > 0, "Failed to fund account." fc2 = FetchAICrypto() fetchai_api = FetchAIApi(**FETCHAI_TESTNET_CONFIG) amount = 10000 assert amount < balance, "Not enough funds." transfer_transaction = fetchai_api.get_transfer_transaction( sender_address=account.address, destination_address=fc2.address, amount=amount, tx_fee=1000, tx_nonce="", ) assert isinstance( transfer_transaction, Transaction), "Incorrect transfer_transaction constructed." signed_transaction = account.sign_transaction(transfer_transaction) assert (isinstance(signed_transaction, Transaction) and len(signed_transaction.signatures) == 1), "Incorrect signed_transaction constructed." transaction_digest = fetchai_api.send_signed_transaction( signed_transaction) assert transaction_digest is not None, "Failed to submit transfer transaction!" not_settled = True elapsed_time = 0 while not_settled and elapsed_time < 20: elapsed_time += 1 time.sleep(2) transaction_receipt = fetchai_api.get_transaction_receipt( transaction_digest) if transaction_receipt is None: continue is_settled = fetchai_api.is_transaction_settled(transaction_receipt) if is_settled is None: continue not_settled = not is_settled assert transaction_receipt is not None, "Failed to retrieve transaction receipt." assert is_settled, "Failed to verify tx!" tx = fetchai_api.get_transaction(transaction_digest) assert tx != transaction_receipt, "Should be same!" is_valid = fetchai_api.is_transaction_valid(tx, fc2.address, account.address, "", amount) assert is_valid, "Failed to settle tx correctly!"
def test_handle_tx_signing_fetchai(self): """Test tx signing for fetchai.""" fetchai_api = FetchAIApi(**FETCHAI_TESTNET_CONFIG) account = FetchAICrypto() fc2 = FetchAICrypto() amount = 10000 transfer_transaction = fetchai_api.get_transfer_transaction( sender_address=account.address, destination_address=fc2.address, amount=amount, tx_fee=1000, tx_nonce="something", ) signing_dialogues = SigningDialogues( str(PublicId("author", "a_skill", "0.1.0"))) signing_msg = SigningMessage( performative=SigningMessage.Performative.SIGN_TRANSACTION, dialogue_reference=signing_dialogues. new_self_initiated_dialogue_reference(), 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, transfer_transaction), ) signing_dialogue = signing_dialogues.create_with_message( "decision_maker", signing_msg) assert signing_dialogue is not None self.decision_maker.message_in_queue.put_nowait(signing_msg) signing_msg_response = self.decision_maker.message_out_queue.get( timeout=2) recovered_dialogue = signing_dialogues.update(signing_msg_response) assert recovered_dialogue is not None and recovered_dialogue == signing_dialogue assert (signing_msg_response.performative == SigningMessage.Performative.SIGNED_TRANSACTION) assert type(signing_msg_response.signed_transaction.body) == dict