Moonbeam Blockchain API
Table of Contents
Introduction
MoonBeam
Moonbeam is much more than just an EVM implementation: it’s a highly specialized Layer 1 chain that mirrors Ethereum’s Web3 RPC, accounts, keys, subscriptions, logs, and more. The Moonbeam platform extends the base Ethereum feature set with additional features such as on-chain governance, staking, and cross-chain integrations.
Moonbeam is a parachain that enables interoperability between Ethereum and Polkadot. In a nutshell Moonbeam allows developers to move their dApps between these two networks without having to rewrite the code, this is quite useful considering that new blockchains are constantly coming out.
The native token in Moonbeam is GLMR, this means that staking governance as well as fees are paid in GLMR.
The advantages of Moonbeam are:
- Migration of Smart Contracts in an easy and cheap way.
- Poolkadot bridges, oracles and wallets can be accessed through this network.
- Supports any EVM language such as Vyper, Solidity, etc.
Moonbeam’s testnet is Moonriver.
In this article we will see how to interact with the Moonbeam blockchain with Bitquery, some of the things we will see will be:
- Get the total number of transactions in the network
- Get the details of a transaction
- Getting the balance of an account
- Get the DEXs plus trade volume
- etc.
Something curious that will help you to use bitquery with this parachain, is that the scheme is identical to Ethereum-based networks, so you won’t have to learn if you already know a little ethereum.
Transaction Statistics
With the following query we will obtain the total number of transactions that exist in Moonbeam:
query {
ethereum(network: moonbeam){
transactions {
countBigInt
}
}
}
The result will look something like this:
{
"ethereum": {
"transactions": [
{
"countBigInt": "9599629"
}
]
}
}
As we can see at the time of writing this article it gives us around 9.5M total transactions, not bad, right?
Transactions Count by Date
Transacionts over 2022:
Now we will use a query to obtain the transactions during all the months of 2022 and we will represent them in a graph.
query {
ethereum(network:moonbeam){
transactions(
options: {asc: "date.month"}
date: {since: "2022-01-01"}
){
count
date{
month
year
}
}
}
}
As we can see Moonbeam has had many transactions, where its peak was 1.6M in January 2022.
Transaction Average Cost
Now we will see how much gas has been paid on average in all Moonbeam transactions during 2022. Remember that gas is paid for with GLMR.
We will use the following query, which will allow us to know the gas averages for the whole of 2022
query {
ethereum(network: moonbeam) {
transactions(options: {asc: "date.date"}, date: {since: "2022-08-01", till: "2022-08-24"}) {
date: date {
date(format: "%Y-%m-%d")
}
gasPrice
gasValue
average: gasValue(calculate: average)
maxGasPrice: gasPrice(calculate: maximum)
medianGasPrice: gasPrice(calculate: median)
}
}
}
Graph created thanks to Bitquery IDE
We could say that the approximate average gas used per transaction is 0.05 GLMR, but what does the cost of gas depend on? The cost of gas will depend on how complex the transaction itself is, for example, it is not the same to make a GLMR transfer to another account than to call a staking function for a contract, the latter one will consume more due to its complexity.
Moonbeam Blocks
The estimated block time in Moonbeam is 6s. Now we will see a query to obtain the total number of blocks that have been in the network.
{
ethereum(network: moonbeam) {
blocks {
countBigInt
}
}
}
In the output will it be like:
{
"ethereum": {
"blocks": [
{
"countBigInt": "1746589"
}
]
}
}
Now we will proceed to obtain the details of a specific block, where it will give us data such as:
- Miner
- Hash
- Timestamp
- Size
- Reward
- Reward in USD
- etc
This will be the query we will use:
query {
ethereum(network: moonbeam) {
blocks(height: {is: 1735656}) {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
difficulty
hash
miner {
address
annotation
}
totalDifficulty
transactionCount
uncleCount
size
reward
reward_usd: reward(in: USD)
parentHash
rewardCurrency {
symbol
}
}
}
}
The output would it be:
{
"ethereum": {
"blocks": [
{
"timestamp": {
"time": "2022-08-26 17:39:30"
},
"difficulty": 0,
"hash": "0x81402f5a660ae2b00e0bcf428357dcb3cb2f91b5661d66cbca1b041aebedba28",
"miner": {
"address": "0xe596665a52d9bb37865b3a9264ffe038be08f28d",
"annotation": null
},
"totalDifficulty": 0,
"transactionCount": 1,
"uncleCount": 0,
"size": 843,
"reward": 0.0002476755,
"reward_usd": 0.00014081901812097432,
"parentHash": "0x5905e62dd627e1c1b92df6ca6265aefb67ddd2c28b7c385be6e44515d5a1c404",
"rewardCurrency": {
"symbol": "GLMR"
}
}
]
}
}
Balances Moonbeam
Sometimes it is very useful to obtain the balance of an address, in this case we will obtain the balance of the erc-20 tokens and the GLMR balance of a Moonbeam address with the following query:
{
ethereum(network: moonbeam) {
address(address: {is: "0x3b3a3c04911f12cc8cc5e56ee8636c177e8650ed"}) {
glmr_balance: balance
balances {
value
currency {
symbol
name
address
tokenType
}
}
}
}
}
The output we will have would be something like this:
{
"ethereum": {
"address": [
{
"glmr_balance": 4.977934728999999,
"balances": [
{
"value": 0.026201287513444353,
"currency": {
"symbol": "WGLMR",
"name": "Wrapped GLMR",
"address": "0xacc15dc74880c9944775448304b263d191c6077f",
"tokenType": "ERC20"
}
},
{
"value": 612957.89388993,
"currency": {
"symbol": "mFRAX",
"name": "Moonwell FRAX",
"address": "0x1c55649f73cda2f72cef3dd6c5ca3d49efcf484c",
"tokenType": "ERC20"
}
}
]
}
]
}
}
The output has been trimmed to make it easier to read.
Smart Contracts
As we have seen Moonbeam has compatibility with Ethereum, this includes smart contracts. Any smart contract that can be deployed in any EVM (ethereum virtual machine) can be deployed without any problem in Moonbeam.
Bitquery allows to obtain details and information of the smart contracts in Moonbeam, in this case we will see the total number of smart contracts that have been created in Moonbeam with this query:
query {
ethereum(network: moonbeam) {
smartContractCalls {
contracts: count(uniq: smart_contracts)
}
}
}
The output will it be like:
{
"ethereum": {
"smartContractCalls": [
{
"contracts": 59091
}
]
}
}
Not bad considering it is a relatively new ecosystem.
Smart Contracts per Month
Now we will do something similar, we will obtain the contracts that have been created but by month with the following query:
query {
ethereum(network: moonbeam) {
smartContractCalls(
options: {asc: "date.date"}
date: {since: "2022-05-01", till: "2022-08-26"}
) {
date: date {
date(format: "%Y-%m")
}
contracts: count(uniq: smart_contracts)
}
}
}
The output will it be like:
{
"ethereum": {
"smartContractCalls": [
{
"date": {
"date": "2022-05"
},
"contracts": 5246
},
{
"date": {
"date": "2022-06"
},
"contracts": 1522
},
{
"date": {
"date": "2022-07"
},
"contracts": 1444
},
{
"date": {
"date": "2022-08"
},
"contracts": 1565
}
]
}
}
Overview:
Moonbeam is a parachain that will offer us an almost 100% compatibility with Ethereum in Polkadot, with Bitquery we will be able to obtain that data in an easy and simple way with a GraphQL scheme identical to Ethereum.
Some links that will help you:
— Bitquery Resources —
— Moonbeam Resources —