Skip to content

Commit

Permalink
validate balance before purchase (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Sep 30, 2024
1 parent 3fcef50 commit 8ee1495
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"arrival_airport_not_whitelisted": "Buying protection for flights arriving into {{airport}} is currently not allowed",
"purchase_protection_not_possible": "To purchase protection, please connect your wallet, choose a flight and departure date and try again.",
"no_wallet_connected": "Please connect your wallet to view policies",
"no_policies": "No policies found"
"no_policies": "No policies found",
"insufficient_balance": "Your wallet balance is insufficient to purchase protection. Please top up your wallet and try again."
}
}
2 changes: 2 additions & 0 deletions src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export const DEPARTURE_DATE_DAYS_MAX = parseInt(process.env.NEXT_PUBLIC_DEPARTUR
export const DEPARTURE_DATE_DATE_FROM = process.env.NEXT_PUBLIC_DEPARTURE_DATE_DATE_FROM || '';
export const DEPARTURE_DATE_DATE_TO = process.env.NEXT_PUBLIC_DEPARTURE_DATE_DATE_TO || '';
export const PREMIUM_TOKEN_SYMBOL = process.env.NEXT_PUBLIC_PREMIUM_TOKEN_SYMBOL || 'USDC';
export const PREMIUM_TOKEN_DECIMALS = parseInt(process.env.NEXT_PUBLIC_PREMIUM_TOKEN_DECIMALS || '6');


18 changes: 17 additions & 1 deletion src/hooks/use_application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,41 @@ import { useState } from "react";
import { useSelector } from "react-redux";
import { RootState } from "../redux/store";
import { useTranslation } from "react-i18next";
import { useERC20Contract } from "./onchain/use_erc20_contract";
import { PREMIUM_TOKEN_DECIMALS } from "../config/constants";
import { useEnvContext } from "next-runtime-env";
import { useWallet } from "./onchain/use_wallet";

export default function useApplication() {
const { t } = useTranslation();
const { NEXT_PUBLIC_ERC20_TOKEN_CONTRACT_ADDRESS } = useEnvContext();

const { hasBalance } = useERC20Contract(NEXT_PUBLIC_ERC20_TOKEN_CONTRACT_ADDRESS!, PREMIUM_TOKEN_DECIMALS);
const { getSigner } = useWallet();

const [error, setError] = useState<string | null>(null);
const departureAirport = useSelector((state: RootState) => state.flightData.arrivalAirport);
const isDepartureAirportWhiteListed = useSelector((state: RootState) => state.flightData.departureAirport?.whitelisted || true);
const isArrivalAirportWhiteListed = useSelector((state: RootState) => state.flightData.arrivalAirport?.whitelisted || true);
const premium = useSelector((state: RootState) => state.flightData.premium);

async function purchaseProtection() {
setError(null);
// do nothing, just log for now

const canPurchase = departureAirport !== null && isDepartureAirportWhiteListed && isArrivalAirportWhiteListed;
const canPurchase = departureAirport !== null && isDepartureAirportWhiteListed && isArrivalAirportWhiteListed && premium !== null;

if (!canPurchase) {
setError(t("error.purchase_protection_not_possible"));
return;
}


if (! await hasBalance(BigInt(premium!), (await getSigner())!)) {
setError(t("error.insufficient_balance"));
return;
}

console.log("purchaseProtection");

// TODO: 1. check wallet balance for payment token
Expand Down
1 change: 0 additions & 1 deletion src/redux/slices/flightData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
import dayjs from 'dayjs';
import { ARRIVAL_AIRPORTS_WHITELIST, DEPARTURE_AIRPORTS_WHITELIST } from '../../config/constants';
import { Reason } from '../../types/errors';
import { Airport as FsAirport } from '../../types/flightstats/airport';
Expand Down

0 comments on commit 8ee1495

Please sign in to comment.