Skip to content

Commit

Permalink
Move to util
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboydiamonds committed Apr 3, 2024
1 parent 9f09239 commit 6d12c41
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import toast from 'react-hot-toast'
import React, { useEffect, useState, useRef, useCallback, useMemo } from 'react'
import { useAppSelector } from '@/store/hooks'
import { useDispatch } from 'react-redux'
import { zeroAddress, formatGwei } from 'viem'
import { zeroAddress } from 'viem'
import { useAccount, useNetwork } from 'wagmi'
import { initialState, updateFromValue } from '@/slices/bridge/reducer'
import MiniMaxButton from '../buttons/MiniMaxButton'
Expand All @@ -17,27 +17,7 @@ import { FromChainSelector } from './FromChainSelector'
import { FromTokenSelector } from './FromTokenSelector'
import { useBridgeState } from '@/slices/bridge/hooks'
import { usePortfolioState } from '@/slices/portfolio/hooks'

/**
* Calculates the estimated gas fee in Gwei.
* TODO: Hardcoding gas limit to 200k for now, update dynamically
* @param {string} gasPrice - The current gas price in Gwei as a string.
* @param {number} gasLimit - Function to format a value as Gwei.
* @returns {number|null} The formatted estimated gas cost, or null if the calculation is not possible.
*/
const calculateGasFeeInGwei = (gasPrice?: string, gasLimit = 200_000) => {
if (!gasPrice) return null

const estimatedGasCostInGwei = gasLimit * parseFloat(gasPrice)

const oneGwei = parseFloat(formatGwei(1n))

const formattedEstimatedGasCost = estimatedGasCostInGwei
? estimatedGasCostInGwei * oneGwei
: null

return formattedEstimatedGasCost
}
import { calculateGasFeeInGwei } from '../../utils/calculateGasFeeInGwei'

export const inputRef = React.createRef<HTMLInputElement>()

Expand Down
22 changes: 22 additions & 0 deletions packages/synapse-interface/utils/calculateGasFeeInGwei.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { formatGwei } from 'viem'

/**
* Calculates the estimated gas fee in Gwei.
* TODO: Hardcoding gas limit to 200k for now, update dynamically

Check failure on line 5 in packages/synapse-interface/utils/calculateGasFeeInGwei.ts

View workflow job for this annotation

GitHub Actions / lint

There must be a newline after the description of the JSDoc block

Check failure on line 5 in packages/synapse-interface/utils/calculateGasFeeInGwei.ts

View workflow job for this annotation

GitHub Actions / lint

There must be a newline after the description of the JSDoc block
* @param {string} gasPrice - The current gas price in Gwei as a string.
* @param {number} gasLimit - Function to format a value as Gwei.
* @returns {number|null} The formatted estimated gas cost, or null if the calculation is not possible.
*/
export const calculateGasFeeInGwei = (gasPrice?: string, gasLimit = 200000) => {
if (!gasPrice) return null

const estimatedGasCostInGwei = gasLimit * parseFloat(gasPrice)

const oneGwei = parseFloat(formatGwei(1n))

const formattedEstimatedGasCost = estimatedGasCostInGwei
? estimatedGasCostInGwei * oneGwei
: null

return formattedEstimatedGasCost
}

0 comments on commit 6d12c41

Please sign in to comment.