-
-
Save dabit3/d73a69da9c05cc31ecabfcfe68effc2d to your computer and use it in GitHub Desktop.
NFT Marketplace V2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* pages/resell-nft.js */ | |
import { useEffect, useState } from 'react' | |
import { ethers } from 'ethers' | |
import { useRouter } from 'next/router' | |
import axios from 'axios' | |
import Web3Modal from 'web3modal' | |
import { | |
marketplaceAddress | |
} from '../config' | |
import NFTMarketplace from '../artifacts/contracts/NFTMarketplace.sol/NFTMarketplace.json' | |
export default function ResellNFT() { | |
const [formInput, updateFormInput] = useState({ price: '', image: '' }) | |
const router = useRouter() | |
const { id, tokenURI } = router.query | |
const { image, price } = formInput | |
useEffect(() => { | |
fetchNFT() | |
}, [id]) | |
async function fetchNFT() { | |
if (!tokenURI) return | |
const meta = await axios.get(tokenURI) | |
updateFormInput(state => ({ ...state, image: meta.data.image })) | |
} | |
async function listNFTForSale() { | |
if (!price) return | |
const web3Modal = new Web3Modal() | |
const connection = await web3Modal.connect() | |
const provider = new ethers.providers.Web3Provider(connection) | |
const signer = provider.getSigner() | |
const priceFormatted = ethers.utils.parseUnits(formInput.price, 'ether') | |
let contract = new ethers.Contract(marketplaceAddress, NFTMarketplace.abi, signer) | |
let listingPrice = await contract.getListingPrice() | |
listingPrice = listingPrice.toString() | |
let transaction = await contract.resellToken(id, priceFormatted, { 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 Price in Eth" | |
className="mt-2 border rounded p-4" | |
onChange={e => updateFormInput({ ...formInput, price: e.target.value })} | |
/> | |
{ | |
image && ( | |
<img className="rounded mt-4" width="350" src={image} /> | |
) | |
} | |
<button onClick={listNFTForSale} className="font-bold mt-4 bg-pink-500 text-white rounded p-4 shadow-lg"> | |
List NFT | |
</button> | |
</div> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment