Minted fine with web3.js, but returning error with ethers.js

I learned that migrating to ethers.js is better since [email protected] is default to that.
But I have an issue when I mint if I remove web3.js and use Moralis.enableWeb3(). This is the shortened code:

const web3 = (await Moralis.enableWeb3())
async function mintToken(_uri){
  const encodedFunction = web3.eth.abi.encodeFunctionCall({
  name: "mintToken",
    type: "function",
    inputs: [{
      type: 'string',
      name: 'tokenURI'
      }]
  }, [_uri]);

Code above return me an error:

It used to work perfectly fine with web3.js with the following code:

const web3 = new Web3(window.ethereum);
async function mintToken(_uri){
  const encodedFunction = web3.eth.abi.encodeFunctionCall({
  name: "mintToken",
    type: "function",
    inputs: [{
      type: 'string',
      name: 'tokenURI'
      }]
  }, [_uri]);

Why am I getting an error with ethers.js?
Thanks advance.

the syntax is different for web3 and ethers, you will have to adapt the code to be specific to ethers, or you could still use web3 with latest version of moralis v1 sdk

you can read more here:

Do you mean this part? web3.eth.abi.encodeFunctionCall
I read the link you gave me, but couldn’t find what exactly I should change the code to.
Could you elaborate a bit?

Thank you.

there is a way to specify on how to still use web3 in that forum post, in case that you don’t know ethers syntax now that is different than web3 syntax

You can replace this having such

await Moralis.enableWeb3()
const web3 = new Web3(Moralis.provider);

well, I decided to migrate to ethers.js because I could’t get the following code work:

Moralis.onAccountChanged( async (account) => {
const confirmed = confirm("Link this account?");

if (confirmed) {
await Moralis.link(account);
let user = Moralis.User.current();
document.getElementById('address').innerHTML = user.get("ethAddress");
}
});

I assumed because the code above is supposed to work along with ethers.js, right??
Anyway, do you think I should stay on web3.js?
If the Moralis tutorial is inclined to ethers.js, I thought I should move to ethers.js.

Thanks for returning the alternative.
I get an error with the code:

It seems that const web3 is not caught. Can you tell me why? (Sorry, I’m still new to JS)

you may need to include a line in the html header that imports web3.js

1 Like

Looks like you didn’t have the web3js script tag <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>

1 Like

Yes, It worked!
But, I have a question.
Am I using web3.js to mint?
If that so, why would I change it
from:
const web3 = new Web3(window.ethereum);
To:
await Moralis.enableWeb3() const web3 = new Web3(Moralis.provider);

I’m still unsure if I am using ethers.js or web3.js and which I should better to use.

it depends on what you want to do, in that link that I pasted in a previous post it gives more information about how to set web3 as a library

1 Like