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

chore: Update SwapSettingsSlippageInput to use Config #1263

Merged
merged 6 commits into from
Sep 17, 2024

Conversation

cpcramer
Copy link
Contributor

@cpcramer cpcramer commented Sep 16, 2024

What changed? Why?
Add the Swap Config to the Context.

Update SwapSettingsSlippageInput updates:

  • Use the config defaultMaxSlippage rather than our hardcoded maxSlippage value.
  • Remove local slippage state.
  • Remove getMaxSlippage function. useCallback is not providing many performance benefits here. Likely it's even slower than accessing lifecycleStatus.statusData.maxSlippage directly.

Notes to reviewers

How has it been tested?
Export-1726524676893

Copy link

vercel bot commented Sep 16, 2024

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

Name Status Preview Comments Updated (UTC)
onchainkit-coverage ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 17, 2024 6:15pm
onchainkit-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 17, 2024 6:15pm
onchainkit-routes ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 17, 2024 6:15pm

src/swap/types.ts Outdated Show resolved Hide resolved

// Set initial slippage values to match previous selection or default,
// ensuring consistency when dropdown is reopened
const [slippage, setSlippage] = useState(getMaxSlippage());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not needed anymore

Comment on lines -16 to -18
const getMaxSlippage = useCallback(() => {
return lifecycleStatus.statusData.maxSlippage;
}, [lifecycleStatus.statusData]);
Copy link
Contributor Author

@cpcramer cpcramer Sep 16, 2024

Choose a reason for hiding this comment

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

Removed the getMaxSlippage function. useCallback is not providing many performance benefits here. Likely it's even slower than accessing lifecycleStatus.statusData.maxSlippage directly

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

@cpcramer cpcramer changed the title working chore: Update. SwapSettingsSlippageInput to use config values Sep 16, 2024
@cpcramer cpcramer changed the title chore: Update. SwapSettingsSlippageInput to use config values chore: Update. SwapSettingsSlippageInput to use default config slippage Sep 16, 2024
@cpcramer cpcramer changed the title chore: Update. SwapSettingsSlippageInput to use default config slippage chore: Update. SwapSettingsSlippageInput to config slippage Sep 16, 2024
@cpcramer cpcramer changed the title chore: Update. SwapSettingsSlippageInput to config slippage chore: Update SwapSettingsSlippageInput to use Config Sep 16, 2024
const [slippageSetting, setSlippageSetting] = useState(
getMaxSlippage() === DEFAULT_MAX_SLIPPAGE
lifecycleStatus.statusData.maxSlippage === defaultMaxSlippage
Copy link
Contributor

Choose a reason for hiding this comment

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

ok nice

// Handles user input for custom slippage
// Parses the input and updates slippage if valid
// Handles user input for custom slippage.
// Parses the input and updates slippage state.
const handleSlippageChange = useCallback(
Copy link
Contributor

Choose a reason for hiding this comment

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

Well done in updating the comments as well

@@ -119,7 +113,7 @@ export function SwapSettingsSlippageInput({
<input
id="slippage-input"
type="text"
value={slippage}
value={lifecycleStatus.statusData.maxSlippage}
onChange={(e) => handleSlippageChange(e.target.value)}
Copy link
Contributor

Choose a reason for hiding this comment

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

if you onChange={handleSlippageChange} and handle the event in your callback react won't have to create a new function here on every render

Copy link
Contributor Author

@cpcramer cpcramer Sep 17, 2024

Choose a reason for hiding this comment

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

Very cool! Curious if we prefer that optimization over (in my opinion) code simplicity

Before:

  onChange={(e) => handleSlippageChange(e.target.value)}

  const handleSlippageChange = useCallback(
    (newSlippage: string) => {
      const parsedSlippage = Number.parseFloat(newSlippage);
      const isValidNumber = !Number.isNaN(parsedSlippage);

      // Update slippage to parsed value if valid, otherwise set to 0
      updateSlippage(isValidNumber ? parsedSlippage : 0);
    },
    [updateSlippage],
  );

After:

onChange={handleSlippageChange}

const handleSlippageChange = useCallback(
  (e: React.ChangeEvent<HTMLInputElement>) => {
    const newSlippage = e.target.value;
    const parsedSlippage = Number.parseFloat(newSlippage);
    const isValidNumber = !Number.isNaN(parsedSlippage);

    // Update slippage to parsed value if valid, otherwise set to 0
    updateSlippage(isValidNumber ? parsedSlippage : 0);
  },
  [updateSlippage],
);

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it makes sense for a handleChange callback to accept a changeEvent

Copy link
Contributor

Choose a reason for hiding this comment

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

i agree with Adam here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

setSlippage(0);
return;
}
const parsedSlippage = Number.parseFloat(newSlippage);
Copy link
Contributor

Choose a reason for hiding this comment

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

parseFloat('2.') = 2 preventing you from entering decimals

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 think this is a fast follow update. I belieeve we will need to update getSwapQuote and buildSwapTransaction as well - this is where number <> string conversation happens again

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, parseFloat(2.2) = 2.2, its just typing the interim step will remove the dot before you type the next number

Copy link
Contributor

Choose a reason for hiding this comment

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

@cpcramer in your update to getSwapQuote and buildSwapTransaction - can you prevent the APIs from calling when amount=0? seems to be an edge case fired off from the component occasionally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep!

? SLIPPAGE_SETTINGS.AUTO
: SLIPPAGE_SETTINGS.CUSTOM,
);

const updateSlippage = useCallback(
(newSlippage: number) => {
setSlippage(newSlippage);
updateLifecycleStatus({
statusName: 'slippageChange',
statusData: {
Copy link
Contributor

Choose a reason for hiding this comment

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

only call slippageChange if newSlippage !== lifecycleStatus.statusData.maxSlippage

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

@cpcramer cpcramer merged commit ab69893 into main Sep 17, 2024
16 checks passed
@Zizzamia Zizzamia deleted the paul/clean-swap-settings-slippage-input branch September 18, 2024 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

5 participants