Following code would execute the smart contract, but it fails to get metadata associated. So, the NFT is listed on Opensea without the image. I can’t figure out how to configure the metadataURI
in the transactionParameters
.
const nft_contract_address = "0xDed2d19bF2495b2c45d55FfEFd104292E8eFE38d"
const ABI = ABI
async function mintNFT(){
await Moralis.enableWeb3();
const web3 = new Web3(Moralis.provider);
const getBase64ImageFromUrl=(imageUrl)=>
new Promise(resolve=> fetch(imageUrl).then(res=>res.blob()).then(blob=>{
const reader = new FileReader();
reader.addEventListener('load',()=>{
resolve(reader.result);
});
reader.readAsDataURL(blob);
})
);
(async()=>{
const url='';
const data = await getBase64ImageFromUrl(url);
const file1 = new Moralis.File("image.png", { base64: data });
await file1.saveIPFS();
console.log("ipfsURL:", file1.ipfs());
await file1.saveIPFS();
const imageURI = file1.ipfs();
const metadata = {
"name":"name",
"description":"description",
"image":imageURI,
}
const metadataFile = new Moralis.File("metadata.json", {base64 :btoa(JSON.stringify(metadata))});
await metadataFile.saveIPFS();
const metadataURI = metadataFile.ipfs();
const txt = await mintToken(metadataURI).then(notify)
})();
async function mintToken(_uri){
const encodedFunction = web3.eth.abi.encodeFunctionCall({
name: "mint",
type: "function",
inputs: [{
type: 'string',
name: 'tokenURI'
}]
}, [_uri]);
const options = {
contractAddress: nft_contract_address,
functionName: "mint",
abi: ABI,
msgValue: Moralis.Units.ETH("0.01"),
};
const transactionParameters = {
to: nft_contract_address,
from: ethereum.selectedAddress,
data: encodedFunction
};
const txt = await ethereum.request({
method: 'eth_sendTransaction',
params: [transactionParameters]
});
return txt
}
const transaction = await Moralis.executeFunction(options);
const result = await transaction.wait();
async function notify(_txt){
document.getElementById("resultSpace").innerHTML =
`<input disabled = "true" id="result" type="text" class="form-control" placeholder="Description" aria-label="URL" aria-describedby="basic-addon1" value="Your NFT was minted in transaction ${_txt}">`;
}
}
mintNFT();
Thanks in advance.