Skip to content

Commit

Permalink
fix: offer cards only in permit amount range
Browse files Browse the repository at this point in the history
  • Loading branch information
EresDev committed Jul 29, 2024
1 parent eb72bc2 commit 5d8f4bc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 33 deletions.
7 changes: 6 additions & 1 deletion functions/post-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TransactionReceipt, TransactionResponse } from "@ethersproject/provider
import { JsonRpcProvider } from "@ethersproject/providers/lib/json-rpc-provider";
import { Interface, TransactionDescription } from "ethers/lib/utils";
import { Tokens, chainIdToRewardTokenMap, giftCardTreasuryAddress, permit2Address } from "../shared/constants";
import { getFastestRpcUrl, getGiftCardOrderId } from "../shared/helpers";
import { getFastestRpcUrl, getGiftCardOrderId, isGiftCardAvailable } from "../shared/helpers";
import { getGiftCardValue, isClaimableForAmount } from "../shared/pricing";
import { ExchangeRate, GiftCard, OrderRequestParams } from "../shared/types";
import { permit2Abi } from "../static/scripts/rewards/abis/permit2-abi";
Expand Down Expand Up @@ -66,6 +66,11 @@ export async function onRequest(ctx: Context): Promise<Response> {
const exchangeRateResponse = await getExchangeRate(1, giftCard.recipientCurrencyCode, accessToken);
exchangeRate = exchangeRateResponse.senderAmount;
}

if (!isGiftCardAvailable(giftCard, amountDaiWei)) {
throw new Error(`The ordered gift card does not meet available criteria: ${JSON.stringify(giftCard)}`);
}

const giftCardValue = getGiftCardValue(giftCard, amountDaiWei, exchangeRate);

const orderId = getGiftCardOrderId(txReceipt.from, txParsed.args.signature);
Expand Down
8 changes: 7 additions & 1 deletion shared/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ethers } from "ethers";
import { BigNumberish, ethers } from "ethers";
import { RPCHandler } from "@ubiquity-dao/rpc-handler";
import { GiftCard } from "./types";
import { isRangePriceGiftCardClaimable } from "./pricing";

export function getGiftCardOrderId(rewardToAddress: string, signature: string) {
const checksumAddress = ethers.utils.getAddress(rewardToAddress);
Expand Down Expand Up @@ -30,3 +32,7 @@ export async function getFastestRpcUrl(networkId: string | number) {
const provider = await handler.getFastestRpcProvider();
return provider.connection.url;
}

export function isGiftCardAvailable(giftCard: GiftCard, reward: BigNumberish): boolean {
return giftCard.denominationType == "RANGE" && isRangePriceGiftCardClaimable(giftCard, reward);
}
86 changes: 55 additions & 31 deletions static/scripts/rewards/gift-cards/list-gift-cards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getGiftCardOrderId } from "../../../../shared/helpers";
import { isGiftCardAvailable, getGiftCardOrderId } from "../../../../shared/helpers";
import { GiftCard, OrderTransaction } from "../../../../shared/types";
import { AppState } from "../app-state";
import { attachActivateInfoAction } from "./activate/activate-action";
Expand Down Expand Up @@ -45,47 +45,71 @@ export async function initClaimGiftCard(app: AppState) {

if (retrieveOrderResponse.status == 200) {
const giftCard = giftCards.find((giftCard) => transaction.product.productId == giftCard.productId);
const htmlParts: string[] = [];
htmlParts.push(`<h2 class="heading-gift-card">Your gift card</h2>`);
htmlParts.push(`<div class="gift-cards-wrapper purchased">`);
if (giftCard) {
htmlParts.push(getGiftCardHtml(giftCard, app.reward.amount));
addPurchasedCardHtml(giftCard, transaction, app, giftCardsSection, activateInfoSection);
}
htmlParts.push(getRedeemCodeHtml(transaction));
htmlParts.push(`</div>`);
giftCardsSection.innerHTML = htmlParts.join(",");
} else if (retrieveGiftCardsResponse.status == 200) {
const availableGiftCards = giftCards.filter((giftCard: GiftCard) => {
return giftCard && isGiftCardAvailable(giftCard, app.reward.amount);
});

const activateInfoHtmlParts: string[] = [];
if (giftCard) {
activateInfoHtmlParts.push(getGiftCardActivateInfoHtml(giftCard));
}
addAvailableCardsHtml(availableGiftCards, app, giftCardsSection, activateInfoSection);
} else if (retrieveGiftCardsResponse.status == 404) {
giftCardsSection.innerHTML = "<p class='list-error'>There are no Visa/Mastercard available to claim at the moment.</p>";
} else {
giftCardsSection.innerHTML = "<p class='list-error'>There was a problem in fetching gift cards. Try again later.</p>";
}

attachActivateInfoAction();
}

