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))
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[][]'
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
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)
class Person(EIP712Struct): name = String() addr = Address() numbers = Array(Int(256)) moreNumbers = Array(Uint(256), 8)
class TestStruct(EIP712Struct): byte_array = Array(Bytes(32), 4)
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
def test_struct_arrays(): class Foo(EIP712Struct): s = String() assert Array(Foo).type_name == 'Foo[]' assert Array(Foo, 10).type_name == 'Foo[10]'