Skip to content

Commit

Permalink
fix: subscription page
Browse files Browse the repository at this point in the history
:wq
q
  • Loading branch information
fricoben committed Aug 28, 2024
1 parent a73c856 commit 6db6883
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 30 deletions.
32 changes: 18 additions & 14 deletions components/discount/freeRenewalCheckout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import {
selectedDomainsToArray,
selectedDomainsToEncodedArray,
} from "../../utils/stringService";
import {
applyRateToBigInt,
numberToFixedString,
} from "../../utils/feltService";
import { applyRateToBigInt } from "../../utils/feltService";
import { Call } from "starknet";
import styles from "../../styles/components/registerV2.module.css";
import TextField from "../UI/textField";
Expand All @@ -21,6 +18,7 @@ import RegisterSummary from "../domains/registerSummary";
import { computeMetadataHash, generateSalt } from "../../utils/userDataService";
import {
areDomainSelected,
getApprovalAmount,
getDisplayablePrice,
} from "../../utils/priceService";
import RenewalDomainsBox from "../domains/renewalDomainsBox";
Expand Down Expand Up @@ -93,7 +91,7 @@ const FreeRenewalCheckout: FunctionComponent<FreeRenewalCheckoutProps> = ({
const { addTransaction } = useNotificationManager();
const router = useRouter();
const [loadingPrice, setLoadingPrice] = useState<boolean>(false);
const needsAllowances = useNeedsAllowances(address);
const allowanceStatus = useNeedsAllowances(address);
const needSubscription = useNeedSubscription(address);

useEffect(() => {
Expand Down Expand Up @@ -236,12 +234,12 @@ const FreeRenewalCheckout: FunctionComponent<FreeRenewalCheckoutProps> = ({
useEffect(() => {
if (isSwissResident) {
setSalesTaxRate(swissVatRate);
setSalesTaxAmount(applyRateToBigInt(offer.price, swissVatRate));
setSalesTaxAmount(applyRateToBigInt(potentialPrice, swissVatRate));
} else {
setSalesTaxRate(0);
setSalesTaxAmount(BigInt(0));
}
}, [isSwissResident, offer.price]);
}, [isSwissResident, potentialPrice]);

// build free renewal call
useEffect(() => {
Expand All @@ -262,12 +260,19 @@ const FreeRenewalCheckout: FunctionComponent<FreeRenewalCheckoutProps> = ({

displayedCurrencies.map((currency) => {
// Add ERC20 allowance for all currencies if needed
if (needsAllowances[currency]) {
if (allowanceStatus[currency].needsAllowance) {
const amountToApprove = getApprovalAmount(
potentialPrice,
salesTaxAmount,
1,
allowanceStatus[currency].currentAllowance
);

calls.unshift(
autoRenewalCalls.approve(
ERC20Contract[currency],
AutoRenewalContracts[currency],
offer.price ?? BigInt(0)
amountToApprove
)
);
}
Expand Down Expand Up @@ -310,14 +315,14 @@ const FreeRenewalCheckout: FunctionComponent<FreeRenewalCheckoutProps> = ({
}, [
selectedDomains,
salesTaxAmount,
needsAllowances,
allowanceStatus,
metadataHash,
salesTaxRate,
needMetadata,
quoteData,
displayedCurrencies,
needSubscription,
offer.price,
potentialPrice,
]);

useEffect(() => {
Expand Down Expand Up @@ -393,16 +398,15 @@ const FreeRenewalCheckout: FunctionComponent<FreeRenewalCheckoutProps> = ({
<RegisterSummary
priceInEth={potentialPrice}
price={offer.price}
durationInYears={Number(
numberToFixedString(offer.durationInDays / 365)
)}
durationInYears={offer.durationInDays / 365}
salesTaxRate={salesTaxRate}
isSwissResident={isSwissResident}
customMessage={customCheckoutMessage}
displayedCurrency={displayedCurrencies}
onCurrencySwitch={onCurrencySwitch}
loadingPrice={loadingPrice}
areArCurrenciesEnabled
isUsdPriceHidden
/>
<Divider className="w-full" />
<RegisterCheckboxes
Expand Down
11 changes: 3 additions & 8 deletions components/domains/autorenewal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ const Subscription: FunctionComponent<SubscriptionProps> = ({ groups }) => {
useNeedSubscription(address);
const [currencyError, setCurrencyError] = useState<boolean>(false);

// Console log hook for needSubscription
useEffect(() => {
console.log("needSubscription:", needSubscription);
}, [needSubscription]);

useEffect(() => {
if (!address) return;
fetch(
Expand Down Expand Up @@ -276,17 +271,17 @@ const Subscription: FunctionComponent<SubscriptionProps> = ({ groups }) => {
displayedCurrencies.map((currency) => {
// Add ERC20 allowance for all currencies if needed
if (allowanceStatus[currency].needsAllowance) {
console.log("price", price);
console.log(
"allowanceStatus[currency].currentAllowance",
allowanceStatus[currency].currentAllowance
"Allowance status for " + currency,
allowanceStatus[currency]
);
const amountToApprove = getApprovalAmount(
price,
salesTaxAmount,
1,
allowanceStatus[currency].currentAllowance
);
console.log("amountToApprove for " + currency, amountToApprove);

calls.push(
autoRenewalCalls.approve(
Expand Down
6 changes: 5 additions & 1 deletion components/domains/registerSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type RegisterSummaryProps = {
discountedPrice?: bigint;
discountedPriceInEth?: bigint;
areArCurrenciesEnabled?: boolean;
isUsdPriceHidden?: boolean;
};

const RegisterSummary: FunctionComponent<RegisterSummaryProps> = ({
Expand All @@ -47,6 +48,7 @@ const RegisterSummary: FunctionComponent<RegisterSummaryProps> = ({
discountedPrice,
discountedPriceInEth,
areArCurrenciesEnabled = false,
isUsdPriceHidden = false,
}) => {
const [ethUsdPrice, setEthUsdPrice] = useState<string>("0"); // price of 1 ETH in USD
const [usdRegistrationPrice, setUsdRegistrationPrice] = useState<string>("0");
Expand Down Expand Up @@ -167,7 +169,9 @@ const RegisterSummary: FunctionComponent<RegisterSummaryProps> = ({
) : (
displayTokenPrice()
)}
<p className={styles.legend}>≈ ${usdRegistrationPrice}</p>
{isUsdPriceHidden ? null : (
<p className={styles.legend}>≈ ${usdRegistrationPrice}</p>
)}
</div>
</div>
{areArCurrenciesEnabled ? (
Expand Down
9 changes: 2 additions & 7 deletions hooks/useNeedAllowances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
ERC20Contract,
CurrencyType,
AutoRenewalContracts,
UINT_128_MAX,
} from "../utils/constants";
import { isApprovalInfinite } from "@/utils/priceService";

export default function useNeedsAllowances(
address?: string
Expand Down Expand Up @@ -74,13 +74,8 @@ export default function useNeedsAllowances(
const newNeedsAllowances: TokenNeedsAllowance = {};
const erc20AllowanceRes = erc20AllowanceData as CallResult[];
currencyNames.forEach((currency, index) => {
console.log(
"erc20AllowanceRes[index] " + currency,
erc20AllowanceRes[index]
);

newNeedsAllowances[currency] = {
needsAllowance: erc20AllowanceRes[index][0] !== UINT_128_MAX,
needsAllowance: !isApprovalInfinite(erc20AllowanceRes[index][0]),
currentAllowance: erc20AllowanceRes[index][0],
};
});
Expand Down
32 changes: 32 additions & 0 deletions tests/utils/priceService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getYearlyPriceWei,
getApprovalAmount,
getDisplayablePrice,
isApprovalInfinite,
} from "../../utils/priceService";
import { generateString } from "../../utils/stringService";

Expand Down Expand Up @@ -251,3 +252,34 @@ describe("getDisplayablePrice function", () => {
expect(getDisplayablePrice(price)).toBe("123456.789");
});
});

describe("isApprovalInfinite function", () => {
it("should return true for values equal to or greater than UINT_128_MAX", () => {
const UINT_128_MAX = (BigInt(1) << BigInt(128)) - BigInt(1);
expect(isApprovalInfinite(UINT_128_MAX)).toBe(true);
expect(isApprovalInfinite(UINT_128_MAX + BigInt(1))).toBe(true);
});

it("should return true for UINT_256_MINUS_UINT_128", () => {
const UINT_256_MINUS_UINT_128 =
(BigInt(1) << BigInt(256)) - (BigInt(1) << BigInt(128));
expect(isApprovalInfinite(UINT_256_MINUS_UINT_128)).toBe(true);
});

it("should return false for values less than UINT_128_MAX", () => {
const UINT_128_MAX = (BigInt(1) << BigInt(128)) - BigInt(1);
expect(isApprovalInfinite(UINT_128_MAX - BigInt(1))).toBe(false);
expect(isApprovalInfinite(BigInt(1000000))).toBe(false);
});

it("should handle string inputs", () => {
const UINT_128_MAX = ((BigInt(1) << BigInt(128)) - BigInt(1)).toString();
expect(isApprovalInfinite(UINT_128_MAX)).toBe(true);
expect(isApprovalInfinite("1000000")).toBe(false);
});

it("should return false for zero", () => {
expect(isApprovalInfinite(BigInt(0))).toBe(false);
expect(isApprovalInfinite("0")).toBe(false);
});
});
17 changes: 17 additions & 0 deletions utils/priceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,20 @@ export function getApprovalAmount(

return amountToApprove;
}

export function isApprovalInfinite(approval: bigint | string): boolean {
// Convert approval to a BigInt if it's not already
const approvalBigInt = BigInt(approval);

// Define the threshold values
const UINT_128_MAX = (BigInt(1) << BigInt(128)) - BigInt(1);
const UINT_256_MINUS_UINT_128 =
(BigInt(1) << BigInt(256)) - (BigInt(1) << BigInt(128));

// Define a threshold that considers anything near or above UINT_128_MAX as infinite
const threshold = UINT_128_MAX;

return (
approvalBigInt >= threshold || approvalBigInt === UINT_256_MINUS_UINT_128
);
}

0 comments on commit 6db6883

Please sign in to comment.