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

feat: reset inputs after submission #165

Merged
merged 7 commits into from
Aug 24, 2022
Merged

feat: reset inputs after submission #165

merged 7 commits into from
Aug 24, 2022

Conversation

zzmp
Copy link
Contributor

@zzmp zzmp commented Aug 24, 2022

Zeroes out the inputs and sets Field.INPUT as the independent field (ie TradeType.EXACT_INPUT) once a swap or wrap is submitted. This is in line with github.com/Uniswap/interface, as per @infredible.

Screen.Recording.2022-08-23.at.5.55.12.PM.mov

@vercel
Copy link

vercel bot commented Aug 24, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
widgets ✅ Ready (Inspect) Visit Preview Aug 24, 2022 at 6:17PM (UTC)

@zzmp zzmp requested review from vm and cmcewen and removed request for vm August 24, 2022 00:57
// TODO(zzmp): Surface errors from wrap.
console.log(e)
} finally {
setIsPending(false)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this not necessary anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

onSubmit will always return, so a finally is no longer needed.

invariant(trade.trade)
return { type: TransactionType.SWAP, response, tradeType: trade.trade.tradeType, trade: trade.trade }
})
setOpen(false)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should only be called in the case of a successful submission.

@zzmp zzmp requested a review from vm August 24, 2022 16:43
@vercel vercel bot temporarily deployed to Preview August 24, 2022 16:43 Inactive
setDisplayTxHash(transaction.hash)
const submitted = await onSubmit(async () => {
const response = await swapCallback?.()
if (!response) return

// Set the block containing the response to the oldest valid block to ensure that the
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't all these oldest references be newest?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we want the oldest valid block because that implies that nothing older than that is still valid.
I'm happy to change this to something less ambiguous in a separate PR, but would prefer not to change the naming in this PR.

} catch (e) {
// TODO(zzmp): Surface errors from swap.
console.log(e)
invariant(trade.trade)
Copy link
Contributor

Choose a reason for hiding this comment

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

is adding an invariant call here the correct move? iirc it throws if the argument is falsy, so this would be an uncaught error and trigger the error boundary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

onWrap will not be called if wrapType === undefined, but due to the complex type that's not evident to the type checker. We could replace this with a check, but if wrapCallback() returns a valid response, then wrapType should also be valid. I can annotate this with a comment.

Comment on lines 72 to 73
const [, setSwapAmount] = useSwapAmount(Field.INPUT)
const resetSwapAmount = useCallback(() => setSwapAmount(''), [setSwapAmount])
Copy link
Contributor

Choose a reason for hiding this comment

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

I was looking for where the the Field.INPUT setting was happening, then realized it's probably happening implicitly here. Just want to point out that this feels a little hard to parse. I don't know if someone reading this code w/o context would be able to recognize that is happening.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought that by semantically naming a derived setter I would make it easier to parse, so this is great feedback. I'll opt for the more explicit setSwapAmount('') inlined, and alias setSwapAmount to setInputAmount or similar.

Copy link
Contributor

@JFrankfurt JFrankfurt Aug 24, 2022

Choose a reason for hiding this comment

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

specifically I mean it's not immediately clear to me that these lines reset the widget to an exactInput swap if it was not initially in that state. The derived setter makes sense for the Amount for sure, just not the Field/tradeType

@@ -67,80 +67,82 @@ export default memo(function SwapButton({ disabled }: SwapButtonProps) {
// Close the review modal on chain change.
useEffect(() => setOpen(false), [chainId])

const addTransaction = useAddTransaction()
const addTransactionInfo = useAddTransaction()
Copy link
Contributor

Choose a reason for hiding this comment

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

should this hook name change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is adding a TransactionInfo type (Transaction type is different), so I'm going to propagate this change to all callsites.

const response = await wrapCallback()
if (!response) return

invariant(wrapType)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a subtly different assertion than the one being removed. WrapType.NONE (0) will throw here now. Is that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

WrapType.NONE was removed in #159 - this is now a type alias for TransactionType.WRAP | TransaactionType.UNWRAP | undefined.

I can (and should) still make this more explicit, so I'll update to wrapType !== undefined.

@vercel vercel bot temporarily deployed to Preview August 24, 2022 18:17 Inactive
@zzmp zzmp merged commit a665480 into main Aug 24, 2022
@zzmp zzmp deleted the reset-inputs branch August 24, 2022 19:09
@github-actions
Copy link

🎉 This PR is included in version 2.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants