From a4a4a8be85f8b8273e968ad475f4f99ed2063988 Mon Sep 17 00:00:00 2001 From: Jacob Nastaskin Date: Wed, 14 Aug 2024 11:17:16 -0700 Subject: [PATCH] update file locations --- .../lib/components/foil/addEditLiquidity.tsx | 6 +-- packages/app/src/lib/components/util/util.ts | 31 ----------- packages/app/src/lib/constants/constants.ts | 1 + .../lib/{components => }/util/positionUtil.ts | 0 packages/app/src/lib/util/util.ts | 52 +++++++++++++++++++ 5 files changed, 56 insertions(+), 34 deletions(-) delete mode 100644 packages/app/src/lib/components/util/util.ts create mode 100644 packages/app/src/lib/constants/constants.ts rename packages/app/src/lib/{components => }/util/positionUtil.ts (100%) create mode 100644 packages/app/src/lib/util/util.ts diff --git a/packages/app/src/lib/components/foil/addEditLiquidity.tsx b/packages/app/src/lib/components/foil/addEditLiquidity.tsx index 11f35a66..26267dc9 100644 --- a/packages/app/src/lib/components/foil/addEditLiquidity.tsx +++ b/packages/app/src/lib/components/foil/addEditLiquidity.tsx @@ -34,8 +34,9 @@ import { } from 'wagmi'; import erc20ABI from '../../erc20abi.json'; -import { getNewLiquidity } from '../util/positionUtil'; -import { renderContractErrorToast, renderToastSuccess } from '../util/util'; +import { getNewLiquidity } from '../../util/positionUtil'; +import { renderContractErrorToast, renderToastSuccess } from '../../util/util'; +import { TOKEN_DECIMALS } from '~/lib/constants/constants'; import { useLoading } from '~/lib/context/LoadingContext'; import { MarketContext } from '~/lib/context/MarketProvider'; import type { FoilPosition } from '~/lib/interfaces/interfaces'; @@ -45,7 +46,6 @@ import useFoilDeployment from './useFoilDeployment'; const tickSpacingDefault = 200; // 1% - Hardcoded for now, should be retrieved with pool.tickSpacing() -const TOKEN_DECIMALS = 18; // should be retrieved from the contract? const priceToTick = (price: number, tickSpacing: number): number => { const tick = Math.log(price) / Math.log(1.0001); return Math.round(tick / tickSpacing) * tickSpacing; diff --git a/packages/app/src/lib/components/util/util.ts b/packages/app/src/lib/components/util/util.ts deleted file mode 100644 index a8ca7e6c..00000000 --- a/packages/app/src/lib/components/util/util.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { ToastId, UseToastOptions } from '@chakra-ui/react'; -import { ReadContractErrorType, WriteContractErrorType } from 'viem'; - -export const renderContractErrorToast = ( - error: ReadContractErrorType | WriteContractErrorType | null, - toast: (options?: UseToastOptions) => ToastId, - desc: string -) => { - if (error) { - console.error(desc, error.message); - toast({ - title: desc, - description: error.message, - status: 'error', - duration: 5000, - isClosable: true, - }); - } -}; - -export const renderToastSuccess = ( - toast: (options?: UseToastOptions) => ToastId, - desc: string -) => { - toast({ - title: desc, - status: 'success', - duration: 5000, - isClosable: true, - }); -}; diff --git a/packages/app/src/lib/constants/constants.ts b/packages/app/src/lib/constants/constants.ts new file mode 100644 index 00000000..754ddc36 --- /dev/null +++ b/packages/app/src/lib/constants/constants.ts @@ -0,0 +1 @@ +export const TOKEN_DECIMALS = 18; // should be retrieved from the contract? diff --git a/packages/app/src/lib/components/util/positionUtil.ts b/packages/app/src/lib/util/positionUtil.ts similarity index 100% rename from packages/app/src/lib/components/util/positionUtil.ts rename to packages/app/src/lib/util/positionUtil.ts diff --git a/packages/app/src/lib/util/util.ts b/packages/app/src/lib/util/util.ts new file mode 100644 index 00000000..a2c67634 --- /dev/null +++ b/packages/app/src/lib/util/util.ts @@ -0,0 +1,52 @@ +import type { ToastId, UseToastOptions } from '@chakra-ui/react'; +import type { ReadContractErrorType, WriteContractErrorType } from 'viem'; + +export const renderContractErrorToast = ( + error: ReadContractErrorType | WriteContractErrorType | null, + toast: (options?: UseToastOptions) => ToastId, + desc: string +) => { + if (error) { + console.error(desc, error.message); + toast({ + title: desc, + description: error.message, + status: 'error', + duration: 5000, + isClosable: true, + }); + } +}; + +export const renderToastSuccess = ( + toast: (options?: UseToastOptions) => ToastId, + desc: string +) => { + toast({ + title: desc, + status: 'success', + duration: 5000, + isClosable: true, + }); +}; + +export function decimalToFraction( + decimal: number, + decimalPlaces?: number +): [number, number] { + if (Number.isInteger(decimal)) { + return [decimal, 1]; + } + + const precision = decimalPlaces || 1000000; // Adjust this for more/less precision + let numerator = Math.round(decimal * precision); + let denominator = precision; + + const gcd = (a: number, b: number): number => (b ? gcd(b, a % b) : a); + const divisor = gcd(numerator, denominator); + + numerator /= divisor; + denominator /= divisor; + + return [numerator, denominator]; +}