Moralis JS SDK v1.0 (migration to Ethers.js)

To convert your app to EthersJs, you need to check where you use the web3Js functions, and find similar functions from EthersJs. It is a different library so there is no 1-on-1 replacement.
For example, see in their docs on how to use contract interactions: https://docs.ethers.io/v5/api/contract/example/
Alternatively, you could still use web3Js via

import Web3 from 'web3' // Only when using package mananger. 
import Moralis from 'moralis'  // Only when using package mananger

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

Thank you that!

I have updated my code but now I get this error:

Here is the code:

But it’s odd, earlier in the code, the same adjustment seems to have resolved the error for this line:

So I cannot figure out why it works in one part of the code, but not the other.

1 Like

I think this is because enableWeb3 replaces Moralis.provider, so If you call new Web3(Moralis.provider) twice, then the old web3 instance will have a reference to a provider that no longer exists.

What you can do is to call enableWeb3 once, then create a web3Js instance via new Web3(Moralis.provider), and store this as a globally variable web3 or store it in the window.web3.

@Erno :
I follow the tutorial “I Cloned Rarible in 24H”.
For information, in my frontend index.html, i had upgraded my Moralis SDK to v. 1.0.1, then 1.0.2 and today to 1.0.3 and i indeed noticed in my browser console new issues like :

Uncaught (in promise) ReferenceError: tokenContract is not defined
    at mintNFT (main.js:227:19)
    at HTMLButtonElement.createItem (main.js:182:23)
mintNFT	@	main.js:227
createItem	@	main.js:182

I have then downgraded my SDK to 0.0.184 as you stated and everything works fine now. I can mint back again + list in my marketplace. So, untill the new SDK fix, i’m going to use this:

<script src="https://unpkg.com/[email protected]/dist/moralis.js"></script>

1 Like

I would like to do Moralis.authenticate() with let’s say ethers.Wallet.createRandom(). Can I? if yes, how?

So I am changing all of my code to Ethers.js and have run into a snag.

Here is the method I am accessing:

image +

And here is my code:

This is now returning false when I am using the admin address. Do I need to ship the address with the request?

Hey @DavisX, it’s hard to say but most likely because you might be using web3js syntax in your code and the newer version of the SDK >1.0.0 are using etherjs by default

2 Likes

Thank you very much, YosephKs.
Indeed, for my NFT marketplace, i still use the classical web3 call syntax which is :

window.web3 = await Moralis.enableWeb3();
window.tokenContract = new web3.eth.Contract(tokenContractAbi, TOKEN_CONTRACT_ADDRESS);
window.marketplaceContract = new web3.eth.Contract(marketplaceContractAbi, MARKETPLACE_CONTRACT_ADDRESS);

The problem is i didn’t find in the Moralis documentation the new syntax. Do you have the corresponding syntax for etherjs for my example, please ?

1 Like

For anyone experiencing issues with WalletConnect, please update to v1.0.4, we just released a fix

@Erno : do you have the explanation for my issue in my post just before yours, please ?
The new syntax to use for “new web3.eth.Contract()” with SDK 1.0.4.

The web3 response is now an instance of the EthersJs library. You need to check their docs on how these implementations work.

For example, here is some info on how to use contacts in EthersJs:

2 Likes

Hey Erno, I just upgraded my boilerplate project to be using “moralis”: “1.0.4”, and “react-moralis”: “1.0.0”,

Can you confirm that I am understanding this update correctly, when run enableWeb3(), then I destructure const {web3} = useMoralis(). The web3 variable is now an instance of ethers?

Since when i log the web3 object it doesn’t have any other properties of the old web3 instance or the new ethers instance, am I thinking about this wrong?

Yes that’s correct. The details are in the first post or in the docs: https://docs.moralis.io/moralis-server/web3/web3

Hey Thanks for your quick response, what’s throwing me off here is that when I log the instance of ethers, it doesn’t have any of the objects/functions, like utils, any idea what’d be causing that? see screenshot: Screen Shot 2022-01-20 at 5.18.00 PM

Etherjs is very different compare to web3js so some of those field you’re looking for might exist but in a different instance, what are you trying to do here? and any syntax that we can try to help with?

1 Like

Well for whatever reason I am unable to destructure the instance of ethers from useMoralis(). I believe everything is up to date per my initial post, so I’m just not sure why I wouldn’t be getting the full instance.

I tried installing ethersjs via yarn and that instance of ethersjs works just fine. I wish I could be more helpful in my response I’m just really unsure regarding what is happening here

Hi,

In order to use etherjs utils functions, do I have to manually import etherjs directly and use it like this:
ethers.utils.xxxx()?

The following was working with web3 and now I can’t see utils functions append to the provider when doing:

const web3 = (await Moralis.enableWeb3())

Thanks!

Having the exact same issue on my end! glad I am not crazy ha

Ah I see what you mean. Seems like this is a difference between web3 and ethers. Web3 returns utils etc. as well in the provider. EthersJs does not do this.

You will get back a Web3Provider, so if you want to use other EthersJs functionalities, then you need to import these from EthersJs

1 Like

Ah Ok i see now, that makes sense, thanks for clarifying!

1 Like