Skip to content

Commit

Permalink
fix: bugfix for transfer nft (#10428)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuanyang233 committed Aug 16, 2023
1 parent 1f72d74 commit 9b85cca
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 34 deletions.
1 change: 1 addition & 0 deletions packages/mask/shared-ui/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"available_balance": "Available Balance",
"available_amount": "{{- amount}} available",
"failed_to_transfer_token": "Failed to transfer token: {{- message}}",
"failed_to_transfer_nft": "Network error or execute smart contract failed.",
"operation": "Operation",
"gas_limit": "Gas Limit",
"gas_price": "Gas Price",
Expand Down
2 changes: 1 addition & 1 deletion packages/mask/src/extension/popups/hook/useHasPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function useHasPassword() {
const { value: hasPassword, loading, retry } = useAsyncRetry(Services.Wallet.hasPassword, [])

useEffect(() => {
CrossIsolationMessages.events.passwordStatusUpdated.on(retry)
return CrossIsolationMessages.events.passwordStatusUpdated.on(retry)
}, [retry])

return { hasPassword, loading }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const GasSettingDialog = memo<GasSettingDialogProps>(function GasSettingM
} else {
return !gasPrice || isZero(gasPrice)
}
}, [maxPriorityFeePerGas, maxFeePerGas, gasOptions, isSupport1559, maxFeePerGasError])
}, [maxPriorityFeePerGas, maxFeePerGas, gasOptions, isSupport1559, maxFeePerGasError, gasPrice])

const error = gasPriceError || maxPriorityFeePerGasError || maxFeePerGasError

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Icons } from '@masknet/icons'
import { useUpdateEffect } from '@react-hookz/web'
import { UnlockERC20Token } from '../../../components/UnlockERC20Token/index.js'
import urlcat from 'urlcat'
import { compact, mapKeys, omit } from 'lodash-es'
import { compact, mapValues, omit } from 'lodash-es'

