-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-item.js
129 lines (117 loc) · 3.79 KB
/
create-item.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* eslint-disable @next/next/no-img-element */
import { useState } from "react";
import { ethers } from "ethers";
import { create as ipfsHttpClient } from "ipfs-http-client";
import { useRouter } from "next/router";
import Web3Modal from "web3modal";
const client = ipfsHttpClient("https://ipfs.infura.io:5001/api/v0");
import { nftaddress, nftmarketaddress } from "../config.js";
// import NFT from "../artifacts/contracts/nft.sol/nft.json";
// import Market from "../artifacts/contracts/nftmarket.sol/nftmarket.json";
import { abi } from "../abi/abi.js";
import { marketAbi } from "../abi/marketAbi.js";
export default function CreateItem() {
const [fileUrl, setFileUrl] = useState(null);
const [formInput, updateFormInput] = useState({
price: "",
name: "",
description: "",
});
const router = useRouter();
async function onChangeFunc(e) {
const file = e.target.files[0];
try {
const added = await client.add(file, {
progress: (prog) => console.log(`received: ${prog}`),
});
const url = `https://ipfs.infura.io/ipfs/${added.path}`;
setFileUrl(url);
} catch (error) {
console.log("Error uploading file: ", error);
}
}
async function createMarket() {
const { name, description, price } = formInput;
if (!name || !description || !price || !fileUrl) return;
const data = JSON.stringify({
name,
description,
image: fileUrl,
});
try {
const added = await client.add(data);
const url = `https://ipfs.infura.io/ipfs/${added.path}`;
createSale(url);
} catch (error) {
console.log("Error uploading file: ", error);
}
}
async function createSale(url) {
const web3Modal = new Web3Modal();
const connection = await web3Modal.connect();
const provider = new ethers.providers.Web3Provider(connection);
const signer = provider.getSigner();
let contract = new ethers.Contract(nftaddress, abi, signer);
let transaction = await contract.createToken(url);
let tx = await transaction.wait();
let event = tx.events[0];
let value = event.args[2];
let tokenId = value.toNumber();
const price = ethers.utils.parseUnits(formInput.price, "ether");
contract = new ethers.Contract(nftmarketaddress, marketAbi, signer);
let listingPrice = await contract.getListingPrice();
listingPrice = listingPrice.toString();
transaction = await contract.createMarketItem(nftaddress, tokenId, price, {
value: listingPrice,
});
await transaction.wait();
router.push("/");
}
return (
<div className="flex justify-center">
<div className="w-1/2 flex flex-col pb-12">
<input
placeholder="Asset Name"
className="mt-8 border rounded p-4"
onChange={(e) =>
updateFormInput({ ...formInput, name: e.target.value })
}
/>
<textarea
placeholder="Asset Description"
className="mt-2 border rounded p-4"
onChange={(e) =>
updateFormInput({ ...formInput, description: e.target.value })
}
/>
<input
placeholder="Asset Price in Eth"
className="mt-2 border rounded p-4"
onChange={(e) =>
updateFormInput({ ...formInput, price: e.target.value })
}
/>
<input
type="file"
name="Asset"
className="my-4"
onChange={onChangeFunc}
/>
{fileUrl && (
<img
className="rounded mt-4"
width="350"
src={fileUrl}
alt="picture"
/>
)}
<button
onClick={createMarket}
className="font-bold mt-4 bg-pink-500 text-white rounded p-4 shadow-lg"
>
Create Digital Asset
</button>
</div>
</div>
);
}