From b25fdf6086f744ef7e19c2c5f7f934ad7a6760dc Mon Sep 17 00:00:00 2001 From: Connor Barr Date: Tue, 13 Aug 2024 20:52:27 +0100 Subject: [PATCH] fix: user balance display on buy/sell --- .../complex/orders-history/cells/actions.tsx | 4 +- .../complex/orders-history/order-modal.tsx | 4 +- .../web/hooks/limit-orders/use-place-limit.ts | 46 +++++++++++-------- packages/web/localizations/en.json | 1 + 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/packages/web/components/complex/orders-history/cells/actions.tsx b/packages/web/components/complex/orders-history/cells/actions.tsx index 9cfde67df4..6e810a4a0a 100644 --- a/packages/web/components/complex/orders-history/cells/actions.tsx +++ b/packages/web/components/complex/orders-history/cells/actions.tsx @@ -110,7 +110,9 @@ const ClaimAndCloseButton = observer( > {claiming && } - {t("limitOrders.claimAndClose")} + {order.percentFilled > order.percentClaimed + ? t("limitOrders.claimAndClose") + : t("limitOrders.close")} ); diff --git a/packages/web/components/complex/orders-history/order-modal.tsx b/packages/web/components/complex/orders-history/order-modal.tsx index 556db07c8f..d077ef616a 100644 --- a/packages/web/components/complex/orders-history/order-modal.tsx +++ b/packages/web/components/complex/orders-history/order-modal.tsx @@ -172,7 +172,9 @@ const OrderDetails = observer( return order?.status === "open" ? "limitOrders.cancel" - : "limitOrders.claimAndClose"; + : order?.percentFilled > order?.percentClaimed + ? "limitOrders.claimAndClose" + : "limitOrders.close"; }, [order]); const orderAmount = useMemo(() => { diff --git a/packages/web/hooks/limit-orders/use-place-limit.ts b/packages/web/hooks/limit-orders/use-place-limit.ts index 6aaa32a4f6..11a9342bbb 100644 --- a/packages/web/hooks/limit-orders/use-place-limit.ts +++ b/packages/web/hooks/limit-orders/use-place-limit.ts @@ -378,33 +378,39 @@ export const usePlaceLimit = ({ placeLimitMsg, ]); - const { data: balances, isLoading: isBalancesLoading } = - api.local.balances.getUserBalances.useQuery( - { bech32Address: account?.address ?? "" }, + const { data, isLoading: isBalancesLoading } = + api.edge.assets.getUserAssets.useQuery( + { + userOsmoAddress: account?.address ?? "", + }, { enabled: !!account?.address, - select: (balances) => - balances.filter( - ({ denom }) => - denom === baseAsset?.coinMinimalDenom || - denom === quoteAsset?.coinMinimalDenom + select: ({ items }) => + items.filter( + ({ coinMinimalDenom }) => + coinMinimalDenom === baseAsset?.coinMinimalDenom || + coinMinimalDenom === quoteAsset?.coinMinimalDenom ), } ); - const quoteTokenBalance = useMemo(() => { - if (!balances) return; - - return balances.find(({ denom }) => denom === quoteAsset?.coinMinimalDenom) - ?.coin; - }, [balances, quoteAsset]); - - const baseTokenBalance = useMemo(() => { - if (!balances) return; + const baseTokenBalance = useMemo( + () => + data?.find( + ({ coinMinimalDenom }) => + coinMinimalDenom === baseAsset?.coinMinimalDenom + )?.amount, + [data, baseAsset] + ); + const quoteTokenBalance = useMemo( + () => + data?.find( + ({ coinMinimalDenom }) => + coinMinimalDenom === quoteAsset?.coinMinimalDenom + )?.amount, + [data, quoteAsset] + ); - return balances.find(({ denom }) => denom === baseAsset?.coinMinimalDenom) - ?.coin; - }, [balances, baseAsset]); const insufficientFunds = useMemo(() => { return orderDirection === "bid" ? (quoteTokenBalance?.toDec() ?? new Dec(0)).lt( diff --git a/packages/web/localizations/en.json b/packages/web/localizations/en.json index b67cf16348..c7969350a5 100644 --- a/packages/web/localizations/en.json +++ b/packages/web/localizations/en.json @@ -1249,6 +1249,7 @@ "filled": "Filled", "claimable": "Claimable", "claimAndClose": "Claim and close", + "close": "Close", "claim": "Claim", "accept": "Accept", "cancelled": "Cancelled",