forked from thirdweb-example/custom-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.tsx
141 lines (128 loc) · 4.24 KB
/
deploy.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, { useState } from "react";
import { ConnectWallet, useAddress, useChainId, useSDK } from "@thirdweb-dev/react";
import { ChainId, ContractType } from "@thirdweb-dev/sdk";
import styles from "../styles/Home.module.css";
import {
contractsToShowOnDeploy as contracts,
contractTypeToDisplayNameMapping as nameMapping,
contractTypeToImageMapping as imageMapping,
} from "../const/contractToDisplayMappings";
import { useRouter } from "next/router";
export default function Deploy() {
const router = useRouter();
const address = useAddress();
const sdk = useSDK();
const [isLoading, setIsLoading] = useState(false);
const [isBlurred, setIsBlurred] = useState(false);
const chainId = useChainId();
let chainName = "";
if (chainId === ChainId.Mumbai) {
chainName = "mumbai";
} else if (chainId === ChainId.Polygon) {
chainName = "polygon";
} else if (chainId === ChainId.Mainnet) {
chainName = "ethereum";
} else if (chainId === ChainId.BinanceSmartChainMainnet) {
chainName = "binance";
} else if (chainId === 8453) {
chainName = "base"
}
async function deployContract(contractSelected: ContractType) {
if (!address || !sdk) {
return;
}
try {
setIsLoading(true);
setIsBlurred(true);
const contractAddress = await sdk.deployer.deployBuiltInContract(
// @ts-ignore - we're excluding custom contracts from the demo
contractSelected,
{
name: `My ${contractSelected}`,
primary_sale_recipient: address,
voting_token_address: address,
description: `My awesome ${contractSelected} contract`,
// Recipients are required when trying to deploy a split contract
recipients: [
{
address,
sharesBps: 100 * 100,
},
],
}
);
// This is the contract address of the contract you just deployed
setIsLoading(false);
setIsBlurred(false);
alert(`Successfully deployed ${contractSelected} at ${contractAddress}`);
const newTabUrl = `https://thirdweb.com/${chainName}/${contractAddress}`;
const newTab = window.open(newTabUrl, "_blank");
if (newTab) {
newTab.focus();
}
router.push(`/`);
} catch (error) {
setIsLoading(false);
setIsBlurred(false);
console.error("Error deploying contract:", error);
// Handle error, display error message, etc.
}
}
return (
<>
{/* Content */}
<div className={`${styles.container} ${isBlurred ? styles.blurred : ""}`}>
<h1 className={styles.h1}>thirdweb Custom Dashboard</h1>
<p className={styles.explain}>
Learn how to dynamically create smart contracts using the thirdweb SDK
and view all of the contracts you've created, similar to our{" "}
<b>
<a
href="https://thirdweb.com/dashboard"
target="_blank"
rel="noopener noreferrer"
className={styles.purple}
>
thirdweb dashboard
</a>
</b>
.
</p>
<hr className={styles.divider} />
<h2>What do you want to deploy?</h2>
{!address ? (
<>
<p>
<b>Connect Your Wallet to deploy a contract</b>
</p>
<ConnectWallet />
</>
) : (
<>
<div className={styles.contractBoxGrid}>
{contracts.map((c) => (
<div
className={styles.contractBox}
key={c}
onClick={() => deployContract(c as ContractType)}
>
<div className={styles.contractImage}>
<img src={imageMapping[c as ContractType]} alt={c} />
</div>
<b className={styles.cardName}>
{nameMapping[c as ContractType]}
</b>
</div>
))}
</div>
</>
)}
</div>
{isLoading && (
<div className={styles.loadingOverlay}>
<div className={styles.spinner}></div>
</div>
)}
</>
);
}