Skip to content

Commit

Permalink
fix: validate correct token transfer on different chains
Browse files Browse the repository at this point in the history
  • Loading branch information
EresDev committed May 17, 2024
1 parent 7cc3fc5 commit 8fe9b2b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
14 changes: 7 additions & 7 deletions functions/post-order.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TransactionReceipt, TransactionResponse } from "@ethersproject/providers";
import { JsonRpcProvider } from "@ethersproject/providers/lib/json-rpc-provider";
import { Interface, TransactionDescription } from "ethers/lib/utils";
import { Tokens, giftCardTreasuryAddress, permit2Address, permitTokenOwner } from "../shared/constants";
import { Tokens, chainIdToRewardTokenMap, giftCardTreasuryAddress, permit2Address, permitTokenOwner } from "../shared/constants";
import { getGiftCardOrderId } from "../shared/helpers";
import { ExchangeRate, NotOkReloadlyApiResponse, OrderRequestParams, ReloadlyOrderResponse, GiftCard } from "../shared/types";
import { permit2Abi } from "../static/scripts/rewards/abis/permit2Abi";
Expand Down Expand Up @@ -60,9 +60,9 @@ export const onRequest: PagesFunction<Env> = async (ctx) => {

console.log("Parsed transaction data: ", JSON.stringify(txParsed));

const errorResposne = validateTransaction(txParsed, txReceipt, giftCard);
if (errorResposne) {
return errorResposne;
const errorResponse = validateTransaction(txParsed, txReceipt, chainId, giftCard);
if (errorResponse) {
return errorResponse;
}

const amountDaiWei = txParsed.args.transferDetails.requestedAmount;
Expand Down Expand Up @@ -201,7 +201,7 @@ async function getExchangeRate(usdAmount: number, fromCurrency: string, accessTo
return responseJson as ExchangeRate;
}

function validateTransaction(txParsed: TransactionDescription, txReceipt: TransactionReceipt, giftCard: GiftCard) {
function validateTransaction(txParsed: TransactionDescription, txReceipt: TransactionReceipt, chainId: number, giftCard: GiftCard) {
const rewardAmount = txParsed.args.transferDetails.requestedAmount;

if (!isClaimableForAmount(giftCard, rewardAmount)) {
Expand Down Expand Up @@ -232,11 +232,11 @@ function validateTransaction(txParsed: TransactionDescription, txReceipt: Transa
return errorResponse;
}

if (txParsed.args.permit[0].token.toLowerCase() != Tokens.WXDAI.toLowerCase()) {
if (txParsed.args.permit[0].token.toLowerCase() != chainIdToRewardTokenMap[chainId].toLowerCase()) {
console.error(
"Given transaction hash is not transferring the required ERC20 token.",
JSON.stringify({
tranferedToken: txParsed.args.permit[0].token,
transferredToken: txParsed.args.permit[0].token,
requiredToken: Tokens.WXDAI.toLowerCase(),
})
);
Expand Down
6 changes: 6 additions & 0 deletions shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ export enum Tokens {
DAI = "0x6b175474e89094c44da98b954eedeac495271d0f",
WXDAI = "0xe91d153e0b41518a2ce8dd3d7944fa863463a97d",
}

export const permit2Address = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
export const giftCardTreasuryAddress = "0x3B47E3e4758E133acf72684727Dc10550C40e4B9";

// Signer of the permit and owner of the tokens are same in SignatureTransfer
export const permitTokenOwner = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8";

export const chainIdToRewardTokenMap = {
1: Tokens.DAI,
100: Tokens.WXDAI,
};

0 comments on commit 8fe9b2b

Please sign in to comment.