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 Transaction(EIP712Struct): txType = Uint(256) input0 = Input input1 = Input input2 = Input input3 = Input output0 = Output output1 = Output output2 = Output output3 = Output txData = Uint(256) metadata = Bytes(32)
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_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_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 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)
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)
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 Output(EIP712Struct): outputType = Uint(256) outputGuard = Bytes(20) currency = Address() amount = Uint(256)
class Input(EIP712Struct): blknum = Uint(256) txindex = Uint(256) oindex = Uint(256)
class Message(EIP712Struct): actionType = String() timestamp = Uint(256) authorizer = Identity
class Identity(EIP712Struct): userId = Uint(256) wallet = Address()
class Bar(EIP712Struct): bar_uint = Uint(256)
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 EIP712Domain(EIP712Struct): name = String() version = String() chainId = Uint(256) verifyingContract = Address()
class Output(EIP712Struct): owner = Address() currency = Address() amount = Uint(256)