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)
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)
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`
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)