activateInfoSection.innerHTML = activateInfoHtmlParts.join("");
function addPurchasedCardHtml(
giftCard: GiftCard,
transaction: OrderTransaction,
app: AppState,
giftCardsSection: HTMLElement,
activateInfoSection: HTMLElement
) {
const htmlParts: string[] = [];
htmlParts.push(`<h2 class="heading-gift-card">Your gift card</h2>`);
htmlParts.push(`<div class="gift-cards-wrapper purchased">`);

attachRevealAction(transaction, app);
} else if (retrieveGiftCardsResponse.status == 200) {
const htmlParts: string[] = [];
htmlParts.push(`<h2 class="heading-gift-card">Or claim in virtual visa/mastercard</h2>`);
htmlParts.push(getGiftCardHtml(giftCard, app.reward.amount));

htmlParts.push(getRedeemCodeHtml(transaction));
htmlParts.push(`</div>`);

giftCardsSection.innerHTML = htmlParts.join("");

const activateInfoHtmlParts: string[] = [];

activateInfoHtmlParts.push(getGiftCardActivateInfoHtml(giftCard));

activateInfoSection.innerHTML = activateInfoHtmlParts.join("");

attachRevealAction(transaction, app);
}

function addAvailableCardsHtml(giftCards: GiftCard[], app: AppState, giftCardsSection: HTMLElement, activateInfoSection: HTMLElement) {
const htmlParts: string[] = [];
htmlParts.push(`<h2 class="heading-gift-card">Or claim in virtual visa/mastercard</h2>`);
if (giftCards.length) {
htmlParts.push(`<div class="gift-cards-wrapper${giftCards.length < 3 ? " center" : ""}">`);
giftCards.forEach((giftCard: GiftCard) => {
htmlParts.push(getGiftCardHtml(giftCard, app.reward.amount));
});
htmlParts.push(`</div>`);

giftCardsSection.innerHTML = htmlParts.join("");

const activateInfoHtmlParts: string[] = [];
giftCards.forEach((giftCard: GiftCard) => {
activateInfoHtmlParts.push(getGiftCardActivateInfoHtml(giftCard));
});
activateInfoSection.innerHTML = activateInfoHtmlParts.join("");

attachClaimAction("claim-gift-card-btn", giftCards, app);
} else if (retrieveGiftCardsResponse.status == 404) {
giftCardsSection.innerHTML = "<p class='list-error'>There are no Visa/Mastercard available to claim at the moment.</p>";
} else {
giftCardsSection.innerHTML = "<p class='list-error'>There was a problem in fetching gift cards. Try again later.</p>";
htmlParts.push(`<p>There are no Visa/Mastercard available to claim at the moment.</p>`);
}

attachActivateInfoAction();
giftCardsSection.innerHTML = htmlParts.join("");

const activateInfoHtmlParts: string[] = [];
giftCards.forEach((giftCard: GiftCard) => {
activateInfoHtmlParts.push(getGiftCardActivateInfoHtml(giftCard));
});
activateInfoSection.innerHTML = activateInfoHtmlParts.join("");

attachClaimAction("claim-gift-card-btn", giftCards, app);
}

0 comments on commit 5d8f4bc

Please sign in to comment.