const useStyles = makeStyles()((theme) => ({
left: {
Expand Down Expand Up @@ -193,11 +193,12 @@ const Interaction = memo(function Interaction() {
return {
...x,
...(gasConfig
? mapKeys(omit(gasConfig, 'gasOptionType'), (value, key) => {
? mapValues(omit(gasConfig, 'gasOptionType'), (value, key) => {
if (key === 'gasCurrency' || !value) return
return toHex(value)
})
: {}),

chainId: toHex(x.chainId),
nonce: toHex(x.nonce),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CollectibleList, ElementAnchor } from '@masknet/shared'
import { EMPTY_LIST, NetworkPluginID } from '@masknet/shared-base'
import { ActionButton, LoadingBase, makeStyles } from '@masknet/theme'
import { ActionButton, LoadingBase, makeStyles, usePopupCustomSnackbar } from '@masknet/theme'
import type { Web3Helper } from '@masknet/web3-helpers'
import { useChainContext, useNonFungibleAsset, useNonFungibleAssets, useWeb3Connection } from '@masknet/web3-hooks-base'
import { isSameAddress } from '@masknet/web3-shared-base'
Expand Down Expand Up @@ -120,9 +120,18 @@ export const NonFungibleTokenSection = memo(function NonFungibleTokenSection() {
chainId,
})
const recipient = params.get('recipient')

const { showSnackbar } = usePopupCustomSnackbar()

const [state, transfer] = useAsyncFn(async () => {
if (!address || !tokenId || !recipient) return
return Web3.transferNonFungibleToken(address!, tokenId, recipient, '1')
try {
if (!address || !tokenId || !recipient) return
await Web3.transferNonFungibleToken(address!, tokenId, recipient, '1')
return
} catch (error) {
showSnackbar(t('failed_to_transfer_nft'), { variant: 'error' })
return
}
}, [address, tokenId || recipient])

const tokenNotReady = !address || !tokenId
Expand Down
29 changes: 19 additions & 10 deletions packages/plugins/Tips/src/contexts/Tip/useNftTip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useChainContext, useWeb3Connection, useWeb3State } from '@masknet/web3-
import type { ConnectionOptions } from '@masknet/web3-providers/types'
import { NetworkPluginID } from '@masknet/shared-base'
import type { TipTuple } from './type.js'
import { useCustomSnackbar } from '@masknet/theme'
import { useI18N } from '../../locales/i18n_generated.js'

export function useNftTip<T extends NetworkPluginID>(
pluginID: T,
Expand All @@ -11,25 +13,32 @@ export function useNftTip<T extends NetworkPluginID>(
tokenId?: string | null,
options?: ConnectionOptions<T>,
): TipTuple {
const t = useI18N()
const { Token } = useWeb3State<'all'>(pluginID)
const { account, chainId } = useChainContext()
const Web3 = useWeb3Connection(pluginID, {
account,
...options,
overrides: {},
})
const { showSnackbar } = useCustomSnackbar()
const [{ loading: isTransferring }, sendTip] = useAsyncFn(async () => {
if (!contractAddress) return
if (pluginID === NetworkPluginID.PLUGIN_EVM && !tokenId) return
const txid = await Web3.transferNonFungibleToken(contractAddress, tokenId ?? '', recipient, '1')
const tokenDetailed = await Web3.getNonFungibleToken(contractAddress, tokenId ?? '', undefined, {
chainId,
account,
})
if (tokenDetailed) {
await Token?.removeToken?.(account, tokenDetailed)
try {
if (!contractAddress) return
if (pluginID === NetworkPluginID.PLUGIN_EVM && !tokenId) return
const txid = await Web3.transferNonFungibleToken(contractAddress, tokenId ?? '', recipient, '1')
const tokenDetailed = await Web3.getNonFungibleToken(contractAddress, tokenId ?? '', undefined, {
chainId,
account,
})
if (tokenDetailed) {
await Token?.removeToken?.(account, tokenDetailed)
}
return txid
} catch {
showSnackbar(t.failed_to_transfer_nft(), { variant: 'error' })
return
}
return txid
}, [account, tokenId, pluginID, contractAddress, recipient, Web3])

return [isTransferring, sendTip]
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/Tips/src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"gas_fee": "Gas Fee",
"token_insufficient_balance": "Insufficient balance",
"manage_wallet": "Manage wallet",
"failed_to_transfer_nft": "Network error or execute smart contract failed.",
"save": "Save",
"save_successfully": "Save successfully",
"save_failed": "Save failed",
Expand Down
1 change: 1 addition & 0 deletions packages/web3-providers/src/Web3/EVM/apis/HubBaseAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class HubBaseAPI extends HubBaseAPI_Base<
try {
const isEIP1559 = new ChainResolverAPI().isFeatureSupported(options.chainId, 'EIP1559')
if (isEIP1559 && chainId !== ChainId.Astar) return await this.MetaSwap.getGasOptions(options.chainId)
if (chainId === ChainId.Aurora) return this.GasOptions.getGasOptions(options.chainId)
if (chainId === ChainId.Astar) return await this.AstarGas.getGasOptions(options.chainId)
return await this.DeBankGasOption.getGasOptions(options.chainId)
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class Popups implements Middleware<ConnectionContext> {
private async getPaymentToken(context: ConnectionContext) {
const maskAddress = getMaskTokenAddress(context.chainId)
try {
if (!context.paymentToken) return DEFAULT_PAYMENT_TOKEN_STATE
const smartPayChainId = await this.Bundler.getSupportedChainId()
if (context.chainId !== smartPayChainId || !context.owner) return DEFAULT_PAYMENT_TOKEN_STATE

Expand Down
27 changes: 10 additions & 17 deletions packages/web3-shared/evm/src/libs/ContractTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,18 @@ export class ContractTransaction<T extends BaseContract | null> {
const transaction = resolve(transactionResolver, this.contract)
const transactionEncoded = this.fill(transactionResolver, overrides)

// estimate gas
if (!transactionEncoded.gas) {
try {
const gas = await transaction?.estimateGas({
from: transactionEncoded.from,
to: transactionEncoded.to,
data: transactionEncoded.data,
value: transactionEncoded.value,
})
const gas = await transaction?.estimateGas({
from: transactionEncoded.from,
to: transactionEncoded.to,
data: transactionEncoded.data,
value: transactionEncoded.value,
})

if (gas) {
transactionEncoded.gas = gas.toFixed()
}
} catch {
// do nothing
} finally {
if (transactionEncoded.gas) {
transactionEncoded.gas = toHex(transactionEncoded.gas)
}
}
if (!gas) throw new Error('Estimate gas failed')

transactionEncoded.gas = toHex(gas)
}

return transactionEncoded
Expand Down

0 comments on commit 9b85cca

Please sign in to comment.