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

automatically perform swap estimation on user input #1920

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rude-fireants-pump.md
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
Expand Up @@ -2,7 +2,7 @@ import { SwapExecution_Trace } from '@penumbra-zone/protobuf/penumbra/core/compo
import { Trace } from './trace';
import { Metadata, ValueView } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb';
import { ValueViewComponent } from '@penumbra-zone/ui-deprecated/components/ui/value';
import { ArrowDown, ArrowUp } from 'lucide-react';
import { ArrowRight } from 'lucide-react';

export const Traces = ({
traces,
Expand All @@ -29,17 +29,14 @@ export const Traces = ({

<div className='mt-4 flex overflow-auto [scrollbar-width:thin]'>
<div className='mx-2 w-min grow'>
<div className='-mx-2 -mb-8 flex justify-between'>
<div className='flex flex-col items-start gap-2'>
<ValueViewComponent view={input} size='sm' />
<ArrowDown size={17} className='relative z-10' />
</div>
<div className='flex flex-col items-end gap-2'>
<ValueViewComponent view={output} size='sm' />
<ArrowUp size={17} className='relative z-10' />
<div className='relative flex items-center justify-between'>
<ValueViewComponent view={input} size='sm' />
<div className='absolute left-1/2 flex -translate-x-1/2 items-center'>
<ArrowRight size={20} strokeWidth={2.5} className='text-white' />
</div>
<ValueViewComponent view={output} size='sm' />
</div>
<div className='inline-flex w-max min-w-full flex-col pb-10'>
<div className='mt-2 inline-flex w-max min-w-full flex-col pb-10'>
{traces.map((trace, index) => (
<div
key={index}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ export const Trace = ({
metadataByAssetId: Record<string, Metadata>;
}) => {
return (
<div className='flex w-full items-center justify-between gap-8'>
<div className='flex w-full justify-between gap-8'>
{trace.value.map((value, index) => (
<div key={index} className='flex shrink-0 flex-col gap-1'>
<div
key={index}
className='flex min-w-[60px] max-w-[150px] shrink-0 grow basis-auto flex-col items-center gap-1'
>
Comment on lines +30 to +35
Copy link
Contributor Author

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

Screenshot 2024-11-21 at 10 48 58 PM

after

Swaps are filed in descending order of price to get you the best possible trade for your tokens

<ValueViewComponent view={getValueView(metadataByAssetId, value)} size='sm' />

{index === 0 && <Price trace={trace} metadataByAssetId={metadataByAssetId} />}
Expand Down
19 changes: 18 additions & 1 deletion apps/minifront/src/components/swap/swap-form/simulate-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Copy link
Collaborator

@grod220 grod220 Nov 22, 2024

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@TalDerei TalDerei Nov 22, 2024

Choose a reason for hiding this comment

The 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();
Copy link
Contributor Author

@TalDerei TalDerei Nov 22, 2024

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down
Loading