Skip to content

Commit

Permalink
fix transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
starknetdev committed Sep 10, 2024
1 parent 08a4030 commit 574dfd3
Showing 1 changed file with 112 additions and 103 deletions.
215 changes: 112 additions & 103 deletions ui/src/app/components/start/AdventurerListCard.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { useState } from "react";
import { useEffect, useState } from "react";
import { Button } from "@/app/components/buttons/Button";
import { Adventurer } from "@/app/types";
import Info from "@/app/components/adventurer/Info";
import {
AccountInterface,
Contract,
// validateAndParseAddress,
// constants,
validateAndParseAddress,
constants,
} from "starknet";
import useTransactionCartStore from "@/app/hooks/useTransactionCartStore";
import { useAccount } from "@starknet-react/core";
import { useAccount, useProvider } from "@starknet-react/core";
import useAdventurerStore from "@/app/hooks/useAdventurerStore";
// import { padAddress } from "@/app/lib/utils";
// import { StarknetIdNavigator } from "starknetid.js";
import { padAddress } from "@/app/lib/utils";
import { StarknetIdNavigator } from "starknetid.js";
import { CartridgeIcon, StarknetIdIcon } from "../icons/Icons";

export interface AdventurerListCardProps {
Expand All @@ -33,76 +33,86 @@ export const AdventurerListCard = ({
handleSwitchAdventurer,
transferAdventurer,
}: AdventurerListCardProps) => {
const { address } = useAccount();
// const { provider } = useProvider();
// const starknetIdNavigator = new StarknetIdNavigator(
// provider,
// constants.StarknetChainId.SN_MAIN
// );
const { account, address } = useAccount();
const { provider } = useProvider();
const starknetIdNavigator = new StarknetIdNavigator(
provider,
constants.StarknetChainId.SN_MAIN
);
const [isTransferOpen, setIsTransferOpen] = useState(false);
const [transferAddress, setTransferAddress] = useState("");
// const [validAddress, setValidAddress] = useState<string | false>(false);
// const [subdomain, setSubdomain] = useState(".ctrl");
// const [resolvedAddresses, setResolvedAddresses] = useState<{
// ctrl: string | null;
// starknetId: string | null;
// }>({
// ctrl: null,
// starknetId: null,
// });
const [validAddress, setValidAddress] = useState<string | false>(false);
const [subdomain, setSubdomain] = useState(".ctrl");
const [resolvedAddresses, setResolvedAddresses] = useState<{
ctrl: string | null;
starknetId: string | null;
}>({
ctrl: null,
starknetId: null,
});

const setAdventurer = useAdventurerStore((state) => state.setAdventurer);

// useEffect(() => {
// const validateAddress = async () => {
// try {
// // Try to resolve Starknet ID with .ctrl
// const ctrlId = transferAddress + ".ctrl.stark";
// const ctrlAddress = await starknetIdNavigator.getAddressFromStarkName(
// ctrlId
// );
useEffect(() => {
const validateAddress = async () => {
let ctrlAddress = null;
let starknetIdAddress = null;

// Try to resolve Starknet ID with .ctrl
try {
const ctrlId = transferAddress + ".ctrl.stark";
ctrlAddress = await starknetIdNavigator.getAddressFromStarkName(ctrlId);
} catch (e) {
console.log("Failed to resolve .ctrl address:", e);
}

// // Try to resolve Starknet ID without subdomain
// const starknetId = transferAddress + ".stark";
// const starknetIdAddress =
// await starknetIdNavigator.getAddressFromStarkName(starknetId);
// Try to resolve Starknet ID without subdomain
try {
const starknetId = transferAddress + ".stark";
starknetIdAddress = await starknetIdNavigator.getAddressFromStarkName(
starknetId
);
} catch (e) {
console.log("Failed to resolve .stark address:", e);
}

// setResolvedAddresses({
// ctrl: ctrlAddress || null,
// starknetId: starknetIdAddress || null,
// });
setResolvedAddresses({
ctrl: ctrlAddress,
starknetId: starknetIdAddress,
});

// // Set validAddress based on the current subdomain
// if (
// subdomain === ".ctrl" &&
// ctrlAddress &&
// !transferAddress.startsWith("0x")
// ) {
// setValidAddress(ctrlAddress);
// } else if (
// subdomain === "" &&
// starknetIdAddress &&
// !transferAddress.startsWith("0x")
// ) {
// setValidAddress(starknetIdAddress);
// } else {
// // If not a Starknet ID, validate as a regular address
// const paddedAddress = padAddress(transferAddress.toLowerCase());
// if (paddedAddress.length === 66 && transferAddress.startsWith("0x")) {
// const parsedAddress = validateAndParseAddress(paddedAddress);
// setValidAddress(parsedAddress);
// } else {
// setValidAddress(false);
// }
// }
// } catch {
// setValidAddress(false);
// setResolvedAddresses({ ctrl: null, starknetId: null });
// }
// };
// Set validAddress based on the current subdomain
if (
subdomain === ".ctrl" &&
ctrlAddress &&
!transferAddress.startsWith("0x")
) {
setValidAddress(ctrlAddress);
} else if (
subdomain === "" &&
starknetIdAddress &&
!transferAddress.startsWith("0x")
) {
setValidAddress(starknetIdAddress);
} else {
// If not a Starknet ID, validate as a regular address
try {
const paddedAddress = padAddress(transferAddress.toLowerCase());
if (paddedAddress.length === 66 && transferAddress.startsWith("0x")) {
const parsedAddress = validateAndParseAddress(paddedAddress);
setValidAddress(parsedAddress);
} else {
setValidAddress(false);
}
} catch (e) {
console.log("Failed to validate address:", e);
setValidAddress(false);
}
}
};

// validateAddress();
// }, [transferAddress, subdomain, starknetIdNavigator]);
validateAddress();
}, [transferAddress, subdomain]);

const addToCalls = useTransactionCartStore((state) => state.addToCalls);

Expand Down Expand Up @@ -135,7 +145,6 @@ export const AdventurerListCard = ({
variant={"token"}
onClick={() => setIsTransferOpen(!isTransferOpen)}
className={`w-1/2 ${isTransferOpen && "animate-pulse"}`}
disabled={true}
>
Transfer
</Button>
Expand All @@ -149,32 +158,32 @@ export const AdventurerListCard = ({
</span>
<span className="flex flex-row w-1/4 justify-end gap-2">
<Button
// onClick={() => setSubdomain(".ctrl")}
// variant={
// subdomain === ".ctrl" || resolvedAddresses.ctrl
// ? "default"
// : "ghost"
// }
// className={
// subdomain !== ".ctrl" && resolvedAddresses.ctrl
// ? "animate-pulse"
// : ""
// }
onClick={() => setSubdomain(".ctrl")}
variant={
subdomain === ".ctrl" || resolvedAddresses.ctrl
? "default"
: "ghost"
}
className={
subdomain !== ".ctrl" && resolvedAddresses.ctrl
? "animate-pulse"
: ""
}
>
<CartridgeIcon className="w-6 h-6" />
</Button>
<Button
// onClick={() => setSubdomain("")}
// variant={
// subdomain === "" || resolvedAddresses.starknetId
// ? "default"
// : "ghost"
// }
// className={
// subdomain !== "" && resolvedAddresses.starknetId
// ? "animate-pulse"
// : ""
// }
onClick={() => setSubdomain("")}
variant={
subdomain === "" || resolvedAddresses.starknetId
? "default"
: "ghost"
}
className={
subdomain !== "" && resolvedAddresses.starknetId
? "animate-pulse"
: ""
}
>
<StarknetIdIcon className="w-6 h-6" />
</Button>
Expand All @@ -190,24 +199,24 @@ export const AdventurerListCard = ({
}}
className="p-1 h-12 text-2xl w-3/4 bg-terminal-black border border-terminal-green animate-pulse transform uppercase"
/>
{/* {transferAddress && !validAddress && (
{transferAddress && !validAddress && (
<p className="absolute bottom-15 text-terminal-yellow">
INVALID ADDRESS!
</p>
)} */}
)}
<div className="flex flex-row gap-2 items-center">
<Button
size={"lg"}
// onClick={() => {
// transferAdventurer(
// account!,
// adventurer?.id!,
// address!,
// validAddress as string
// );
// setIsTransferOpen(false);
// }}
// disabled={!validAddress}
onClick={() => {
transferAdventurer(
account!,
adventurer?.id!,
address!,
validAddress as string
);
setIsTransferOpen(false);
}}
disabled={!validAddress}
>
Send
</Button>
Expand All @@ -218,7 +227,7 @@ export const AdventurerListCard = ({
handleAddTransferTx(transferAddress);
setIsTransferOpen(false);
}}
// disabled={!validAddress}
disabled={!validAddress}
>
Add to Cart
</Button>
Expand Down

0 comments on commit 574dfd3

Please sign in to comment.