Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starter code #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .vscode/launch.json
Empty file.
123 changes: 1 addition & 122 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,106 +17,10 @@ import config from './config.json';
function App() {
const [provider, setProvider] = useState(null)
const [account, setAccount] = useState(null)
const [nft, setNFT] = useState(null)

const [name, setName] = useState("")
const [description, setDescription] = useState("")
const [image, setImage] = useState(null)
const [url, setURL] = useState(null)

const [message, setMessage] = useState("")
const [isWaiting, setIsWaiting] = useState(false)

const loadBlockchainData = async () => {
const provider = new ethers.providers.Web3Provider(window.ethereum)
setProvider(provider)

const network = await provider.getNetwork()

const nft = new ethers.Contract(config[network.chainId].nft.address, NFT, provider)
setNFT(nft)
}

const submitHandler = async (e) => {
e.preventDefault()

if (name === "" || description === "") {
window.alert("Please provide a name and description")
return
}

setIsWaiting(true)

// Call AI API to generate a image based on description
const imageData = await createImage()

// Upload image to IPFS (NFT.Storage)
const url = await uploadImage(imageData)

// Mint NFT
await mintImage(url)

setIsWaiting(false)
setMessage("")
}

const createImage = async () => {
setMessage("Generating Image...")

// You can replace this with different model API's
const URL = `https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2`

// Send the request
const response = await axios({
url: URL,
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.REACT_APP_HUGGING_FACE_API_KEY}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
data: JSON.stringify({
inputs: description, options: { wait_for_model: true },
}),
responseType: 'arraybuffer',
})

const type = response.headers['content-type']
const data = response.data

const base64data = Buffer.from(data).toString('base64')
const img = `data:${type};base64,` + base64data // <-- This is so we can render it on the page
setImage(img)

return data
}

const uploadImage = async (imageData) => {
setMessage("Uploading Image...")

// Create instance to NFT.Storage
const nftstorage = new NFTStorage({ token: process.env.REACT_APP_NFT_STORAGE_API_KEY })

// Send request to store image
const { ipnft } = await nftstorage.store({
image: new File([imageData], "image.jpeg", { type: "image/jpeg" }),
name: name,
description: description,
})

// Save the URL
const url = `https://ipfs.io/ipfs/${ipnft}/metadata.json`
setURL(url)

return url
}

const mintImage = async (tokenURI) => {
setMessage("Waiting for Mint...")

const signer = await provider.getSigner()
const transaction = await nft.connect(signer).mint(tokenURI, { value: ethers.utils.parseUnits("1", "ether") })
await transaction.wait()
}

useEffect(() => {
Expand All @@ -126,33 +30,8 @@ function App() {
return (
<div>
<Navigation account={account} setAccount={setAccount} />
<p>Edit App.js to get started.</p>

<div className='form'>
<form onSubmit={submitHandler}>
<input type="text" placeholder="Create a name..." onChange={(e) => { setName(e.target.value) }} />
<input type="text" placeholder="Create a description..." onChange={(e) => setDescription(e.target.value)} />
<input type="submit" value="Create & Mint" />
</form>

<div className="image">
{!isWaiting && image ? (
<img src={image} alt="AI generated image" />
) : isWaiting ? (
<div className="image__placeholder">
<Spinner animation="border" />
<p>{message}</p>
</div>
) : (
<></>
)}
</div>
</div>

{!isWaiting && url && (
<p>
View&nbsp;<a href={url} target="_blank" rel="noreferrer">Metadata</a>
</p>
)}
</div>
);
}
Expand Down