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

Refactor: reactive recommended nonce #2050

Merged
merged 3 commits into from
May 31, 2023
Merged

Conversation

katspaugh
Copy link
Member

@katspaugh katspaugh commented May 26, 2023

What it solves

Resolves #2034

What this PR does

The "recommended" nonce was previously fetched and set only once, when generating a SafeTransaction.

If the queue updates (and thus the recommended nonce) while the user is still looking at the Sign/Execute modal, neither the Advanced Params form, not the SafeTransaction would pick up an updated recommended nonce.

With this PR, the recommended nonce is fetched every time the queue is updated.

It still doesn't guarantee that the user doesn't submit the modal with an outdated nonce, but it makes it much more robust.

@katspaugh katspaugh requested a review from schmanu May 26, 2023 15:19
@github-actions
Copy link

github-actions bot commented May 26, 2023

Branch preview

✅ Deploy successful!

https://recommended_nonce--walletweb.review-wallet-web.5afe.dev

@github-actions
Copy link

github-actions bot commented May 26, 2023

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

@katspaugh katspaugh force-pushed the recommended-nonce branch 2 times, most recently from 6644c32 to c8f882b Compare May 26, 2023 15:26
@katspaugh katspaugh marked this pull request as draft May 26, 2023 15:42
@katspaugh katspaugh removed the request for review from schmanu May 26, 2023 15:46
@katspaugh katspaugh force-pushed the recommended-nonce branch from c8f882b to c76ff83 Compare May 30, 2023 09:37
@katspaugh katspaugh requested a review from usame-algan May 30, 2023 11:03
@katspaugh katspaugh marked this pull request as ready for review May 30, 2023 11:03
@katspaugh katspaugh changed the base branch from dev to epic-tx-flow May 30, 2023 12:35
Reduce re-renders

Nonce -1
@katspaugh katspaugh force-pushed the recommended-nonce branch from a815695 to 35c095a Compare May 30, 2023 12:37
@katspaugh katspaugh force-pushed the recommended-nonce branch from b5c1e9b to 94cc648 Compare May 30, 2023 13:18
import { type SafeTransaction } from '@safe-global/safe-core-sdk-types'

const useWalletCanRelay = (tx: SafeTransaction | undefined) => {
const { safe } = useSafeInfo()
const wallet = useWallet()
const hasEnoughSignatures = tx && tx.signatures.size >= safe.threshold
Copy link
Member

Choose a reason for hiding this comment

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

I think we could even move this into the useAsync call since it already has a dependency on safe.threshold

Copy link
Member Author

Choose a reason for hiding this comment

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

Tx changes whenever the nonce changes, so that would be bad. That's the whole point why I moved it out here and in gas estimation.

Copy link
Member

Choose a reason for hiding this comment

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

Its probably more of a nit but the hook depends on hasEnoughSignatures which updates every time tx updates so it would still run when the nonce changes I assume. Just noticed that we have safe.threshold in that dependency array even though it is not being used accessed anymore.

Copy link
Member Author

@katspaugh katspaugh May 30, 2023

Choose a reason for hiding this comment

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

No, it will update only if the number of signatures change.
Good catch with the safe.threshold, removed.

src/components/tx/SignOrExecuteForm/index.tsx Outdated Show resolved Hide resolved
src/components/tx/SignOrExecuteForm/hooks.ts Show resolved Hide resolved
src/components/tx/SignOrExecuteForm/hooks.ts Show resolved Hide resolved
Comment on lines +146 to +149
| {
nonce: number
safeTxGas: number
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: We could extract this into a type that can be reused by getRecommendedTxParams as well.

[safeTxData?.to, safeTxData?.value, safeTxData?.data, safeTxData?.operation],
)

const [recommendedParams] = useAsync(() => {
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest also exporting the loading flag here since there is a small window when creating transactions now where it will show the execute option in any case until the nonce is estimated.

@katspaugh katspaugh force-pushed the recommended-nonce branch from 63319fa to 2337f6b Compare May 30, 2023 14:36
@katspaugh katspaugh force-pushed the recommended-nonce branch from 2337f6b to c4598b2 Compare May 30, 2023 14:37
<DialogContent>
{props.children}

<TxSimulation
Copy link
Member

Choose a reason for hiding this comment

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

This is now in the wrong order. I think it should be below the ExecuteCheckbox and Simulation block


return (
<form onSubmit={handleSubmit}>
Copy link
Member

Choose a reason for hiding this comment

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

By removing the form element here we lost some padding. We could add a div instead or adjust the css for DialogContent

Copy link
Member Author

Choose a reason for hiding this comment

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

Why does it matter? There'll be a new design for all of these things.

Comment on lines -62 to -71

// If the nonce is not provided, we get the recommended one
if (nonce === undefined) {
const recParams = (await getRecommendedTxParams(txParams)) || {}
txParams = { ...txParams, ...recParams }
} else {
// Otherwise, we use the provided one
txParams = { ...txParams, nonce }
}

Copy link
Member

Choose a reason for hiding this comment

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

I just realized that we don't estimate the nonce anymore for the initial tx that is passed to SignOrExecuteForm. Thats probably the reason why the execute checkbox is showing initially. I suggest we restore this block.

Copy link
Member

Choose a reason for hiding this comment

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

It also applies to the other create calls. We might need the withRecommendedNonce helper after all.

@katspaugh katspaugh merged commit 6387992 into epic-tx-flow May 31, 2023
@katspaugh katspaugh deleted the recommended-nonce branch May 31, 2023 10:38
@github-actions github-actions bot locked and limited conversation to collaborators May 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants