Do you need the full abi? MyNFT.json (the abi) has 17008 lines in it. That’s what
stopped me from wanting to do that.
I’ve tried amending the mintTokens function like so
var abi = ({
"inputs": [
{
"internalType": "string",
"name": "tokenURI",
"type": "string"
}
],
"name": "mintToken",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
});
const MyNFTContract = new web3.eth.Contract(JSON.parse(abi).abi, contractAddress);
MyNFTContract.methods.mintToken({
"image" : "static/Images/4.Hobbit.jpg",
"name" : "Hobbit",
"description" : "A Hobbit"
}).send();
}
I’m getting this error
Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1
you don’t need all those lines to call that specific contract function, only the line that contains that function name. But you can also load all those lines in a separate js file, by writing something like windows.abi = […] in a file named abi.js
that you load similar to logic.js
Check the last post again, I edited it.
I think that you have to use a list for abi. Like abi =[{}], and is no need to do that json conversion
Got it, worked.
When I click the button, I get:
moralis.js:7005 Uncaught (in promise) Error: Web3Api not initialized, run Moralis.start() first
at Function.<anonymous> (moralis.js:7005)
at tryCatch (moralis.js:28071)
at Generator.invoke [as _invoke] (moralis.js:28301)
at Generator.next (moralis.js:28126)
at asyncGeneratorStep (moralis.js:27580)
at _next (moralis.js:27602)
at moralis.js:27609
at new Promise (<anonymous>)
at new Wrapper (moralis.js:32483)
at Function.<anonymous> (moralis.js:27598)
I tried adding
Moralis.start();
In a bunch of places, at the top and in the function.
Also I have properly filled in at the top.
Moralis.serverURL
Moralis.initialize
You have to use parameters for Moralis.start, you need to call it only once, and the other syntax with initialise is the old syntax before Moralis.start, meaning that you don’t have to use that syntax
Okay, created a Morlis.start(params)
async function mintTokens() {
const contractAddress = "0x570D251af3cc07bE28d930FaB0F31cD338EaC8f3";
var abi = ([{
"inputs": [
{
"internalType": "string",
"name": "tokenURI",
"type": "string"
}
],
"name": "mintToken",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
}]);
const options = {
chain: "mumbai",
address: contractAddress,
function_name: "mintToken",
abi: abi,
params: {"image": "static/Images/4.Hobbit.jpg",
"name": "Hobbit",
"description": "A Hobbit"}
};
await Moralis.executeFunction(options);
}
document.getElementById(Mintbtn).onclick = mintTokens();
error:
Uncaught (in promise) Error: Function does not exist in abi
You have a set of extra parenthesis ([{ instead of only [{
Oh, your right.
Took them out.
Error remains.
It looks like it expects a tokenUri as parameter, but I don’t know why you get that error with function name now.
As I understand it.
“tokenURI”
is part of the ABI not a variable that contains the token uri.
From that abi it looks like mint function has only one parameter named tokenURI
How should I format it?
const options = {
chain: "mumbai",
address: contractAddress,
function_name: "mintToken",
abi: abi,
params: {"tokenURI": {"image": "static/Images/4.Hobbit.jpg",
"name": "Hobbit",
"description": "A Hobbit"
}}};
(Just a guess)
I’m also getting
Uncaught ReferenceError: mintTokens is not defined
at HTMLButtonElement.onclick
I’m also getting this
Uncaught (in promise) Error: Function does not exist in abi
at Function.<anonymous> (moralis.js:6445)
at tryCatch (moralis.js:28070)
at Generator.invoke [as _invoke] (moralis.js:28300)
at Generator.next (moralis.js:28125)
at asyncGeneratorStep (moralis.js:27579)
at _next (moralis.js:27601)
at moralis.js:27608
at new Promise (<anonymous>)
at new Wrapper (moralis.js:32482)
at Function.<anonymous> (moralis.js:27597)
Which I’m not sure is relevant to this function which is all the scope I care about right now.
here you usually save that data in a json file and upload it on your Moralis Server as a static file or on IPFS and use something like
params: {"tokenURI": "https://...../1.json"}
mintTokens is the function that you defined
Thanks,
here you usually save that data in a json file and upload it on your Moralis Server as a static file or on IPFS and use something like
params: {"tokenURI": "https://...../1.json"}
I was purely using local hosting to mint simple nfts for testing not for launching.
I see now I was going about it the wrong way.
Am I getting
Uncaught ReferenceError: mintTokens is not defined
at HTMLButtonElement.onclick
Because the HTML cannot find my logic script?
I have in logic.js
document.getElementById(Mintbtn).onclick = mintTokens();
To make sure, will that do the trick?
you have to import that logic.js in your html, something like adding in your html a line like:
<script type="text/javascript" src="./logic.js"></script>
Yes.
Although the scripts are under where the button is in the index.html.
Perhaps the logic.js is executed later?
what you mean by this?
I’m just assuming html is executed sequentially each line down the page.
And the onclick=“mintTokens()”
Won’t run, because the logic.js script were mintTokens() is, in this case hasn’t been imported yet.
It’s okay, it’ll take 10 seconds to check for myself.