Skip to content

Commit

Permalink
Add skip to result in ABI, small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvorogme committed Oct 23, 2024
1 parent de1a45e commit b847f9c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def finalize_options(self):

setup(
name="tonpy" if not IS_DEV else "tonpy-dev",
version="0.0.0.1.2c0" if not IS_DEV else "0.0.0.5.2b1",
version="0.0.0.1.2c0" if not IS_DEV else "0.0.0.5.2c1",
author="Disintar LLP",
author_email="andrey@head-labs.com",
description="Types / API for TON blockchain",
Expand Down
35 changes: 27 additions & 8 deletions src/tonpy/abi/getter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from traceback import format_exc

from tonpy import StackEntry, add_tlb
from tonpy import StackEntry, add_tlb, Address
from tonpy.tvm import TVM
from loguru import logger

Expand Down Expand Up @@ -35,12 +35,17 @@ def __init__(self, instance):
self.dton_type = self.labels.get('dton_type', self.instance['type'])
self.type = self.instance['type']
self.required = self.instance['required']
self.parse_special = None
self.skip_parse = self.labels.get('skipParse', False)

if self.dton_type == 'Int':
self.dton_type = 'UInt256'
elif self.dton_type in ['Slice', 'Cell', 'Continuation', 'Builder']:
if self.labels.get('address'):
if self.labels.get('address', False):
self.dton_type = 'Address'
elif self.labels.get('string', False):
self.dton_type = 'String'
self.parse_special = 'String'
else:
self.dton_type = 'String'
elif self.dton_type == 'Null':
Expand All @@ -60,7 +65,7 @@ def __init__(self, instance):
self.dton_parse_prefix = self.labels['dton_parse_prefix']

def get_columns(self):
if self.labels.get('skipLive', False):
if self.skip_parse:
return {}
elif self.required is not None:
return {}
Expand Down Expand Up @@ -101,20 +106,36 @@ def get_columns(self):

return {f'{self.dton_parse_prefix}{self.name}': self.dton_type}

def parse_stack_item(self, stack_entry: StackEntry, tlb_sources) -> dict:
def parse_stack_item(self, stack_entry: StackEntry, tlb_sources, force_all: bool = False) -> dict:
if self.skip_parse and not force_all:
return {}

if self.dton_type == 'Address':
if stack_entry.get_type() is StackEntry.Type.t_cell:
address = stack_entry.as_cell().begin_parse().load_address()
elif stack_entry.get_type() is StackEntry.Type.t_slice:
address = stack_entry.as_cell_slice().load_address()
elif stack_entry.get_type() is StackEntry.Type.t_builder:
address = stack_entry.as_cell_builder().end_cell().begin_parse().load_address()
else:
address = Address()

return {
f'{self.dton_parse_prefix}{self.name}_workchain': address.workchain,
f'{self.dton_parse_prefix}{self.name}_address': address.address,
f'{self.dton_parse_prefix}{self.name}_type': address.type,
}
elif self.parse_special == 'String':
if stack_entry.get_type() is StackEntry.Type.t_cell:
tmp = stack_entry.as_cell().begin_parse().load_string()
elif stack_entry.get_type() is StackEntry.Type.t_slice:
tmp = stack_entry.as_cell_slice().load_string()
elif stack_entry.get_type() is StackEntry.Type.t_builder:
tmp = stack_entry.as_cell_builder().end_cell().begin_parse().load_string()
else:
tmp = None

return {f"{self.dton_parse_prefix}{self.name}": tmp}
elif self.type in ['Slice', 'Cell', 'Continuation', 'Builder']:
if self.instance.get('tlb', None):
tlb = self.instance.get('tlb')
Expand Down Expand Up @@ -171,8 +192,6 @@ def parse_stack_item(self, stack_entry: StackEntry, tlb_sources) -> dict:
tmp[f'{self.dton_parse_prefix}{self.name}_{name}'] = old

return tmp
else:
pass

return {f"{self.dton_parse_prefix}{self.name}": stack_entry.get().to_boc()}
elif self.dton_type in ['UInt8', 'UInt16', 'UInt32', 'UInt64', 'UInt128', 'UInt256']:
Expand Down Expand Up @@ -228,7 +247,7 @@ def get_columns(self):

return tmp

def parse_getters(self, tvm: TVM, tlb_sources) -> dict:
def parse_getters(self, tvm: TVM, tlb_sources, force_all: bool = False) -> dict:
if self.method_args and len(self.method_args) > 0:
return {}

Expand All @@ -250,7 +269,7 @@ def parse_getters(self, tvm: TVM, tlb_sources) -> dict:
continue

try:
tmp.update(getter.parse_stack_item(stack_entry, tlb_sources))
tmp.update(getter.parse_stack_item(stack_entry, tlb_sources, force_all))
except Exception as e:
logger.error(f"Can't parse {getter}: {e}, {format_exc()}")

Expand Down
13 changes: 12 additions & 1 deletion src/tonpy/abi/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def get_parsers(self, code_hash: str, getters: List[int]) -> set[ABIInterfaceIns
else:
logger.warning("Code hash not found in ABI, provide getters for parse methods")

unique_by_versions = set()
versioned = {}

return parsers

def parse_getters(self, tvm: TVM, getters: List[int] = None) -> dict:
Expand Down Expand Up @@ -98,6 +101,14 @@ def parse_getter_lazy(self, code_hash, get_tvm: Callable, getters: List[int] = N

for parser in parsers:
result['abi_interfaces'].append(parser.name)
result.update(parser.parse_getters(tvm, self.tlb_sources))
for key, value in parser.parse_getters(tvm, self.tlb_sources).items():
if key not in result:
result[key] = value
else:
if result[key] is not None:
result[key] = value
else:
logger.warning(f"Got multiple not null answers for getter {key} in {parser.name}")
result.update()

return result
2 changes: 1 addition & 1 deletion src/tonpy/abi/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def parse_getters(self, tvm: TVM, tlb_sources):
result[f"{self.dton_parse_prefix}{i}"] = tmp[i]

except Exception as e:
logger.info(f"Can't parse {self.name}, (getter: {getter.method_name}): {e}")
logger.warning(f"Can't parse {self.name}, (getter: {getter.method_name}): {e}")

return result

0 comments on commit b847f9c

Please sign in to comment.