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: Add transferGasEstimate to oftV2TransferStarter #2315

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { BigNumber, constants, utils } from 'ethers'
import useSWR from 'swr'
import { useAccount } from 'wagmi'

import { DepositGasEstimates, GasEstimates } from '../arbTokenBridge.types'
import { BridgeTransferStarterFactory } from '@/token-bridge-sdk/BridgeTransferStarterFactory'
import { getProviderForChainId } from '@/token-bridge-sdk/utils'
import { useBalanceOnSourceChain } from '../useBalanceOnSourceChain'
import { useNetworks } from '../useNetworks'
import { useSelectedToken } from '../useSelectedToken'
import { useArbQueryParams } from '../useArbQueryParams'
import { TransferEstimateGasResult } from '@/token-bridge-sdk/BridgeTransferStarter'

async function fetcher([
walletAddress,
Expand All @@ -26,7 +26,7 @@ async function fetcher([
destinationChainErc20Address: string | undefined,
destinationAddress: string | undefined,
amount: BigNumber
]): Promise<GasEstimates | DepositGasEstimates | undefined> {
]): Promise<TransferEstimateGasResult> {
const _walletAddress = walletAddress ?? constants.AddressZero
const sourceProvider = getProviderForChainId(sourceChainId)
const signer = sourceProvider.getSigner(_walletAddress)
Expand Down Expand Up @@ -54,7 +54,7 @@ export function useGasEstimates({
destinationChainErc20Address?: string
amount: BigNumber
}): {
gasEstimates: GasEstimates | DepositGasEstimates | undefined
gasEstimates: TransferEstimateGasResult
error: any
} {
const [{ sourceChain, destinationChain }] = useNetworks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { DepositGasEstimates } from '../arbTokenBridge.types'
import { percentIncrease } from '@/token-bridge-sdk/utils'
import { DEFAULT_GAS_PRICE_PERCENT_INCREASE } from '@/token-bridge-sdk/Erc20DepositStarter'
import { useSelectedToken } from '../useSelectedToken'
import { useIsOftV2Transfer } from '../../components/TransferPanel/hooks/useIsOftV2Transfer'
import { useOftV2FeeEstimates } from './useOftV2FeeEstimates'
import {
isWithdrawalFromArbOneToEthereum,
isWithdrawalFromArbSepoliaToSepolia
Expand All @@ -43,17 +41,13 @@ export function getGasSummaryStatus({
amountBigNumber,
balance,
gasEstimatesError,
oftFeeEstimatesError,
oftFeeSummaryLoading,
sourceChainId,
destinationChainId
}: {
selectedTokenAddress: string | undefined
amountBigNumber: BigNumber
balance: BigNumber | null
gasEstimatesError: any
oftFeeEstimatesError: boolean
oftFeeSummaryLoading: boolean
sourceChainId: number
destinationChainId: number
}): GasEstimationStatus {
Expand All @@ -72,15 +66,15 @@ export function getGasSummaryStatus({
return 'unavailable'
}

if (balance === null || oftFeeSummaryLoading) {
if (balance === null) {
return 'loading'
}

if (amountBigNumber.gt(balance)) {
return 'insufficientBalance'
}

if (gasEstimatesError || oftFeeEstimatesError) {
if (gasEstimatesError) {
return 'error'
}

Expand Down Expand Up @@ -126,28 +120,7 @@ export function useGasSummary(): UseGasSummaryResult {
: selectedToken?.address
})

const isOft = useIsOftV2Transfer()
const {
feeEstimates: oftFeeEstimates,
error: oftFeeEstimatesError,
isLoading: oftFeeSummaryLoading
} = useOftV2FeeEstimates({
sourceChainErc20Address: isDepositMode
? selectedToken?.address
: selectedToken?.l2Address
})

const estimatedParentChainGasFees = useMemo(() => {
if (isOft && oftFeeEstimates) {
return parseFloat(
utils.formatEther(
isDepositMode
? oftFeeEstimates.sourceChainGasFee
: oftFeeEstimates.destinationChainGasFee
)
)
}

if (!estimateGasResult?.estimatedParentChainGas) {
return
}
Expand All @@ -156,25 +129,9 @@ export function useGasSummary(): UseGasSummaryResult {
estimateGasResult.estimatedParentChainGas.mul(parentChainGasPrice)
)
)
}, [
estimateGasResult,
parentChainGasPrice,
isOft,
oftFeeEstimates,
isDepositMode
])
}, [estimateGasResult, parentChainGasPrice])

const estimatedChildChainGasFees = useMemo(() => {
if (isOft && oftFeeEstimates) {
return parseFloat(
utils.formatEther(
isDepositMode
? oftFeeEstimates.destinationChainGasFee
: oftFeeEstimates.sourceChainGasFee
)
)
}

if (!estimateGasResult?.estimatedChildChainGas) {
return
}
Expand Down Expand Up @@ -203,13 +160,7 @@ export function useGasSummary(): UseGasSummaryResult {
estimateGasResult.estimatedChildChainGas.mul(childChainGasPrice)
)
)
}, [
childChainGasPrice,
estimateGasResult,
isDepositMode,
oftFeeEstimates,
isOft
])
}, [childChainGasPrice, estimateGasResult, isDepositMode])

const gasSummaryStatus = useMemo(
() =>
Expand All @@ -218,20 +169,10 @@ export function useGasSummary(): UseGasSummaryResult {
amountBigNumber,
balance,
gasEstimatesError,
oftFeeEstimatesError,
oftFeeSummaryLoading,
sourceChainId: networks.sourceChain.id,
destinationChainId: networks.destinationChain.id
}),
[
selectedToken,
amountBigNumber,
balance,
gasEstimatesError,
oftFeeEstimatesError,
oftFeeSummaryLoading,
networks
]
[selectedToken, amountBigNumber, balance, gasEstimatesError, networks]
)

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,17 @@ describe('getGasSummaryStatus', () => {
}

it('should return error if there is an OFT fee estimate error', async () => {
const result = getGasSummaryStatus({
...mockedGasSummaryParams,
oftFeeSummaryLoading: false,
oftFeeEstimatesError: true
})
const result = getGasSummaryStatus(mockedGasSummaryParams)
expect(result).toEqual('error')
})

it('should return success if there is not an OFT fee estimate error', async () => {
const result = getGasSummaryStatus({
...mockedGasSummaryParams,
oftFeeSummaryLoading: false,
oftFeeEstimatesError: false
})
const result = getGasSummaryStatus(mockedGasSummaryParams)
expect(result).toEqual('success')
})

it('should return loading if OFT fee summary is loading', async () => {
const result = getGasSummaryStatus({
...mockedGasSummaryParams,
oftFeeSummaryLoading: true,
oftFeeEstimatesError: false
})
const result = getGasSummaryStatus(mockedGasSummaryParams)
expect(result).toEqual('loading')
})
})
Expand All @@ -105,8 +93,6 @@ describe('getGasSummaryStatus', () => {
gasEstimatesError: new Error('cannot estimate gas'),
amountBigNumber: BigNumber.from(100_000),
balance: BigNumber.from(100_000),
oftFeeSummaryLoading: false,
oftFeeEstimatesError: false,
sourceChainId: ChainId.ArbitrumOne,
destinationChainId: ChainId.Ethereum
})
Expand All @@ -119,8 +105,6 @@ describe('getGasSummaryStatus', () => {
gasEstimatesError: 'walletNotConnected',
amountBigNumber: BigNumber.from(100_000),
balance: BigNumber.from(100_000),
oftFeeSummaryLoading: false,
oftFeeEstimatesError: false,
sourceChainId: ChainId.ArbitrumOne,
destinationChainId: ChainId.Ethereum
})
Expand All @@ -133,8 +117,6 @@ describe('getGasSummaryStatus', () => {
gasEstimatesError: 'walletNotConnected',
amountBigNumber: BigNumber.from(100_000),
balance: BigNumber.from(100_000),
oftFeeSummaryLoading: false,
oftFeeEstimatesError: false,
sourceChainId: ChainId.ArbitrumSepolia,
destinationChainId: ChainId.Sepolia
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export type TransferProps = {
overrides?: TransferOverrides
}

export type TransferEstimateGasResult =
| GasEstimates
| DepositGasEstimates
| undefined

export type RequiresNativeCurrencyApprovalProps = {
amount: BigNumber
signer: Signer
Expand Down Expand Up @@ -136,7 +141,7 @@ export abstract class BridgeTransferStarter {

public abstract transferEstimateGas(
props: TransferEstimateGasProps
): Promise<GasEstimates | DepositGasEstimates | undefined>
): Promise<TransferEstimateGasResult>

public abstract transfer(props: TransferProps): Promise<BridgeTransfer>
}
Loading
Loading