Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update utils.py #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions python-sdk/src/forta_agent/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sys
import os
from jsonc_parser.parser import JsoncParser
import sha3
import sys

from enum import Enum
from jsonc_parser.parser import JsoncParser


def get_forta_config():
Expand Down Expand Up @@ -47,25 +49,33 @@ def create_transaction_event(dict):
return TransactionEvent(dict)


def assert_non_empty_string_in_dict(dict, key):
assert_key_in_dict(dict, key)
assert isinstance(dict[key], str) and len(
dict[key]) > 0, f'{key} must be non-empty string'
def is_valid_key(func: callable):

def wrap(dict: dict, key: str or int, *args, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the key type should always be str

if not key in dict:
raise KeyError(f'{key} is requierd!')
else:
return func(dict, key, *args, **kwargs)
return wrap


def assert_enum_value_in_dict(dict, key, enum):
assert_key_in_dict(dict, key)
assert isinstance(dict[key], enum), f'{key} must be valid enum value'
@is_valid_key
def is_valid_string(dict: dict, key: int or str):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets keep the function names the same (i.e. assert_enum_value_in_dict and assert_non_empty_string_in_dict) as they are used in other places as well

if not (isinstance(dict[key], str) and len(dict[key]) > 0):
raise KeyError(f'The key <{key}> must not be an empty str!')


def assert_key_in_dict(dict, key):
assert key in dict, f'{key} is required'
@is_valid_key
def is_valid_enum(dict: dict, key: int or str, enum):
if not isinstance(dict[key], enum):
raise KeyError(f'The key <{key}> must be valid enum value!')


def hex_to_int(strVal):
if not strVal or type(strVal) == int:
return strVal
return int(strVal, 16) if type(strVal) == str and strVal.startswith('0x') else int(strVal, 10)
def hex_to_int(value: str) -> int:
if not (isinstance(value, str) and len(value) > 0):
raise TypeError('The value must not be an empty str!')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we still want to allow None as a valid input (this would happen when creating a test block_event or transaction_event without having to specify every field)

else:
return int(value, 16) if value.startswith('0x') else int(value, 10)


def keccak256(val):
Expand Down