feat: publishing infernet-container-starter v0.2.0

This commit is contained in:
ritual-all
2024-03-29 10:50:13 -04:00
parent 41aaa152e6
commit 4545223364
155 changed files with 6086 additions and 257 deletions

View File

@ -0,0 +1,10 @@
import { ButtonHTMLAttributes, PropsWithChildren } from "react";
export const Button = (
p: PropsWithChildren<ButtonHTMLAttributes<HTMLButtonElement>>,
) => (
<button
className={"bg-emerald-700 font-light px-4 py-2 rounded-xl text-white"}
{...p}
/>
);

View File

@ -0,0 +1,15 @@
import { PropsWithChildren, useEffect, useState } from "react";
export const ClientRendered = ({ children }: PropsWithChildren) => {
// look at here:https://nextjs.org/docs/messages/react-hydration-error#solution-1-using-useeffect-to-run-on-the-client-only
const [isClient, setIsClient] = useState(false);
useEffect(() => {
setIsClient(true);
}, []);
if (!isClient) {
return null;
}
return <>{children}</>;
};

View File

@ -0,0 +1,38 @@
import { http, useAccount } from "wagmi";
import { createWalletClient, parseUnits } from "viem";
import { anvilNode } from "@/util/chain";
import { privateKeyToAccount } from "viem/accounts";
import { Button } from "@/components/Button";
export const FaucetButton = () => {
const account = useAccount();
const requestEth = async () => {
const { address: _address } = account;
if (!_address) {
console.log("No address found");
return;
}
const address = _address!;
const faucetAccount = privateKeyToAccount(
"0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6",
);
const client = createWalletClient({
account: faucetAccount,
chain: anvilNode,
transport: http(),
});
await client.sendTransaction({
to: address,
value: parseUnits("1", 18),
});
};
return (
<Button onClick={requestEth}>
{account ? "Request 1 ETH" : "Connect Your Wallet"}
</Button>
);
};

View File

@ -0,0 +1,69 @@
import { useEffect, useState } from "react";
export const LoadImg = ({ url, tokenId }: { url: string; tokenId: number }) => {
const [loaded, setLoaded] = useState(false);
const [attempts, setAttempts] = useState(0);
useEffect(() => {
if (loaded) {
return;
}
let img = new Image();
const loadImg = () => {
console.log(`trying: ${attempts}`);
img = new Image();
img.src = url;
img.onload = () => {
setLoaded(true);
};
img.onerror = () => {
if (attempts < 100) {
// Set a max number of attempts
setTimeout(() => {
setAttempts((prev) => prev + 1);
loadImg(); // Retry loading the image
}, 1000); // Retry after 1 seconds
}
};
};
if (!loaded) {
loadImg();
}
// Cleanup function to avoid memory leaks
return () => {
img.onload = null;
img.onerror = null;
};
}, [url, loaded, attempts]);
return (
<div
className={
"bg-teal-600 bg-opacity-20 rounded-lg flex flex-1 justify-center items-center"
}
>
{loaded ? (
<img className={"rounded-lg"} src={url} alt={`NFT ${tokenId}`} />
) : (
<div className={""}>
<button
type="button"
className="py-3 px-4 inline-flex items-center gap-x-2 text-sm rounded-lg
border border-transparent bg-emerald-700 text-white hover:bg-blue-700 disabled:opacity-50
disabled:pointer-events-none dark:focus:outline-none dark:focus:ring-1 dark:focus:ring-gray-600"
>
<span
className="animate-spin inline-block size-4 border-[2px] border-current border-t-transparent text-white
rounded-full"
role="status"
aria-label="loading"
></span>
Fetching from Arweave
</button>{" "}
</div>
)}
</div>
);
};

View File

@ -0,0 +1,28 @@
import { useAccount, useWriteContract } from "wagmi";
import { nftAbi } from "@/util/nftAbi";
import { NFT_ADDRESS } from "@/util/constants";
import {Button} from "@/components/Button";
export const MintButton = ({ prompt }: { prompt: string }) => {
const { address } = useAccount();
const { writeContract } = useWriteContract();
return (
<Button
onClick={() => {
if (!address) {
return;
}
writeContract({
chainId: 31337,
abi: nftAbi,
address: NFT_ADDRESS,
functionName: "mint",
args: [prompt, address],
});
}}
>
<span className={"text-xl"}>Generate NFT</span>
</Button>
);
};

View File

@ -0,0 +1,24 @@
import { useAccount, useReadContract } from "wagmi";
import { nftAbi } from "@/util/nftAbi";
import { NFT_ADDRESS } from "@/util/constants";
const NFTBalance = () => {
const { address } = useAccount();
const readContract = useReadContract({
address: NFT_ADDRESS,
account: address,
abi: nftAbi,
query: {
enabled: Boolean(address),
refetchInterval: 1000,
},
functionName: "counter",
});
if (!readContract.data) {
return <>loading...</>;
}
return <>your nft balance: {readContract.data.toString()}</>;
};

View File

@ -0,0 +1,56 @@
import { useAccount, useReadContract } from "wagmi";
import { NFT_ADDRESS } from "@/util/constants";
import { nftAbi } from "@/util/nftAbi";
import { NftImage } from "@/components/NftImage";
export const NftCollection = () => {
const { address } = useAccount();
const readContract = useReadContract({
address: NFT_ADDRESS,
account: address,
abi: nftAbi,
query: {
enabled: Boolean(address),
refetchInterval: 1000,
},
functionName: "counter",
});
if (readContract.data === 0n) {
return <>No NFTs</>;
}
console.log("read contract data", readContract.data);
if (!readContract.data) {
return <>Please connect your wallet.</>;
}
const counter = parseInt(readContract.data.toString());
const nftIds = new Array(counter).fill(0n).map((_, index) => index + 1);
console.log(`counter: ${counter}`);
return (
<div
className={
"bg-emerald-700 bg-opacity-10 p-3 flex-1 flex flex-col w-[100%]"
}
>
<h2 className={"text-2xl ml-2 my-3"}>The Collection</h2>
{nftIds.length === 0 ? (
<div className={"justify-center flex mt-20 text-opacity-80"}>
No NFTs minted.
</div>
) : (
<div className={"flex flex-wrap"}>
{nftIds.map((id) => {
return (
<NftImage key={id} tokenId={id} contractAddress={NFT_ADDRESS} />
);
})}
</div>
)}
</div>
);
};

View File

@ -0,0 +1,46 @@
import { Address } from "viem";
import { useAccount, useReadContract } from "wagmi";
import { nftAbi } from "@/util/nftAbi";
import { LoadImg } from "@/components/LoadImg";
export const NftImage = ({
tokenId,
contractAddress,
}: {
tokenId: number;
contractAddress: Address;
}) => {
const { address } = useAccount();
console.log(
"tokenid",
tokenId,
"contractAddress",
contractAddress,
"address",
address,
);
const { data } = useReadContract({
address: contractAddress,
abi: nftAbi,
account: address,
functionName: "tokenURI",
query: {
enabled: Boolean(address),
refetchInterval: 1000,
},
args: [BigInt(tokenId)],
});
console.log("nft image data", data);
if (!data) {
return <>loading...</>;
}
return (
<div className={"p-2 w-[100%] md:w-1/2 lg:w-1/3 flex"}>
<LoadImg url={data} tokenId={tokenId} />
</div>
);
};