From 69b3cd9bc4ac1dc12430529ec1c85486f9ed205c Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Mon, 6 Jun 2022 12:04:12 +0800 Subject: [PATCH 01/11] feat: add useAccountName --- packages/plugin-infra/src/web3/index.ts | 1 + .../plugin-infra/src/web3/useAccountName.ts | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 packages/plugin-infra/src/web3/useAccountName.ts diff --git a/packages/plugin-infra/src/web3/index.ts b/packages/plugin-infra/src/web3/index.ts index c51a3a0820cc..5bced7295e19 100644 --- a/packages/plugin-infra/src/web3/index.ts +++ b/packages/plugin-infra/src/web3/index.ts @@ -1,6 +1,7 @@ export * from './Context' export * from './useAccount' +export * from './useAccountName' export * from './useAddressBook' export * from './useSocialAddressList' export * from './useSocialAddressListAll' diff --git a/packages/plugin-infra/src/web3/useAccountName.ts b/packages/plugin-infra/src/web3/useAccountName.ts new file mode 100644 index 000000000000..fce466030711 --- /dev/null +++ b/packages/plugin-infra/src/web3/useAccountName.ts @@ -0,0 +1,25 @@ +import { useMemo } from 'react' +import { isSameAddress, NetworkPluginID } from '@masknet/web3-shared-base' +import { useWeb3State } from './useWeb3State' +import { useAccount } from './useAccount' +import { useWallets } from './useWallets' +import { useProviderType } from './useProviderType' +import type { Web3Helper } from '../web3-helpers' + +export function useAccountName(pluginID?: T, expectedAccount?: string) { + type ProviderName = (providerType: Web3Helper.Definition[T]['ProviderType']) => string + + const { Others } = useWeb3State(pluginID) + const account = useAccount(pluginID, expectedAccount) + const proivderType = useProviderType(pluginID) + const wallets = useWallets(pluginID) + + return useMemo(() => { + // if the currently selected account is a mask wallet then use the wallet name as the account name + const wallet = wallets.find((x) => isSameAddress(account, x.address)) + if (wallet?.name) return wallet.name + + // else use the provider name as the account name + return (Others?.providerResolver.providerName as ProviderName | undefined)?.(proivderType) + }, [account, proivderType, wallets.map((x) => x.address.toLowerCase()), Others]) +} From 7e6bb11d8f0b635aa7a3ee4ebbbc4895514fe327 Mon Sep 17 00:00:00 2001 From: guanbinrui <52657989+guanbinrui@users.noreply.github.com> Date: Mon, 6 Jun 2022 12:06:35 +0800 Subject: [PATCH 02/11] refactor: update comment --- packages/plugin-infra/src/web3/useAccountName.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-infra/src/web3/useAccountName.ts b/packages/plugin-infra/src/web3/useAccountName.ts index fce466030711..9ed90478f5b1 100644 --- a/packages/plugin-infra/src/web3/useAccountName.ts +++ b/packages/plugin-infra/src/web3/useAccountName.ts @@ -15,7 +15,7 @@ export function useAccountName(pluginID?: T, expected const wallets = useWallets(pluginID) return useMemo(() => { - // if the currently selected account is a mask wallet then use the wallet name as the account name + // if the currently selected account is a mask wallet, then use the wallet name as the account name const wallet = wallets.find((x) => isSameAddress(account, x.address)) if (wallet?.name) return wallet.name From e8590ad2b60218ed7b2e2f9bfee57e1fe347bc83 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Mon, 6 Jun 2022 12:14:34 +0800 Subject: [PATCH 03/11] fix: typo --- packages/plugin-infra/src/web3/useAccountName.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/plugin-infra/src/web3/useAccountName.ts b/packages/plugin-infra/src/web3/useAccountName.ts index 9ed90478f5b1..95f26cb8700f 100644 --- a/packages/plugin-infra/src/web3/useAccountName.ts +++ b/packages/plugin-infra/src/web3/useAccountName.ts @@ -11,7 +11,7 @@ export function useAccountName(pluginID?: T, expected const { Others } = useWeb3State(pluginID) const account = useAccount(pluginID, expectedAccount) - const proivderType = useProviderType(pluginID) + const providerType = useProviderType(pluginID) const wallets = useWallets(pluginID) return useMemo(() => { @@ -20,6 +20,6 @@ export function useAccountName(pluginID?: T, expected if (wallet?.name) return wallet.name // else use the provider name as the account name - return (Others?.providerResolver.providerName as ProviderName | undefined)?.(proivderType) - }, [account, proivderType, wallets.map((x) => x.address.toLowerCase()), Others]) + return (Others?.providerResolver.providerName as ProviderName | undefined)?.(providerType) + }, [account, providerType, wallets.map((x) => x.address.toLowerCase()), Others]) } From 6fd115ec790a14d7b4c747c3dcfabe727bfd6c4a Mon Sep 17 00:00:00 2001 From: nuanyang233 Date: Mon, 6 Jun 2022 13:15:19 +0800 Subject: [PATCH 04/11] fix: replace wallet to account and account name in trader --- .../Trader/SNSAdaptor/trader/ConfirmDialog.tsx | 9 +++++++-- .../Trader/SNSAdaptor/trader/TradeForm.tsx | 15 ++++----------- .../plugins/Trader/SNSAdaptor/trader/Trader.tsx | 6 ++++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/mask/src/plugins/Trader/SNSAdaptor/trader/ConfirmDialog.tsx b/packages/mask/src/plugins/Trader/SNSAdaptor/trader/ConfirmDialog.tsx index 29e06f43bec1..84095dcaa2fc 100644 --- a/packages/mask/src/plugins/Trader/SNSAdaptor/trader/ConfirmDialog.tsx +++ b/packages/mask/src/plugins/Trader/SNSAdaptor/trader/ConfirmDialog.tsx @@ -125,11 +125,12 @@ export interface ConfirmDialogUIProps extends withClasses { onConfirm: () => void onClose?: () => void wallet?: Wallet | null + account?: string } export function ConfirmDialogUI(props: ConfirmDialogUIProps) { const { t } = useI18N() - const { open, trade, wallet, inputToken, outputToken, onConfirm, onClose, gas, gasPrice } = props + const { open, trade, wallet, inputToken, outputToken, onConfirm, onClose, gas, gasPrice, account } = props const [cacheTrade, setCacheTrade] = useState() const [priceUpdated, setPriceUpdated] = useState(false) @@ -220,7 +221,11 @@ export function ConfirmDialogUI(props: ConfirmDialogUIProps) { {t('plugin_red_packet_nft_account_name')} - ({wallet?.name}) + {wallet?.name ? ( + `(${wallet.name})` + ) : ( + + )} ()((them }) export interface AllTradeFormProps { - wallet?: Wallet | null + account?: string | null inputAmount: string inputToken?: FungibleToken outputToken?: FungibleToken @@ -199,7 +192,7 @@ export interface AllTradeFormProps { export const TradeForm = memo( ({ - wallet, + account, trades, inputAmount, inputToken, @@ -456,7 +449,7 @@ export const TradeForm = memo( - {wallet ? ( + {account ? ( () const wallet = useWallet(NetworkPluginID.PLUGIN_EVM) + const account = useAccount(NetworkPluginID.PLUGIN_EVM) const currentChainId = useChainId(NetworkPluginID.PLUGIN_EVM) const chainId = targetChainId ?? currentChainId const chainIdValid = useChainIdValid(NetworkPluginID.PLUGIN_EVM) @@ -356,7 +357,7 @@ export function Trader(props: TraderProps) { return (
{focusedTrade?.value && !isNativeTokenWrapper(focusedTrade.value) && inputToken && outputToken ? ( Date: Mon, 6 Jun 2022 16:25:11 +0800 Subject: [PATCH 05/11] fix: unlock loading button --- .../Trader/SNSAdaptor/trader/TradeForm.tsx | 7 ++-- .../UI/EthereumERC20TokenApprovedBoundary.tsx | 38 ++++++++++--------- .../src/state/Connection/translators/Base.ts | 2 +- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx b/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx index 048c289486d3..d887cc0a9f92 100644 --- a/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx +++ b/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx @@ -100,7 +100,6 @@ const useStyles = makeStyles<{ isDashboard: boolean; isPopup: boolean }>()((them fontSize: 18, lineHeight: '22px', fontWeight: 600, - borderRadius: isDashboard ? 8 : 24, height: 'auto', padding: '13px 0', marginTop: '0 !important', @@ -110,7 +109,6 @@ const useStyles = makeStyles<{ isDashboard: boolean; isPopup: boolean }>()((them lineHeight: '22px', fontWeight: 600, padding: '13px 0', - borderRadius: isDashboard ? 8 : 24, height: 'auto', }, selectedTokenChip: { @@ -476,6 +474,7 @@ export const TradeForm = memo( withChildren ActionButtonProps={{ color: 'primary', + variant: 'roundedContained', }} infiniteUnlockContent={ @@ -510,7 +509,7 @@ export const TradeForm = memo( ( {render ? (render(false) as any) : children} + if (transactionState.loading || approveStateType === ApproveStateType.UPDATING) + return ( + + + {transactionState.loading + ? t('plugin_ito_unlocking_symbol', { symbol: token.symbol }) + : `Updating ${token.symbol}`} + … + + {withChildren ? ( + {render ? (render(true) as any) : children} + ) : null} + + ) + if (approveStateType === ApproveStateType.UNKNOWN) return ( @@ -151,23 +172,6 @@ export function EthereumERC20TokenApprovedBoundary(props: EthereumERC20TokenAppr ) : null} ) - if (transactionState.loading || approveStateType === ApproveStateType.UPDATING) - return ( - - - {transactionState.loading - ? t('plugin_ito_unlocking_symbol', { symbol: token.symbol }) - : `Updating ${token.symbol}`} - … - - - ) if (approveStateType === ApproveStateType.APPROVED) return ( diff --git a/packages/plugins/EVM/src/state/Connection/translators/Base.ts b/packages/plugins/EVM/src/state/Connection/translators/Base.ts index f633830797da..6a5d57eabbf8 100644 --- a/packages/plugins/EVM/src/state/Connection/translators/Base.ts +++ b/packages/plugins/EVM/src/state/Connection/translators/Base.ts @@ -31,7 +31,7 @@ export class Base implements Translator { slowOption?.suggestedMaxFeePerGas && normalOption && isLessThan( - config.maxFeePerGas ? formatWeiToGwei(config.maxFeePerGas as string) : 0, + config.maxPriorityFeePerGas ? formatWeiToGwei(config.maxPriorityFeePerGas as string) : 0, slowOption.suggestedMaxPriorityFeePerGas, ) ) { From f448d3844cc7cf844a948cdb3096c89e1713e7aa Mon Sep 17 00:00:00 2001 From: nuanyang233 Date: Mon, 6 Jun 2022 16:45:13 +0800 Subject: [PATCH 06/11] revert: trader form --- .../src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx b/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx index d887cc0a9f92..048c289486d3 100644 --- a/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx +++ b/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx @@ -100,6 +100,7 @@ const useStyles = makeStyles<{ isDashboard: boolean; isPopup: boolean }>()((them fontSize: 18, lineHeight: '22px', fontWeight: 600, + borderRadius: isDashboard ? 8 : 24, height: 'auto', padding: '13px 0', marginTop: '0 !important', @@ -109,6 +110,7 @@ const useStyles = makeStyles<{ isDashboard: boolean; isPopup: boolean }>()((them lineHeight: '22px', fontWeight: 600, padding: '13px 0', + borderRadius: isDashboard ? 8 : 24, height: 'auto', }, selectedTokenChip: { @@ -474,7 +476,6 @@ export const TradeForm = memo( withChildren ActionButtonProps={{ color: 'primary', - variant: 'roundedContained', }} infiniteUnlockContent={ @@ -509,7 +510,7 @@ export const TradeForm = memo( ( Date: Mon, 6 Jun 2022 18:06:43 +0800 Subject: [PATCH 07/11] fix: unlock loading state --- .../plugin-infra/src/web3/EVM/useERC20TokenApproveCallback.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-infra/src/web3/EVM/useERC20TokenApproveCallback.ts b/packages/plugin-infra/src/web3/EVM/useERC20TokenApproveCallback.ts index 8302173b3fad..183d902484f2 100644 --- a/packages/plugin-infra/src/web3/EVM/useERC20TokenApproveCallback.ts +++ b/packages/plugin-infra/src/web3/EVM/useERC20TokenApproveCallback.ts @@ -116,7 +116,7 @@ export function useERC20TokenApproveCallback(address?: string, amount?: string, spender, balance, }, - state, + { ...state, loading: loadingAllowance || loadingBalance || state.loading }, approveCallback, resetCallback, ] as const From edce4ce4f6f4036f2a109e789ecc31b8707fba8f Mon Sep 17 00:00:00 2001 From: nuanyang233 Date: Mon, 6 Jun 2022 18:41:12 +0800 Subject: [PATCH 08/11] fix: make gas to hex string --- .../EVM/src/state/Connection/translators/Base.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/plugins/EVM/src/state/Connection/translators/Base.ts b/packages/plugins/EVM/src/state/Connection/translators/Base.ts index 6a5d57eabbf8..dd40597559a8 100644 --- a/packages/plugins/EVM/src/state/Connection/translators/Base.ts +++ b/packages/plugins/EVM/src/state/Connection/translators/Base.ts @@ -13,9 +13,15 @@ export class Base implements Translator { // #region polyfill transaction config { - // add gas margin - if (config.gas) - config.gas = BigNumber.max(toHex(addGasMargin(config.gas as string).toFixed()), 21000).toFixed() + try { + // add gas margin + if (config.gas) + config.gas = toHex( + BigNumber.max(toHex(addGasMargin(config.gas as string).toFixed()), 21000).toFixed(), + ) + } catch (error) { + console.log(error) + } // add gas price const hub = await Web3StateSettings.value.Hub?.getHub?.({ @@ -52,7 +58,6 @@ export class Base implements Translator { config.gasPrice = toHex(normalOption.suggestedMaxFeePerGas) } } - context.config = config } // #endregion From 907b4c6712f9f5ce738697c2ecb930b93eabcfcf Mon Sep 17 00:00:00 2001 From: nuanyang233 Date: Mon, 6 Jun 2022 19:09:41 +0800 Subject: [PATCH 09/11] fix: remove once wrap --- .../mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx | 1 + .../src/web3/EVM/useERC20TokenApproveCallback.ts | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx b/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx index 048c289486d3..edb2a32dbdc0 100644 --- a/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx +++ b/packages/mask/src/plugins/Trader/SNSAdaptor/trader/TradeForm.tsx @@ -476,6 +476,7 @@ export const TradeForm = memo( withChildren ActionButtonProps={{ color: 'primary', + style: { borderRadius: isDashboard ? 8 : 24 }, }} infiniteUnlockContent={ diff --git a/packages/plugin-infra/src/web3/EVM/useERC20TokenApproveCallback.ts b/packages/plugin-infra/src/web3/EVM/useERC20TokenApproveCallback.ts index 183d902484f2..41bf0e70d8cb 100644 --- a/packages/plugin-infra/src/web3/EVM/useERC20TokenApproveCallback.ts +++ b/packages/plugin-infra/src/web3/EVM/useERC20TokenApproveCallback.ts @@ -1,6 +1,5 @@ import type { NonPayableTx } from '@masknet/web3-contracts/types/types' import { isLessThan, NetworkPluginID, toFixed } from '@masknet/web3-shared-base' -import { once } from 'lodash-unified' import { useCallback, useMemo } from 'react' import { useAsyncFn } from 'react-use' import { useERC20TokenContract } from './useERC20TokenContract' @@ -80,10 +79,10 @@ export function useERC20TokenApproveCallback(address?: string, amount?: string, // send transaction and wait for hash return new Promise(async (resolve, reject) => { - const revalidate = once(() => { + const revalidate = () => { revalidateBalance() revalidateAllowance() - }) + } erc20Contract.methods .approve(spender, useExact ? amount : MaxUint256) .send(config as NonPayableTx) From 885e8fbc96b45f06b22467e1c8a93faa268dc4ec Mon Sep 17 00:00:00 2001 From: nuanyang233 Date: Mon, 6 Jun 2022 19:28:49 +0800 Subject: [PATCH 10/11] fix: incorrect formatter with approve function --- .../EVM/src/state/TransactionFormatter/descriptors/ERC20.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/plugins/EVM/src/state/TransactionFormatter/descriptors/ERC20.ts b/packages/plugins/EVM/src/state/TransactionFormatter/descriptors/ERC20.ts index ac9d67f0a813..1d2b141bca35 100644 --- a/packages/plugins/EVM/src/state/TransactionFormatter/descriptors/ERC20.ts +++ b/packages/plugins/EVM/src/state/TransactionFormatter/descriptors/ERC20.ts @@ -12,6 +12,7 @@ export class ERC20Descriptor implements TransactionDescriptor { chainId: context.chainId, }) + console.log(context) switch (context.name) { case 'approve': return { @@ -19,7 +20,7 @@ export class ERC20Descriptor implements TransactionDescriptor { title: 'Approve', description: `Approve spend ${getTokenAmountDescription( context.parameters?.value, - await connection?.getFungibleToken(context.parameters?.to ?? '', { + await connection?.getFungibleToken(context.to ?? '', { chainId: context.chainId, }), )}`, From 763ca772ce9e53a247c0daa21ca3369c38d6a5ed Mon Sep 17 00:00:00 2001 From: nuanyang233 Date: Mon, 6 Jun 2022 19:39:49 +0800 Subject: [PATCH 11/11] fix: add missing prop --- .../src/plugins/Collectible/SNSAdaptor/OpenSea/ActionBar.tsx | 2 +- packages/mask/src/web3/UI/ChainBoundary.tsx | 1 + .../EVM/src/state/TransactionFormatter/descriptors/ERC20.ts | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/mask/src/plugins/Collectible/SNSAdaptor/OpenSea/ActionBar.tsx b/packages/mask/src/plugins/Collectible/SNSAdaptor/OpenSea/ActionBar.tsx index 7b6cda385941..f1edde7ac3e4 100644 --- a/packages/mask/src/plugins/Collectible/SNSAdaptor/OpenSea/ActionBar.tsx +++ b/packages/mask/src/plugins/Collectible/SNSAdaptor/OpenSea/ActionBar.tsx @@ -54,7 +54,7 @@ export function ActionBar(props: ActionBarProps) { const isOwner = isSameAddress(asset.value.owner?.address, account) return ( - + {!isOwner && asset.value.auction ? ( (props: ChainBoundaryPro <> {!props.hiddenConnectButton ? ( } variant="contained" size={props.ActionButtonPromiseProps?.size} diff --git a/packages/plugins/EVM/src/state/TransactionFormatter/descriptors/ERC20.ts b/packages/plugins/EVM/src/state/TransactionFormatter/descriptors/ERC20.ts index 1d2b141bca35..716834484be1 100644 --- a/packages/plugins/EVM/src/state/TransactionFormatter/descriptors/ERC20.ts +++ b/packages/plugins/EVM/src/state/TransactionFormatter/descriptors/ERC20.ts @@ -12,7 +12,6 @@ export class ERC20Descriptor implements TransactionDescriptor { chainId: context.chainId, }) - console.log(context) switch (context.name) { case 'approve': return {