From ec400151551cb2b85f584134653f87bb79050134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Est=C3=A1cio=20=7C=20stacio=2Eeth?= Date: Tue, 24 May 2022 21:11:20 -0300 Subject: [PATCH] fix: price per token calculation (#209) * fix: preice token calculation * chore: remove unused imports --- .../app/src/pages/SwapPage/PricePerToken.tsx | 66 ++++++++++++------- packages/app/src/pages/SwapPage/index.tsx | 6 +- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/packages/app/src/pages/SwapPage/PricePerToken.tsx b/packages/app/src/pages/SwapPage/PricePerToken.tsx index 4fb34eeb..92c5f53d 100644 --- a/packages/app/src/pages/SwapPage/PricePerToken.tsx +++ b/packages/app/src/pages/SwapPage/PricePerToken.tsx @@ -1,12 +1,14 @@ import { toNumber } from "fuels"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { AiOutlineSwap } from "react-icons/ai"; import { useValueIsTyping } from "./jotai"; +import type { SwapState } from "./types"; import { ActiveInput } from "./types"; import { Button } from "~/components/Button"; import { ONE_ASSET } from "~/config"; +import { ZERO } from "~/lib/constants"; import { divideBigInt } from "~/lib/utils"; const style = { @@ -14,55 +16,71 @@ const style = { }; function getPricePerToken( - direction?: ActiveInput, fromAmount?: bigint | null, toAmount?: bigint | null ) { if (!toAmount || !fromAmount) return ""; - const ratio = - direction === ActiveInput.from - ? divideBigInt(fromAmount, toAmount) - : divideBigInt(toAmount, fromAmount); + const ratio = divideBigInt(toAmount, fromAmount); const price = ratio * toNumber(ONE_ASSET); return (price / toNumber(ONE_ASSET)).toFixed(6); } type PricePerTokenProps = { - fromCoin?: string; - fromAmount?: bigint | null; - toCoin?: string; - toAmount?: bigint | null; + swapState?: SwapState | null; + previewAmount?: bigint | null; isLoading?: boolean; }; +type Asset = { + symbol: string; + amount: bigint; +}; + +const createAsset = ( + symbol?: string | null, + amount?: bigint | null +): Asset => ({ + symbol: symbol || "", + amount: amount || ZERO, +}); + export function PricePerToken({ - fromCoin, - fromAmount, - toCoin, - toAmount, + swapState, + previewAmount, isLoading, }: PricePerTokenProps) { - const [direction, setDirection] = useState(ActiveInput.to); + const [[assetFrom, assetTo], setAssets] = useState< + [Asset | null, Asset | null] + >([null, null]); const isTyping = useValueIsTyping(); - const pricePerToken = getPricePerToken(direction, fromAmount, toAmount); - const from = direction === ActiveInput.from ? toCoin : fromCoin; - const to = direction === ActiveInput.from ? fromCoin : toCoin; + useEffect(() => { + if (swapState?.direction === ActiveInput.from) { + setAssets([ + createAsset(swapState.coinFrom.symbol, swapState.amount), + createAsset(swapState.coinTo.symbol, previewAmount), + ]); + } else if (swapState) { + setAssets([ + createAsset(swapState.coinFrom.symbol, previewAmount), + createAsset(swapState.coinTo.symbol, swapState.amount), + ]); + } + }, [swapState, previewAmount]); function toggle() { - setDirection((dir) => - dir === ActiveInput.from ? ActiveInput.to : ActiveInput.from - ); + setAssets([assetTo, assetFrom]); } if (isTyping || isLoading) return null; - if (!fromAmount || !toAmount) return null; + if (!assetFrom?.amount || !assetTo?.amount) return null; + const pricePerToken = getPricePerToken(assetFrom.amount, assetTo.amount); return (
- 1 {from} ={" "} - {pricePerToken} {to} + 1 {assetFrom.symbol} ={" "} + {pricePerToken} {assetTo.symbol}