forked from davepartner/opensea-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-item.js
148 lines (127 loc) · 5.29 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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';
import NFT from '../artifacts/contracts/NFT.sol/NFT.json';
import Market from '../artifacts/contracts/NFTMarket.sol/NFTMarket.json';
import { EtherscanProvider } from '@ethersproject/providers'
import Image from 'next/Image'
export default function CreateItem() {
const [fileUrl, setFileUrl] = useState(null)
const [formInput, updateFormInput] = useState({price: '', name: '', description:''})
const router = useRouter();
async function onChange(e) {
const file = e.target.files[0]
try{ //try uploading the file
const added = await client.add(
file,
{
progress: (prog) => console.log(`received: ${prog}`)
}
)
//file saved in the url path below
const url = `https://ipfs.infura.io/ipfs/${added.path}`
setFileUrl(url)
}catch(e){
console.log('Error uploading file: ', e)
}
}
//1. create item (image/video) and upload to ipfs
async function createItem(){
const {name, description, price} = formInput; //get the value from the form input
//form validation
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}`
//pass the url to sav eit on Polygon adter it has been uploaded to IPFS
createSale(url)
}catch(error){
console.log(`Error uploading file: `, error)
}
}
//2. List item for sale
async function createSale(url){
const web3Modal = new Web3Modal();
const connection = await web3Modal.connect();
const provider = new ethers.providers.Web3Provider(connection);
//sign the transaction
const signer = provider.getSigner();
let contract = new ethers.Contract(nftaddress, NFT.abi, signer);
let transaction = await contract.createToken(url);
let tx = await transaction.wait()
//get the tokenId from the transaction that occured above
//there events array that is returned, the first item from that event
//is the event, third item is the token id.
console.log('Transaction: ',tx)
console.log('Transaction events: ',tx.events[0])
let event = tx.events[0]
let value = event.args[2]
let tokenId = value.toNumber() //we need to convert it a number
//get a reference to the price entered in the form
const price = ethers.utils.parseUnits(formInput.price, 'ether')
contract = new ethers.Contract(nftmarketaddress, Market.abi, signer);
//get the listing price
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-8 border rounded p-4"
type="number"
onChange={e => updateFormInput({...formInput, price: e.target.value})}
/>
<input
type="file"
name="Asset"
className="my-4"
onChange={onChange}
/>
{
fileUrl && (
<Image
src={fileUrl}
alt="Picture of the author"
className="rounded mt-4"
width={350}
height={500}
// blurDataURL="data:..." automatically provided
// placeholder="blur" // Optional blur-up while loading
/>
)
}
<button onClick={createItem}
className="font-bold mt-4 bg-pink-500 text-white rounded p-4 shadow-lg"
>Create NFT</button>
</div>
</div>
)
}