def test_encode_abi(self, multiple_of_eight): type = "uint{}".format(multiple_of_eight) assert tohex(encode_abi([type], [0])) == \ b'0000000000000000000000000000000000000000000000000000000000000000' assert tohex(encode_abi([type], [1])) == \ b'0000000000000000000000000000000000000000000000000000000000000001' assert tohex(encode_abi([type], [42])) == \ b'000000000000000000000000000000000000000000000000000000000000002a'
def test_enc(self, multiple_of_eight): t = UIntType("uint{}".format(multiple_of_eight)) assert tohex(t.enc(0)) == \ b'0000000000000000000000000000000000000000000000000000000000000000' assert tohex(t.enc(1)) == \ b'0000000000000000000000000000000000000000000000000000000000000001' assert tohex(t.enc(42)) == \ b'000000000000000000000000000000000000000000000000000000000000002a'
def test_enc(self): t = self.type("ufixed128x128") assert tohex(t.enc(1.1)) == \ b"00000000000000000000000000000001199999999999a0000000000000000000" t = self.type("ufixed64x64") assert tohex(t.enc(1.1)) == \ b"000000000000000000000000000000000000000000000001199999999999a000"
def test_enc(self, one_to_thirtytwo): t = self.type("bytes{}".format(one_to_thirtytwo)) assert tohex(t.enc(b'\x00')) == \ b'0000000000000000000000000000000000000000000000000000000000000000' assert tohex(t.enc(b'\x01')) == \ b'0100000000000000000000000000000000000000000000000000000000000000' assert tohex(t.enc(b'\xff')) == \ b'ff00000000000000000000000000000000000000000000000000000000000000' assert tohex(t.enc(b'A')) == \ b'4100000000000000000000000000000000000000000000000000000000000000'
def test_encode_abi(self, one_to_thirtytwo): t = "bytes{}".format(one_to_thirtytwo) assert tohex(encode_abi([t], [b'\x00'])) == \ b'0000000000000000000000000000000000000000000000000000000000000000' assert tohex(encode_abi([t], [b'\x01'])) == \ b'0100000000000000000000000000000000000000000000000000000000000000' assert tohex(encode_abi([t], [b'\xff'])) == \ b'ff00000000000000000000000000000000000000000000000000000000000000' assert tohex(encode_abi([t], [b'A'])) == \ b'4100000000000000000000000000000000000000000000000000000000000000'
def test_enc(self, multiple_of_eight): t = self.type("int{}".format(multiple_of_eight)) assert tohex(t.enc(0)) == \ b'0000000000000000000000000000000000000000000000000000000000000000' assert tohex(t.enc(1)) == \ b'0000000000000000000000000000000000000000000000000000000000000001' assert tohex(t.enc(42)) == \ b'000000000000000000000000000000000000000000000000000000000000002a' bytes = multiple_of_eight // 8 assert tohex(t.enc(-1)) == \ b'00' * (32 - bytes) + b'ff' * (bytes - 1) + b'ff' assert tohex(t.enc(-42)) == \ b'00' * (32 - bytes) + b'ff' * (bytes - 1) + b'd6'
def test_enc(self): t = self.type("fixed128x128") assert tohex(t.enc(-1.1)) == \ b"fffffffffffffffffffffffffffffffee6666666666660000000000000000000" t = self.type("fixed64x64") assert tohex(t.enc(-1.1)) == \ b"fffffffffffffffffffffffffffffffffffffffffffffffee666666666666000" t = self.type("fixed192x64") assert tohex(t.enc(-2**191)) == \ b"8000000000000000000000000000000000000000000000000000000000000000" t = self.type("fixed192x64") assert tohex(t.enc(2**191 - 1)) == \ b"7fffffffffffffffffffffffffffffffffffffffffffffff0000000000000000"
def test_dynamic_2_args(self): assert tohex(encode_abi( ["uint32[]"], [[6, 69]])) == (b'00000000000000000000000000000000' b'00000000000000000000000000000020' b'00000000000000000000000000000000' b'00000000000000000000000000000002' b'00000000000000000000000000000000' b'00000000000000000000000000000006' b'00000000000000000000000000000000' b'00000000000000000000000000000045')
def test_enc_bytes(self): t = self.type("string") assert tohex( t.enc('Hello World How Are you?' ' 123456789012345678901234567890')) == ( b"00000000000000000000000000000000" b"00000000000000000000000000000037" b"48656c6c6f20576f726c6420486f7720" b"41726520796f753f2031323334353637" b"38393031323334353637383930313233" b"34353637383930000000000000000000")
def test_enc_bytes_encode_abi(self): res = encode_abi( ["string"], ['Hello World How Are you? ' '123456789012345678901234567890']) assert tohex(res) == (b"00000000000000000000000000000000" b"00000000000000000000000000000020" b"00000000000000000000000000000000" b"00000000000000000000000000000037" b"48656c6c6f20576f726c6420486f7720" b"41726520796f753f2031323334353637" b"38393031323334353637383930313233" b"34353637383930000000000000000000")
def test_complex(self): assert tohex( encode_abi( ["uint256", "uint32[]", "bytes10", "bytes"], (0x123, [0x456, 0x789], b"1234567890", b"Hello, world!")) ) == ( b'0000000000000000000000000000000000000000000000000000000000000123' b'0000000000000000000000000000000000000000000000000000000000000080' b'3132333435363738393000000000000000000000000000000000000000000000' b'00000000000000000000000000000000000000000000000000000000000000e0' b'0000000000000000000000000000000000000000000000000000000000000002' b'0000000000000000000000000000000000000000000000000000000000000456' b'0000000000000000000000000000000000000000000000000000000000000789' b'000000000000000000000000000000000000000000000000000000000000000d' b'48656c6c6f2c20776f726c642100000000000000000000000000000000000000' )
def test_multiple_dynamics(self): res = tohex( encode_abi(["uint256[]", "uint256[]", "uint256[]"], [[1], [1, 2], [1, 2, 3]])) assert res == ( b'0000000000000000000000000000000000000000000000000000000000000060' b'00000000000000000000000000000000000000000000000000000000000000a0' b'0000000000000000000000000000000000000000000000000000000000000100' b'0000000000000000000000000000000000000000000000000000000000000001' b'0000000000000000000000000000000000000000000000000000000000000001' b'0000000000000000000000000000000000000000000000000000000000000002' b'0000000000000000000000000000000000000000000000000000000000000001' b'0000000000000000000000000000000000000000000000000000000000000002' b'0000000000000000000000000000000000000000000000000000000000000003' b'0000000000000000000000000000000000000000000000000000000000000001' b'0000000000000000000000000000000000000000000000000000000000000002' b'0000000000000000000000000000000000000000000000000000000000000003' )
def test_abi_example_sam(self): # https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#examples assert tohex(enc_method("sam(bytes,bool,uint256[])")) == b'a5643bf2'
def test_static_size_2(self): assert tohex(encode_abi( ["uint32[2]"], [[6, 69]])) == (b'00000000000000000000000000000000' b'00000000000000000000000000000006' b'00000000000000000000000000000000' b'00000000000000000000000000000045')
def test_abi_example_baz(self): # https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#examples assert tohex(enc_method("baz(uint32,bool)")) == b'cdcd77c0'
def test_uint32_69(self): assert tohex(UIntType("uint32").enc(69)) == ( b'00000000000000000000000000000000' b'00000000000000000000000000000045')
def test_simple(self): assert tohex(encode_abi(["uint32"], [6])) == (b'00000000000000000000000000000000' b'00000000000000000000000000000006')
def test_enc_false(self, falsy): t = self.type("bool") assert tohex(t.enc(falsy)) == \ b'0000000000000000000000000000000000000000000000000000000000000000'
def test_uint256_6(self): assert tohex(UIntType("uint256").enc(6)) == ( b'00000000000000000000000000000000' b'00000000000000000000000000000006')
def test_enc(self, one_to_thirtytwo): t = self.type("string{}".format(one_to_thirtytwo)) assert tohex(t.enc('A')) == \ b'4100000000000000000000000000000000000000000000000000000000000000'
def test_abi_example_bar(self): # https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#examples assert tohex(enc_method("bar(fixed128x128[2])")) == b'ab55044d'
def test_too_small(self, multiple_of_eight): t = self.type("int{}".format(multiple_of_eight)) with pytest.raises(ValueError): tohex(t.enc(-2**(multiple_of_eight - 1) - 1))
def test_encode_abi(self): assert tohex(encode_abi(["bool"], [True])) == \ b'0000000000000000000000000000000000000000000000000000000000000001' assert tohex(encode_abi(["bool"], [False])) == \ b'0000000000000000000000000000000000000000000000000000000000000000'
def test_enc_true(self, truthy): t = self.type("bool") assert tohex(t.enc(truthy)) == \ b'0000000000000000000000000000000000000000000000000000000000000001'
def test_encode_abi(self, one_to_thirtytwo): t = "string{}".format(one_to_thirtytwo) assert tohex(encode_abi([t], ['A'])) == \ b'4100000000000000000000000000000000000000000000000000000000000000'
def test_string_hello_world(self): assert tohex(StringType("string").enc("Hello, world")) == ( b"00000000000000000000000000000000" b"0000000000000000000000000000000c" b"48656c6c6f2c20776f726c6400000000" b"00000000000000000000000000000000")
def test_too_large(self, multiple_of_eight): t = self.type("uint{}".format(multiple_of_eight)) with pytest.raises(ValueError): tohex(t.enc(2**multiple_of_eight))
def test_short(self): assert tohex(enc_method("s()")) == b'86b714e2'