-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
480 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
import React from "react"; | ||
import { FunctionComponent, useEffect, useState } from "react"; | ||
import Button from "../UI/button"; | ||
import { useEtherContract } from "../../hooks/contracts"; | ||
import { | ||
useAccount, | ||
useContractRead, | ||
useContractWrite, | ||
useTransactionManager, | ||
} from "@starknet-react/core"; | ||
import { utils } from "starknetid.js"; | ||
import { getDomainWithStark, isValidEmail } from "../../utils/stringService"; | ||
import { gweiToEth, applyRateToBigInt } from "../../utils/feltService"; | ||
import { Abi, Call } from "starknet"; | ||
import { posthog } from "posthog-js"; | ||
import TxConfirmationModal from "../UI/txConfirmationModal"; | ||
import styles from "../../styles/components/registerV2.module.css"; | ||
import TextField from "../UI/textField"; | ||
import UsForm from "./usForm"; | ||
import { Divider } from "@mui/material"; | ||
import RegisterCheckboxes from "./registerCheckboxes"; | ||
import RegisterSummary from "./registerSummary"; | ||
import salesTax from "sales-tax"; | ||
import Wallets from "../UI/wallets"; | ||
import { computeMetadataHash, generateSalt } from "../../utils/userDataService"; | ||
import { getPriceFromDomains } from "../../utils/priceService"; | ||
import RenewalDomainsBox from "./renewalDomainsBox"; | ||
|
||
type RenewalProps = { | ||
domain: string; | ||
groups: string[]; | ||
}; | ||
|
||
const Renewal: FunctionComponent<RenewalProps> = ({ domain, groups }) => { | ||
const [email, setEmail] = useState<string>(""); | ||
const [emailError, setEmailError] = useState<boolean>(true); | ||
const [isUsResident, setIsUsResident] = useState<boolean>(false); | ||
const [usState, setUsState] = useState<string>("DE"); | ||
const [salesTaxRate, setSalesTaxRate] = useState<number>(0); | ||
const [salesTaxAmount, setSalesTaxAmount] = useState<string>("0"); | ||
const [callData, setCallData] = useState<Call[]>([]); | ||
const [price, setPrice] = useState<string>(""); | ||
const [balance, setBalance] = useState<string>(""); | ||
const [invalidBalance, setInvalidBalance] = useState<boolean>(false); | ||
const { contract: etherContract } = useEtherContract(); | ||
const [isTxModalOpen, setIsTxModalOpen] = useState(false); | ||
const encodedDomain = utils | ||
.encodeDomain(domain) | ||
.map((element) => element.toString())[0]; | ||
const [termsBox, setTermsBox] = useState<boolean>(true); | ||
const [renewalBox, setRenewalBox] = useState<boolean>(true); | ||
const [walletModalOpen, setWalletModalOpen] = useState<boolean>(false); | ||
const [sponsor, setSponsor] = useState<string>("0"); | ||
const [salt, setSalt] = useState<string | undefined>(); | ||
const [metadataHash, setMetadataHash] = useState<string | undefined>(); | ||
const [selectedDomains, setSelectedDomains] = | ||
useState<Record<string, boolean>>(); | ||
const { address } = useAccount(); | ||
const { data: userBalanceData, error: userBalanceDataError } = | ||
useContractRead({ | ||
address: etherContract?.address as string, | ||
abi: etherContract?.abi as Abi, | ||
functionName: "balanceOf", | ||
args: [address], | ||
}); | ||
const { writeAsync: execute, data: renewData } = useContractWrite({ | ||
// calls: renewalBox | ||
// ? callData.concat(registerCalls.renewal(encodedDomain, price)) | ||
// : callData, | ||
calls: callData, | ||
}); | ||
const [domainsMinting, setDomainsMinting] = useState<Map<string, boolean>>( | ||
new Map() | ||
); | ||
|
||
const { addTransaction } = useTransactionManager(); | ||
|
||
useEffect(() => { | ||
if (!renewData?.transaction_hash || !salt) return; | ||
posthog?.capture("renewal from page"); | ||
|
||
// register the metadata to the sales manager db | ||
fetch(`${process.env.NEXT_PUBLIC_SALES_SERVER_LINK}/add_metadata`, { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
meta_hash: metadataHash, | ||
email, | ||
groups, | ||
tax_state: isUsResident ? usState : "none", | ||
salt: salt, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.catch((err) => console.log("Error on sending metadata:", err)); | ||
|
||
addTransaction({ hash: renewData.transaction_hash }); | ||
setIsTxModalOpen(true); | ||
}, [renewData, salt, email, usState]); | ||
|
||
// on first load, we generate a salt | ||
useEffect(() => { | ||
setSalt(generateSalt()); | ||
}, []); | ||
|
||
// we update compute the purchase metadata hash | ||
useEffect(() => { | ||
// salt must not be empty to preserve privacy | ||
if (!salt) return; | ||
(async () => { | ||
setMetadataHash( | ||
await computeMetadataHash( | ||
email, | ||
groups, // default group for domain Owner | ||
isUsResident ? usState : "none", | ||
salt | ||
) | ||
); | ||
})(); | ||
}, [email, usState, salt]); | ||
|
||
useEffect(() => { | ||
if (!selectedDomains) return; | ||
setPrice(getPriceFromDomains(selectedDomains, 1).toString()); | ||
}, [selectedDomains]); | ||
|
||
useEffect(() => { | ||
if (userBalanceDataError || !userBalanceData) setBalance(""); | ||
else { | ||
const high = userBalanceData?.["balance"].high << BigInt(128); | ||
setBalance((userBalanceData?.["balance"].low + high).toString(10)); | ||
} | ||
}, [userBalanceData, userBalanceDataError]); | ||
|
||
useEffect(() => { | ||
if (balance && price) { | ||
if (gweiToEth(balance) > gweiToEth(price)) { | ||
setInvalidBalance(false); | ||
} else { | ||
setInvalidBalance(true); | ||
} | ||
} | ||
}, [balance, price]); | ||
|
||
useEffect(() => { | ||
const referralData = localStorage.getItem("referralData"); | ||
if (referralData) { | ||
const data = JSON.parse(referralData); | ||
if (data.sponsor && data?.expiry >= new Date().getTime()) { | ||
setSponsor(data.sponsor); | ||
} else { | ||
setSponsor("0"); | ||
} | ||
} else { | ||
setSponsor("0"); | ||
} | ||
}, []); | ||
|
||
function changeEmail(value: string): void { | ||
setEmail(value); | ||
setEmailError(isValidEmail(value) ? false : true); | ||
} | ||
|
||
useEffect(() => { | ||
if (isUsResident) { | ||
salesTax.getSalesTax("US", usState).then((tax) => { | ||
setSalesTaxRate(tax.rate); | ||
if (price) setSalesTaxAmount(applyRateToBigInt(price, tax.rate)); | ||
}); | ||
} else { | ||
setSalesTaxRate(0); | ||
setSalesTaxAmount(""); | ||
} | ||
}, [isUsResident, usState, price]); | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<div className={styles.card}> | ||
<div className={styles.form}> | ||
<div className="flex flex-col items-start gap-4 self-stretch"> | ||
<p className={styles.legend}>Your renewal</p> | ||
<h3 className={styles.domain}>{getDomainWithStark(domain)}</h3> | ||
</div> | ||
<div className="flex flex-col items-start gap-6 self-stretch"> | ||
<TextField | ||
helperText="Secure your domain's future and stay ahead with vital updates. Your email stays private with us, always." | ||
label="Email address" | ||
value={email} | ||
onChange={(e) => changeEmail(e.target.value)} | ||
color="secondary" | ||
error={emailError} | ||
errorMessage={"Please enter a valid email address"} | ||
type="email" | ||
/> | ||
<UsForm | ||
isUsResident={isUsResident} | ||
onUsResidentChange={() => setIsUsResident(!isUsResident)} | ||
usState={usState} | ||
changeUsState={(value) => setUsState(value)} | ||
/> | ||
<RenewalDomainsBox | ||
helperText="Check the box of the domains you want to renew" | ||
setSelectedDomains={setSelectedDomains} | ||
selectedDomains={selectedDomains} | ||
/> | ||
</div> | ||
</div> | ||
<div className={styles.summary}> | ||
<RegisterSummary | ||
ethRegistrationPrice={price} | ||
duration={1} | ||
renewalBox={false} | ||
salesTaxRate={salesTaxRate} | ||
isUsResident={isUsResident} | ||
/> | ||
<Divider className="w-full" /> | ||
<RegisterCheckboxes | ||
onChangeRenewalBox={() => setRenewalBox(!renewalBox)} | ||
onChangeTermsBox={() => setTermsBox(!termsBox)} | ||
termsBox={termsBox} | ||
renewalBox={renewalBox} | ||
/> | ||
{address ? ( | ||
<Button | ||
onClick={() => | ||
execute().then(() => { | ||
setDomainsMinting((prev) => | ||
new Map(prev).set(encodedDomain.toString(), true) | ||
); | ||
}) | ||
} | ||
disabled={ | ||
(domainsMinting.get(encodedDomain) as boolean) || | ||
!address || | ||
invalidBalance || | ||
!termsBox || | ||
(isUsResident && !usState) || | ||
emailError | ||
} | ||
> | ||
{!termsBox | ||
? "Please accept terms & policies" | ||
: isUsResident && !usState | ||
? "We need your US State" | ||
: invalidBalance | ||
? "You don't have enough eth" | ||
: emailError | ||
? "Enter a valid Email" | ||
: "Register my domain"} | ||
</Button> | ||
) : ( | ||
<Button onClick={() => setWalletModalOpen(true)}> | ||
Connect wallet | ||
</Button> | ||
)} | ||
</div> | ||
</div> | ||
<img className={styles.image} src="/visuals/registerV2.webp" /> | ||
<TxConfirmationModal | ||
txHash={renewData?.transaction_hash} | ||
isTxModalOpen={isTxModalOpen} | ||
closeModal={() => setIsTxModalOpen(false)} | ||
title="Your domain is on it's way !" | ||
/> | ||
<Wallets | ||
closeWallet={() => setWalletModalOpen(false)} | ||
hasWallet={walletModalOpen} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Renewal; |
Oops, something went wrong.