-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.tsx
185 lines (165 loc) · 6.03 KB
/
create.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import {
useMarketplace,
useNetwork,
useNetworkMismatch,
} from "@thirdweb-dev/react";
import { NATIVE_TOKEN_ADDRESS, TransactionResult } from "@thirdweb-dev/sdk";
import type { NextPage } from "next";
import { useRouter } from "next/router";
import styles from "../styles/Home.module.css";
const Create: NextPage = () => {
// Next JS Router hook to redirect to other pages
const router = useRouter();
const networkMismatch = useNetworkMismatch();
const [, switchNetwork] = useNetwork();
// Connect to our marketplace contract via the useMarketplace hook
const marketplace = useMarketplace(
"0x277C0FB19FeD09c785448B8d3a80a78e7A9B8952" // Your marketplace contract address here
);
// This function gets called when the form is submitted.
async function handleCreateListing(e: any) {
try {
// Ensure user is on the correct network
if (networkMismatch) {
switchNetwork && switchNetwork(4);
return;
}
// Prevent page from refreshing
e.preventDefault();
// Store the result of either the direct listing creation or the auction listing creation
let transactionResult: undefined | TransactionResult = undefined;
// De-construct data from form submission
const { listingType, contractAddress, tokenId, price } =
e.target.elements;
// Depending on the type of listing selected, call the appropriate function
// For Direct Listings:
if (listingType.value === "directListing") {
transactionResult = await createDirectListing(
contractAddress.value,
tokenId.value,
price.value
);
}
// For Auction Listings:
if (listingType.value === "auctionListing") {
transactionResult = await createAuctionListing(
contractAddress.value,
tokenId.value,
price.value
);
}
// If the transaction succeeds, take the user back to the homepage to view their listing!
if (transactionResult) {
router.push(`/`);
}
} catch (error) {
console.error(error);
}
}
async function createAuctionListing(
contractAddress: string,
tokenId: string,
price: string
) {
try {
const transaction = await marketplace?.auction.createListing({
assetContractAddress: contractAddress, // Contract Address of the NFT
buyoutPricePerToken: price, // Maximum price, the auction will end immediately if a user pays this price.
currencyContractAddress: NATIVE_TOKEN_ADDRESS, // NATIVE_TOKEN_ADDRESS is the crpyto curency that is native to the network. i.e. Rinkeby ETH.
listingDurationInSeconds: 60 * 60 * 24 * 7, // When the auction will be closed and no longer accept bids (1 Week)
quantity: 1, // How many of the NFTs are being listed (useful for ERC 1155 tokens)
reservePricePerToken: 0, // Minimum price, users cannot bid below this amount
startTimestamp: new Date(), // When the listing will start
tokenId: tokenId, // Token ID of the NFT.
});
return transaction;
} catch (error) {
console.error(error);
}
}
async function createDirectListing(
contractAddress: string,
tokenId: string,
price: string
) {
try {
const transaction = await marketplace?.direct.createListing({
assetContractAddress: contractAddress, // Contract Address of the NFT
buyoutPricePerToken: price, // Maximum price, the auction will end immediately if a user pays this price.
currencyContractAddress: NATIVE_TOKEN_ADDRESS, // NATIVE_TOKEN_ADDRESS is the crpyto curency that is native to the network. i.e. Rinkeby ETH.
listingDurationInSeconds: 60 * 60 * 24 * 7, // When the auction will be closed and no longer accept bids (1 Week)
quantity: 1, // How many of the NFTs are being listed (useful for ERC 1155 tokens)
startTimestamp: new Date(0), // When the listing will start
tokenId: tokenId, // Token ID of the NFT.
});
return transaction;
} catch (error) {
console.error(error);
}
}
return (
<form onSubmit={(e) => handleCreateListing(e)}>
<div className={styles.container}>
{/* Form Section */}
<div className={styles.collectionContainer}>
<h1 className={styles.ourCollection}>
Upload your NFT to the marketplace:
</h1>
{/* Toggle between direct listing and auction listing */}
<div className={styles.listingTypeContainer}>
<input
type="radio"
name="listingType"
id="directListing"
value="directListing"
defaultChecked
className={styles.listingType}
/>
<label htmlFor="directListing" className={styles.listingTypeLabel}>
Direct Listing
</label>
<input
type="radio"
name="listingType"
id="auctionListing"
value="auctionListing"
className={styles.listingType}
/>
<label htmlFor="auctionListing" className={styles.listingTypeLabel}>
Auction Listing
</label>
</div>
{/* NFT Contract Address Field */}
<input
type="text"
name="contractAddress"
className={styles.textInput}
placeholder="NFT Contract Address"
/>
{/* NFT Token ID Field */}
<input
type="text"
name="tokenId"
className={styles.textInput}
placeholder="NFT Token ID"
/>
{/* Sale Price For Listing Field */}
<input
type="text"
name="price"
className={styles.textInput}
placeholder="Sale Price"
/>
<button
type="submit"
className={styles.mainButton}
style={{ marginTop: 32, borderStyle: "none" }}
>
List NFT
</button>
</div>
</div>
</form>
);
};
export default Create;