-
Notifications
You must be signed in to change notification settings - Fork 0
/
nft-marketplace.js
113 lines (105 loc) · 3.2 KB
/
nft-marketplace.js
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
const accountId = props.accountId || context.accountId;
const contractId =
props.contractId || context.contractId || "mrbrownproject.near";
const marketId = "simple.market.mintbase1.near";
const AFFILIATE_ACCOUNT = props.affiliateAccount || "microchipgnu.near";
const data = fetch("https://graph.mintbase.xyz", {
method: "POST",
headers: {
"mb-api-key": "omni-site",
"Content-Type": "application/json",
"x-hasura-role": "anonymous",
},
body: JSON.stringify({
query: `
query MyQuery {
mb_views_active_listings_by_contract(limit: 100, order_by: {created_at: desc}, where: {market_id: {_eq: "simple.market.mintbase1.near"}, nft_contract_id: {_eq: "${contractId}"}}) {
listed_by
created_at
price
nft_contract_id
token_id
metadata_id
}
}
`,
}),
});
const YoctoToNear = (amountYocto) =>
new Big(amountYocto).div(new Big(10).pow(24)).toString();
let buy = (price, token_id, nft_contract_id) => {
const gas = 200000000000000;
const deposit = new Big(price).toFixed(0);
Near.call(
marketId,
"buy",
{
nft_contract_id: nft_contract_id,
token_id: token_id,
referrer_id: AFFILIATE_ACCOUNT,
},
gas,
deposit
);
};
if (!data.ok) {
return "Loading";
}
const size = "10em";
return data !== null ? (
<>
<h1>My Market</h1>
<p>Buying from this widget will redirect 1.25% of the sale for me.</p>
<div className="d-flex gap-4 flex-wrap">
{data.body.data?.mb_views_active_listings_by_contract.map(
(listing, i) => {
const priceYocto = listing.price.toLocaleString().replace(/,/g, "");
const priceNear = YoctoToNear(priceYocto);
return (
<div className="d-flex flex-column gap-1">
<a
href={`https://mintbase.xyz/meta/${listing.metadata_id}/`}
target="_blank"
>
<Widget
src="mob.near/widget/NftImage"
props={{
nft: {
tokenId: listing.token_id,
contractId: listing.nft_contract_id,
},
style: {
width: size,
height: size,
objectFit: "cover",
minWidth: size,
minHeight: size,
maxWidth: size,
maxHeight: size,
overflowWrap: "break-word",
},
thumbnail: "thumbnail",
className: "",
fallbackUrl:
"https://ipfs.near.social/ipfs/bafkreihdiy3ec4epkkx7wc4wevssruen6b7f3oep5ylicnpnyyqzayvcry",
}}
/>
</a>
<button
disabled={!accountId}
onClick={() => {
if (!accountId) return;
buy(priceYocto, listing.token_id, listing.nft_contract_id);
}}
>
Buy for {priceNear} N
</button>
</div>
);
}
)}
</div>
</>
) : (
<p>loading...</p>
);