8000 feat: Add Disburser native out of funds error by wsdt · Pull Request #492 · bobanetwork/gateway · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Add Disburser native out of funds error #492

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

Merged
merged 3 commits into from
Apr 24, 2024
Merged
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
7 changes: 7 additions & 0 deletions src/actions/bridgeAction.ts
8000
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ export const setTeleportationOfAssetSupported =
payload,
})

export const setTeleportationDisburserBalance =
(payload: string) => (dispatch) =>
dispatch({
type: 'BRIDGE/TELEPORTER/DISBURSER_BALANCE',
payload,
})

export const setTeleportationDestChainId =
(payload: BigNumberish) => (dispatch) =>
dispatch({
Expand Down
5 changes: 5 additions & 0 deletions src/actions/networkAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export const isTeleportationOfAssetSupported = (
networkService.isTeleportationOfAssetSupported(layer, asset, destChainId)
)

export const getDisburserBalance = (layer: string, asset: string) =>
createAction('DEPOSIT/TELEPORTATION/DISBURSER_BALANCE', () =>
networkService.getNativeDisburserBalance(layer, asset)
)

export const depositWithLightBridge = (
layer: string,
currency: string,
Expand Down
6 changes: 6 additions & 0 deletions src/containers/modals/tokenPicker/TokenPickerModal.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React, { FC, useEffect, useState } from 'react'
import {
setTeleportationOfAssetSupported,
setTeleportationDisburserBalance,
updateToken,
} from 'actions/bridgeAction'
import {
fetchBalances,
getDisburserBalance,
isTeleportationOfAssetSupported,
} from 'actions/networkAction'
import { closeModal } from 'actions/uiAction'
Expand Down Expand Up @@ -146,7 +148,11 @@ const TokenPickerModal: FC<TokenPickerModalProps> = ({ open, tokenIndex }) => {
const isSupported = await dispatch(
isTeleportationOfAssetSupported(layer, token.address, destChainId)
)
const disburserBalance = await dispatch(
getDisburserBalance(layer, token.address)
)
dispatch(setTeleportationOfAssetSupported(isSupported))
dispatch(setTeleportationDisburserBalance(disburserBalance))
}
handleClose()
}
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useBridgeAlerts/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ describe('useBridgeAlerts', () => {
'VALUE_LESS_THAN_MIN_BRIDGE_CONFIG_AMOUNT',
'VALUE_GREATER_THAN_MAX_BRIDGE_CONFIG_AMOUNT',
'MAX_BRIDGE_AMOUNT_PER_DAY_EXCEEDED',
'TELEPORTATION_DISBURSER_OUT_OF_FUNDS',
],
},
type: 'BRIDGE/ALERT/CLEAR',
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/useBridgeAlerts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
selectBobaFeeChoice,
selectBobaPriceRatio,
selectBridgeType,
selectTeleportationDisburserBalance,
selectExitFee,
selectFastDepositCost,
selectFastExitCost,
Expand All @@ -33,6 +34,7 @@ import BN from 'bignumber.js'
import { BRIDGE_TYPE } from 'containers/Bridging/BridgeTypeSelector'
import { Network } from 'util/network/network.util'
import { BigNumber, BigNumberish, ethers } from 'ethers'
import { formatEther } from '@ethersproject/units'
import { useNetworkInfo } from 'hooks/useNetworkInfo'

enum ALERT_KEYS {
Expand All @@ -47,6 +49,7 @@ enum ALERT_KEYS {
DEPRECATION_WARNING = 'DEPRECATION_WARNING',
TELEPORTATION_ASSET_NOT_SUPPORTED = 'TELEPORTER_ASSET_NOT_SUPPORTED',
TELEPORTATION_NO_UNCONVENTIONAL_WALLETS = 'TELEPORTATION_NO_UNCONVENTIONAL_WALLETS',
TELEPORTATION_DISBURSER_OUT_OF_FUNDS = 'TELEPORTATION_DISBURSER_OUT_OF_FUNDS',
}

interface ITeleportationTokenSupport {
Expand All @@ -68,6 +71,9 @@ const useBridgeAlerts = () => {
const activeNetwork = useSelector(selectActiveNetwork())
const tokenForTeleportationSupported: ITeleportationTokenSupport =
useSelector(selectIsTeleportationOfAssetSupported())
const disburserBalance: BigNumber | undefined = useSelector(
selectTeleportationDisburserBalance()
)

const { isActiveNetworkBnb } = useNetworkInfo()
// fast input layer 1
Expand Down Expand Up @@ -116,10 +122,21 @@ const useBridgeAlerts = () => {
ALERT_KEYS.VALUE_LESS_THAN_MIN_BRIDGE_CONFIG_AMOUNT,
ALERT_KEYS.VALUE_GREATER_THAN_MAX_BRIDGE_CONFIG_AMOUNT,
ALERT_KEYS.MAX_BRIDGE_AMOUNT_PER_DAY_EXCEEDED,
ALERT_KEYS.TELEPORTATION_DISBURSER_OUT_OF_FUNDS,
],
})
)

if (disburserBalance && disburserBalance.lt(amountToBridge)) {
dispatch(
setBridgeAlert({
meta: ALERT_KEYS.TELEPORTATION_DISBURSER_OUT_OF_FUNDS,
type: 'error',
text: `LightBridge has not enough funds for destination network left.`,
})
)
}

const maxDepositAmount = Number(
ethers.utils.formatEther(
tokenForTeleportationSupported.maxDepositAmount
Expand Down
2 changes: 2 additions & 0 deletions src/reducers/bridgeReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface IBridgeReducerState {
amountToBridge: number
destChainIdBridge: number
isFetchTxBlockNumber: boolean
teleportationDisburserBalance: BigNumberish
isTeleportationOfAssetSupported: {
supported: boolean
minDepositAmount: number
Expand All @@ -57,6 +58,7 @@ const initialState: IBridgeReducerState = {
amountToBridge: 0,
destChainIdBridge: 0,
isFetchTxBlockNumber: false,
teleportationDisburserBalance: 0,
isTeleportationOfAssetSupported: {
supported: false,
minDepositAmount: 0,
Expand Down
3 changes: 3 additions & 0 deletions src/selectors/bridgeSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ export const selectIsFetchTxBlockNumber = () => (state) =>
export const selectIsTeleportationOfAssetSupported = () => (state) =>
state.bridge.isTeleportationOfAssetSupported

export const selectTeleportationDisburserBalance = () => (state) =>
state.bridge.teleportationDisburserBalance

export const selectReenterWithdrawalConfig = () => (state) =>
state.bridge.withdrawalConfig
20 changes: 20 additions & 0 deletions src/services/networkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
CHAIN_ID_LIST,
getNetworkDetail,
getRpcUrl,
getRpcUrlByChainId,
Network,
networkLimitedAvailability,
NetworkType,
Expand Down Expand Up @@ -2007,6 +2008,7 @@ class NetworkService {
// FIXME: move this to separate service of earn.
/***********************************************/
/***** Get Reward *****/

/***********************************************/
async getReward(
currencyAddress,
Expand Down Expand Up @@ -2145,6 +2147,24 @@ class NetworkService {
return this.LightBridge!.attach(lightBridgeAddr).connect(provider)
}

async getNativeDisburserBalance(destChainId, token) {
// not just simply L2/L1 as also L2<>L2 supported, ..
const destProvider = new ethers.providers.StaticJsonRpcProvider(
getRpcUrlByChainId(destChainId)
)
const isNative =
token === ethers.constants.AddressZero ||
token === this.addresses.L2_ETH_Address

const disburserAddr =
await this.getLightBridgeContract(destChainId)?.disburser()
if (!disburserAddr || !isNative) {
return
}
// TODO: Add token support later on (proper token mapping, avoid code duplication & maintenance overhead)
return destProvider.getBalance(disburserAddr)
}

async isTeleportationOfAssetSupported(layer, token, destChainId) {
const lightBridgeAddr =
layer === Layer.L1
Expand Down
0