Skip to content

ABI encode and decode

TRON's ABI has a little difference from ETH's ABI.

  • Different Address format
  • Add a new type trcToken to denote TRC10 token ID.

TronPy provides ABI encoding and decoding methods via the tronpy.abi module.

>>> from tronpy.abi import trx_abi

# function arguments as a tuple
>>> raw = trx_abi.encode_single("(address,uint256)", ["TLfuw4tRywtxCusvTudbjf7PbcXjfe7qrw", 100_000_000])
>>> print(raw.hex())
'0000000000000000000000007564105e977516c53be337314c7e53838967bdac
 0000000000000000000000000000000000000000000000000000000005f5e100'

# function arguments as list
>>> raw = trx_abi.encode_abi(['address', 'uint256'], ["TLfuw4tRywtxCusvTudbjf7PbcXjfe7qrw", 100_000_000])

>>> trx_abi.decode_abi(['address', 'uint256'],
...   bytes.fromhex('0000000000000000000000007564105e977516c53be337314c7e53838967bdac' + \
...       '0000000000000000000000000000000000000000000000000000000005f5e100'))
('TLfuw4tRywtxCusvTudbjf7PbcXjfe7qrw', 100000000)

Key API reference

ABICodec


              flowchart TD
              tronpy.abi.ABICodec[ABICodec]

              

              click tronpy.abi.ABICodec href "" "tronpy.abi.ABICodec"
            

Methods:

encode_abi

encode_abi(types, args)
Source code in tronpy/abi.py
93
94
def encode_abi(self, types, args):
    return super().encode(types, args)

encode_single

encode_single(typ, arg)
Source code in tronpy/abi.py
84
85
86
def encode_single(self, typ, arg):
    encoder = self._registry.get_encoder(typ)
    return encoder(arg)

decode_abi

decode_abi(types, data, strict=True)
Source code in tronpy/abi.py
96
97
def decode_abi(self, types, data, strict=True):
    return super().decode(types, data, strict)

decode_single

decode_single(typ, data)
Source code in tronpy/abi.py
88
89
90
91
def decode_single(self, typ, data):
    decoder = self._registry.get_decoder(typ)
    stream = self.stream_class(data)
    return decoder(stream)