Skip to content

Commit

Permalink
fix: auto renwal with tax
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed Nov 15, 2023
1 parent 6cc7956 commit a7d39b8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
44 changes: 26 additions & 18 deletions components/identities/actions/autoRenewalModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
computeMetadataHash,
generateSalt,
} from "../../../utils/userDataService";
import registrationCalls from "../../../utils/callData/registrationCalls";
import { formatHexString, isValidEmail } from "../../../utils/stringService";
import { applyRateToBigInt, gweiToEth } from "../../../utils/feltService";
import autoRenewalCalls from "../../../utils/callData/autoRenewalCalls";
Expand Down Expand Up @@ -51,7 +50,6 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
const [isTxSent, setIsTxSent] = useState(false);
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 [email, setEmail] = useState<string>("");
const groups: string[] = [
Expand All @@ -61,6 +59,7 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
const [salt, setSalt] = useState<string | undefined>();
const [metadataHash, setMetadataHash] = useState<string | undefined>();
const [needMedadata, setNeedMetadata] = useState<boolean>(true);
const [salesTaxRate, setSalesTaxRate] = useState<number>(0);
const [callData, setCallData] = useState<Call[]>([]);
const { contract: pricingContract } = usePricingContract();
const { contract: etherContract } = useEtherContract();
Expand Down Expand Up @@ -103,15 +102,16 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
}, []);

useEffect(() => {
if (!identity?.addr) return;
if (!identity?.owner_addr) return;
fetch(
`${process.env.NEXT_PUBLIC_SERVER_LINK}/renewal/get_metahash?addr=${identity?.addr}`
`${process.env.NEXT_PUBLIC_SERVER_LINK}/renewal/get_metahash?addr=${identity?.owner_addr}`
)
.then((response) => response.json())
.then((data) => {
if (data.meta_hash) {
setNeedMetadata(false);
setMetadataHash(data.meta_hash);
setSalesTaxRate(data.tax_rate);
} else setNeedMetadata(true);
})
.catch((err) => {
Expand All @@ -126,26 +126,25 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
if (!salt || !needMedadata) return;
(async () => {
setMetadataHash(
await computeMetadataHash(
email,
// groups,
isUsResident ? usState : "none",
salt
)
await computeMetadataHash(email, isUsResident ? usState : "none", salt)
);
})();
}, [usState, salt, email, needMedadata]);

useEffect(() => {
if (isUsResident) {
salesTax.getSalesTax("US", usState).then((tax) => {
setSalesTaxRate(tax.rate);
if (price) setSalesTaxAmount(applyRateToBigInt(price, tax.rate));
});
if (!needMedadata && price) {
setSalesTaxAmount(applyRateToBigInt(price, salesTaxRate));
} else {
setSalesTaxRate(0);
if (isUsResident) {
salesTax.getSalesTax("US", usState).then((tax) => {
setSalesTaxRate(tax.rate);
if (price) setSalesTaxAmount(applyRateToBigInt(price, tax.rate));
});
} else {
setSalesTaxRate(0);
}
}
}, [isUsResident, usState, price]);
}, [isUsResident, usState, price, needMedadata, salesTaxRate]);

// Set Enable Auto Renewal Multicall
useEffect(() => {
Expand Down Expand Up @@ -203,7 +202,16 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
.then((res) => res.json())
.catch((err) => console.log("Error on registering to email:", err));

// addTransaction({ hash: autorenewData?.transaction_hash ?? "" });
addTransaction({
timestamp: Date.now(),
subtext: `Enabled auto renewal for ${domain}`,
type: NotificationType.TRANSACTION,
data: {
type: TransactionType.ENABLE_AUTORENEW,
hash: autorenewData.transaction_hash,
status: "pending",
},
});
setIsTxSent(true);
}, [autorenewData, usState, salt]);

Expand Down
12 changes: 11 additions & 1 deletion components/identities/actions/identityActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const IdentityActions: FunctionComponent<IdentityActionsProps> = ({
)
.then((response) => response.json())
.then((data) => {
console.log("data autorenewal", data);
if (!data.error && data.enabled) {
setIsAutoRenewalEnabled(true);
setAllowance(BigInt(data.allowance).toString(10));
Expand Down Expand Up @@ -160,7 +161,16 @@ const IdentityActions: FunctionComponent<IdentityActionsProps> = ({

useEffect(() => {
if (!disableRenewalData?.transaction_hash) return;
// addTransaction({ hash: disableRenewalData?.transaction_hash ?? "" });
addTransaction({
timestamp: Date.now(),
subtext: `Disabled auto renewal for ${identity?.domain}`,
type: NotificationType.TRANSACTION,
data: {
type: TransactionType.DISABLE_AUTORENEW,
hash: disableRenewalData.transaction_hash,
status: "pending",
},
});
setIsTxSent(true);
}, [disableRenewalData]);

Expand Down
7 changes: 5 additions & 2 deletions hooks/useNotificationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ export function useNotificationManager() {
setNotifications(updatedTransactions);
} else if (
data?.status === "ACCEPTED_ON_L2" ||
data?.status === "ACCEPTED_ON_L1"
data?.status === "ACCEPTED_ON_L1" ||
data?.finality_status === "ACCEPTED_ON_L2" ||
data?.finality_status === "ACCEPTED_ON_L1"
) {
updatedTransactions[index].data.txStatus = data.status;
updatedTransactions[index].data.txStatus =
data.status ?? data.finality_status;
updatedTransactions[index].data.status = "success";
setNotifications(updatedTransactions);
}
Expand Down

0 comments on commit a7d39b8

Please sign in to comment.