def test_args_parser():
    account = (
        b"\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07\x00\x08"
        b"\x00\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07"
    )

    amount = 123456

    args = [{"name": "amount", "value": {"long_value": amount}},
            {"name": "account", "value": {"bytes_value": account.hex()}},
            {"name": "purse_id", "value": {"optional_value": {}}},
            {"name": "number", "value": {"big_int": {"value": "2", "bit_width": 512}}}]

    json_str = json.dumps(args)

    assert ABI.args_from_json(json_str) == ABI.args(
        [ABI.long_value(amount), ABI.account(account), ABI.optional_value(None), ABI.big_int(2)]
    )
Exemple #2
0
def test_args_from_json():
    account = bytes(range(32))
    long_value = 123456
    big_int_value = 123456789012345678901234567890
    args = [
        {
            "name": "amount",
            "value": {
                "long_value": long_value
            }
        },
        {
            "name": "account",
            "value": {
                "bytes_value": account.hex()
            }
        },
        {
            "name": "purse_id",
            "value": {
                "optional_value": {}
            }
        },
        {
            "name": "number",
            "value": {
                "big_int": {
                    "value": f"{big_int_value}",
                    "bit_width": 512
                }
            },
        },
    ]

    json_str = json.dumps(args)
    args = ABI.args_from_json(json_str)
    assert args[0] == ABI.long_value("amount", long_value)
    assert args[1] == ABI.account("account", account)
    assert args[2] == ABI.optional_value("purse_id", None)
    assert args[3] == ABI.big_int("number", big_int_value)
Exemple #3
0
def _call_pos_bonding(
    node,
    from_address: str,
    amount: int,
    public_key: str,
    private_key: str,
    gas_price: int = 1,
    session_contract: str = Contract.POS_BONDING,
    method: bytes = b"bond",
):
    if method == b"bond":
        session_args = ABI.args(
            [ABI.byte_array("method", method),
             ABI.u512("amount", amount)])
    elif method == b"unbond":
        session_args = ABI.args([
            ABI.byte_array("method", method),
            ABI.optional_value(
                "amount",
                ABI.u512("amount", amount) if amount is not None else None),
        ])
    else:
        raise Exception(f"_call_pos_bonding: method {method} not supported")

    node.p_client.deploy(
        from_address=from_address,
        session_contract=session_contract,
        gas_price=gas_price,
        public_key=public_key,
        private_key=private_key,
        session_args=session_args,
    )
    block_hash = node.p_client.propose().block_hash.hex()
    for deploy_info in node.p_client.show_deploys(block_hash):
        if deploy_info.is_error:
            raise Exception(f"bond: {deploy_info.error_message}")
    return block_hash