TLB

Abstract Class

Abstract class for TLB wrappers looks like this:

class TlbScheme(ABC):
    @abstractmethod
    def serialize(self, *args): ...

    @classmethod
    @abstractmethod
    def deserialize(cls, *args): ...

    def __repr__(self):
        return str(self.__dict__)

Inherted classes (i.e. wrappers for schemes) should have serialize and deserialize methods. Almost all wrappers used in pytoniq have __init__ method with scheme fields arguments, except some such as VmStack or VmStackList, which was made for usage convenience.

For schemes where could be Pruned branches (for e.g. ValueFlow in Block) if provided Slice is special it returns None.

LiteClient methods often return instances of TLB schemes, you can call their attributes and print them:

blk = await client.raw_get_block(client.last_mc_block)
print(blk.info)
print(blk.info.gen_utime)

>>> {'version': 0, 'not_master': 0, 'after_merge': 0, 'before_split': 0, 'after_split': 0, 'want_split': False, 'want_merge': True, 'key_block': False, 'vert_seqno_incr': 0, 'flags': 1, 'seqno': 31346266, 'vert_seqno': 1, 'shard': {'shard_pfx_bits': 0, 'workchain_id': -1, 'shard_prefix': 0}, 'gen_utime': 1690122131, 'start_lt': 39522305000000, 'end_lt': 39522305000004, 'gen_validator_list_hash_short': 2310445811, 'gen_catchain_seqno': 461098, 'min_ref_mc_seqno': 31346263, 'prev_key_block_seqno': 31345625, 'gen_software': {'version': 3, 'capabilities': 46}, 'master_ref': None, 'prev_ref': {'type_': 'prev_blk_info', 'prev': {'end_lt': 39522304000004, 'seqno': 31346265, 'root_hash': b'\xfa\xf7U\xe6\xc4\x88\\(\x16\xdc\x95\xa6Y\xc4\xac\xa4\xe1\xe1\xacj\xef\xdb\x97\xec\xf1\x13\x90)e\x95)\xde', 'file_hash': b'\x9b\x14F\xda\xfd{)\xab\x0eN\xb5h\xfe\xf0E\x80\x99\xb0\xc9:tY\x06\xda\x17l\x9d\xc6\xeaw4.'}}, 'prev_vert_ref': None}
>>> 1690122131

Custom schemes

Custom TLB schemes wrappers are used somewhere in the library. You can find them in pytoniq/tlb/custom. For example there are wrappers for wallets dara so you can get attributes directly from contract storage instead of get methods (which is not really recommended).

class WalletV3(BaseWallet):

    @property
    def public_key(self) -> bytes:
        """
        :return: public_key taken from contract data
        """
        return WalletV3Data.deserialize(self.state.data.begin_parse()).public_key

Last updated