Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mute wallet ui error message on rejected request #460

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
Loading