Moonbeam
Moonbeam combines the easy-to-use tooling of Ethereum and the scalable, interoperable architecture of Polkadot. Moonbeam is EVM compatible to an extent but there are important Moonbeam differences that developers should know and understand in terms of the Ethereum API JSON-RPC support.
Quick links
Connect wallet
You can set up your MetaMask wallet to connect to Moonbeam RPC. You can then perform transactions and interact with the network.
- Open your Metamask Extension and click the 'Network' drop down menu at the top.
- Select 'Custom RPC'.
- Enter the settings for the required project as follows in the table below:
Chain | Custom RPC Category | Details |
---|---|---|
Celo | NETWORK NAME: | Moonbeam RPC |
NEW RPC URL: | https://rpc.ankr.com/moonbeam/ | |
CHAIN ID: | 1284 (hex: 0x504) | |
SYMBOL: | GLMR | |
BLOCK EXPLORER URL: | https://moonscan.io/ |
Get Started
Using Ethereum API Libraries
The Moonbeam API is very similar to the Ethereum API and is compatible with the majority of Ethereum-style JSON-RPC methods. Developers can leverage this compatibility and use the web3.js library, ethers.js library and the web3.py library to interact with a Moonbeam node as if they were doing so on Ethereum.
However, not all Ethereum JSON RPC methods are supported, and some of the supported ones return default values.
The following methods are supported on the Moonbeam RPC.
- eth_protocolVersion — Returns
1
by default - eth_syncing — Returns an object with data about the sync status or
false
- eth_hashrate — Not supported
- eth_coinbase — Returns the latest block author. Not necessarily a finalized block
- eth_mining — Not supported
- eth_chainId — Returns the chain ID used for signing at the current block
- eth_gasPrice — Returns the current gas price
- eth_accounts — Returns a list of addresses owned by the client
- eth_blockNumber — Returns the highest available block number
- eth_getBalance — Returns the balance of the given address
- eth_getStorageAt — Returns content of the storage at a given address
- eth_getBlockByHash — Returns the block of the given hash
- eth_getBlockByNumber — Returns the block of the given block number
- eth_getTransactionCount — Returns the number of transactions sent from the given address (nonce)
- eth_getBlockTransactionCountByHash — Returns the number of transactions in a block with a given block hash
- eth_getBlockTransactionCountByNumber — Returns the number of transactions in a block with a given block number
- eth_getUncleCountByBlockHash — Returns
"0x0"
by default - eth_getUncleCountByBlockNumber — Returns
"0x0"
by default - eth_getCode — Returns the code at given address at given block number
- eth_sendTransaction — Creates new message call transaction or a contract creation, if the data field contains code. Returns the transaction hash, or the zero hash if the transaction is not yet available
- eth_sendRawTransaction — Creates new message call transaction or a contract creation for signed transactions. Returns the transaction hash, or the zero hash if the transaction is not yet available
- eth_call — Executes a new message call immediately without creating a transaction on the block chain, returning the value of the executed call
- eth_estimateGas — Returns an estimate amount of how much gas is necessary for a given transaction to succeed
- eth_getTransactionByHash — Returns the information about a transaction with a given hash
- eth_getTransactionByBlockHashAndIndex — Returns information about a transaction at a given block hash, and a given index position
- eth_getTransactionByBlockNumberAndIndex — Returns information about a transaction at a given block number, and a given index position
- eth_getTransactionReceipt — Returns the transaction receipt of a given transaction hash
- eth_getUncleByBlockHashAndIndex — Returns
"null"
by default - eth_getUncleByBlockNumberAndIndex — Returns
null
by default - eth_getLogs — Returns the transaction receipt of a given transaction hash
- eth_getWork — Returns
["0x0","0x0","0x0"]
by default - eth_submitWork — Not supported
- eth_submitHashrate — Not supported
How to integrate with Moonbeam
If you're using the Public RPCs, your endpoint is https://rpc.ankr.com/moonbeam
.
If you're using the Premium Plan, you can copy your two endpoints for HTTPS and WSS from Ankr Protocol
In the code samples below, use EITHER the public endpoint OR the premium endpoint (if you have signed up to the Premium Plan)
const Web3 = require('web3'); //Load Web3 library
// Create local Web3 instance - set Moonbeam as provider
const web3 = new Web3("https://rpc.ankr.com/moonbeam"); // Public RPC URL
const web3 = new Web3("https://rpc.ankr.com/moonbeam/YOUR-API-KEY"); // Premium RPC URL
const ethers = require('ethers');
const providerURL = "https://rpc.ankr.com/moonbeam"; // Public RPC URL
const providerURL = "https://rpc.ankr.com/moonbeam/YOUR-API-KEY"; // Premium RPC URL
// Define Provider
const provider = new ethers.providers.StaticJsonRpcProvider(providerURL, {
chainId: 1284,
name: 'moonbeam'
});
Using Substrate API libraries
Polkadot.js API library allows application developers to query a Moonbeam node and interact with the node's Polkadot or Substrate interfaces using JavaScript.
-
Install node.js (if not already installed)
# homebrew (https://docs.brew.sh/Installation) brew install node # nvm (https://github.com/nvm-sh/nvm) nvm install node
-
Install Polkadot.js API library
You can install this through a package manager such as
yarn
. Install it in your project directory with the following command:yarn add @polkadot/api
-
Install Moonbeam Types Bundle
To decode Moonbeam custom events and types, you need to include the Moonbeam Types Bundle into your project by adding the following package information to your
package.json
:"@polkadot/api": "^6.9.1", "moonbeam-types-bundle": "^2.0.1", "typescript": "4.3.2"
-
Add this
import
statementAdd this statement to the start of your project file.
import { typesBundlePre900 } from "moonbeam-types-bundle"