From 6db68839910bed5f60f180727e6e7f4e4e1ec929 Mon Sep 17 00:00:00 2001 From: Fricoben Date: Tue, 27 Aug 2024 10:08:00 -0300 Subject: [PATCH] fix: subscription page :wq q --- components/discount/freeRenewalCheckout.tsx | 32 ++++++++++++--------- components/domains/autorenewal.tsx | 11 ++----- components/domains/registerSummary.tsx | 6 +++- hooks/useNeedAllowances.tsx | 9 ++---- tests/utils/priceService.test.js | 32 +++++++++++++++++++++ utils/priceService.ts | 17 +++++++++++ 6 files changed, 77 insertions(+), 30 deletions(-) diff --git a/components/discount/freeRenewalCheckout.tsx b/components/discount/freeRenewalCheckout.tsx index 8ccec0bd..5d6759fb 100644 --- a/components/discount/freeRenewalCheckout.tsx +++ b/components/discount/freeRenewalCheckout.tsx @@ -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"; @@ -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"; @@ -93,7 +91,7 @@ const FreeRenewalCheckout: FunctionComponent = ({ const { addTransaction } = useNotificationManager(); const router = useRouter(); const [loadingPrice, setLoadingPrice] = useState(false); - const needsAllowances = useNeedsAllowances(address); + const allowanceStatus = useNeedsAllowances(address); const needSubscription = useNeedSubscription(address); useEffect(() => { @@ -236,12 +234,12 @@ const FreeRenewalCheckout: FunctionComponent = ({ 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(() => { @@ -262,12 +260,19 @@ const FreeRenewalCheckout: FunctionComponent = ({ 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 ) ); } @@ -310,14 +315,14 @@ const FreeRenewalCheckout: FunctionComponent = ({ }, [ selectedDomains, salesTaxAmount, - needsAllowances, + allowanceStatus, metadataHash, salesTaxRate, needMetadata, quoteData, displayedCurrencies, needSubscription, - offer.price, + potentialPrice, ]); useEffect(() => { @@ -393,9 +398,7 @@ const FreeRenewalCheckout: FunctionComponent = ({ = ({ onCurrencySwitch={onCurrencySwitch} loadingPrice={loadingPrice} areArCurrenciesEnabled + isUsdPriceHidden /> = ({ groups }) => { useNeedSubscription(address); const [currencyError, setCurrencyError] = useState(false); - // Console log hook for needSubscription - useEffect(() => { - console.log("needSubscription:", needSubscription); - }, [needSubscription]); - useEffect(() => { if (!address) return; fetch( @@ -276,10 +271,9 @@ const Subscription: FunctionComponent = ({ 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, @@ -287,6 +281,7 @@ const Subscription: FunctionComponent = ({ groups }) => { 1, allowanceStatus[currency].currentAllowance ); + console.log("amountToApprove for " + currency, amountToApprove); calls.push( autoRenewalCalls.approve( diff --git a/components/domains/registerSummary.tsx b/components/domains/registerSummary.tsx index 5c38e313..6fea5f35 100644 --- a/components/domains/registerSummary.tsx +++ b/components/domains/registerSummary.tsx @@ -30,6 +30,7 @@ type RegisterSummaryProps = { discountedPrice?: bigint; discountedPriceInEth?: bigint; areArCurrenciesEnabled?: boolean; + isUsdPriceHidden?: boolean; }; const RegisterSummary: FunctionComponent = ({ @@ -47,6 +48,7 @@ const RegisterSummary: FunctionComponent = ({ discountedPrice, discountedPriceInEth, areArCurrenciesEnabled = false, + isUsdPriceHidden = false, }) => { const [ethUsdPrice, setEthUsdPrice] = useState("0"); // price of 1 ETH in USD const [usdRegistrationPrice, setUsdRegistrationPrice] = useState("0"); @@ -167,7 +169,9 @@ const RegisterSummary: FunctionComponent = ({ ) : ( displayTokenPrice() )} -

≈ ${usdRegistrationPrice}

+ {isUsdPriceHidden ? null : ( +

≈ ${usdRegistrationPrice}

+ )} {areArCurrenciesEnabled ? ( diff --git a/hooks/useNeedAllowances.tsx b/hooks/useNeedAllowances.tsx index 3f9ceafb..e21e5fd8 100644 --- a/hooks/useNeedAllowances.tsx +++ b/hooks/useNeedAllowances.tsx @@ -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 @@ -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], }; }); diff --git a/tests/utils/priceService.test.js b/tests/utils/priceService.test.js index cc753b84..6963f3bc 100644 --- a/tests/utils/priceService.test.js +++ b/tests/utils/priceService.test.js @@ -9,6 +9,7 @@ import { getYearlyPriceWei, getApprovalAmount, getDisplayablePrice, + isApprovalInfinite, } from "../../utils/priceService"; import { generateString } from "../../utils/stringService"; @@ -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); + }); +}); diff --git a/utils/priceService.ts b/utils/priceService.ts index 7a36c212..e573a422 100644 --- a/utils/priceService.ts +++ b/utils/priceService.ts @@ -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 + ); +}