Skip to content

Commit

Permalink
fix: swap and bridge
Browse files Browse the repository at this point in the history
mf-6474
mf-6475
mf-6476
  • Loading branch information
swkatmask committed Nov 7, 2024
1 parent 308540b commit 616fcc5
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useMessages } from '@masknet/web3-hooks-base'
export function useMessageGuard() {
const matchInteraction = useMatch(PopupRoutes.ContractInteraction)
const messages = useMessages()
const nonSilentMessages = messages.filter((x) => !x.request.options.silent)

return !matchInteraction && messages.length > 0
return !matchInteraction && nonSilentMessages.length > 0
}
2 changes: 2 additions & 0 deletions packages/plugins/Trader/src/SiteAdaptor/storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EMPTY_LIST, type ScopedStorage } from '@masknet/shared-base'
import { queryClient } from '@masknet/shared-base-ui'
import { useQuery } from '@tanstack/react-query'
import { sortBy } from 'lodash-es'
import type { OkxTransaction } from '../types/trader.js'
Expand Down Expand Up @@ -38,6 +39,7 @@ export async function addTransaction<T extends OkxTransaction>(address: string,
...txObject.value,
[addr]: [...transactions, transaction],
})
queryClient.removeQueries({ queryKey: ['trade-history'] })
}

export async function updateTransaction<T extends OkxTransaction>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export function useApprove() {
const hash = await Web3.sendTransaction(
{
to: tokenAddress,
// gas provided by API for Arbitrum is too low, let wallet estimate itself
gas: chainId === ChainId.Arbitrum ? undefined : addGasMargin(approveInfo.gasLimit).toFixed(0),
// Add more gas margin for Arbitrum, since gas for Arbitrum that provided by API is too low.
gas: addGasMargin(approveInfo.gasLimit, chainId === ChainId.Arbitrum ? 1 : 0.3),
gasPrice: approveInfo.gasPrice,
data: approveInfo.data,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { msg, Select, Trans } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { Icons } from '@masknet/icons'
import { CopyButton, LoadingStatus, NetworkIcon, PluginWalletStatusBar, ProgressiveText } from '@masknet/shared'
import { NetworkPluginID, Sniffings } from '@masknet/shared-base'
Expand All @@ -20,7 +21,7 @@ import {
multipliedBy,
rightShift,
} from '@masknet/web3-shared-base'
import { ChainId, formatEthereumAddress, formatWeiToEther } from '@masknet/web3-shared-evm'
import { addGasMargin, ChainId, formatEthereumAddress, formatWeiToEther } from '@masknet/web3-shared-evm'
import { Box, Link as MuiLink, Typography } from '@mui/material'
import { useQueryClient } from '@tanstack/react-query'
import { BigNumber } from 'bignumber.js'
Expand All @@ -41,7 +42,6 @@ import { useBridgeData } from '../hooks/useBridgeData.js'
import { useLeave } from '../hooks/useLeave.js'
import { useToken } from '../hooks/useToken.js'
import { useTokenPrice } from '../hooks/useTokenPrice.js'
import { useLingui } from '@lingui/react'

const useStyles = makeStyles()((theme) => ({
container: {
Expand Down Expand Up @@ -261,7 +261,7 @@ export const BridgeConfirm = memo(function BridgeConfirm() {
from: account,
value: transaction.value,
gasPrice: gasConfig.gasPrice ?? transaction.gasPrice,
gas: fromChainId !== ChainId.Arbitrum && gas ? multipliedBy(gas, 1.2).toFixed(0) : undefined,
gas: gas ? addGasMargin(gas, fromChainId === ChainId.Arbitrum ? 1 : 0.3) : undefined,
maxPriorityFeePerGas:
'maxPriorityFeePerGas' in gasConfig && gasConfig.maxFeePerGas ?
gasConfig.maxFeePerGas
Expand Down
10 changes: 4 additions & 6 deletions packages/plugins/Trader/src/SiteAdaptor/trader/views/Confirm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { msg, Select, Trans } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { Icons } from '@masknet/icons'
import { LoadingStatus, PluginWalletStatusBar, ProgressiveText, TokenIcon } from '@masknet/shared'
import { EMPTY_LIST, NetworkPluginID, Sniffings } from '@masknet/shared-base'
Expand All @@ -10,10 +11,9 @@ import {
formatCompact,
GasOptionType,
leftShift,
multipliedBy,
rightShift,
} from '@masknet/web3-shared-base'
import { ChainId, formatWeiToEther } from '@masknet/web3-shared-evm'
import { addGasMargin, ChainId, formatWeiToEther } from '@masknet/web3-shared-evm'
import { Box, Link as MuiLink, Typography } from '@mui/material'
import { useQueryClient } from '@tanstack/react-query'
import { BigNumber } from 'bignumber.js'
Expand All @@ -33,7 +33,6 @@ import { useLiquidityResources } from '../hooks/useLiquidityResources.js'
import { useSwapData } from '../hooks/useSwapData.js'
import { useSwappable } from '../hooks/useSwappable.js'
import { useWaitForTransaction } from '../hooks/useWaitForTransaction.js'
import { useLingui } from '@lingui/react'

const useStyles = makeStyles()((theme) => ({
container: {
Expand Down Expand Up @@ -253,7 +252,7 @@ export const Confirm = memo(function Confirm() {
from: account,
value: transaction.value,
gasPrice: gasConfig.gasPrice ?? transaction.gasPrice,
gas: chainId !== ChainId.Arbitrum && gas ? multipliedBy(gas, 1.2).toFixed(0) : undefined,
gas: gas ? addGasMargin(gas, chainId === ChainId.Arbitrum ? 1 : 0.3) : undefined,
maxPriorityFeePerGas:
'maxPriorityFeePerGas' in gasConfig && gasConfig.maxFeePerGas ?
gasConfig.maxFeePerGas
Expand All @@ -270,7 +269,6 @@ export const Confirm = memo(function Confirm() {

const isApproving = approveMutation.isPending
const isCheckingApprove = isLoadingApproveInfo || isLoadingSpender || isLoadingAllowance
const showStale = isQuoteStale && !isSending && !isApproving

const leaveRef = useLeave()
const queryClient = useQueryClient()
Expand Down Expand Up @@ -388,8 +386,8 @@ export const Confirm = memo(function Confirm() {
mode,
waitForTransaction,
gasOptions,
approveMutation.mutateAsync,
])
const showStale = isQuoteStale && !isSending && !isApproving && !submitting
const loading = isSending || isCheckingApprove || isApproving || submitting
const disabled = !isSwappable || loading || dexIdsCount === 0

Expand Down
2 changes: 2 additions & 0 deletions packages/web3-providers/src/Web3/EVM/apis/Web3StateAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ export async function createEVMState(context: WalletAPI.IOContext): Promise<Web3
transactions: state.Transaction!.transactions!,
}),
})
// Warming up
state.TransactionWatcher
return state
}
4 changes: 2 additions & 2 deletions packages/web3-shared/evm/src/helpers/addGasMargin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigNumber } from 'bignumber.js'

export function addGasMargin(value: BigNumber.Value, scale = 3000) {
return new BigNumber(value).multipliedBy(new BigNumber(10000).plus(scale)).dividedToIntegerBy(10000)
export function addGasMargin(value: BigNumber.Value, scale = 0.3) {
return new BigNumber(value).multipliedBy(1 + scale).toFixed(0)
}

0 comments on commit 616fcc5

Please sign in to comment.