コード例 #1
0
def test_construct_sign_and_submit_transfer_transaction():
    """Test the construction, signing and submitting of a transfer transaction."""
    account = EthereumCrypto(private_key_path=ETHEREUM_PRIVATE_KEY_PATH)
    ec2 = EthereumCrypto()
    ethereum_api = EthereumApi(**ETHEREUM_TESTNET_CONFIG)

    amount = 40000
    tx_nonce = ethereum_api.generate_tx_nonce(ec2.address, account.address)
    transfer_transaction = ethereum_api.get_transfer_transaction(
        sender_address=account.address,
        destination_address=ec2.address,
        amount=amount,
        tx_fee=30000,
        tx_nonce=tx_nonce,
        chain_id=3,
    )
    assert (isinstance(transfer_transaction, dict)
            and len(transfer_transaction)
            == 7), "Incorrect transfer_transaction constructed."

    signed_transaction = account.sign_transaction(transfer_transaction)
    assert (isinstance(signed_transaction,
                       eth_account.datastructures.AttributeDict)
            and len(signed_transaction)
            == 5), "Incorrect signed_transaction constructed."

    transaction_digest = ethereum_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 = ethereum_api.get_transaction_receipt(
            transaction_digest)
        if transaction_receipt is None:
            continue
        is_settled = ethereum_api.is_transaction_settled(transaction_receipt)
        not_settled = not is_settled
    assert transaction_receipt is not None, "Failed to retrieve transaction receipt."
    assert is_settled, "Failed to verify tx!"

    tx = ethereum_api.get_transaction(transaction_digest)
    is_valid = ethereum_api.is_transaction_valid(tx, ec2.address,
                                                 account.address, tx_nonce,
                                                 amount)
    assert is_valid, "Failed to settle tx correctly!"
    assert tx != transaction_receipt, "Should not be same!"
コード例 #2
0
def test_ethereum_api_get_transfer_transaction(*args):
    """Test EthereumApi.get_transfer_transaction."""
    ethereum_api = EthereumApi()
    assert ethereum_api.get_transfer_transaction(*[MagicMock()] * 7) is None