From bd0b4c969b247bea632adfe904a4994928f1a657 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 5 Nov 2022 13:06:56 +0100 Subject: [PATCH] AmountIn/Out/Balance --- packages/react-app/src/components/AmountIn.js | 2 +- .../react-app/src/components/AmountOut.js | 67 +++++++++++++++++++ packages/react-app/src/components/Balance.js | 24 +++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/packages/react-app/src/components/AmountIn.js b/packages/react-app/src/components/AmountIn.js index aeca9d6..da8d4bf 100644 --- a/packages/react-app/src/components/AmountIn.js +++ b/packages/react-app/src/components/AmountIn.js @@ -8,7 +8,7 @@ const AmountIn = () => { const [showList, setShowList] = useState(false); const [activeCurrency, setActiveCurrency] = useState("Select"); const ref = useRef() - + useOnClickOutside(ref, () => setShowList(false)) useEffect(() => { diff --git a/packages/react-app/src/components/AmountOut.js b/packages/react-app/src/components/AmountOut.js index e69de29..698b621 100644 --- a/packages/react-app/src/components/AmountOut.js +++ b/packages/react-app/src/components/AmountOut.js @@ -0,0 +1,67 @@ +import React, { useState, useEffect, useRef } from "react"; +import { formatUnits } from "ethers/lib/utils"; + +import { chevronDown } from "../assets"; +import { useAmountsOut, useOnClickOutside } from "../utils"; +import styles from "../styles"; + +const AmountOut = ({ fromToken, toToken, amountIn, pairContract, currencyValue, onSelect, currencies }) => { + const [showList, setShowList] = useState(false); + const [activeCurrency, setActiveCurrency] = useState("Select"); + const ref = useRef() + + const amountOut = useAmountsOut(pairContract, amountIn, fromToken, toToken) ?? 0; + + useOnClickOutside(ref, () => setShowList(false)) + + useEffect(() => { + if (Object.keys(currencies).includes(currencyValue)) { + setActiveCurrency(currencies[currencyValue]); + } else { + setActiveCurrency("Select") + } + }, [currencyValue, currencies]); + + return ( +
+ + +
setShowList(!showList)}> + + + {showList && ( +
    + {Object.entries(currencies).map(([token, tokenName], index) => ( +
  • { + if (typeof onSelect === "function") onSelect(token); + setActiveCurrency(tokenName); + setShowList(false); + }} + > + {tokenName} +
  • + ))} +
+ )} +
+
+ ); +}; + +export default AmountOut; diff --git a/packages/react-app/src/components/Balance.js b/packages/react-app/src/components/Balance.js index e69de29..dacba32 100644 --- a/packages/react-app/src/components/Balance.js +++ b/packages/react-app/src/components/Balance.js @@ -0,0 +1,24 @@ +import React from "react"; +import { formatUnits, parseUnits } from "ethers/lib/utils"; + +import styles from "../styles"; + +const Balance = ({ tokenBalance }) => { + + return ( +
+

+ {tokenBalance ? ( + <> + Balance: + {formatUnits(tokenBalance ?? parseUnits("0"))} + + ) : ( + "" + )} +

+
+ ); +}; + +export default Balance;