-
Notifications
You must be signed in to change notification settings - Fork 7
Added output (returndata) decoding APIs to CallDecoder #59
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
Open
Pratham6392
wants to merge
4
commits into
enviodev:main
Choose a base branch
from
Pratham6392:feat/call-decoder-decode-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−7
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import os | ||
| import asyncio | ||
| import hypersync | ||
| from dotenv import load_dotenv | ||
| from hypersync import TraceField | ||
|
|
||
| load_dotenv() | ||
|
|
||
| ADDR = "1e037f97d730Cc881e77F01E409D828b0bb14de0" | ||
| USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" | ||
| BALANCE_OF_SIGHASH = "0x70a08231" | ||
| SIG_IN = "balanceOf(address)" | ||
| SIG_OUT = "balanceOf(address)(uint256)" | ||
|
|
||
|
|
||
| def make_client(): | ||
| token = os.getenv("ENVIO_API_TOKEN") | ||
| if not token: | ||
| raise ValueError("ENVIO_API_TOKEN is required") | ||
| return hypersync.HypersyncClient( | ||
| hypersync.ClientConfig( | ||
| url="https://eth.hypersync.xyz", | ||
| api_token=token, | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| async def main(): | ||
| client = make_client() | ||
| height = await client.get_height() | ||
|
|
||
| # recent window; increase if you get no traces | ||
| from_block = max(0, height - 500_000) | ||
|
|
||
| query = hypersync.Query( | ||
| from_block=from_block, | ||
| to_block=height, | ||
| traces=[ | ||
| hypersync.TraceSelection( | ||
| to=[USDC], | ||
| sighash=[BALANCE_OF_SIGHASH], | ||
| ) | ||
| ], | ||
| field_selection=hypersync.FieldSelection( | ||
| trace=[ | ||
| TraceField.INPUT, | ||
| TraceField.OUTPUT, | ||
| TraceField.TO, | ||
| TraceField.SIGHASH, | ||
| TraceField.CALL_TYPE, | ||
| TraceField.TRANSACTION_HASH, | ||
| ] | ||
| ), | ||
| max_num_traces=50, | ||
| ) | ||
|
|
||
| res = await client.get(query) | ||
| traces = [t for t in res.data.traces if t.input and t.output] | ||
|
|
||
| print(f"got traces={len(res.data.traces)} usable={len(traces)}") | ||
| if not traces: | ||
| print("No usable traces with both input+output. Increase from_block window.") | ||
| return | ||
|
|
||
| decoder = hypersync.CallDecoder([SIG_IN]) | ||
|
|
||
| # 1) decode input (should contain the address argument) | ||
| decoded_inputs = decoder.decode_traces_input_sync(traces) | ||
|
|
||
| # filter to traces where the decoded address equals your ADDR | ||
| target = "0x" + ADDR.lower() | ||
| filtered_traces = [] | ||
| for tr, din in zip(traces, decoded_inputs): | ||
| if not din: | ||
| continue | ||
| # din[0].val should be the address argument | ||
| if isinstance(din[0].val, str) and din[0].val.lower() == target: | ||
| filtered_traces.append(tr) | ||
|
|
||
| print(f"traces calling balanceOf({target}) = {len(filtered_traces)}") | ||
| if not filtered_traces: | ||
| print("No traces found for that specific holder address in this window.") | ||
| print("Still: decode_traces_output_sync can be tested without this filter.") | ||
| filtered_traces = traces[:5] | ||
|
|
||
| # 2) decode output (uint256 balance) | ||
| sigs = [SIG_OUT] * len(filtered_traces) | ||
| decoded_outputs = decoder.decode_traces_output_sync(filtered_traces, sigs) | ||
|
|
||
| for i, (tr, dout) in enumerate(zip(filtered_traces[:5], decoded_outputs[:5])): | ||
| print(f"\n[{i}] tx={tr.transaction_hash} call_type={tr.call_type} to={tr.to} sighash={tr.sighash}") | ||
| print(f" output={tr.output}") | ||
| if dout is None: | ||
| print(" decoded_output=None (missing output or signature mismatch)") | ||
| else: | ||
| print(f" decoded_output_uint256={dout[0].val}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
This file contains hidden or 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
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.