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(minifront): #1211: remove swap restrictions #1337

Merged
merged 9 commits into from
Jun 21, 2024

Conversation

VanishMax
Copy link
Contributor

Closes #1211

What's done:

  • extend BalanceSelector to show not only assets with balances but all available assets
  • fix the issues with empty wallets not rendering a swap block correctly
  • reduce the height of BalanceSelecor and AssetSelector to 90dvh
  • auto focus the search inputs in BalanceSelecor and AssetSelector
  • change validations of the swap input to allow entering any possible values

The balance selector now looks like this:

image

@VanishMax VanishMax requested a review from a team June 19, 2024 12:28
@VanishMax VanishMax self-assigned this Jun 19, 2024
Copy link
Contributor

@jessepinho jessepinho left a comment

Choose a reason for hiding this comment

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

Great work! A lot of little notes here and there, but they're all relatively small things, so feel free to merge after addressing.

return false;
};

const mergeBalancesAndAssets = (balances: BalancesResponse[] = [], assets: Metadata[] = []) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (non-blocking): add a return type to make it clear what this function does

Suggested change
const mergeBalancesAndAssets = (balances: BalancesResponse[] = [], assets: Metadata[] = []) => {
const mergeBalancesAndAssets = (balances: BalancesResponse[] = [], assets: Metadata[] = []): (BalancesResponse | Metadata)[] => {

@@ -9,19 +9,101 @@ import { getAddressIndex } from '@penumbra-zone/getters/address-view';
import { getDisplayDenomFromView, getSymbolFromValueView } from '@penumbra-zone/getters/value-view';
import { Box } from '@repo/ui/components/ui/box';
import { motion } from 'framer-motion';
import { Metadata } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb';
import { filterMetadataBySearch } from './asset-selector';
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: importing a helper from inside another component is a little messy.

suggestion: how about putting it in a ./helpers.ts file?


const bySearch = (search: string) => (balancesResponse: BalancesResponse) =>
const isMetadata = (asset: BalancesResponse | Metadata): asset is Metadata => {
return Boolean('symbol' in asset && 'name' in asset && 'display' in asset);
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: I believe you can use asset.getType() to canonically get the message type.

e.g.,

Suggested change
return Boolean('symbol' in asset && 'name' in asset && 'display' in asset);
return asset.getType() === Metadata

This is provided by the bufbuild tooling, although I can't find docs for getType()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, didn't know about this! Got to modify the suggestion a little to return asset.getType().typeName === Metadata.typeName; to make it work

};

const isBalance = (asset: BalancesResponse | Metadata): asset is BalancesResponse => {
return 'balanceView' in asset && 'accountAddress' in asset;
Copy link
Contributor

Choose a reason for hiding this comment

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

same note here

onSelect: (value: BalancesResponse | Metadata) => void;
}

const BalanceItem = ({ asset, value, onSelect }: BalanceItemProps) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: could we move this into its own file?

This file is quite long. I'm generally opposed to having two components in the same file unless one is very small. But this one has its own helpers, etc., so the two are a bit tangled up.

I'd move BalanceSelector to shared/balance-selector/index.tsx, and BalanceItem to shared/balance-selector/balance-item.tsx

icon,
placeholder,
}: {
interface IconInputProps extends Omit<InputProps, 'onChange'> {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: we shouldn't be allowing all props to pass through to underlying components in our component design system

This is part of a bigger discussion*, but in short, we've had issues with our component system becoming quite messy because any props can be passed through. For example, this change to the props interface now allows children, className, etc. to be passed to <IconInput />. children doesn't make sense to pass to it, and className allows consumers to drastically change the appearance of <IconInput /> in ways they shouldn't be able to.

Instead, please just add autoFocus as a prop; we can keep adding more props as we need them, rather than allowing all props by default.

* I'm soon going to be converting #432 to a tracking issue so that we can gradually shift our design system components away from our current model.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the detailed explanation, it really helped me understand the idea! Changed this to only add autoFocus

@@ -31,6 +34,7 @@ import { divideAmounts } from '@penumbra-zone/types/amount';
import { bech32mAssetId } from '@penumbra-zone/bech32m/passet';
import { SwapSlice } from '.';
import { sendSimulateTradeRequest } from './helpers';
import { amountMoreThanBalance } from '../send';
Copy link
Contributor

Choose a reason for hiding this comment

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

Since amountMoreThanBalance is now used by multiple slices, please extract it to the ./helpers.ts file, so that slices aren't reaching into each other's code.

Comment on lines +178 to +180
if (!assetIn) throw new Error('`assetIn` is undefined');
if (!assetOut) throw new Error('`assetOut` is undefined');
if (!isValidAmount(amount, assetIn)) throw new Error('Invalid amount');
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch 👍🏻

@@ -15,6 +15,7 @@ import {
InstantSwapSlice,
createInstantSwapSlice,
instantSwapSubmitButtonDisabledSelector,
isValidAmount,
Copy link
Contributor

Choose a reason for hiding this comment

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

Please extract isValidAmount to ./helpers.ts, so that slices aren't reaching into each other's code.

Comment on lines 35 to 37
setAssetIn: (asset?: BalancesResponse) => void;
setAmount: (amount: string) => void;
setAssetOut: (metadata: Metadata) => void;
setAssetOut: (metadata?: Metadata) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these changes necessary? Won't they always be defined if these methods are being called?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see why they're needed from the swap loader.

I have a PR (#1316) to remove this loader, so I guess it's up to you whether to rebase on that once it's merged and then change this requirement, or just change this requirement now and tweak the loader which is going to be removed soon anyway... if that makes sense 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noticed the removal of swap loader. I added some changes to the zquery fetchers of swappableAssets and balancesResponses, so this code with optional parameters is irrelevant now

# Conflicts:
#	apps/minifront/src/components/shared/balance-selector.tsx
#	apps/minifront/src/components/swap/swap-form/token-swap-input.tsx
#	apps/minifront/src/components/swap/swap-loader.ts
#	apps/minifront/src/state/swap/index.ts
@VanishMax VanishMax merged commit 97b7231 into main Jun 21, 2024
6 checks passed
@VanishMax VanishMax deleted the feat/#1211-remove-swap-restrictions branch June 21, 2024 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove swap restriction input asset
2 participants