-
Notifications
You must be signed in to change notification settings - Fork 16
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
automatically perform swap estimation on user input #1920
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'minifront': minor | ||
--- | ||
|
||
automatically perform swap estimation on user input |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,33 @@ import { SimulateSwapResult } from './simulate-swap-result'; | |
import { AllSlices } from '../../../state'; | ||
import { useStoreShallow } from '../../../utils/use-store-shallow'; | ||
import { EstimateButton } from './estimate-button'; | ||
import { useEffect } from 'react'; | ||
|
||
const simulateSwapSelector = (state: AllSlices) => ({ | ||
simulateSwap: state.swap.instantSwap.simulateSwap, | ||
resetSimulateSwap: state.swap.instantSwap.reset, | ||
disabled: | ||
state.swap.txInProgress || !state.swap.amount || state.swap.instantSwap.simulateSwapLoading, | ||
result: state.swap.instantSwap.simulateSwapResult, | ||
amount: state.swap.amount, | ||
}); | ||
|
||
// Automatically trigger swap simulation on user input | ||
export const SimulateSwap = ({ layoutId }: { layoutId: string }) => { | ||
const { simulateSwap, disabled, result } = useStoreShallow(simulateSwapSelector); | ||
const { simulateSwap, resetSimulateSwap, disabled, result, amount } = | ||
useStoreShallow(simulateSwapSelector); | ||
|
||
useEffect(() => { | ||
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. Not critical to fix, but in the future, we should always see if it's possible to use react-query first (versus useEffect()) for all async tasks. The complexities of loading/error/cache invalidation is abstracted away. 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. totally, forgot about using react-query here – that's an easy refactor |
||
if (!disabled && amount && parseFloat(amount) > 0) { | ||
// Prevents re-triggering the swap simulation if it's already computed | ||
if (!result) { | ||
void simulateSwap(); | ||
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. comment: we can optionally wrap this in a framer motion div for smoother rendering 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. Could be future work |
||
} | ||
} else if (result) { | ||
// Reset simulation | ||
resetSimulateSwap(); | ||
} | ||
}, [simulateSwap, resetSimulateSwap, disabled, result, amount]); | ||
|
||
return ( | ||
<Box | ||
|
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.
tightening pill to dynamically shrink and fit within the available space
before
after