JS-SDK v1 Beta (Ethers.js support)
The JS-SDK is switching to using ethers.js instead of web3.js.
This is a change that will affect current builds, and thatās why we make it only accessible in beta releases for now.
The main changes are
- Ethers.js is used as internal web3-library
- Support Web3.js for Moralis.web3 calls
- Changed responses of on-chain functions
- Custom authentication / enableWeb3 (previously overwriting enableWeb3)
How to use
Install the sdk via
npm install moralis@next
Or install moralis by specifying a beta release. (see all releases here)
For the CDN script version, you can use:
https://unpkg.com/[email protected].<VERSION>/dist/moralis.js
And replace <VERSION>
the beta release version to the latest release (see all releases here)
Note: if you use react-moralis, then make sure to also install the latest beta release of react-moralis
See: https://github.com/MoralisWeb3/react-moralis/issues/144
Note: if you want to switch back to a non-beta build, then install a moralis via
npm install moralis@latest
(or specify a release) or use a non-beta CDN script.
Breaking Changes
Ethers.js as internal web3-library
the Moralis SDK will use Ethers.js instead of Web3.js as internal library. This means:
- When you call
enableWeb3()
orauthenticate()
thenMoralis.web3
will be an instance of Ethers.js instead of Web3.js. - Smaller bundle size
- The responses of on-chain functionalities (Moralis.transfer, Moralis.executeFunction and some plugins) are different
Support Web3.js for Moralis.web3 calls
If you want to use a Web3.js
instance when calling Moralis.authenticate()
or Moralis.enableWeb3()
, then you need to specify the library constructor when you call Moralis.start
.:
Example where web3 is an instance of Ethers.js
await Moralis.start({
serverUrl: "YOUR_SERVER_URL",
appId: "YOUR_APP_ID",
});
// Get the initialized web3 instance from Ethers.js
await Moralis.enableWeb3();
const web3 = Moralis.web3;
Example where web3 is an instance of Web3.js
When using CDN build, make sure to include the Web3.js library:
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
When using npm/yarn: make sure to install web3
:
npm install web3
import Web3 from "web3"; // Only when using npm/yarn
await Moralis.start({
serverUrl: "YOUR_SERVER_URL",
appId: "YOUR_APP_ID",
web3Library: Web3,
});
// Get the initialized web3 instance from Web3.js
await Moralis.enableWeb3();
const web3 = Moralis.web3;
Changed responses
- Responses for on-chain calls are different, as Ethers.js is used for these calls. The main difference is that Web3.js returned a
PromiEvent
, where you could subscribe to events, for callbacks. The responses from Ethers.js do not have this functionality. Instead, you get more information in the receipt, and can wait for the result (after confirmation) viaresponse.wait()
(see docs). - Data might be differently formatted. For example, Ethers.js includes numbers as a BigNumber object in the response.
This has effect on:
Moralis.transfer()
Moralis.executeFunction()
- Some plugins
Custom authentication / enableWeb3 (previously overwriting enableWeb3)
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.
Feedback
Please provide any feedback regarding the changes in this forum post. In your post, please include:
- What beta version do you use
- If you use npm, yarn or a CDN (script) build
- if you use react-moralis along with it (if so, what beta-build)