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

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

@YosephKS : Thank you !!
yes, i was still developing under Web3js but now i want to migrate my scripts to ethersjs. I know I have to update my webpages scripts but i’m stuck on only 1 point. If i use the alias of the new Moralis SDK (>= 1.0.7):
<script src="https://unpkg.com/moralis@latest/dist/moralis.js"></script>

i could not figure out how to change the code in my index main.js (it’s for Clone-Rarible-24H) :

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);

i have tried tons of syntax combinations regarding some tricks found on this forum + reading the ethersjs’ pages, as adviced by Erno : https://docs.ethers.io/v5/api/contract/example/

For the old web3js syntax:
new web3.eth.Contract(tokenContractAbi, TOKEN_CONTRACT_ADDRESS);

… the new syntax for ethers is:
new ethers.Contract(TOKEN_CONTRACT_ADDRESS, tokenContractAbi, providerOrSigner)

But i always finish with an error in browser console such as:
Uncaught (in promise) TypeError: ethers.Contract is not a constructor

If you could tell me how to translate the 3 first lines above, you will earn a lot of Karma points :wink:

2 Likes

Guys I have an issue with enabling web3 after the update, Can you help me to fix this?

Hey @Erno , I’m using react-moralis and useWeb3ExecuteFunction to run smart contract functions. It worked fine on version 0.3.11, but after installing the latest version v1.1.0, somehow the function stays in loading and fetching phase. Nothing changed in my code, I used the snippet from github passing the parameters in fetch function. I’m aware of migration on ether.js, I do await data.wait() to get my smart contract data, but it’s null all the time.
If you have any idea, let me know, please.
Thanks for your time and effort!

Hi, i have the same problem, event works fine some times (i can see my log of the current chain, and switch with five differents test chains), but some times no more emit (everything break, but no error)…
I call enableWeb3 in first, no problem.
After that, i call a initEvents function, that create listeners on Moralis.onChainChanged((chain) => {console.log('new chain : ’ + chain);});

There is an issue from onChainChanged event ?

Thanks in advance !

I confirm, when i use metamask events, no problem :

    //Not work everytime, when it break no more event and no error
    this.web3.getOnChainEvent((chain: string | null) => {
      console.log('Chain changed');
      console.log(chain);
      this.getNativeBalance();
    });

    // Work fine
    (window as any).ethereum.on("chainChanged", (chain: any) => {
      console.log("chain");
      console.log(chain);
    });
1 Like

I’re right, it’s Metamask. I moved from Brave to Firefox installing Metamask there to test it and it passed well. I get all my data and event, huh.
Thanks for direction in debugging. :slight_smile:

Hi I used Moralis provider and met issues below…
How can I set network as “any” in Moralis provider to avoid network changes error?

1 Like

YosephKS,
I tried again today to upgrade my project to Ethers.js, as we have the new SDK 1.1.0, but one more time my browser console returns error : it says my contracts are undefined.
The doc at https://docs.ethers.io/v5/api/contract/example/ is not solving my issue. If i tried to update the old :
new web3.eth.Contract(tokenContractAbi, TOKEN_CONTRACT_ADDRESS);
by this :
const tokenContract = new ethers.Contract(TOKEN_CONTRACT_ADDRESS, tokenContractAbi);
my console reports that ethers is not a constructor.
I tried to import the ethers CDN :
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js" type="application/javascript"></script>
but the issue remains.

Any migration instructions for Cloud Code?

Do you have more info? When does this error occur?
What code dit you run, and what version are you using?