-
Notifications
You must be signed in to change notification settings - Fork 49
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
Return jobs endpoint #876
Merged
Merged
Return jobs endpoint #876
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc06edc
Return back jobs endpoint.
9d9fef0
Add changes.
d9f82b7
add changes
9a722ab
Merge branch 'main' into return-jobs-endpoint
28c660c
Add changes.
cc47bc7
Fix single calls logic.
6b9fd67
remove traceback.
1ddf96b
Add changes.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -486,7 +486,7 @@ def get_all_entries_from_search( | |
limit=limit, | ||
offset=offset, | ||
) | ||
results.extend(existing_methods.results) # type: ignore | ||
results.extend(existing_methods.results) # type: ignore | ||
|
||
if len(results) != existing_methods.total_results: | ||
for offset in range(limit, existing_methods.total_results, limit): | ||
|
@@ -499,7 +499,7 @@ def get_all_entries_from_search( | |
limit=limit, | ||
offset=offset, | ||
) | ||
results.extend(existing_methods.results) # type: ignore | ||
results.extend(existing_methods.results) # type: ignore | ||
|
||
return results | ||
|
||
|
@@ -783,6 +783,62 @@ def query_parameter_hash(params: Dict[str, Any]) -> str: | |
return hash | ||
|
||
|
||
def parse_abi_to_name_tags(user_abi: List[Dict[str, Any]]): | ||
return [ | ||
f"abi_name:{method['name']}" | ||
for method in user_abi | ||
if method["type"] in ("event", "function") | ||
] | ||
|
||
|
||
def filter_tasks(entries, tag_filters): | ||
return [entry for entry in entries if any(tag in tag_filters for tag in entry.tags)] | ||
|
||
|
||
def fetch_and_filter_tasks( | ||
journal_id, address, subscription_type_id, token, user_abi, limit=100 | ||
) -> List[BugoutSearchResult]: | ||
""" | ||
Fetch tasks from journal and filter them by user abi | ||
""" | ||
entries = get_all_entries_from_search( | ||
journal_id=journal_id, | ||
search_query=f"tag:address:{address} tag:subscription_type:{subscription_type_id}", | ||
limit=limit, | ||
token=token, | ||
) | ||
|
||
user_loaded_abi_tags = parse_abi_to_name_tags(json.loads(user_abi)) | ||
|
||
moonworm_tasks = filter_tasks(entries, user_loaded_abi_tags) | ||
|
||
return moonworm_tasks | ||
|
||
|
||
def get_moonworm_tasks( | ||
subscription_type_id: str, | ||
address: str, | ||
user_abi: List[Dict[str, Any]], | ||
) -> List[BugoutSearchResult]: | ||
""" | ||
Get moonworm tasks from journal and filter them by user abi | ||
""" | ||
|
||
try: | ||
moonworm_tasks = fetch_and_filter_tasks( | ||
journal_id=MOONSTREAM_MOONWORM_TASKS_JOURNAL, | ||
address=address, | ||
subscription_type_id=subscription_type_id, | ||
token=MOONSTREAM_ADMIN_ACCESS_TOKEN, | ||
user_abi=user_abi, | ||
) | ||
except Exception as e: | ||
logger.error(f"Error get moonworm tasks: {str(e)}") | ||
MoonstreamHTTPException(status_code=500, internal_error=e) | ||
|
||
return moonworm_tasks | ||
|
||
|
||
def get_list_of_support_interfaces( | ||
blockchain_type: AvailableBlockchainType, | ||
address: str, | ||
|
@@ -793,97 +849,86 @@ def get_list_of_support_interfaces( | |
Returns list of interfaces supported by given address | ||
""" | ||
|
||
_, _, is_contract = check_if_smart_contract( | ||
blockchain_type=blockchain_type, address=address, user_token=user_token | ||
) | ||
|
||
if not is_contract: | ||
raise AddressNotSmartContractException(f"Address not are smart contract") | ||
|
||
web3_client = connect(blockchain_type, user_token=user_token) | ||
|
||
contract = web3_client.eth.contract( | ||
address=Web3.toChecksumAddress(address), | ||
abi=supportsInterface_abi, | ||
) | ||
|
||
calls = [] | ||
try: | ||
_, _, is_contract = check_if_smart_contract( | ||
blockchain_type=blockchain_type, address=address, user_token=user_token | ||
) | ||
|
||
list_of_interfaces = list(selectors.keys()) | ||
if not is_contract: | ||
raise AddressNotSmartContractException(f"Address not are smart contract") | ||
|
||
list_of_interfaces.sort() | ||
web3_client = connect(blockchain_type, user_token=user_token) | ||
|
||
for interaface in list_of_interfaces: | ||
calls.append( | ||
( | ||
contract.address, | ||
FunctionSignature(contract.get_function_by_name("supportsInterface")) | ||
.encode_data([bytes.fromhex(interaface)]) | ||
.hex(), | ||
) | ||
contract = web3_client.eth.contract( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would put this after line 868 |
||
address=Web3.toChecksumAddress(address), | ||
abi=supportsInterface_abi, | ||
) | ||
|
||
result = {} | ||
result = {} | ||
|
||
if blockchain_type in multicall_contracts: | ||
calls = [] | ||
if blockchain_type in multicall_contracts: | ||
calls = [] | ||
|
||
list_of_interfaces = list(selectors.keys()) | ||
list_of_interfaces = list(selectors.keys()) | ||
|
||
list_of_interfaces.sort() | ||
list_of_interfaces.sort() | ||
|
||
for interface in list_of_interfaces: | ||
calls.append( | ||
( | ||
contract.address, | ||
FunctionSignature( | ||
contract.get_function_by_name("supportsInterface") | ||
for interface in list_of_interfaces: | ||
calls.append( | ||
( | ||
contract.address, | ||
FunctionSignature( | ||
contract.get_function_by_name("supportsInterface") | ||
) | ||
.encode_data([bytes.fromhex(interface)]) | ||
.hex(), | ||
) | ||
.encode_data([bytes.fromhex(interface)]) | ||
.hex(), | ||
) | ||
) | ||
|
||
try: | ||
multicall_result = multicall( | ||
web3_client=web3_client, | ||
blockchain_type=blockchain_type, | ||
calls=calls, | ||
method=multicall_method, | ||
) | ||
except Exception as e: | ||
logger.error(f"Error while getting list of support interfaces: {e}") | ||
|
||
for i, selector in enumerate(list_of_interfaces): | ||
if multicall_result[i][0]: | ||
supported = FunctionSignature( | ||
contract.get_function_by_name("supportsInterface") | ||
).decode_data(multicall_result[i][1]) | ||
|
||
if supported[0]: | ||
result[selectors[selector]["name"]] = { # type: ignore | ||
"selector": selector, | ||
"abi": selectors[selector]["abi"], # type: ignore | ||
} | ||
try: | ||
multicall_result = multicall( | ||
web3_client=web3_client, | ||
blockchain_type=blockchain_type, | ||
calls=calls, | ||
method=multicall_method, | ||
) | ||
except Exception as e: | ||
logger.error(f"Error while getting list of support interfaces: {e}") | ||
|
||
else: | ||
general_interfaces = ["IERC165", "IERC721", "IERC1155", "IERC20"] | ||
for i, selector in enumerate(list_of_interfaces): | ||
if multicall_result[i][0]: | ||
supported = FunctionSignature( | ||
contract.get_function_by_name("supportsInterface") | ||
).decode_data(multicall_result[i][1]) | ||
|
||
basic_selectors = { | ||
interface["name"]: selector | ||
for selector, interface in selectors.items() | ||
if interface["name"] in general_interfaces | ||
} | ||
if supported[0]: | ||
result[selectors[selector]["name"]] = { # type: ignore | ||
"selector": selector, | ||
"abi": selectors[selector]["abi"], # type: ignore | ||
} | ||
|
||
for selector_name in basic_selectors: | ||
selector_result = contract.get_function_by_name("supportsInterface").call( | ||
bytes.fromhex(selectors[selector_name]) | ||
) | ||
if selector_result: | ||
result[selector_name] = { | ||
"selector": basic_selectors[selector_name], | ||
"abi": selectors[selectors[selector_name]]["abi"], | ||
} | ||
else: | ||
general_interfaces = ["IERC165", "IERC721", "IERC1155", "IERC20"] | ||
|
||
basic_selectors = { | ||
interface["name"]: selector | ||
for selector, interface in selectors.items() | ||
if interface["name"] in general_interfaces | ||
} | ||
|
||
for interface_name, selector in basic_selectors.items(): | ||
selector_result = contract.functions.supportsInterface( | ||
bytes.fromhex(selector) | ||
).call() # returns bool | ||
|
||
if selector_result: | ||
result[interface_name] = { | ||
"selector": basic_selectors[interface_name], | ||
"abi": selectors[basic_selectors[interface_name]]["abi"], | ||
} | ||
except Exception as err: | ||
logger.error(f"Error while getting list of support interfaces: {err}") | ||
MoonstreamHTTPException(status_code=500, internal_error=err) | ||
|
||
return result | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we need this func if we use it only in 1 place?