-
Notifications
You must be signed in to change notification settings - Fork 12
receipts: FetchReceipts #330
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7c402ea
receipts: FetchReceipts
attente 51bc4a8
digest -> opHash
attente 51dd4ea
receipts: MaxFilterBlockRange
attente 84e0683
receipts: Receipts.IsSuccess()
attente 0ca6e34
receipts: Receipts.IsSuccess(): recurse
attente ed8a4c2
comment
pkieltyka 80827d0
comment
pkieltyka 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 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,144 @@ | ||
| package receipts | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "math/big" | ||
|
|
||
| "github.com/0xsequence/ethkit/ethrpc" | ||
| "github.com/0xsequence/ethkit/go-ethereum" | ||
| "github.com/0xsequence/ethkit/go-ethereum/common" | ||
| "github.com/0xsequence/ethkit/go-ethereum/core/types" | ||
| sequence "github.com/0xsequence/go-sequence" | ||
| ) | ||
|
|
||
| const MaxFilterBlockRange = 10_000 | ||
|
|
||
| // FetchReceipts looks up the transaction that emitted Call* events for the given | ||
| // digest and returns all decoded Sequence receipts along with the native receipt. | ||
| // | ||
| // The `opHash` is also known as the "MetaTxnID" | ||
| // | ||
| // NOTE: toBlock can also be nil, in which case the latest block is used. | ||
| // | ||
| // Finally, please note that this method will not find meta-transactions if there is a | ||
| // native transaction which fails. In that case, the Call* events are not emitted and | ||
| // thus cannot be found. In such cases, you will need to look up the native transaction | ||
| // receipt directly. However, this is not to be confused with where a "Call" inside | ||
| // of the native transaction fails, but the native transaction itself succeeds which | ||
| // is more common and works fine. | ||
| func FetchReceipts(ctx context.Context, opHash common.Hash, provider *ethrpc.Provider, fromBlock, toBlock *big.Int) (Receipts, *types.Receipt, error) { | ||
| if provider == nil { | ||
| return Receipts{}, nil, fmt.Errorf("no provider") | ||
| } | ||
|
|
||
| fromBlock_ := fromBlock | ||
| if fromBlock_ == nil { | ||
| fromBlock_ = new(big.Int) | ||
| } | ||
|
|
||
| toBlock_ := toBlock | ||
| if toBlock_ == nil { | ||
| latest, err := provider.BlockNumber(ctx) | ||
| if err != nil { | ||
| return Receipts{}, nil, fmt.Errorf("unable to get latest block: %w", err) | ||
| } | ||
| toBlock_ = new(big.Int).SetUint64(latest) | ||
| } | ||
|
|
||
| if new(big.Int).Sub(toBlock_, fromBlock_).Cmp(big.NewInt(MaxFilterBlockRange)) > 0 { | ||
| return Receipts{}, nil, fmt.Errorf("block range %v to %v exceeds %v, reduce block range", fromBlock_, toBlock_, MaxFilterBlockRange) | ||
| } | ||
|
|
||
| query := ethereum.FilterQuery{ | ||
| FromBlock: fromBlock, | ||
| ToBlock: toBlock, | ||
| Topics: [][]common.Hash{ | ||
| {sequence.V3CallSucceeded, sequence.V3CallFailed, sequence.V3CallAborted, sequence.V3CallSkipped}, | ||
| {opHash}, | ||
| }, | ||
| } | ||
|
|
||
| logs, err := provider.FilterLogs(ctx, query) | ||
| if err != nil { | ||
| return Receipts{}, nil, fmt.Errorf("unable to filter logs: %w", err) | ||
| } | ||
| if len(logs) == 0 { | ||
| // Fallback for legacy events where the digest is not indexed. | ||
| query.Topics = [][]common.Hash{{sequence.V3CallSucceeded, sequence.V3CallFailed, sequence.V3CallAborted, sequence.V3CallSkipped}} | ||
| logs, err = provider.FilterLogs(ctx, query) | ||
| if err != nil { | ||
| return Receipts{}, nil, fmt.Errorf("unable to filter logs without digest topic: %w", err) | ||
| } | ||
| } | ||
|
|
||
| log, err := findDigestLog(logs, opHash) | ||
| if err != nil { | ||
| return Receipts{}, nil, err | ||
| } | ||
|
|
||
| receipt, err := provider.TransactionReceipt(ctx, log.TxHash) | ||
| if err != nil { | ||
| return Receipts{}, nil, fmt.Errorf("unable to get transaction receipt %v: %w", log.TxHash, err) | ||
| } | ||
|
|
||
| decoded, err := TransactionReceiptsForReceipt(ctx, receipt, provider) | ||
| if err != nil { | ||
| return Receipts{}, receipt, fmt.Errorf("unable to decode transaction receipt %v: %w", receipt.TxHash, err) | ||
| } | ||
|
|
||
| receipts := decoded.Find(opHash) | ||
| if receipts == nil { | ||
| return Receipts{}, receipt, fmt.Errorf("decoded receipts do not include digest %v", opHash) | ||
| } | ||
|
|
||
| return *receipts, receipt, nil | ||
| } | ||
|
|
||
| func findDigestLog(logs []types.Log, digest common.Hash) (*types.Log, error) { | ||
| var selected *types.Log | ||
|
|
||
| for i := range logs { | ||
| log := &logs[i] | ||
|
|
||
| if !matchesDigest(log, digest) { | ||
| continue | ||
| } | ||
|
|
||
| if selected == nil || isNewerLog(log, selected) { | ||
| selected = log | ||
| } | ||
| } | ||
|
|
||
| if selected == nil { | ||
| return nil, fmt.Errorf("no Call* events found for digest %v", digest) | ||
| } | ||
|
|
||
| return selected, nil | ||
| } | ||
|
|
||
| func matchesDigest(log *types.Log, digest common.Hash) bool { | ||
| if hash, _, err := sequence.V3DecodeCallSucceededEvent(log); err == nil && hash == digest { | ||
| return true | ||
| } | ||
| if hash, _, _, err := sequence.V3DecodeCallFailedEvent(log); err == nil && hash == digest { | ||
| return true | ||
| } | ||
| if hash, _, _, err := sequence.V3DecodeCallAbortedEvent(log); err == nil && hash == digest { | ||
| return true | ||
| } | ||
| if hash, _, err := sequence.V3DecodeCallSkippedEvent(log); err == nil && hash == digest { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func isNewerLog(a, b *types.Log) bool { | ||
| if a.BlockNumber != b.BlockNumber { | ||
| return a.BlockNumber > b.BlockNumber | ||
| } | ||
| if a.TxIndex != b.TxIndex { | ||
| return a.TxIndex > b.TxIndex | ||
| } | ||
| return a.Index > b.Index | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.