From 9a80c9bc216a517e3df9594623758011e10ee681 Mon Sep 17 00:00:00 2001 From: stanleyyuen <102275989+stanleyyconsensys@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:30:40 +0800 Subject: [PATCH 1/3] fix: mute error message --- .../wallet-ui/src/services/useStarkNetSnap.ts | 80 +++++++++++-------- packages/wallet-ui/src/utils/constants.ts | 2 + packages/wallet-ui/src/utils/utils.ts | 8 ++ 3 files changed, 57 insertions(+), 33 deletions(-) diff --git a/packages/wallet-ui/src/services/useStarkNetSnap.ts b/packages/wallet-ui/src/services/useStarkNetSnap.ts index 53c3405b..f15547f8 100644 --- a/packages/wallet-ui/src/services/useStarkNetSnap.ts +++ b/packages/wallet-ui/src/services/useStarkNetSnap.ts @@ -21,6 +21,7 @@ import { retry, isGTEMinVersion, getTokenBalanceWithDetails, + isUserDenyError, } from '../utils/utils'; import { setWalletConnection } from '../slices/walletSlice'; import { Network, VoyagerTransactionType } from '../types'; @@ -39,6 +40,7 @@ import { TransactionType, UniversalDetails, } from 'starknet'; +import { DENY_ERROR_CODE } from 'utils/constants'; export const useStarkNetSnap = () => { const dispatch = useAppDispatch(); @@ -409,13 +411,14 @@ export const useStarkNetSnap = () => { }, }, }); - dispatch(disableLoading()); + return response; } catch (err) { + if (!isUserDenyError(err)) { + throw err; + } + } finally { dispatch(disableLoading()); - //eslint-disable-next-line no-console - console.error(err); - throw err; } } @@ -494,13 +497,14 @@ export const useStarkNetSnap = () => { }, }, }); - dispatch(disableLoading()); return response; } catch (err) { + if (!isUserDenyError(err)) { + throw err; + } + return false; + } finally { dispatch(disableLoading()); - //eslint-disable-next-line no-console - console.error(err); - throw err; } }; @@ -526,13 +530,15 @@ export const useStarkNetSnap = () => { }, }, }); - dispatch(disableLoading()); + return response; } catch (err) { + if (!isUserDenyError(err)) { + throw err; + } + return false; + } finally { dispatch(disableLoading()); - //eslint-disable-next-line no-console - console.error(err); - throw err; } }; @@ -618,7 +624,7 @@ export const useStarkNetSnap = () => { ) => { dispatch(enableLoadingWithMessage('Adding Token...')); try { - const token = await provider.request({ + await provider.request({ method: 'wallet_invokeSnap', params: { snapId, @@ -635,28 +641,36 @@ export const useStarkNetSnap = () => { }, }, }); - if (token) { - const tokenBalance = await getTokenBalance( - tokenAddress, - accountAddress, - chainId, - ); - const usdPrice = await getAssetPriceUSD(token); - const tokenWithBalance: Erc20TokenBalance = getTokenBalanceWithDetails( - tokenBalance, - token, - usdPrice, - ); - dispatch(upsertErc20TokenBalance(tokenWithBalance)); - dispatch(disableLoading()); - return tokenWithBalance; - } else { - dispatch(disableLoading()); - return null; - } + + const token = { + address: tokenAddress, + name: tokenName, + symbol: tokenSymbol, + decimals: tokenDecimals, + chainId, + }; + + const tokenBalance = await getTokenBalance( + tokenAddress, + accountAddress, + chainId, + ); + + const usdPrice = await getAssetPriceUSD(token); + const tokenWithBalance: Erc20TokenBalance = getTokenBalanceWithDetails( + tokenBalance, + token, + usdPrice, + ); + dispatch(upsertErc20TokenBalance(tokenWithBalance)); + return tokenWithBalance; } catch (err) { + if (!isUserDenyError(err)) { + throw err; + } + return null; + } finally { dispatch(disableLoading()); - throw err; } }; diff --git a/packages/wallet-ui/src/utils/constants.ts b/packages/wallet-ui/src/utils/constants.ts index 76d33c55..4c930ead 100644 --- a/packages/wallet-ui/src/utils/constants.ts +++ b/packages/wallet-ui/src/utils/constants.ts @@ -66,3 +66,5 @@ export const DUMMY_ADDRESS = '0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; export const DEFAULT_FEE_TOKEN = FeeToken.ETH; export const MIN_METAMASK_VERSION = '12.5.0'; + +export const DENY_ERROR_CODE = 113; diff --git a/packages/wallet-ui/src/utils/utils.ts b/packages/wallet-ui/src/utils/utils.ts index 8b8de05b..b09c40f7 100644 --- a/packages/wallet-ui/src/utils/utils.ts +++ b/packages/wallet-ui/src/utils/utils.ts @@ -7,6 +7,7 @@ import { SEPOLIA_CHAINID, TIMEOUT_DURATION, MIN_ACC_CONTRACT_VERSION, + DENY_ERROR_CODE, } from './constants'; import { Erc20Token, Erc20TokenBalance, TokenBalance } from 'types'; import { constants } from 'starknet'; @@ -248,3 +249,10 @@ export const isValidStarkName = (starkName: string): boolean => { starkName, ); }; + +export const isUserDenyError = (error: any): Boolean => { + if (error?.data?.walletRpcError?.code) { + return error?.data?.walletRpcError?.code === DENY_ERROR_CODE; + } + return false; +}; From 99f200ed1d1a0f97bb09966dfc12747fb22d6837 Mon Sep 17 00:00:00 2001 From: stanleyyuen <102275989+stanleyyconsensys@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:32:32 +0800 Subject: [PATCH 2/3] chore: add error handle on display private key rpc --- packages/wallet-ui/src/services/useStarkNetSnap.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/wallet-ui/src/services/useStarkNetSnap.ts b/packages/wallet-ui/src/services/useStarkNetSnap.ts index f15547f8..bdc169c4 100644 --- a/packages/wallet-ui/src/services/useStarkNetSnap.ts +++ b/packages/wallet-ui/src/services/useStarkNetSnap.ts @@ -324,8 +324,9 @@ export const useStarkNetSnap = () => { }, }); } catch (err) { - //eslint-disable-next-line no-console - console.error(err); + if (!isUserDenyError(err)) { + throw err; + } } } From 7ec044f4e5fc60407cfd297000991c79fb5689d7 Mon Sep 17 00:00:00 2001 From: stanleyyuen <102275989+stanleyyconsensys@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:45:19 +0800 Subject: [PATCH 3/3] chore: fix lint --- packages/wallet-ui/src/services/useStarkNetSnap.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/wallet-ui/src/services/useStarkNetSnap.ts b/packages/wallet-ui/src/services/useStarkNetSnap.ts index bdc169c4..3ce4222e 100644 --- a/packages/wallet-ui/src/services/useStarkNetSnap.ts +++ b/packages/wallet-ui/src/services/useStarkNetSnap.ts @@ -40,7 +40,6 @@ import { TransactionType, UniversalDetails, } from 'starknet'; -import { DENY_ERROR_CODE } from 'utils/constants'; export const useStarkNetSnap = () => { const dispatch = useAppDispatch();