Skip to content

Commit

Permalink
ref: update front to work with latest version of autorenew contract
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed Oct 6, 2023
1 parent afe8762 commit 9783924
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 40 deletions.
39 changes: 10 additions & 29 deletions components/identities/actions/autoRenewalModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useTransactionManager,
} from "@starknet-react/core";
import React, { FunctionComponent, useEffect, useState } from "react";
import { useEtherContract, usePricingContract } from "../../../hooks/contracts";
import { usePricingContract } from "../../../hooks/contracts";
import styles from "../../../styles/components/autoRenewal.module.css";
import Button from "../../UI/button";
import { timestampToReadableDate } from "../../../utils/dateService";
Expand All @@ -30,7 +30,7 @@ type AutoRenewalModalProps = {
callDataEncodedDomain: (number | string)[];
identity?: Identity;
domain?: string;
limit?: string;
allowance?: string;
};

const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
Expand All @@ -39,12 +39,13 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
callDataEncodedDomain,
identity,
domain,
limit,
allowance,
}) => {
const { address } = useAccount();
const [price, setPrice] = useState<string>("0");
const [limitPrice, setLimitPrice] = useState<string>(limit ?? "0");
const [needApproval, setNeedApproval] = useState<boolean>(false);
const [renewalAllowance, setRenewalAllowance] = useState<string>(
allowance ?? "0"
);
const [isTxSent, setIsTxSent] = useState(false);
const [isUsResident, setIsUsResident] = useState<boolean>(false);
const [usState, setUsState] = useState<string>("DE");
Expand All @@ -59,23 +60,13 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
const [needMedadata, setNeedMetadata] = useState<boolean>(true);
const [callData, setCallData] = useState<Call[]>([]);
const { contract: pricingContract } = usePricingContract();
const { contract: etherContract } = useEtherContract();
const { addTransaction } = useTransactionManager();
const { data: priceData, error: priceError } = useContractRead({
address: pricingContract?.address as string,
abi: pricingContract?.abi as Abi,
functionName: "compute_renew_price",
args: [callDataEncodedDomain[1], 365],
});
const { data: approvalData, error: approvalError } = useContractRead({
address: etherContract?.address as string,
abi: etherContract?.abi as Abi,
functionName: "allowance",
args: [
address as string,
process.env.NEXT_PUBLIC_RENEWAL_CONTRACT as string,
],
});
const { writeAsync: execute, data: autorenewData } = useContractWrite({
calls: callData,
});
Expand All @@ -89,8 +80,8 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
}, [priceData, priceError]);

useEffect(() => {
if (limitPrice === "0") setLimitPrice(price);
}, [address, domain, price, limitPrice]);
if (renewalAllowance === "0") setRenewalAllowance(price);
}, [address, domain, price, renewalAllowance]);

// on first load, we generate a salt
useEffect(() => {
Expand Down Expand Up @@ -131,16 +122,6 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
})();
}, [usState, salt, email, needMedadata]);

useEffect(() => {
// check approval has been granted to renewal contract
if (approvalError || !approvalData) return;
if (
approvalData?.["remaining"].low === BigInt(0) &&
approvalData?.["remaining"].high === BigInt(0)
)
setNeedApproval(true);
}, [approvalData, approvalError]);

useEffect(() => {
if (isUsResident) {
salesTax.getSalesTax("US", usState).then((tax) => {
Expand All @@ -163,7 +144,7 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
txMetadataHash
)
);
}, [needApproval, price, salesTaxRate, metadataHash]);
}, [price, salesTaxRate, metadataHash]);

useEffect(() => {
if (!autorenewData?.transaction_hash || !salt) return;
Expand Down Expand Up @@ -233,7 +214,7 @@ const AutoRenewalModal: FunctionComponent<AutoRenewalModalProps> = ({
? `Your domain will be renewed for 1 year on
${timestampToReadableDate(
identity.domain_expiry - 2592000
)} at ${gweiToEth(limitPrice)} ETH.`
)} at ${gweiToEth(renewalAllowance)} ETH.`
: null}
</p>
</div>
Expand Down
15 changes: 6 additions & 9 deletions components/identities/actions/identityActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const IdentityActions: FunctionComponent<IdentityActionsProps> = ({
const [isAutoRenewalOpen, setIsAutoRenewalOpen] = useState<boolean>(false);
const [isAutoRenewalEnabled, setIsAutoRenewalEnabled] =
useState<boolean>(false);
const [limitPrice, setLimitPrice] = useState<string>("0");
const [allowance, setAllowance] = useState<string>("0");
const [isTxSent, setIsTxSent] = useState(false);
const [disableRenewalCalldata, setDisableRenewalCalldata] = useState<Call[]>(
[]
Expand Down Expand Up @@ -120,9 +120,9 @@ const IdentityActions: FunctionComponent<IdentityActionsProps> = ({
)
.then((response) => response.json())
.then((data) => {
if (!data.error && data.auto_renewal_enabled) {
if (!data.error && data.enabled) {
setIsAutoRenewalEnabled(true);
setLimitPrice(data.limit_price);
setAllowance(data.allowance);
} else {
setIsAutoRenewalEnabled(false);
}
Expand All @@ -144,13 +144,10 @@ const IdentityActions: FunctionComponent<IdentityActionsProps> = ({
useEffect(() => {
if (isAutoRenewalEnabled) {
setDisableRenewalCalldata(
registerCalls.disableRenewal(
callDataEncodedDomain[1].toString(),
limitPrice
)
registerCalls.disableRenewal(callDataEncodedDomain[1].toString())
);
}
}, [limitPrice, isAutoRenewalEnabled]);
}, [allowance, isAutoRenewalEnabled]);

useEffect(() => {
if (!disableRenewalData?.transaction_hash) return;
Expand Down Expand Up @@ -328,7 +325,7 @@ const IdentityActions: FunctionComponent<IdentityActionsProps> = ({
identity={identity}
// isEnabled={isAutoRenewalEnabled}
domain={identity?.domain}
limit={limitPrice}
allowance={allowance}
/>
{isTxSent ? (
<ConfirmationTx
Expand Down
4 changes: 2 additions & 2 deletions utils/registerCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ function enableRenewal(
];
}

function disableRenewal(encodedDomain: string, price: string): Call[] {
function disableRenewal(encodedDomain: string): Call[] {
return [
{
contractAddress: process.env.NEXT_PUBLIC_RENEWAL_CONTRACT as string,
entrypoint: "disable_renewals",
calldata: [encodedDomain.toString(), price, 0],
calldata: [encodedDomain.toString()],
},
];
}
Expand Down

0 comments on commit 9783924

Please sign in to comment.