def _prepare_parameter(self, *args, **kwargs) -> "tronpy.tron.TransactionBuilder": """Prepare parameter.""" parameter = "" if args and kwargs: raise ValueError("do not mix positional arguments and keyword arguments") if len(self.inputs) == 0: if args or kwargs: raise TypeError("{} expected {} arguments".format(self.name, len(self.inputs))) elif args: if len(args) != len(self.inputs): raise TypeError("wrong number of arguments, require {} got {}".format(len(self.inputs), len(args))) parameter = trx_abi.encode_single(self.input_type, args).hex() elif kwargs: if len(kwargs) != len(self.inputs): raise TypeError("wrong number of arguments, require {} got {}".format(len(self.inputs), len(args))) args = [] for arg in self.inputs: try: args.append(kwargs[arg["name"]]) except KeyError: raise TypeError("missing argument '{}'".format(arg["name"])) parameter = trx_abi.encode_single(self.input_type, args).hex() else: raise TypeError("wrong number of arguments, require {}".format(len(self.inputs))) return parameter
def encode_parameter(self, *args, **kwargs) -> str: """Encode constructor parameters according to ABI.""" parameter = "" if args and kwargs: raise ValueError("do not mix positional arguments and keyword arguments") if len(self.inputs) == 0: if args or kwargs: raise TypeError("{} constructor requires {} arguments".format(self._contract.name, len(self.inputs))) elif args: if len(args) != len(self.inputs): raise TypeError("wrong number of arguments, require {} got {}".format(len(self.inputs), len(args))) parameter = trx_abi.encode_single(self.input_type, args).hex() elif kwargs: if len(kwargs) != len(self.inputs): raise TypeError("wrong number of arguments, require {} got {}".format(len(self.inputs), len(args))) args = [] for arg in self.inputs: try: args.append(kwargs[arg["name"]]) except KeyError: raise TypeError("missing argument '{}'".format(arg["name"])) parameter = trx_abi.encode_single(self.input_type, args).hex() return parameter
def __call__(self, *args, **kwargs) -> "tronpy.tron.TransactionBuilder": """Call the contract method.""" parameter = "" if args and kwargs: raise ValueError("do not mix positional arguments and keyword arguments") if len(self.inputs) == 0: if args or kwargs: raise TypeError("{} expected {} arguments".format(self.name, len(self.inputs))) elif args: if len(args) != len(self.inputs): raise TypeError("wrong number of arguments, require {} got {}".format(len(self.inputs), len(args))) parameter = trx_abi.encode_single(self.input_type, args).hex() elif kwargs: if len(kwargs) != len(self.inputs): raise TypeError("wrong number of arguments, require {} got {}".format(len(self.inputs), len(args))) args = [] for arg in self.inputs: try: args.append(kwargs[arg["name"]]) except KeyError: raise TypeError("missing argument '{}'".format(arg["name"])) parameter = trx_abi.encode_single(self.input_type, args).hex() else: raise TypeError("wrong number of arguments, require {}".format(len(self.inputs))) if self._abi.get("stateMutability", None).lower() in ["view", "pure"]: # const call, contract ret ret = self._client.trigger_const_smart_contract_function( self._owner_address, self._contract.contract_address, self.function_signature, parameter, ) return self.parse_output(ret) else: return self._client.trx._build_transaction( "TriggerSmartContract", { "owner_address": keys.to_hex_address(self._owner_address), "contract_address": keys.to_hex_address(self._contract.contract_address), "data": self.function_signature_hash + parameter, "call_token_value": self.call_token_value, "call_value": self.call_value, "token_id": self.call_token_id, }, method=self, )
def test_abi_encode(): assert (trx_abi.encode_single( "address", "TLfuw4tRywtxCusvTudbjf7PbcXjfe7qrw").hex() == '0000000000000000000000007564105e977516c53be337314c7e53838967bdac') assert trx_abi.encode_single("(address,uint256)", [ "TLfuw4tRywtxCusvTudbjf7PbcXjfe7qrw", 100_000_000 ]).hex() == ( '0000000000000000000000007564105e977516c53be337314c7e53838967bdac' + '0000000000000000000000000000000000000000000000000000000005f5e100')