From 4108ed7e09f59ed62367ce8eecbee4954aa574f4 Mon Sep 17 00:00:00 2001 From: lubej <9722540+lubej@users.noreply.github.com> Date: Wed, 13 Dec 2023 14:33:12 +0100 Subject: [PATCH] Estimate gas price via provider --- src/components/WrapForm/index.tsx | 29 +++++-- src/constants/config.ts | 2 + src/hooks/useInterval.ts | 18 +++++ src/hooks/{useWeb3.tsx => useWeb3.ts} | 2 +- src/hooks/{useWrapForm.tsx => useWrapForm.ts} | 2 +- src/pages/Wrapper/index.tsx | 8 +- src/providers/Web3Context.ts | 31 ++++++++ src/providers/Web3Provider.tsx | 55 +++++-------- src/providers/WrapFormContext.ts | 26 +++++++ src/providers/WrapFormProvider.tsx | 77 +++++++++++-------- src/utils/number.utils.ts | 11 ++- 11 files changed, 178 insertions(+), 83 deletions(-) create mode 100644 src/hooks/useInterval.ts rename src/hooks/{useWeb3.tsx => useWeb3.ts} (80%) rename src/hooks/{useWrapForm.tsx => useWrapForm.ts} (79%) create mode 100644 src/providers/Web3Context.ts create mode 100644 src/providers/WrapFormContext.ts diff --git a/src/components/WrapForm/index.tsx b/src/components/WrapForm/index.tsx index 9dac6fb..4973b89 100644 --- a/src/components/WrapForm/index.tsx +++ b/src/components/WrapForm/index.tsx @@ -1,4 +1,4 @@ -import { FC, FormEvent, MouseEvent, useEffect, useState } from 'react' +import { FC, FormEvent, MouseEvent, useEffect, useRef, useState } from 'react' import { Input } from '../Input' import classes from './index.module.css' import { Button } from '../Button' @@ -8,6 +8,8 @@ import { useNavigate } from 'react-router-dom' import { ToggleButton } from '../ToggleButton' import { useWrapForm } from '../../hooks/useWrapForm' import { WrapFormType } from '../../utils/types' +import { useInterval } from '../../hooks/useInterval' +import { NumberUtils } from '../../utils/number.utils' const AMOUNT_PATTERN = '^[0-9]*[.,]?[0-9]*$' @@ -33,14 +35,25 @@ const labelMapByFormType: Record = { export const WrapForm: FC = () => { const navigate = useNavigate() const { - state: { formType, amount, isLoading, balance }, + state: { formType, amount, isLoading, balance, estimatedFee }, toggleFormType, submit, - getFeeAmount, + debounceLeadingSetFeeAmount, } = useWrapForm() const { firstInputLabel, secondInputLabel, submitBtnLabel } = labelMapByFormType[formType] const [value, setValue] = useState('') const [error, setError] = useState('') + const debouncedSetFeeAmount = useRef(debounceLeadingSetFeeAmount()) + + useEffect(() => { + // Trigger fee calculation on value change + debouncedSetFeeAmount.current() + }, [value]) + + useInterval(() => { + // Trigger fee calculation every minute, in case fee data becomes stale + debouncedSetFeeAmount.current() + }, 60000) useEffect(() => { setError('') @@ -76,9 +89,10 @@ export const WrapForm: FC = () => { const parsedValue = formType === WrapFormType.WRAP && value ? utils.parseUnits(value || '0', 'ether') : null const showRoseMaxAmountWarning = - parsedValue && parsedValue.gt(0) - ? utils.parseUnits(value, 'ether').eq(balance.sub(getFeeAmount())) - : false + parsedValue && parsedValue.gt(0) ? parsedValue.eq(balance.sub(estimatedFee)) : false + + const estimatedFeeTruncated = + estimatedFee && estimatedFee.gt(0) ? `~${NumberUtils.getTruncatedAmount(estimatedFee)} ROSE` : '/' return (
@@ -107,8 +121,7 @@ export const WrapForm: FC = () => {
- {/*This is hardcoded for now, as are gas prices*/} -

Estimated fee: <0.01 ROSE (~10 sec)

+

Estimated fee: {estimatedFeeTruncated}