Skip to content

Commit

Permalink
Put gas price in a react context
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Aug 16, 2023
1 parent 0835572 commit 9c5d481
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/components/common/GasStation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const GasStation = () => {
const gasGwei = maxFeePerGas ? formatVisualAmount(maxFeePerGas, 'gwei', 0) : ''

return (
<Box display="flex" alignItems="center">
<Typography sx={{ minWidth: '1.4em' }}>{gasGwei}</Typography>
<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>
)
Expand Down
2 changes: 1 addition & 1 deletion src/components/safe-apps/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const FEATURES = [
'speaker-selection',
]

export type AllowedFeatures = typeof FEATURES[number]
export type AllowedFeatures = (typeof FEATURES)[number]

export const isBrowserFeature = (featureKey: string): featureKey is AllowedFeatures => {
return FEATURES.includes(featureKey as AllowedFeatures)
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

0 comments on commit 9c5d481

Please sign in to comment.