forked from thirdweb-example/marketplace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
[listingId].tsx
181 lines (162 loc) · 5.37 KB
/
[listingId].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
import {
MediaRenderer,
useNetwork,
useNetworkMismatch,
useListing,
useContract,
} from "@thirdweb-dev/react";
import {
ChainId,
ListingType,
Marketplace,
NATIVE_TOKENS,
} from "@thirdweb-dev/sdk";
import type { NextPage } from "next";
import { useRouter } from "next/router";
import { useState } from "react";
import { marketplaceContractAddress } from "../../addresses";
import styles from "../../styles/Home.module.css";
const ListingPage: NextPage = () => {
// Next JS Router hook to redirect to other pages and to grab the query from the URL (listingId)
const router = useRouter();
// De-construct listingId out of the router.query.
// This means that if the user visits /listing/0 then the listingId will be 0.
// If the user visits /listing/1 then the listingId will be 1.
const { listingId } = router.query as { listingId: string };
// Hooks to detect user is on the right network and switch them if they are not
const networkMismatch = useNetworkMismatch();
const [, switchNetwork] = useNetwork();
// Initialize the marketplace contract
const { contract: marketplace } = useContract(marketplaceContractAddress, "marketplace");
// Fetch the listing from the marketplace contract
const { data: listing, isLoading: loadingListing } = useListing(
marketplace,
listingId
);
// Store the bid amount the user entered into the bidding textbox
const [bidAmount, setBidAmount] = useState<string>("");
if (loadingListing) {
return <div className={styles.loadingOrError}>Loading...</div>;
}
if (!listing) {
return <div className={styles.loadingOrError}>Listing not found</div>;
}
async function createBidOrOffer() {
try {
// Ensure user is on the correct network
if (networkMismatch) {
switchNetwork && switchNetwork(ChainId.Goerli);
return;
}
// If the listing type is a direct listing, then we can create an offer.
if (listing?.type === ListingType.Direct) {
await marketplace?.direct.makeOffer(
listingId, // The listingId of the listing we want to make an offer for
1, // Quantity = 1
NATIVE_TOKENS[ChainId.Goerli].wrapped.address, // Wrapped Ether address on Goerli
bidAmount // The offer amount the user entered
);
}
// If the listing type is an auction listing, then we can create a bid.
if (listing?.type === ListingType.Auction) {
await marketplace?.auction.makeBid(listingId, bidAmount);
}
alert(
`${
listing?.type === ListingType.Auction ? "Bid" : "Offer"
} created successfully!`
);
} catch (error) {
console.error(error);
alert(error);
}
}
async function buyNft() {
try {
// Ensure user is on the correct network
if (networkMismatch) {
switchNetwork && switchNetwork(ChainId.Goerli);
return;
}
// Simple one-liner for buying the NFT
await marketplace?.buyoutListing(listingId, 1);
alert("NFT bought successfully!");
} catch (error) {
console.error(error);
alert(error);
}
}
return (
<div className={styles.container} style={{}}>
<div className={styles.listingContainer}>
<div className={styles.leftListing}>
<MediaRenderer
src={listing.asset.image}
className={styles.mainNftImage}
/>
</div>
<div className={styles.rightListing}>
<h1>{listing.asset.name}</h1>
<p>
Owned by{" "}
<b>
{listing.sellerAddress?.slice(0, 6) +
"..." +
listing.sellerAddress?.slice(36, 40)}
</b>
</p>
<h2>
<b>{listing.buyoutCurrencyValuePerToken.displayValue}</b>{" "}
{listing.buyoutCurrencyValuePerToken.symbol}
</h2>
<div
style={{
display: "flex",
flexDirection: "row",
gap: 20,
alignItems: "center",
}}
>
<button
style={{ borderStyle: "none" }}
className={styles.mainButton}
onClick={buyNft}
>
Buy
</button>
<p style={{ color: "grey" }}>|</p>
<div
style={{
display: "flex",
flexDirection: "row",
alignItems: "center",
gap: 8,
}}
>
<input
type="text"
name="bidAmount"
className={styles.textInput}
onChange={(e) => setBidAmount(e.target.value)}
placeholder="Amount"
style={{ marginTop: 0, marginLeft: 0, width: 128 }}
/>
<button
className={styles.mainButton}
onClick={createBidOrOffer}
style={{
borderStyle: "none",
background: "transparent",
width: "fit-content",
}}
>
Make Offer
</button>
</div>
</div>
</div>
</div>
</div>
);
};
export default ListingPage;