Skip to content

Commit

Permalink
fix: stop minting card if permit is not claimable
Browse files Browse the repository at this point in the history
  • Loading branch information
EresDev committed Aug 27, 2024
1 parent 3bc8d5c commit 11d6dd9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
91 changes: 46 additions & 45 deletions static/scripts/rewards/gift-cards/claim/claim-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,64 @@ import { OrderRequestParams, GiftCard } from "../../../../../shared/types";
import { isErc20Permit } from "../../render-transaction/render-transaction";
import { initClaimGiftCard } from "../list-gift-cards";

export function attachClaimAction(className: string, giftCard: GiftCard, app: AppState) {
const claimButtons: HTMLCollectionOf<Element> = document.getElementsByClassName(className);
Array.from(claimButtons).forEach((claimButton: Element) => {
(claimButton as HTMLButtonElement).addEventListener("click", async () => {
claimButton.setAttribute("data-loading", "true");
const productId = Number(claimButton.parentElement?.parentElement?.parentElement?.getAttribute("data-product-id"));
export function attachMintAction(giftCard: GiftCard, app: AppState) {
const claimButtons: HTMLCollectionOf<Element> = document.getElementsByClassName("mint-btn");

if (!isErc20Permit(app.reward)) {
toaster.create("error", "Only ERC20 permits are allowed to claim a card.");
} else if (!isClaimableForAmount(giftCard, app.reward.amount)) {
toaster.create("error", "Your reward amount is not equal to the price of available card.");
} else {
await claimGiftCard(productId, app);
}
(claimButtons[0] as HTMLButtonElement).addEventListener("click", async () => {
claimButtons[0].setAttribute("data-loading", "true");
const productId = Number(claimButtons[0].parentElement?.parentElement?.parentElement?.getAttribute("data-product-id"));

if (!isErc20Permit(app.reward)) {
toaster.create("error", "Only ERC20 permits are allowed to claim a card.");
} else if (!isClaimableForAmount(giftCard, app.reward.amount)) {
toaster.create("error", "Your reward amount is not equal to the price of available card.");
} else {
await mintGiftCard(productId, app);
}

claimButton.setAttribute("data-loading", "false");
});
claimButtons[0].setAttribute("data-loading", "false");
});
}

async function claimGiftCard(productId: number, app: AppState) {
async function mintGiftCard(productId: number, app: AppState) {
if (app.signer) {
await checkPermitClaimable(app);
const permit2Contract = new ethers.Contract(permit2Address, permit2Abi, app.signer);
if (!permit2Contract) return;
const isClaimiablle = await checkPermitClaimable(app);
if (isClaimiablle) {
const permit2Contract = new ethers.Contract(permit2Address, permit2Abi, app.signer);
if (!permit2Contract) return;

const reward = {
...app.reward,
};
reward.beneficiary = giftCardTreasuryAddress;
const reward = {
...app.reward,
};
reward.beneficiary = giftCardTreasuryAddress;

const tx = await transferFromPermit(permit2Contract, reward, "Processing... Please wait. Do not close this page.");
if (!tx) return;
await waitForTransaction(tx, `Transaction confirmed. Loading your card now.`);
const tx = await transferFromPermit(permit2Contract, reward, "Processing... Please wait. Do not close this page.");
if (!tx) return;
await waitForTransaction(tx, `Transaction confirmed. Loading your card now.`);

const url = `${getApiBaseUrl()}/post-order`;
const url = `${getApiBaseUrl()}/post-order`;

const orderParams: OrderRequestParams = {
chainId: app.signer.provider.network.chainId,
txHash: tx.hash,
productId,
};
const response = await fetch(url, {
method: "POST",
headers: {
Accept: "application/json",
},
body: JSON.stringify(orderParams),
});
const orderParams: OrderRequestParams = {
chainId: app.signer.provider.network.chainId,
txHash: tx.hash,
productId,
};
const response = await fetch(url, {
method: "POST",
headers: {
Accept: "application/json",
},
body: JSON.stringify(orderParams),
});

if (response.status != 200) {
toaster.create("error", "Order failed. Try again later.");
return;
}
if (response.status != 200) {
toaster.create("error", "Order failed. Try again later.");
return;
}

toaster.create("success", "Gift card claimed successfully.");
await initClaimGiftCard(app);
toaster.create("success", "Gift card claimed successfully.");
await initClaimGiftCard(app);
}
} else {
toaster.create("error", "Connect your wallet to proceed.");
}
Expand Down
4 changes: 2 additions & 2 deletions static/scripts/rewards/gift-cards/list-gift-cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isGiftCardAvailable, getGiftCardOrderId } from "../../../../shared/help
import { GiftCard, OrderTransaction } from "../../../../shared/types";
import { AppState } from "../app-state";
import { attachActivateInfoAction } from "./activate/activate-action";
import { attachClaimAction } from "./claim/claim-action";
import { attachMintAction as attachMintAction } from "./claim/claim-action";
import { attachRevealAction } from "./reveal/reveal-action";
import { getApiBaseUrl, getUserCountryCode } from "./helpers";
import { getGiftCardActivateInfoHtml } from "./activate/activate-html";
Expand Down Expand Up @@ -108,7 +108,7 @@ function addAvailableCardsHtml(giftCard: GiftCard | null, app: AppState, giftCar
activateInfoHtmlParts.push(getGiftCardActivateInfoHtml(giftCard));
activateInfoSection.innerHTML = activateInfoHtmlParts.join("");

attachClaimAction("mint-btn", giftCard, app);
attachMintAction(giftCard, app);
} else {
htmlParts.push(`<p class="list-error">There are no Visa/Mastercard available to claim at the moment.</p>`);
giftCardsSection.innerHTML = htmlParts.join("");
Expand Down

0 comments on commit 11d6dd9

Please sign in to comment.