コード例 #1
0
def test_create_transaction_with_auto_fail(requests_mock):

    requests_mock.register_uri(
        "GET",
        "{}/auth/accounts/band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c".format(TEST_RPC),
        json={
            "height": "650788",
            "result": {
                "type": "cosmos-sdk/Account",
                "value": {
                    "address": "band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c",
                    "coins": [{"denom": "uband", "amount": "104082359107"}],
                    "public_key": {
                        "type": "tendermint/PubKeySecp256k1",
                        "value": "A/5wi9pmUk/SxrzpBoLjhVWoUeA9Ku5PYpsF3pD1Htm8",
                    },
                    "account_number": "36",
                    "sequence": "927",
                },
            },
        },
        status_code=200,
    )

    with pytest.raises(EmptyMsgError, match="messsage is empty, please use with_messages at least 1 message"):
        t = Transaction().with_auto(client)
コード例 #2
0
def test_get_tx_data_success():
    priv = PrivateKey.from_mnemonic("s")
    pubkey = priv.to_pubkey()
    addr = pubkey.to_address()

    t = (
        Transaction()
        .with_messages(
            MsgRequest(
                oracle_script_id=1,
                calldata=bytes.fromhex("000000034254430000000000000001"),
                ask_count=4,
                min_count=3,
                client_id="from_pyband",
                sender=addr,
            )
        )
        .with_account_num(100)
        .with_sequence(30)
        .with_chain_id("bandchain")
    )

    raw_data = t.get_sign_data()
    signature = priv.sign(raw_data)
    raw_tx = t.get_tx_data(signature, pubkey)

    assert raw_tx == {
        "msg": [
            {
                "type": "oracle/Request",
                "value": {
                    "oracle_script_id": "1",
                    "calldata": "AAAAA0JUQwAAAAAAAAAB",
                    "ask_count": "4",
                    "min_count": "3",
                    "client_id": "from_pyband",
                    "sender": "band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte",
                },
            }
        ],
        "fee": {"gas": "200000", "amount": [{"denom": "uband", "amount": "0"}]},
        "memo": "",
        "signatures": [
            {
                "signature": "aFvTgkY9F14dHfp2mtq8V2eCTKUtY1T9WKs99jmR8To3JB16cawbmpa1TRUdzfnqLXBh+o6XUuF4bHWR6xbCCw==",
                "pub_key": {
                    "type": "tendermint/PubKeySecp256k1",
                    "value": "A/5wi9pmUk/SxrzpBoLjhVWoUeA9Ku5PYpsF3pD1Htm8",
                },
                "account_number": "100",
                "sequence": "30",
            }
        ],
    }
コード例 #3
0
def test_get_sign_data_chain_id_fail():
    t = (Transaction().with_messages(
        MsgRequest(
            oracle_script_id=1,
            calldata=bytes.fromhex("000000034254430000000000000001"),
            ask_count=4,
            min_count=3,
            client_id="from_pyband",
            sender=Address.from_acc_bech32(
                "band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c"),
        )).with_account_num(100).with_sequence(30))

    with pytest.raises(ValueError, match="chain_id should be defined"):
        t.get_sign_data()
コード例 #4
0
def main():
    # Step 1 Create a gRPC connection
    grpc_url = "rpc-laozi-testnet2.bandchain.org:9090"
    c = Client(grpc_url, insecure=True)

    # Step 2 Convert a menmonic to private key, public key, and sender
    MNEMONIC = os.getenv("MNEMONIC")
    private_key = PrivateKey.from_mnemonic(MNEMONIC)
    public_key = private_key.to_public_key()
    sender_addr = public_key.to_address()
    sender = sender_addr.to_acc_bech32()

    # Step 3 Prepare a transaction's properties
    request_msg = MsgRequestData(
        oracle_script_id=37,
        calldata=bytes.fromhex(
            "0000000200000003425443000000034554480000000000000064"),
        ask_count=4,
        min_count=3,
        client_id="BandProtocol",
        fee_limit=[Coin(amount="100", denom="uband")],
        prepare_gas=50000,
        execute_gas=200000,
        sender=sender,
    )

    account = c.get_account(sender)
    account_num = account.account_number
    sequence = account.sequence

    fee = [Coin(amount="0", denom="uband")]
    chain_id = c.get_chain_id()

    # Step 4 Construct a transaction
    txn = (Transaction().with_messages(request_msg).with_sequence(
        sequence).with_account_num(account_num).with_chain_id(
            chain_id).with_gas(2000000).with_fee(fee).with_memo(""))

    # Step 5 Sign a transaction by using private key
    sign_doc = txn.get_sign_doc(public_key)
    signature = private_key.sign(sign_doc.SerializeToString())
    tx_raw_bytes = txn.get_tx_data(signature, public_key)

    # Step 6 Broadcast a transaction
    tx_block = c.send_tx_block_mode(tx_raw_bytes)

    # Converting to JSON for readability
    print(MessageToJson(tx_block))
