Web3

Lets talk about Web3 integration!

Moralis comes with a set of plugins to help you focus on your development of your app and leave the rest to us.

Moralis SDK Web3 API

Enable

Similar to window.ethereum.enable() but returns a promise that resolves to a Web3 instance. Use this when you need a fully functional Web3 instance, like for making contract calls.

const web3 = await Moralis.Web3.enable();
const contract = new web3.eth.Contract(contractAbi, contractAddress);

If you just need to access web3.utils without web3 connectivity. Use the following.

const web3 = new Moralis.Web3();
const amountInEth = web3.utils.fromWei(amountInWei, 'ether');

Just beware that calls like web3.eth.getAccounts() will not work. You can re-assign the variable using Moralis.Web3.enable() later if you need full Web3 connectivity.

See the Web3.js docs https://web3js.readthedocs.io/ for more info on the Web3 object.

Events

There are wrappers for the standard Web3 change events.

  • onConnect

  • onDisconnect

  • onChainChanged

  • onAccountsChanged

const unsubscribe = Moralis.Web3.onAccountsChanged(function(accounts) {
console.log(account);
});
// ["0x0f7056a5778b15ebd522f39ab9782abf3ee37f08"]

Linking

Link (unlink) an address to the current user.

  • link(address)

  • unlink(address)

The user may have multiple addresses they wish to associate with their profile. This can be done with the link function after the user has been authenticated.

Moralis.Web3.onAccountsChanged(async function (accounts) {
const confirmed = confirm('Link this address to your account?');
if (confirmed) {
await Moralis.Web3.link(accounts[0]);
alert('Address added!');
}
});

The unlink function removes the given address from the user's profile.

await Moralis.Web3.unlink(address);

Also see the Users section.

Helper functions

Moralis provides you with some really handy helper functions to enable you to easily get the balances and tokens owned by the current user.

getTokenBalances

The getTokenBalances function lets you retrieve all token balances of the current user.

const balances = await Moralis.Web3.getTokenBalances();

Without any parameters specified, it defaults to 'Eth' as chain, but you can also specify the chain in an options object:

const balances = await Moralis.Web3.getTokenBalances({chain: 'Eth'});

Pagination is built in by default, with 100 tokens returned on each page.

To get the next page, you can use the page parameter:

const balances = await Moralis.Web3.getTokenBalances({chain: 'Eth', page: 1});

getBalance

The getBalance function lets you the token balances of the current user.

const ethBalance = await Moralis.Web3.getBalance();

Without any parameters specified, the ETH balance is returned, on the 'Eth' chain, but you can also specify the chain and the symbol in an options object:

const linkBalance = await Moralis.Web3.getBalance({chain: 'Eth', symbol: 'LINK'});

getNFTs

The getNFTs function lets you retrieve all NFTs of the current user.

const ethNFTs = await Moralis.Web3.getNFTs();

Without any parameters specified, it defaults to 'Eth' as chain, but you can also specify the chain in an options object:

const ethNFTs = await Moralis.Web3.getNFTs({chain: 'Eth'});

Pagination is built in by default, with 100 tokens returned on each page.

To get the next page, you can use the page parameter:

const ethNFTs = await Moralis.Web3.getNFTs({chain: 'Eth', page: 1});

You can sort the results based on the block number by specifying 'asc' or 'desc' on the order parameter:

const ethNFTs = await Moralis.Web3.getNFTs({chain: 'Eth', page: 1, order: 'asc'});

getNFTsCount

The getNFTsCount function lets you get the number NFTs owned by the current user on a given chain.

const nftCount = await Moralis.Web3.getNFTsCount();

Without any parameters specified, it defaults to 'Eth' as chain, but you can also specify the chain in an options object:

const ethNftCount = await Moralis.Web3.getNFTsCount({chain: 'Eth'});

getTransactions

The getTransactions function lets you the transactions of the current user.

const transactions = await Moralis.Web3.getTransactions();

Without any parameters specified, it defaults to 'Eth' as chain, but you can also specify the chain in an options object:

const transactions = await Moralis.Web3.getTransactions({chain: 'Eth'});

Pagination is built in by default, with 100 tokens returned on each page.

To get the next page, you can use the page parameter:

const transactions = await Moralis.Web3.getTransactions({chain: 'Eth', page: 1});

You can sort the results based on the block number by specifying 'asc' or 'desc' on the order parameter:

const transactions = await Moralis.Web3.getTransactions({chain: 'Eth', page: 1, order: 'asc'});

getTransactionsCount

The getTransactionsCount function lets you get the number transactions related to the current user on a given chain.

const numTx = await Moralis.Web3.getTransactionsCount();

Without any parameters specified, it defaults to 'Eth' as chain, but you can also specify the chain in an options object:

const ethNumTx = await Moralis.Web3.getTransactionsCount({chain: 'Eth'});