Skip to content

Commit

Permalink
Merge pull request #353 from ariesgun/development
Browse files Browse the repository at this point in the history
feat: decode metamask transaction error
  • Loading branch information
gentlementlegen authored Jan 16, 2025
2 parents 7c352ee + 50809a3 commit 2420644
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"countries-and-timezones": "^3.6.0",
"dotenv": "^16.4.4",
"ethers": "^5.7.2",
"@ubiquity-os/ethers-decode-error": "^1.1.0",
"npm-run-all": "^4.1.5",
"zod": "^3.23.8"
},
Expand Down
9 changes: 9 additions & 0 deletions static/scripts/rewards/toaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ export type MetaMaskError = {
hex: "0x012c5a";
};
};
error: {
code: string;
message: string;
data: {
code: string;
message: string;
data: string;
};
};
};
16 changes: 9 additions & 7 deletions static/scripts/rewards/web3/erc20-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { buttonController, getMakeClaimButton, viewClaimButton } from "../button
import { toaster, errorToast, MetaMaskError } from "../toaster";
import { connectWallet } from "./connect-wallet";
import { convertToNetworkId } from "./use-rpc-handler";
import { decodeError } from "@ubiquity-os/ethers-decode-error";

export async function fetchTreasury(permit: Permit): Promise<{ balance: BigNumber; allowance: BigNumber; decimals: number; symbol: string }> {
let balance: BigNumber, allowance: BigNumber, decimals: number, symbol: string;
Expand Down Expand Up @@ -89,9 +90,9 @@ export async function transferFromPermit(permit2Contract: Contract, reward: Perm
buttonController.hideLoader();
buttonController.showMakeClaim();
} else {
// Handle other errors
console.error("Error in permitTransferFrom:", e);
errorToast(e, e.reason);
console.error(e);
const { error } = decodeError(e, permit2Abi);
errorToast(e, `Error in permitTransferFrom: ${error}`);
}
}
return null;
Expand Down Expand Up @@ -272,11 +273,12 @@ invalidateButton.addEventListener("click", async function invalidateButtonClickH

if (!app.signer) return;
await invalidateNonce(app.signer, app.reward.nonce);
} catch (error: unknown) {
if (error instanceof Error) {
const e = error as unknown as MetaMaskError;
} catch (err: unknown) {
if (err instanceof Error) {
const e = err as unknown as MetaMaskError;
console.error(e);
errorToast(e, e.reason);
const { error } = decodeError(e, permit2Abi);
errorToast(e, `Error in invalidateNonce: ${error}`);
return;
}
}
Expand Down
15 changes: 12 additions & 3 deletions static/scripts/rewards/web3/erc721-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { ERC721Permit } from "@ubiquibot/permit-generation/types";
import { BigNumber, ethers } from "ethers";
import { nftRewardAbi } from "../abis/nft-reward-abi";
import { app } from "../app-state";
import { toaster } from "../toaster";
import { errorToast, MetaMaskError, toaster } from "../toaster";
import { buttonController, getMakeClaimButton } from "../button-controller";
import { connectWallet } from "./connect-wallet";
import { decodeError } from "@ubiquity-os/ethers-decode-error";

export function claimErc721PermitHandler(reward: ERC721Permit) {
return async function claimHandler() {
Expand Down Expand Up @@ -33,7 +34,6 @@ export function claimErc721PermitHandler(reward: ERC721Permit) {
buttonController.showLoader();
try {
const nftContract = new ethers.Contract(reward.tokenAddress, nftRewardAbi, signer);

const tx: TransactionResponse = await nftContract.safeMint(
{
beneficiary: reward.beneficiary,
Expand Down Expand Up @@ -62,7 +62,16 @@ export function claimErc721PermitHandler(reward: ERC721Permit) {
} catch (error: unknown) {
console.error(error);
if (error instanceof Error) {
toaster.create("error", `Error claiming NFT: ${error.message}`);
const e = error as unknown as MetaMaskError;
if (e.code == "ACTION_REJECTED") {
// Handle the user rejection case
toaster.create("info", `Transaction was not sent because it was rejected by the user.`);
buttonController.hideLoader();
buttonController.showMakeClaim();
} else {
const { error } = decodeError(e, nftRewardAbi);
errorToast(e, `Error in permitTransferFrom: ${error}`);
}
} else if (typeof error === "string") {
toaster.create("error", `Error claiming NFT: ${error}`);
} else {
Expand Down

0 comments on commit 2420644

Please sign in to comment.