def encode_abi(self, *args: Tuple) -> str: """Returns encoded ABI data to call the method with the given arguments. Args: *args: Contract method inputs Returns: Hexstring of encoded ABI data.""" data = format_input(self.abi, args) types = [i[1] for i in _params(self.abi["inputs"])] return self.signature + eth_abi.encode_abi(types, data).hex()
def encode_abi(self, *args): '''Returns encoded ABI data to call the method with the given arguments. Args: *args: Contract method inputs Returns: Hexstring of encoded ABI data.''' data = format_input(self.abi, args) types = [i['type'] for i in self.abi['inputs']] return self.signature + eth_abi.encode_abi(types, data).hex()
def test_bad_abi(): with pytest.raises(InvalidABI): format_input({'inputs': []}, []) with pytest.raises(InvalidABI): format_input({'name': "bad"}, []) with pytest.raises(InvalidABI): format_input([], [])
def encode_abi(self, *args): bytecode = self._parent.bytecode # find and replace unlinked library pointers in bytecode for marker in re.findall('_{1,}[^_]*_{1,}', bytecode): library = marker.strip('_') if not _contracts.list(library): raise UndeployedLibrary( f"Contract requires '{library}' library but it has not been deployed yet" ) address = _contracts.list(library)[-1].address[-40:] bytecode = bytecode.replace(marker, address) data = format_input(self.abi, args) types = [i['type'] for i in self.abi['inputs']] return bytecode + eth_abi.encode_abi(types, data).hex()
def encode_abi(self, *args: tuple) -> str: bytecode = self._parent.bytecode # find and replace unlinked library pointers in bytecode for marker in re.findall("_{1,}[^_]*_{1,}", bytecode): library = marker.strip("_") if not self._parent._project[library]: raise UndeployedLibrary( f"Contract requires '{library}' library, but it has not been deployed yet" ) address = self._parent._project[library][-1].address[-40:] bytecode = bytecode.replace(marker, address) data = format_input(self.abi, args) types = [i[1] for i in _params(self.abi["inputs"])] return bytecode + eth_abi.encode_abi(types, data).hex()
def test_non_sequence(): with pytest.raises(TypeError): format_input(abi, ["123", (1, ), ([1, 1], [2, 2]), "0xff"])
def test_wrong_length_nested_array(): with pytest.raises(ValueError): format_input(abi, [(1, 2, 3), (2, ), ([2, 2, 2], [2, 2, 2]), "0xff"])
def test_wrong_length_initial(): with pytest.raises(TypeError): format_input(abi, [(1, 2, 3), (1, ), ([1, 1], [2, 2])]) with pytest.raises(TypeError): format_input(abi, [(1, 2, 3), (1, ), ([1, 1], [2, 2]), "0xff", "0xff"])
def test_success(): assert format_input(abi, [(1, 2, 3), (1, ), ([1, 1], [2, 2]), "0xff"])
def test_empty(): with pytest.raises(TypeError): format_input({'inputs': [], 'name': "empty"}, [1])
def test_empty(): with pytest.raises(TypeError): format_input({"inputs": [], "name": "empty"}, [1])