-
This is a React Dapp. I am calling the Mint function on button click and trying to upload to IPFS, then get the URI and pass it into the contract’s mint function.
-
Yes I already have a cloud function configured in the repo. Should I send the base64 there and then upload to IPFS via cloud function? And then Call the mint contract’s mint function from cloud? Or should I send the URI back from the cloud and call the mint function in react again…
Code below:
async function mintMyNFT() {
if(!checkTraits()) {
alert("Some of the selected traits are not in your wallet. Ensure all trait-titles are yellow. Click 'My Owned Traits' again to refresh wallet traits.")
}
else {
const base64 = await getImage()
const imageData = new Moralis.File("img.png", {base64: base64});
await imageData.saveIPFS();
const imgURL = imageData.ipfs();
const metadata = {
"name": "Goatd",
"description": "Customizable PFP on Avax",
"image": imgURL,
"attributes": [
{
"trait_type": "Background",
"value": props.chosenTrait.Background
},
{
"trait_type": "Body",
"value": props.chosenTrait.Body
},
{
"trait_type": "Head",
"value": props.chosenTrait.Head
},
{
"trait_type": "Eyes",
"value": props.chosenTrait.Eyes
},
{
"trait_type": "Mouth",
"value": props.chosenTrait.Mouth
},
{
"trait_type": "Headwear",
"value": false&&"none"
}
],
}
const tokenMetadata = new Moralis.File("metadata.json", {
base64: Buffer.from(JSON.stringify(metadata)).toString('base64')
});
await tokenMetadata.saveIPFS();
const metadataUrl = await tokenMetadata.ipfs();
const mintResult = await mintFetch({
abi: spotNFTAbiFuji,
contractAddress: spotNFTContractFuji,
functionName: "mint",
params: {
bg: props.chosenTrait.BackgroundID,
body: props.chosenTrait.BodyID,
head: props.chosenTrait.HeadID,
eyes: props.chosenTrait.EyesID,
mouth: props.chosenTrait.MouthID,
headwear: props.chosenTrait.HeadwearID,
uri: metadataUrl,
},
msgValue: Moralis.Units.ETH(1.0),
onError: (err)=> {
console.log(err)
alert(JSON.stringify(err.data.message))
},
onSuccess: (data) => {
console.log(data)
alert("PFP Successfully Minted! Contract address: 0x60e26Afaca30396AAF91Ea3aA355aCf22eeF080e")
}
})
}
}