Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: gas price in header #2413

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/components/common/GasStation/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Box, SvgIcon, Typography } from '@mui/material'
import LocalGasStationIcon from '@mui/icons-material/LocalGasStation'
import useGasPrice from '@/hooks/useGasPrice'
import { formatVisualAmount } from '@/utils/formatters'

const GasStation = () => {
const [gasPrice] = useGasPrice()
const maxFeePerGas = gasPrice?.maxFeePerGas
const gasGwei = maxFeePerGas ? formatVisualAmount(maxFeePerGas, 'gwei', 0) : ''

return (
<Box display="flex" alignItems="center" gap={0.5}>
<Typography minWidth={gasGwei.length * 0.75 + 'em'} fontWeight="bold">
{gasGwei}
</Typography>
<SvgIcon component={LocalGasStationIcon} inheritViewBox fontSize="small" />
</Box>
)
}

export default GasStation
5 changes: 5 additions & 0 deletions src/components/common/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import SafeLogo from '@/public/images/logo.svg'
import Link from 'next/link'
import useSafeAddress from '@/hooks/useSafeAddress'
import BatchIndicator from '@/components/batch/BatchIndicator'
import GasStation from '../GasStation'

type HeaderProps = {
onMenuToggle?: Dispatch<SetStateAction<boolean>>
Expand Down Expand Up @@ -76,6 +77,10 @@ const Header = ({ onMenuToggle, onBatchToggle }: HeaderProps): ReactElement => {
<NotificationCenter />
</div>

<div className={css.element}>
<GasStation />
</div>

<div className={classnames(css.element, css.connectWallet)}>
<ConnectWallet />
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/__tests__/useGasPrice.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber } from 'ethers'
import { act, renderHook } from '@/tests/test-utils'
import useGasPrice from '@/hooks/useGasPrice'
import { useGasPriceAsync as useGasPrice } from '@/hooks/useGasPrice'
import { useCurrentChain } from '../useChains'

// mock useWeb3Readonly
Expand Down
12 changes: 11 additions & 1 deletion src/hooks/useGasPrice.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useContext, createContext } from 'react'
import { BigNumber } from 'ethers'
import type { FeeData } from '@ethersproject/abstract-provider'
import type {
Expand Down Expand Up @@ -124,7 +125,12 @@ const getGasParameters = (
maxPriorityFeePerGas: undefined,
}
}
const useGasPrice = (): AsyncResult<GasFeeParams> => {

type AsyncGasPrice = AsyncResult<GasFeeParams>

export const GasContext = createContext<AsyncGasPrice>([undefined, undefined, true])

export const useGasPriceAsync = (): AsyncGasPrice => {
const chain = useCurrentChain()
const gasPriceConfigs = chain?.gasPrice
const [counter] = useIntervalCounter(REFRESH_DELAY)
Expand Down Expand Up @@ -154,4 +160,8 @@ const useGasPrice = (): AsyncResult<GasFeeParams> => {
return [gasPrice, gasPriceError, isLoading]
}

const useGasPrice = (): AsyncResult<GasFeeParams> => {
return useContext(GasContext)
}

export default useGasPrice
6 changes: 5 additions & 1 deletion src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import useSafeMessageNotifications from '@/hooks/messages/useSafeMessageNotifica
import useSafeMessagePendingStatuses from '@/hooks/messages/useSafeMessagePendingStatuses'
import useChangedValue from '@/hooks/useChangedValue'
import { TxModalProvider } from '@/components/tx-flow'
import { GasContext, useGasPriceAsync } from '@/hooks/useGasPrice'

const GATEWAY_URL = IS_PRODUCTION || cgwDebugStorage.get() ? GATEWAY_URL_PRODUCTION : GATEWAY_URL_STAGING

Expand Down Expand Up @@ -67,13 +68,16 @@ const clientSideEmotionCache = createEmotionCache()
export const AppProviders = ({ children }: { children: ReactNode | ReactNode[] }) => {
const isDarkMode = useDarkMode()
const themeMode = isDarkMode ? 'dark' : 'light'
const gasPrice = useGasPriceAsync()

return (
<SafeThemeProvider mode={themeMode}>
{(safeTheme: Theme) => (
<ThemeProvider theme={safeTheme}>
<Sentry.ErrorBoundary showDialog fallback={ErrorBoundary}>
<TxModalProvider>{children}</TxModalProvider>
<GasContext.Provider value={gasPrice}>
<TxModalProvider>{children}</TxModalProvider>
</GasContext.Provider>
</Sentry.ErrorBoundary>
</ThemeProvider>
)}
Expand Down
Loading