def test_2_msgs_in_1_tx(blockchain_accounts,
                        local_test_network_config: "NetworkConfig"):
    client = GrpcClient(local_test_network_config)
    alice_info = get_blockchain_account_info(blockchain_accounts, ALICE)
    alice_wallet = Wallet(alice_info["mnemonic"])
    alice_account = client.query_account(alice_info["address"])
    bob_info = get_blockchain_account_info(blockchain_accounts, BOB)
    bob_wallet = Wallet(bob_info["mnemonic"])
    alice_bal_init = client.query_account_balance(alice_wallet.address)
    bob_bal_init = client.query_account_balance(bob_wallet.address)
    alice_coin_init = CROCoin(alice_bal_init.balance.amount,
                              alice_bal_init.balance.denom,
                              local_test_network_config)
    bob_coin_init = CROCoin(bob_bal_init.balance.amount,
                            bob_bal_init.balance.denom,
                            local_test_network_config)

    ten_cro = CROCoin("10", CRO_DENOM, local_test_network_config)
    twnenty_cro = CROCoin("20", CRO_DENOM, local_test_network_config)
    one_cro_fee = CROCoin("1", CRO_DENOM, local_test_network_config)
    msg_send_10_cro = MsgSend(
        from_address=alice_info["address"],
        to_address=bob_info["address"],
        amount=[ten_cro.protobuf_coin_message],
    )
    msg_send_20_cro = MsgSend(
        from_address=alice_info["address"],
        to_address=bob_info["address"],
        amount=[twnenty_cro.protobuf_coin_message],
    )

    tx = Transaction(
        chain_id=local_test_network_config.chain_id,
        from_wallets=[alice_wallet],
        msgs=[msg_send_10_cro],
        account_number=alice_account.account_number,
        fee=[one_cro_fee.protobuf_coin_message],
        client=client,
    ).append_message(msg_send_20_cro)

    signature_alice = alice_wallet.sign(tx.sign_doc.SerializeToString())
    signed_tx = tx.set_signatures(signature_alice).signed_tx

    client.broadcast_transaction(signed_tx.SerializeToString())

    alice_bal_aft = client.query_account_balance(alice_wallet.address)
    bob_bal_aft = client.query_account_balance(bob_wallet.address)
    alice_coin_aft = CROCoin(alice_bal_aft.balance.amount,
                             alice_bal_aft.balance.denom,
                             local_test_network_config)
    bob_coin_aft = CROCoin(bob_bal_aft.balance.amount,
                           bob_bal_aft.balance.denom,
                           local_test_network_config)

    assert alice_coin_aft == alice_coin_init - ten_cro - twnenty_cro - one_cro_fee
    assert bob_coin_aft == bob_coin_init + ten_cro + twnenty_cro
Ejemplo n.º 2
0
def simple_transaction():
    client = GrpcClient(LOCAL_NETWORK)

    sending_wallet = Wallet(
        MNEMONIC_PHRASE, LOCAL_NETWORK.derivation_path, LOCAL_NETWORK.address_prefix
    )
    sending_account = client.query_account(sending_wallet.address)
    sending_account_init_bal = client.query_account_balance(sending_wallet.address)
    receiving_account_init_bal = client.query_account_balance(TO_ADDRESS)

    print(
        f"sending account initial balance: {sending_account_init_bal.balance.amount}"
        f"{sending_account_init_bal.balance.denom}"
    )
    print(
        f"receiving account initial balance: {receiving_account_init_bal.balance.amount}"
        f"{receiving_account_init_bal.balance.denom}"
    )

    ten_cro = CROCoin("10", "cro", LOCAL_NETWORK)
    one_cro_fee = CROCoin("1", "cro", LOCAL_NETWORK)

    msg_send = MsgSend(
        from_address=sending_wallet.address,
        to_address=TO_ADDRESS,
        amount=[ten_cro.protobuf_coin_message],
    )
    tx = Transaction(
        chain_id=LOCAL_NETWORK.chain_id,
        from_wallets=[sending_wallet],
        msgs=[msg_send],
        account_number=sending_account.account_number,
        fee=[one_cro_fee.protobuf_coin_message],
        client=client,
    )

    signature_alice = sending_wallet.sign(tx.sign_doc.SerializeToString())
    signed_tx = tx.set_signatures(signature_alice).signed_tx

    client.broadcast_transaction(signed_tx.SerializeToString())

    sending_account_aft_bal = client.query_account_balance(sending_wallet.address)
    receiving_account_aft_bal = client.query_account_balance(TO_ADDRESS)

    print("After transaction of sending 10cro with a 1cro fee:")
    print(
        f"sending account after balance: {sending_account_aft_bal.balance.amount}"
        f"{sending_account_aft_bal.balance.denom}"
    )
    print(
        f"receiving account after balance: {receiving_account_aft_bal.balance.amount}"
        f"{receiving_account_aft_bal.balance.denom}"
    )