コード例 #5
0
def test_get_sign_data_memo_fail():
    t = (Transaction().with_messages(
        MsgRequest(
            oracle_script_id=1,
            calldata=bytes.fromhex("000000034254430000000000000001"),
            ask_count=4,
            min_count=3,
            client_id="from_pyband",
            sender=Address.from_acc_bech32(
                "band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c"),
        )).with_account_num(100).with_sequence(30).with_chain_id("bandchain"))

    with pytest.raises(ValueError, match="memo is too large"):
        t.with_memo(
            "This is the longest memo in the world. This is the longest memo in the world. This is the longest memo in the world. This is the longest memo in the world. This is the longest memo in the world. This is the longest memo in the world. This is the longest memo in the world.This is the longest memo in the world. This is the longest memo in the world.This is the longest memo in the world."
        )
コード例 #6
0
def test_get_sign_data_success():
    t = (Transaction().with_messages(
        MsgRequest(
            oracle_script_id=1,
            calldata=bytes.fromhex("000000034254430000000000000001"),
            ask_count=4,
            min_count=3,
            client_id="from_pyband",
            sender=Address.from_acc_bech32(
                "band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c"),
        )).with_account_num(100).with_sequence(30).with_chain_id("bandchain"))

    assert (
        t.get_sign_data() ==
        b'{"account_number":"100","chain_id":"bandchain","fee":{"amount":[{"amount":"0","denom":"uband"}],"gas":"200000"},"memo":"","msgs":[{"type":"oracle/Request","value":{"ask_count":"4","calldata":"AAAAA0JUQwAAAAAAAAAB","client_id":"from_pyband","min_count":"3","oracle_script_id":"1","sender":"band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c"}}],"sequence":"30"}'
    )
コード例 #7
0
def test_get_sign_data_with_auto_success(requests_mock):

    requests_mock.register_uri(
        "GET",
        "{}/auth/accounts/band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c".format(TEST_RPC),
        json={
            "height": "650788",
            "result": {
                "type": "cosmos-sdk/Account",
                "value": {
                    "address": "band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c",
                    "coins": [{"denom": "uband", "amount": "104082359107"}],
                    "public_key": {
                        "type": "tendermint/PubKeySecp256k1",
                        "value": "A/5wi9pmUk/SxrzpBoLjhVWoUeA9Ku5PYpsF3pD1Htm8",
                    },
                    "account_number": "36",
                    "sequence": "927",
                },
            },
        },
        status_code=200,
    )

    t = (
        Transaction()
        .with_messages(
            MsgRequest(
                oracle_script_id=1,
                calldata=bytes.fromhex("000000034254430000000000000001"),
                ask_count=4,
                min_count=3,
                client_id="from_pyband",
                sender=Address.from_acc_bech32("band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c"),
            )
        )
        .with_auto(client)
        .with_chain_id("bandchain")
        .with_gas(500000)
        .with_fee(10)
    )

    assert (
        t.get_sign_data()
        == b'{"account_number":"36","chain_id":"bandchain","fee":{"amount":[{"amount":"10","denom":"uband"}],"gas":"500000"},"memo":"","msgs":[{"type":"oracle/Request","value":{"ask_count":"4","calldata":"AAAAA0JUQwAAAAAAAAAB","client_id":"from_pyband","min_count":"3","oracle_script_id":"1","sender":"band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c"}}],"sequence":"927"}'
    )
コード例 #8
0
def test_get_sign_data_msgs_fail():
    t = Transaction().with_account_num(100).with_sequence(30).with_chain_id("bandchain")

    with pytest.raises(EmptyMsgError, match="message is empty"):
        t.get_sign_data()