This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 363
Speed up safe creation #2473
Merged
Merged
Speed up safe creation #2473
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5416595
wip: txMonitor function
fernandomg d6e4560
Merge remote-tracking branch 'origin/dev' into feature/2362-speedup-s…
juampibermani d5bda12
Wait for speed up tx confirmation
juampibermani f281f1e
Error and indefinitely pending tx handling
juampibermani d07f61f
Delete unnecessary polling
juampibermani 99fac3c
Console logs for debugging
juampibermani 808a19a
End txMonitor if first tx is mined
juampibermani 391d2f9
Added an await
juampibermani 0b824f0
Merge branch 'dev' into feature/2362-speedup-safe-creation
11e5689
Add more delay to txMonitor to check each 5 seconds
b194f53
Timeout after 1 hour of pending txs
juampibermani 665dafe
Refactor tx monitor (#2511)
katspaugh 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { TransactionReceipt } from 'web3-core' | ||
|
||
import { web3ReadOnly } from 'src/logic/wallets/getWeb3' | ||
import { sameAddress } from 'src/logic/wallets/ethAddresses' | ||
import { sameString } from 'src/utils/strings' | ||
|
||
type TxMonitorProps = { | ||
sender: string | ||
hash: string | ||
data: string | ||
nonce?: number | ||
gasPrice?: string | ||
} | ||
|
||
type TxMonitorOptions = { | ||
delay?: number | ||
} | ||
|
||
/** | ||
* Recursively inspects a pending tx. Until it's found, and returns the mined tx receipt | ||
* | ||
* @param {object} txParams | ||
* @param {string} txParams.sender | ||
* @param {string} txParams.hash | ||
* @param {string} txParams.data | ||
* @param {number | undefined} txParams.nonce | ||
* @param {string | undefined} txParams.gasPrice | ||
* @param {function(txReceipt: TransactionReceipt): void} cb - called with the tx receipt as argument when tx is mined | ||
* @param {object} options | ||
* @param {number} options.delay | ||
*/ | ||
export const txMonitor = async ( | ||
{ sender, hash, data, nonce, gasPrice }: TxMonitorProps, | ||
cb: (txReceipt: TransactionReceipt) => void, | ||
options?: TxMonitorOptions, | ||
): Promise<void> => { | ||
setTimeout(async () => { | ||
if (nonce === undefined || gasPrice === undefined) { | ||
// this block is accessed only the first time, to lookup the tx nonce and gasPrice | ||
// find the nonce for the current tx | ||
const transaction = await web3ReadOnly.eth.getTransaction(hash) | ||
|
||
if (transaction !== null) { | ||
// transaction found | ||
return txMonitor({ sender, hash, data, nonce: transaction.nonce, gasPrice: transaction.gasPrice }, cb, options) | ||
} else { | ||
return txMonitor({ sender, hash, data }, cb, options) | ||
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. It looks like
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 solved it in a simpler way, you can see the change in the last commit 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. That was smart! 👏 |
||
} | ||
} | ||
|
||
web3ReadOnly.eth.getTransactionReceipt(hash) | ||
|
||
const latestBlock = await web3ReadOnly.eth.getBlock('latest', true) | ||
|
||
const replacementTransaction = latestBlock.transactions.find((transaction) => { | ||
// TODO: use gasPrice, timestamp or another better way to differentiate | ||
return ( | ||
sameAddress(transaction.from, sender) && | ||
transaction.nonce === nonce && | ||
!sameString(transaction.hash, hash) && | ||
// if `data` differs, then it's a replacement tx, not a speedup | ||
sameString(transaction.input, data) | ||
) | ||
}) | ||
|
||
if (replacementTransaction) { | ||
const transactionReceipt = await web3ReadOnly.eth.getTransactionReceipt(replacementTransaction.hash) | ||
if (transactionReceipt === null) { | ||
// pending transaction | ||
return txMonitor( | ||
{ | ||
sender, | ||
hash: replacementTransaction.hash, | ||
data: replacementTransaction.input, | ||
nonce, | ||
gasPrice: replacementTransaction.gasPrice, | ||
}, | ||
cb, | ||
options, | ||
) | ||
} | ||
cb(transactionReceipt) | ||
return | ||
} | ||
|
||
return txMonitor({ sender, hash, data, nonce, gasPrice }, cb, options) | ||
}, options?.delay ?? 500) | ||
} |
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
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.
There are several places like this in txMonitor, where we don't catch a potential async exception.
I think txMonitor should return a promise instead of taking a callback. Then we can try-catch each
await
call and reject the returned promise in the catch.