Migration Guide JS SDK v0.0.x > v1
-
Removed dependency of web3.js in favour of ethers.js
-
Introducing custom connectors
Update
To update, you need to update your dependency
For CDN::
<script src="https://unpkg.com/moralis@latest/dist/moralis.js"></script>
Please update latest
to the release version you want to use. For all releases, see: https://github.com/MoralisWeb3/Moralis-JS-SDK/releases
Via npm:
npm install moralis@latest
If you do NOT want to update yet
Then you you specify the version to v0.0.184
For CDN:
<script src="https://unpkg.com/[email protected]/dist/moralis.js"></script>
Via npm:
npm install [email protected]
Breaking changes
Moralis.web3 and Moralis.enableWeb3
Moralis.web3 and Moralis.enableWeb3 will now return an instance of Ethers.js instead of an instance of Web3.js.
To account for these changes you should
- Adjust your code to use the Ethers.js library
Or
- Keep using Web3.js or any other library by importing it and initializing it using
Moralis.provider
. Code below shows how to keep using web3.js specifically:
Install web3.js via npm/yarn OR by importing it as a script, then: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)
Return values of Moralis.executeFunction and Moralis.transfer
Return values are changed for Moralis.executeFunction and Moralis.transfer. They are now a transaction response from Ethers.js:
This object contains all data about the transaction. If you need data about the result of the transation, then you need to wait for the transaction to be confirmed.
You can do this via transaction.wait()
to wait for 1 confirmation (or transaction.wait(5)
to wait for 5 confirmations for example):
const transaction = await Moralis.transfer(options)
const result = await transaction.wait()
Moralis.onXXX events have changed
Instead of subscribing to Metamask events directly, moralis generalizes these events so that they can be used for any connector: metamask, walletconnect, network etc. So now the events are fired from the connector that is use to authenticate/enableWeb3.
For example: a user signs in using walletconnect, then switches accounts in walletconnect. Then Moralis.onAccountChange will respond to this change.
To use the previous implementation of listening to Metamask events, you can use:
window.ethereum.on("accountsChanged", (account) =>
console.log("account")
);
window.ethereum.on("chainChanged", (chain) =>
console.log("chain")
);
A notable change is the renamning of Moralis.onAccountsChanged
to Moralis.onAccountChanged
Customize enableWeb3 / custom connectors
Previously, it was possible to overwrite the enableWeb3()
function. This allowed for custom implementation to connect wallets that are not supported by Moralis out-of-the-box.
This approach is not possible anymore, to allow for better scaling and more consistent behavior.
Instead, now you can implement your own connector
, which extends the AbstractConnector class.
This class should implement
-
An
activate()
function that resolves to an object with: -
provider: A valid EIP-1193 provider
-
chainId (optional): the chain that is being connected to (in hex)
-
account (optional): the account of the user that is being connected
-
Subscribes to the EIP-1193 events. This should be mostly done automatically by calling
this.subscribeToEvents(provider)
in theactivate
function. -
the
type
field to indicate a name for the connector -
(optionally) a
deactivate
function that extends the default deactivate function. Implement this when you need to clean up data/subscriptions upon ending/switching the connection.
Then you can include this CustomConnector
in the authenticate/enableWeb3 call as an option:
Moralis.authenticate({ connector: CustomConnector })
See for example implementations:
-
The WalletConnectConnector, that is used when you specify
provider: "walletconnect"
. -
The InjectedWeb3Connector (metamask), that is used when you don’t specify any connector.