Skip to content

Commit

Permalink
refactor: extract calldata to identityActions
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Dec 14, 2023
1 parent 1a1eb7b commit bf4be29
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 78 deletions.
36 changes: 8 additions & 28 deletions components/identities/actions/changeAddressModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ConfirmationTx from "../../UI/confirmationTx";
import { useNotificationManager } from "../../../hooks/useNotificationManager";
import { NotificationType, TransactionType } from "../../../utils/constants";
import { Identity } from "../../../utils/apiObjects";
import { STARKNET } from "../../../utils/verifierFields";
import { setStarknetAddress } from "../../../utils/callData/identityChangeCalls";

type ChangeAddressModalProps = {
handleClose: () => void;
Expand All @@ -31,35 +31,15 @@ const ChangeAddressModal: FunctionComponent<ChangeAddressModalProps> = ({
const { addTransaction } = useNotificationManager();
const [isTxSent, setIsTxSent] = useState(false);

//set_domain_to_address execute
const set_domain_to_address_call = {
contractAddress: process.env.NEXT_PUBLIC_STARKNETID_CONTRACT as string,
entrypoint: "set_user_data",
calldata: [
identity?.id as string,
STARKNET,
hexToDecimal(targetAddress),
0,
],
};

const legacy_address = identity?.data.domain?.legacy_address;
const { writeAsync: set_domain_to_address, data: domainToAddressData } =
useContractWrite({
calls:
Boolean(legacy_address) &&
legacy_address !=
"0x0000000000000000000000000000000000000000000000000000000000000000"
? [
{
contractAddress: process.env
.NEXT_PUBLIC_NAMING_CONTRACT as string,
entrypoint: "clear_legacy_domain_to_address",
calldata: [...callDataEncodedDomain],
},
set_domain_to_address_call,
]
: [set_domain_to_address_call],
calls: identity
? setStarknetAddress(
identity,
hexToDecimal(targetAddress),
callDataEncodedDomain
)
: [],
});

useEffect(() => {
Expand Down
56 changes: 6 additions & 50 deletions components/identities/actions/identityActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useAccount, useContractWrite } from "@starknet-react/core";
import ChangeAddressModal from "./changeAddressModal";
import TransferFormModal from "./transferFormModal";
import SubdomainModal from "./subdomainModal";
import { hexToDecimal, stringToHex } from "../../../utils/feltService";
import { hexToDecimal } from "../../../utils/feltService";
import ClickableAction from "../../UI/iconsComponents/clickableAction";
import styles from "../../../styles/components/identityMenu.module.css";
import { timestampToReadableDate } from "../../../utils/dateService";
Expand All @@ -27,6 +27,7 @@ import { posthog } from "posthog-js";
import { Identity } from "../../../utils/apiObjects";
import { formatHexString } from "../../../utils/stringService";
import { STARKNET } from "../../../utils/verifierFields";
import { setAsMainId } from "../../../utils/callData/identityChangeCalls";

type IdentityActionsProps = {
identity?: Identity;
Expand Down Expand Up @@ -88,53 +89,8 @@ const IdentityActions: FunctionComponent<IdentityActionsProps> = ({
callDataEncodedDomain.push(domain.toString(10));
});

const resetAddrToDomain = {
contractAddress: process.env.NEXT_PUBLIC_NAMING_CONTRACT as string,
entrypoint: "reset_address_to_domain",
calldata: [],
};

const resetLegacyDomainToAddr = {
contractAddress: process.env.NEXT_PUBLIC_NAMING_CONTRACT as string,
entrypoint: "clear_legacy_domain_to_address",
calldata: callDataEncodedDomain,
};

const resetDomainToAddr = {
contractAddress: process.env.NEXT_PUBLIC_STARKNETID_CONTRACT as string,
entrypoint: "set_user_data",
calldata: [identity?.id as string, STARKNET, 0, 0],
};

const set_main_id = {
contractAddress: process.env.NEXT_PUBLIC_STARKNETID_CONTRACT as string,
entrypoint: "set_main_id",
calldata: [identity?.id as string],
};

const targetAddress = identity?.targetAddress;
const legacyAddress = identity?.data.domain?.legacy_address;
const { writeAsync: setMainId, data: mainDomainData } = useContractWrite({
calls: (() => {
const basis: {
contractAddress: string;
entrypoint: string;
calldata: (string | number)[];
}[] = hasRev ? [resetAddrToDomain] : [];
if (
Boolean(targetAddress) &&
targetAddress !== formatHexString(address ? address : "")
) {
if (Boolean(legacyAddress) && legacyAddress != "0x000") {
basis.push(resetLegacyDomainToAddr);
} else {
basis.push(resetDomainToAddr);
}
}
basis.push(set_main_id);
basis.push();
return basis;
})(),
calls: identity ? setAsMainId(identity, hasRev, callDataEncodedDomain) : [],
});

useEffect(() => {
Expand All @@ -151,9 +107,9 @@ const IdentityActions: FunctionComponent<IdentityActionsProps> = ({
fetch(
`${
process.env.NEXT_PUBLIC_SERVER_LINK
}/renewal/get_renewal_data?addr=${hexToDecimal(
address
)}&domain=${identity.domain}`
}/renewal/get_renewal_data?addr=${hexToDecimal(address)}&domain=${
identity.domain
}`
)
.then((response) => response.json())
.then((data) => {
Expand Down
68 changes: 68 additions & 0 deletions utils/callData/identityChangeCalls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@
import { Call } from "starknet";
import { stringToHex } from "../feltService";
import { Identity } from "../apiObjects";
import { STARKNET } from "../verifierFields";

export function setStarknetAddress(
identity: Identity,
address: string,
callDataEncodedDomain: (number | string)[] = []
): Call[] {
const domain = identity.data.domain;
const output = [];
// if that id was linked to a domain with a legacy address before, we remove it
if (
domain !== undefined &&
domain.legacy_address !=
"0x0000000000000000000000000000000000000000000000000000000000000000"
) {
output.push({
contractAddress: process.env.NEXT_PUBLIC_NAMING_CONTRACT as string,
entrypoint: "clear_legacy_domain_to_address",
calldata: [...callDataEncodedDomain],
});
}

output.push(setUserData(identity.id, STARKNET, address));
return output;
}

export function setUserData(
tokenId: string,
field: string,
data: string
): Call {
return {
contractAddress: process.env.NEXT_PUBLIC_STARKNETID_CONTRACT as string,
entrypoint: "set_user_data",
calldata: [tokenId, field, data, 0],
};
}

export function setAsMainId(
identity: Identity,
hasRev: boolean,
callDataEncodedDomain: (number | string)[] = []
): Call[] {
const output = [];
// reset reverse address if set
if (hasRev) {
output.push({
contractAddress: process.env.NEXT_PUBLIC_NAMING_CONTRACT as string,
entrypoint: "reset_address_to_domain",
calldata: [],
});
}
// reset target address if not compatible
if (
Boolean(identity.targetAddress) &&
identity.targetAddress !== identity.ownerAddress
) {
output.push(...setStarknetAddress(identity, "0", callDataEncodedDomain));
}
// set as main id
output.push({
contractAddress: process.env.NEXT_PUBLIC_STARKNETID_CONTRACT as string,
entrypoint: "set_main_id",
calldata: [identity.id as string],
});
return output;
}

function writeVerifierData(
contractAddress: string,
Expand Down

0 comments on commit bf4be29

Please sign in to comment.