Skip to content

Commit

Permalink
fix(monorepo): #1591: refetch balancesResponses after successful swap…
Browse files Browse the repository at this point in the history
… and send (#1642)

* fix(monorepo): #1591: refetch balancesResponses after successfull swap and send

* chore: changeset
  • Loading branch information
VanishMax authored Aug 6, 2024
1 parent b524a03 commit 2c0c167
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-swans-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'minifront': patch
---

Refetch BalancesResponse query after successful swap and send
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { motion } from 'framer-motion';
import { Metadata } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb.js';
import { emptyBalanceResponse } from '../../../utils/empty-balance-response';
import { bySearch } from './search-filters';
import { BalanceOrMetadata, isMetadata, mergeBalancesAndAssets } from './helpers';
import {
BalanceOrMetadata,
isMetadata,
mergeBalancesAndAssets,
useSyncSelectedBalance,
} from './helpers';
import { BalanceItem } from './balance-item';
import { cn } from '@repo/ui/lib/utils';
import { LoadingIndicator } from './loading-indicator';
Expand Down Expand Up @@ -44,6 +49,7 @@ export default function BalanceSelector({

const allAssets = mergeBalancesAndAssets(balances, assets);
const filteredBalances = search ? allAssets.filter(bySearch(search)) : allAssets;
useSyncSelectedBalance({ balances, value, onChange });

const onSelect = (asset: BalanceOrMetadata) => {
if (!isMetadata(asset)) {
Expand Down
35 changes: 34 additions & 1 deletion apps/minifront/src/components/shared/selectors/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { BalancesResponse } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb.js';
import { Metadata } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb.js';
import { getMetadataFromBalancesResponseOptional } from '@penumbra-zone/getters/balances-response';
import {
getAddressIndex,
getMetadataFromBalancesResponseOptional,
} from '@penumbra-zone/getters/balances-response';
import { useEffect } from 'react';

export type BalanceOrMetadata = BalancesResponse | Metadata;

Expand All @@ -24,3 +28,32 @@ export const mergeBalancesAndAssets = (
});
return [...balances, ...filteredAssets];
};

// When `balances` change, emit new value of the selected balance: match by address index and asset metadata
export const useSyncSelectedBalance = ({
balances,
onChange,
value,
}: {
balances?: BalancesResponse[];
value?: BalancesResponse;
onChange: (selection: BalancesResponse) => void;
}) => {
useEffect(() => {
if (value) {
const matchedValue = balances?.find(balance => {
return (
getAddressIndex.optional()(balance)?.equals(getAddressIndex.optional()(value)) &&
getMetadataFromBalancesResponseOptional(balance)?.equals(
getMetadataFromBalancesResponseOptional(value),
)
);
});
if (matchedValue && !matchedValue.equals(value)) {
onChange(matchedValue);
}
}
// we only want to run this on new balances from ZQuery, so don't include `value` as dependency
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [balances]);
};
1 change: 1 addition & 0 deletions apps/minifront/src/state/send/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const createSendSlice = (): SliceCreator<SendSlice> => (set, get) => {
set(state => {
state.send.amount = '';
});
get().shared.balancesResponses.revalidate();
} finally {
set(state => {
state.send.txInProgress = false;
Expand Down
3 changes: 3 additions & 0 deletions apps/minifront/src/state/swap/dutch-auction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export const createDutchAuctionSlice = (): SliceCreator<DutchAuctionSlice> => (s

get().swap.setAmount('');
get().swap.dutchAuction.auctionInfos.revalidate();
get().shared.balancesResponses.revalidate();
} finally {
set(state => {
state.swap.dutchAuction.txInProgress = false;
Expand All @@ -186,6 +187,7 @@ export const createDutchAuctionSlice = (): SliceCreator<DutchAuctionSlice> => (s
});
await planBuildBroadcast('dutchAuctionEnd', req);
get().swap.dutchAuction.auctionInfos.revalidate();
get().shared.balancesResponses.revalidate();
},

withdraw: async (auctionId, currentSeqNum, addressIndex) => {
Expand All @@ -195,6 +197,7 @@ export const createDutchAuctionSlice = (): SliceCreator<DutchAuctionSlice> => (s
});
await planBuildBroadcast('dutchAuctionWithdraw', req);
get().swap.dutchAuction.auctionInfos.revalidate();
get().shared.balancesResponses.revalidate();
},

reset: () =>
Expand Down
1 change: 1 addition & 0 deletions apps/minifront/src/state/swap/instant-swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const createInstantSwapSlice = (): SliceCreator<InstantSwapSlice> => (set
set(state => {
state.swap.amount = '';
});
get().shared.balancesResponses.revalidate();
} finally {
set(state => {
state.swap.instantSwap.txInProgress = false;
Expand Down

0 comments on commit 2c0c167

Please sign in to comment.