Skip to content

Commit

Permalink
fix: mute wallet ui error message on rejected request (#460)
Browse files Browse the repository at this point in the history
* fix: mute error message

* chore: add error handle on display private key rpc

* chore: fix lint
  • Loading branch information
stanleyyconsensys authored Dec 11, 2024
1 parent 85ea8ba commit 8027bc2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
84 changes: 49 additions & 35 deletions packages/wallet-ui/src/services/useStarkNetSnap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
retry,
isGTEMinVersion,
getTokenBalanceWithDetails,
isUserDenyError,
} from '../utils/utils';
import { setWalletConnection } from '../slices/walletSlice';
import { Network, VoyagerTransactionType } from '../types';
Expand Down Expand Up @@ -322,8 +323,9 @@ export const useStarkNetSnap = () => {
},
});
} catch (err) {
//eslint-disable-next-line no-console
console.error(err);
if (!isUserDenyError(err)) {
throw err;
}
}
}

Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
};

Expand All @@ -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;
}
};

Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
};

Expand Down
2 changes: 2 additions & 0 deletions packages/wallet-ui/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 8 additions & 0 deletions packages/wallet-ui/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
};

0 comments on commit 8027bc2

Please sign in to comment.