-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContractDemo.tsx
64 lines (54 loc) · 2.19 KB
/
ContractDemo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import Image from 'next/image';
import { baseSepolia } from 'viem/chains';
import { useAccount, useContractWrite, useNetwork, usePrepareContractWrite } from 'wagmi';
import { useCollectionMetadata } from '../../../onchainKit';
import { useCustom1155Contract } from '../../hooks/contracts';
import NotConnected from './NotConnected';
import SwitchNetwork from './SwitchNetwork';
const EXPECTED_CHAIN = baseSepolia;
export default function MintContractDemo() {
const { address } = useAccount();
const { chain } = useNetwork();
const contract = useCustom1155Contract();
const onCorrectNetwork = chain?.id === EXPECTED_CHAIN.id;
const { collectionName, description, imageAddress, isLoading } = useCollectionMetadata(
onCorrectNetwork,
contract.status === 'ready' ? contract.address : undefined,
contract.abi,
);
const { config } = usePrepareContractWrite({
address: contract.status === 'ready' ? contract.address : undefined,
abi: contract.abi,
functionName: 'mint',
args: address ? [address, BigInt(1), BigInt(1), address] : undefined,
enabled: onCorrectNetwork,
});
// A future enhancement would be to use the `isLoading` and `isSuccess`
// properties returned by `useContractWrite` to indicate transaction
// status in the UI.
const { write: mint } = useContractWrite(config);
if (contract.status === 'notConnected') {
return <NotConnected />;
}
if (contract.status === 'onUnsupportedNetwork') {
return <SwitchNetwork />;
}
if (isLoading) {
// A future enhancement would be a nicer spinner here.
return <span className="text-xl">loading...</span>;
}
return (
<div className="mt-10 grid grid-cols-1 items-stretch justify-start md:grid-cols-2mint md:gap-9">
<div className="align-center flex flex-col justify-start gap-5">
<Image src={imageAddress} alt={collectionName} width="300" height="300" />
</div>
<div className="align-center flex flex-col justify-start gap-5">
<p className="mb-1 text-xl font-bold"> {collectionName}</p>
<p className="text-sm">{description}</p>
<button type="button" onClick={mint}>
Mint for free (requires gas)
</button>
</div>
</div>
);
}