Skip to content

Commit

Permalink
improve: add error handler for transaction (#103)
Browse files Browse the repository at this point in the history
* disable stake button when unstaked balance is zero

Signed-off-by: ryanwolhuter <[email protected]>

* add ethers error codes list

Signed-off-by: ryanwolhuter <[email protected]>

* add ethers error link component

Signed-off-by: ryanwolhuter <[email protected]>

* open error link in new tab

Signed-off-by: ryanwolhuter <[email protected]>

* undo comment out

Signed-off-by: ryanwolhuter <[email protected]>

Signed-off-by: ryanwolhuter <[email protected]>
  • Loading branch information
ryanwolhuter authored Nov 28, 2022
1 parent a9d03dd commit 752eb75
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 3 deletions.
5 changes: 4 additions & 1 deletion components/ErrorBanner/ErrorBanner.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EthersErrorLink } from "components";
import { mobileAndUnder } from "constant";
import { useErrorContext } from "hooks";
import Close from "public/assets/icons/close.svg";
Expand All @@ -17,7 +18,9 @@ export function ErrorBanner({ errorOrigin }: { errorOrigin?: ErrorOriginT }) {
<IconWrapper>
<Warning />
</IconWrapper>
<ErrorMessage>{message}</ErrorMessage>
<ErrorMessage>
<EthersErrorLink errorMessage={message} />
</ErrorMessage>
<CloseButton onClick={() => removeErrorMessage(message)}>
<IconWrapper>
<CloseIcon />
Expand Down
28 changes: 28 additions & 0 deletions components/EthersErrorLink/EthersErrorLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ethersErrorCodes } from "constant";
import NextLink from "next/link";
import styled from "styled-components";

export function EthersErrorLink({ errorMessage }: { errorMessage: string }) {
if (!ethersErrorCodes.some((code) => errorMessage.includes(code))) {
return <span>{errorMessage}</span>;
}

const [firstPart, secondPart] = errorMessage.split("[");

const link = secondPart.replace("See:", "").replace("]", "").trim();

return (
<span>
{firstPart}.{" "}
<Link href={link} target="_blank">
Learn more.
</Link>
</span>
);
}

const Link = styled(NextLink)`
font: inherit;
color: inherit;
text-decoration: underline;
`;
1 change: 1 addition & 0 deletions components/Panel/StakeUnstakePanel/Stake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function Stake({
return false;

return (
unstakedBalance?.eq(0) ||
!disclaimerChecked ||
inputAmount === "" ||
parseEtherSafe(inputAmount).eq(0)
Expand Down
5 changes: 4 additions & 1 deletion components/PanelErrorBanner/PanelErrorBanner.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EthersErrorLink } from "components";
import { useErrorContext } from "hooks";
import Close from "public/assets/icons/close.svg";
import Warning from "public/assets/icons/warning.svg";
Expand All @@ -20,7 +21,9 @@ export function PanelErrorBanner({
</IconWrapper>
<InnerWrapper>
{errorMessages.map((message) => (
<ErrorMessage key={message?.toString()}>{message}</ErrorMessage>
<ErrorMessage key={message?.toString()}>
<EthersErrorLink errorMessage={message} />
</ErrorMessage>
))}
</InnerWrapper>
<CloseButton onClick={() => clearErrorMessages()}>
Expand Down
1 change: 1 addition & 0 deletions components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
} from "./DonutChart/helpers";
export { Dropdown } from "./Dropdown/Dropdown";
export { ErrorBanner } from "./ErrorBanner/ErrorBanner";
export { EthersErrorLink } from "./EthersErrorLink/EthersErrorLink";
export { GlobalStyle } from "./GlobalStyle/GlobalStyle";
export { Header } from "./Header/Header";
export { HowItWorks } from "./HowItWorks/HowItWorks";
Expand Down
1 change: 1 addition & 0 deletions constant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export * from "./voting/earlyRequestMagicNumber";
export * from "./voting/voteTiming";
export * from "./web3/addresses";
export * from "./web3/deployBlocks";
export * from "./web3/ethersErrorCodes";
export * from "./web3/signingMessage";
export * from "./web3/supportedChains";
10 changes: 10 additions & 0 deletions constant/web3/ethersErrorCodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const ethersErrorCodes = [
"CALL_EXCEPTION",
"INSUFFICIENT_FUNDS",
"MISSING_NEW",
"NONCE_EXPIRED",
"NUMERIC_FAULT",
"REPLACEMENT_UNDERPRICED",
"TRANSACTION_REPLACED",
"UNPREDICTABLE_GAS_LIMIT",
];
2 changes: 1 addition & 1 deletion helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export { handleNotifications } from "./util/handleNotifications";
export { logTruthy } from "./util/logTruthy";
export * from "./util/misc";
export { unixTimestampToDate } from "./util/unixTimestampToDate";
export * from "./voting/polymarket";
export * from "./voting/formatVotes";
export { getVoteMetaData } from "./voting/getVoteMetaData";
export { makePriceRequestsByKey } from "./voting/makePriceRequestsByKey";
export { makeUniqueKeyForVote } from "./voting/makeUniqueKeyForVote";
export { onlyOneRequestPerAddress } from "./voting/onlyOneRequestPerAddress";
export * from "./voting/polymarket";
export * from "./voting/voteTiming";
export * from "./web3/crypto";
export { decodeHexString } from "./web3/decodeHexString";
Expand Down

1 comment on commit 752eb75

@vercel
Copy link

@vercel vercel bot commented on 752eb75 Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.