def test_validation_errors():
    bytes_type = Bytes(10)
    int_type = Int(8)    # -128 <= i < 128
    uint_type = Uint(8)  # 0 <= i < 256
    bool_type = Boolean()

    with pytest.raises(ValueError, match='bytes10 was given bytes with length 11'):
        bytes_type.encode_value(os.urandom(11))

    with pytest.raises(OverflowError, match='too big'):
        int_type.encode_value(128)
    with pytest.raises(OverflowError, match='too big'):
        int_type.encode_value(-129)

    with pytest.raises(OverflowError, match='too big'):
        uint_type.encode_value(256)
    assert uint_type.encode_value(0) == bytes(32)
    with pytest.raises(OverflowError, match='negative int to unsigned'):
        uint_type.encode_value(-1)

    assert bool_type.encode_value(True) == bytes(31) + b'\x01'
    assert bool_type.encode_value(False) == bytes(32)
    with pytest.raises(ValueError, match='Must be True or False.'):
        bool_type.encode_value(0)
    with pytest.raises(ValueError, match='Must be True or False.'):
        bool_type.encode_value(1)
Beispiel #2
0
def test_arrays():
    assert Array(String()).type_name == 'string[]'
    assert Array(String(), 4).type_name == 'string[4]'

    assert Array(Bytes(17)).type_name == 'bytes17[]'
    assert Array(Bytes(17), 10).type_name == 'bytes17[10]'

    assert Array(Array(Uint(160))).type_name == 'uint160[][]'
Beispiel #3
0
class Foo(EIP712Struct):
    s = String()
    u_i = Uint(256)
    s_i = Int(8)
    a = Address()
    b = Boolean()
    bytes_30 = Bytes(30)
    dyn_bytes = Bytes()
    bar = Bar
    arr = Array(Bytes(1))
 class TestStruct(EIP712Struct):
     address = Address()
     boolean = Boolean()
     dyn_bytes = Bytes()
     bytes_1 = Bytes(1)
     bytes_32 = Bytes(32)
     int_32 = Int(32)
     int_256 = Int(256)
     string = String()
     uint_32 = Uint(32)
     uint_256 = Uint(256)
Beispiel #5
0
def test_bytes_validation():
    bytes0 = Bytes()
    assert bytes0.type_name == 'bytes'

    bytes0 = Bytes(0)
    assert bytes0.type_name == 'bytes'

    for n in range(1, 33):
        bytes_n = Bytes(n)
        assert bytes_n.type_name == f'bytes{n}'

    with pytest.raises(ValueError):
        Bytes(33)
Beispiel #6
0
class Transaction(EIP712Struct):
    input0 = Input()
    input1 = Input()
    input2 = Input()
    input3 = Input()
    output0 = Output()
    output1 = Output()
    output2 = Output()
    output3 = Output()
    metadata = Bytes(32)
Beispiel #7
0
class EIP712LegacySafeTx(EIP712Struct):
    to = Address()
    value = Uint(256)
    data = Bytes()
    operation = Uint(8)
    safeTxGas = Uint(256)
    dataGas = Uint(256)
    gasPrice = Uint(256)
    gasToken = Address()
    refundReceiver = Address()
    nonce = Uint(256)
Beispiel #8
0
class Transaction(EIP712Struct):
    txType = Uint(256)
    input0 = Input
    input1 = Input
    input2 = Input
    input3 = Input
    output0 = Output
    output1 = Output
    output2 = Output
    output3 = Output
    metadata = Bytes(32)
Beispiel #9
0
class EIP712SafeTx(EIP712Struct):
    to = Address()
    value = Uint(256)
    data = Bytes()
    operation = Uint(8)
    safeTxGas = Uint(256)
    baseGas = Uint(256)  # `dataGas` was renamed to `baseGas` in 1.0.0
    gasPrice = Uint(256)
    gasToken = Address()
    refundReceiver = Address()
    nonce = Uint(256)
Beispiel #10
0
class Order(EIP712Struct):
    sellToken = Address()
    buyToken = Address()
    receiver = Address()
    sellAmount = Uint(256)
    buyAmount = Uint(256)
    validTo = Uint(32)
    appData = Bytes(32)
    feeAmount = Uint(256)
    kind = String()  # `sell` or `buy`
    partiallyFillable = Boolean()
    sellTokenBalance = String()  # `erc20`, `external` or `internal`
    buyTokenBalance = String()  # `erc20` or `internal`
Beispiel #11
0
def test_from_solidity_type():
    assert from_solidity_type('address') == Address()
    assert from_solidity_type('bool') == Boolean()
    assert from_solidity_type('bytes') == Bytes()
    assert from_solidity_type('bytes32') == Bytes(32)
    assert from_solidity_type('int128') == Int(128)
    assert from_solidity_type('string') == String()
    assert from_solidity_type('uint256') == Uint(256)

    assert from_solidity_type('address[]') == Array(Address())
    assert from_solidity_type('address[10]') == Array(Address(), 10)
    assert from_solidity_type('bytes16[32]') == Array(Bytes(16), 32)

    # Sanity check that equivalency is working as expected
    assert from_solidity_type('bytes32') != Bytes(31)
    assert from_solidity_type('bytes16[32]') != Array(Bytes(16), 31)
    assert from_solidity_type('bytes16[32]') != Array(Bytes(), 32)
    assert from_solidity_type('bytes16[32]') != Array(Bytes(8), 32)
Beispiel #12
0
def test_encoded_types(contract):
    """Checks that the encoded types (and the respective hashes) of our structs match."""
    local_bar_sig = Bar.encode_type()
    remote_bar_sig = contract.functions.BarSig().call()
    assert local_bar_sig == remote_bar_sig

    local_foo_sig = Foo.encode_type()
    remote_foo_sig = contract.functions.FooSig().call()
    assert local_foo_sig == remote_foo_sig

    local_bar_hash = Bar.type_hash()
    remote_bar_hash = contract.functions.Bar_TYPEHASH().call()
    assert local_bar_hash == remote_bar_hash

    local_foo_hash = Foo.type_hash()
    remote_foo_hash = contract.functions.Foo_TYPEHASH().call()
    assert local_foo_hash == remote_foo_hash

    array_type = Array(Bytes(1))
    bytes_array = [os.urandom(1) for _ in range(5)]
    local_encoded_array = array_type.encode_value(bytes_array)
    remote_encoded_array = contract.functions.encodeBytes1Array(bytes_array).call()
    assert local_encoded_array == remote_encoded_array
class Output(EIP712Struct):
    outputType = Uint(256)
    outputGuard = Bytes(20)
    currency = Address()
    amount = Uint(256)
Beispiel #14
0
def test_length_str_typing():
    # Ensure that if length is given as a string, it's typecast to int
    assert Array(String(), '5').fixed_length == 5
    assert Bytes('10').length == 10
    assert Int('128').length == 128
    assert Uint('128').length == 128
 class TestStruct(EIP712Struct):
     byte_array = Array(Bytes(32), 4)
 class TestStruct(EIP712Struct):
     b = Bytes(4)
 class Foo(EIP712Struct):
     s = String()
     b = Bytes(32)
 class Foo(EIP712Struct):
     b = Bytes()
 class Bar(EIP712Struct):
     foo = Foo
     b = Bytes(1)