🪙

Web3API.native

You can get the content of any block in past, or address events based on block number.

runContractFunction

Runs a given function of a contract ABI and returns read-only data (asynchronous).

Options:

  • chain(optional): The blockchain to get data from. Valid values are listed on Supported Chains. Default value Eth.
  • function_name (required): The function name
  • address (required): A smart contract address
  • abi (required): contract or function ABI(should be provided as an array)
  • params (optional): Parameters needed for your specific function
JS
React
curl
Unity
const ABI = []; // Add ABI of 0xdAC17F958D2ee523a2206206994597C13D831ec7
const options = {
chain: "eth",
address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
function_name: "balanceOf",
abi: ABI,
params: { who: "0x3355d6E71585d4e619f4dB4C7c5Bfe549b278299" },
};
const allowance = await Moralis.Web3API.native.runContractFunction(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const GetAddressBalanceOfUSDT = () => {
// 0xdAC17F958D2ee523a2206206994597C13D831ec7 = contract address of USDT
const { native } = useMoralisWeb3Api();
const ABI = []; // Add ABI of 0xdAC17F958D2ee523a2206206994597C13D831ec7
const options = {
chain: "eth",
address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
function_name: "balanceOf",
abi: ABI,
params: { who: "0x3355d6E71585d4e619f4dB4C7c5Bfe549b278299" },
};
const { fetch, data, error, isLoading } = useMoralisWeb3ApiCall(
native.runContractFunction,
{ ...options }
);
return (
// Use your custom error component to show errors
<div style={{ height: "100vh", overflow: "auto" }}>
<div>
{error && <ErrorMessage error={error} />}
<button
onClick={() => {
fetch({ params: options });
}}
>
Fetch data
</button>
{data && <pre>{JSON.stringify(data)}</pre>}
</div>
</div>
);
};
curl -X 'POST' \
'https://deep-index.moralis.io/api/v2/0xdAC17F958D2ee523a2206206994597C13D831ec7/function?chain=eth&function_name=balanceOf' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY' \
-H 'Content-Type: application/json' \
-d '{
"abi": MY-CONTRACT-ABI ,
"params": {"who": "0x3355d6E71585d4e619f4dB4C7c5Bfe549b278299" }
}'
using MoralisUnity;
using MoralisUnity.Web3Api.Models;
using UnityEngine;
public class Example
{
public async void ReadFunction()
{
// Function ABI input parameters
object[] inputParams = new object[1];
inputParams[0] = new { internalType = "address", name = "account", type = "address" };
// Function ABI Output parameters
object[] outputParams = new object[1];
outputParams[0] = new { internalType = "uint256", name = "", type = "uint256" };
// Function ABI
object[] abi = new object[1];
abi[0] = new { inputs = inputParams, name = "balanceOf", outputs = outputParams, stateMutability = "view", type = "function" };
// Define request object
RunContractDto rcd = new RunContractDto()
{
Abi = abi,
Params = new { account = "0x3355d6E71585d4e619f4dB4C7c5Bfe549b278299" }
};
string resp = await Moralis.Web3Api.Native.RunContractFunction("0xdAC17F958D2ee523a2206206994597C13D831ec7", "balanceOf", rcd, ChainList.eth);
Debug.Log(resp);
}
}

Example result:

{"result":"5691150038"}

getBlock

Retrieve the contents of a block by block hash. Returns a block object (asynchronous).

Options:

  • chain(optional): The blockchain to get data from. Valid values are listed on Supported Chains. Default value Eth.
  • block_number_or_hash (required): The block hash or block number.
JS
React
curl
Unity
const options = { chain: "bsc", block_number_or_hash: "2" };
// get block content on BSC
const transactions = await Moralis.Web3API.native.getBlock(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
// get block content on BSC
const fetchBlock = async () => {
const options = { chain: "bsc", block_number_or_hash: "2" };
const result = await Web3Api.native.getBlock(options);
console.log(result);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/block/2?chain=bsc' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using MoralisUnity;
using MoralisUnity.Web3Api.Models;
using UnityEngine;
public class Example
{
public async void fetchBlock()
{
Block block = await Moralis.Web3Api.Native.GetBlock(blockNumberOrHash: "2", ChainList.bsc);
Debug.Log(block.ToJson());
}
}

Example result:

{
"timestamp": "2021-05-07T11:08:35.000Z",
"number": "12386788",
"hash": "0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171",
"parent_hash": "0x011d1fc45839de975cc55d758943f9f1d204f80a90eb631f3bf064b80d53e045",
"nonce": "0xedeb2d8fd2b2bdec",
"sha3_uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logs_bloom": "0xdde5fc46c5d8bcbd58207bc9f267bf43298e23791a326ff02661e99790da9996b3e0dd912c0b8202d389d282c56e4d11eb2dec4898a32b6b165f1f4cae6aa0079498eab50293f3b8defbf6af11bb75f0408a563ddfc26a3323d1ff5f9849e95d5f034d88a757ddea032c75c00708c9ff34d2207f997cc7d93fd1fa160a6bfaf62a54e31f9fe67ab95752106ba9d185bfdc9b6dc3e17427f844ee74e5c09b17b83ad6e8fc7360f5c7c3e4e1939e77a6374bee57d1fa6b2322b11ad56ad0398302de9b26d6fbfe414aa416bff141fad9d4af6aea19322e47595e342cd377403f417dfd396ab5f151095a5535f51cbc34a40ce9648927b7d1d72ab9daf253e31daf",
"transactions_root": "0xe4c7bf3aff7ad07f9e80d57f7189f0252592fee6321c2a9bd9b09b6ce0690d27",
"state_root": "0x49e3bfe7b618e27fde8fa08884803a8458b502c6534af69873a3cc926a7c724b",
"receipts_root": "0x7cf43d7e837284f036cf92c56973f5e27bdd253ca46168fa195a6b07fa719f23",
"miner": "0xea674fdde714fd979de3edf0f56aa9716b898ec8",
"difficulty": "7253857437305950",
"total_difficulty": "24325637817906576196890",
"size": "61271",
"extra_data": "0x65746865726d696e652d6575726f70652d7765737433",
"gas_limit": "14977947",
"gas_used": "14964688",
"transaction_count": "252",
"transactions": [
{
"hash": "0x1ed85b3757a6d31d01a4d6677fc52fd3911d649a0af21fe5ca3f886b153773ed",
"nonce": "1848059",
"transaction_index": "108",
"from_address": "0x267be1c1d684f78cb4f6a176c4911b741e4ffdc0",
"to_address": "0x003dde3494f30d861d063232c6a8c04394b686ff",
"value": "115580000000000000",
"gas": "30000",
"gas_price": "52500000000",
"input": "0x",
"receipt_cumulative_gas_used": "4923073",
"receipt_gas_used": "21000",
"receipt_contract_address": null,
"receipt_root": null,
"receipt_status": "1",
"block_timestamp": "2021-05-07T11:08:35.000Z",
"block_number": "12386788",
"block_hash": "0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171",
"logs": [
{
"log_index": "273",
"transaction_hash": "0xdd9006489e46670e0e85d1fb88823099e7f596b08aeaac023e9da0851f26fdd5",
"transaction_index": "204",
"address": "0x3105d328c66d8d55092358cf595d54608178e9b5",
"data": "0x00000000000000000000000000000000000000000000000de05239bccd4d537400000000000000000000000000024dbc80a9f80e3d5fc0a0ee30e2693781a443",
"topic0": "0x2caecd17d02f56fa897705dcc740da2d237c373f70686f4e0d9bd3bf0400ea7a",
"topic1": "0x000000000000000000000000031002d15b0d0cd7c9129d6f644446368deae391",
"topic2": "0x000000000000000000000000d25943be09f968ba740e0782a34e710100defae9",
"topic3": null,
"block_timestamp": "2021-05-07T11:08:35.000Z",
"block_number": "12386788",
"block_hash": "0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171"
}
]
}
]
}

getDateToBlock

Retrieve the closest block of the provided date (asynchronous).

Options:

  • chain(optional): The blockchain to get data from. Valid values are listed on Supported Chains. Default value Eth.
  • date (required): Unix date in miliseconds or a datestring (any format that is accepted by momentjs)
JS
React
curl
Unity
const options = {
chain: "bsc",
date: "2021-09-29T13:09:15+00:00",
};
const date = await Moralis.Web3API.native.getDateToBlock(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchDateToBlock = async () => {
const options = { chain: "bsc", date: "2021-09-29T13:09:15+00:00" };
const date = await Web3Api.native.getDateToBlock(options);
console.log(date);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/dateToBlock?chain=bsc&date=2021-09-29T13%3A09%3A15%2B00%3A00' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using MoralisUnity;
using MoralisUnity.Web3Api.Models;
using UnityEngine;
public class Example
{
public async void fetchDateToBlock()
{
BlockDate blockDate = await Moralis.Web3Api.Native.GetDateToBlock(date: "2021-09-29T13:09:15+00:00", ChainList.eth);
Debug.Log(blockDate.ToJson());
}
}

Example result:

{
"date": "2020-01-01T00:00:00+00:00",
"block": 9193266,
"timestamp": 1577836811
}

🔥 getLogsByAddress (new)

Retrieve the logs from an address (asynchronous).

Options:

  • chain(optional): The blockchain to get data from. Valid values are listed on Supported Chains. Default value Eth.
  • from_date (optional): The date from where to get the transactions (any format that is accepted by momentjs). Provide the param 'from_block' or 'from_date' If 'from_date' and 'from_block' are provided, 'from_block' will be used.
  • to_date (optional): Get the transactions to this date (any format that is accepted by momentjs). Provide the param 'to_block' or 'to_date' If 'to_date' and 'to_block' are provided, 'to_block' will be used.
  • from_block (optional): The minimum block number from where to get the transactions Provide the param 'from_block' or 'from_date' If 'from_date' and 'from_block' are provided, 'from_block' will be used.
  • to_block (optional): The maximum block number from where to get the transactions. Provide the param 'to_block' or 'to_date' If 'to_date' and 'to_block' are provided, 'to_block' will be used.
  • address (required): A smart contract address
  • topic0 (optional): Event topic
  • topic1 (optional): Event topic
  • topic2 (optional): Event topic
  • topic3 (optional): Event topic
JS
React
curl
Unity
const options = {
address: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
chain: "bsc",
topic0: "0x2caecd17d02f56fa897705dcc740da2d237c373f70686f4e0d9bd3bf0400ea7a",
topic1: "0x000000000000000000000000031002d15b0d0cd7c9129d6f644446368deae391",
topic2: "0x000000000000000000000000d25943be09f968ba740e0782a34e710100defae9",
};
const logs = await Moralis.Web3API.native.getLogsByAddress(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchLogsByAddress = async () => {
const options = {
address: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
chain: "bsc",
topic0:
"0x2caecd17d02f56fa897705dcc740da2d237c373f70686f4e0d9bd3bf0400ea7a",
topic1:
"0x000000000000000000000000031002d15b0d0cd7c9129d6f644446368deae391",
topic2:
"0x000000000000000000000000d25943be09f968ba740e0782a34e710100defae9",
};
const logs = await Web3Api.native.getLogsByAddress(options);
console.log(logs);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/0x057Ec652A4F150f7FF94f089A38008f49a0DF88e/logs?chain=bsc&topic0=0x2caecd17d02f56fa897705dcc740da2d237c373f70686f4e0d9bd3bf0400ea7a&topic1=0x000000000000000000000000031002d15b0d0cd7c9129d6f644446368deae391&topic2=0x000000000000000000000000d25943be09f968ba740e0782a34e710100defae9' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using MoralisUnity;
using MoralisUnity.Web3Api.Models;
using UnityEngine;
public class Example
{
public async void fetchLogsByAddress()
{
LogEventByAddress logEvents = await Moralis.Web3Api.Native.GetLogsByAddress(address: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e", ChainList.bsc, topic0: "0x2caecd17d02f56fa897705dcc740da2d237c373f70686f4e0d9bd3bf0400ea7a", topic1: "0x000000000000000000000000031002d15b0d0cd7c9129d6f644446368deae391", topic2: "0x000000000000000000000000d25943be09f968ba740e0782a34e710100defae9");
Debug.Log(logEvents.ToJson());
}
}

Example result:

{
"transaction_hash": "0x2d30ca6f024dbc1307ac8a1a44ca27de6f797ec22ef20627a1307243b0ab7d09",
"address": "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
"block_timestamp": "2021-04-02T10:07:54.000Z",
"block_number": "12526958",
"block_hash": "0x0372c302e3c52e8f2e15d155e2c545e6d802e479236564af052759253b20fd86",
"data": "0x00000000000000000000000000000000000000000000000de05239bccd4d537400000000000000000000000000024dbc80a9f80e3d5fc0a0ee30e2693781a443",
"topic0": "0x2caecd17d02f56fa897705dcc740da2d237c373f70686f4e0d9bd3bf0400ea7a",
"topic1": "0x000000000000000000000000031002d15b0d0cd7c9129d6f644446368deae391",
"topic2": "0x000000000000000000000000d25943be09f968ba740e0782a34e710100defae9",
"topic3": null
}

getContractEvents

Get the events in descending order based on block number. Returns an object with the number of Contract events and the array of Contract events (asynchronous).

Options:

  • chain(optional): The blockchain to get data from. Valid values are listed on Supported Chains. Default value Eth.
  • offset(optional): Offset.
  • limit(optional): Limit.
  • from_block (optional): To get contract events starting from this block
  • to_block (optional): To get contract events up to this block
  • topic (required): The topic of the event
  • address (required): A smart contract address
  • abi (required): Event ABI (do not insert the ABI of the whole smart contract). ABI has a JSON format.
JS
React
curl
Unity
const ABI = {
anonymous: false,
inputs: [
{ indexed: true, name: "from", type: "address" },
{ indexed: true, name: "to", type: "address" },
{ indexed: false, name: "value", type: "uint256" },
],
name: "Transfer",
type: "event",
};
const options = {
chain: "eth",
address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
topic: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
limit: "3",
abi: ABI,
};
const events = await Moralis.Web3API.native.getContractEvents(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const ABI = {
anonymous: false,
inputs: [
{ indexed: true, name: "from", type: "address" },
{ indexed: true, name: "to", type: "address" },
{ indexed: false, name: "value", type: "uint256" },
],
name: "Transfer",
type: "event",
};
const fetchContractEvents = async () => {
const options = {
chain: "eth",
address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
topic: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
limit: "3",
abi: ABI,
};
const events = await Web3Api.native.getContractEvents(options);
console.log(events);
};
curl -X 'POST' \
'https://deep-index.moralis.io/api/v2/0xdAC17F958D2ee523a2206206994597C13D831ec7/events?chain=eth&topic=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef&limit=3' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY' \
-H 'Content-Type: application/json' \
-d '{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}'
using MoralisUnity;
using MoralisUnity.Web3Api.Models;
using System.Collections.Generic;
using UnityEngine;
public class Example
{
public async void fetchContractEvents()
{
string abi = "{anonymous: false, inputs:[{ indexed: true, name: \"from\", type: \"address\" },{ indexed: true, name: \"to\", type: \"address\" },{ indexed: false, name: \"value\", type: \"uint256\" },],name: \"Transfer\", type: \"event\" }";
List<LogEvent> logEvents = await Moralis.Web3Api.Native.GetContractEvents(address: "0xdAC17F958D2ee523a2206206994597C13D831ec7", topic: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", abi: abi, ChainList.eth);
foreach (LogEvent eventlog in logEvents)
{
Debug.Log(eventlog.ToJson());
}
}
}
Example result:

getNFTTransfersByBlock

Retrieve NFT transfers by block number or block hash. Returns an array of NFT transfers (asynchronous).

Options:

  • chain(optional): The blockchain to get data from. Valid values are listed on Supported Chains. Default value Eth.
  • block_number_or_hash (required): The block hash or block number.
JS
React
curl
Unity
const options = { chain: "bsc", block_number_or_hash: "11284830" };
// get NFT transfers by block number or block hash
const NFTTransfers = await Moralis.Web3API.native.getNFTTransfersByBlock(
options
);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchNFTTransfersByBlock = async () => {
// get NFT transfers by block number or block hash
const options = { chain: "bsc", block_number_or_hash: "11284830" };
const NFTTransfers = await Web3Api.native.getNFTTransfersByBlock(options);
console.log(NFTTransfers);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/block/11284830/nft/transfers?chain=bsc&limit=500' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using MoralisUnity;
using MoralisUnity.Web3Api.Models;
using UnityEngine;
public class Example
{
public async void fetchNFTTransfersByBlock()
{
NftTransferCollection nftTransfers = await Moralis.Web3Api.Native.GetNFTTransfersByBlock(blockNumberOrHash: "11284830", ChainList.bsc);
Debug.Log(nftTransfers.ToJson());
}
}

Example result:

[
{
amount: "1",
block_hash:
"0x4c00bcee8900e5fc4f6a5d11dff6edca3b97470da290986940da1d2f0ef221fb",
block_number: "11284830",
block_timestamp: "2021-09-27T17:07:54.000Z",
contract_type: "ERC721",
from_address: "0x6102f780a5c51dee8ce46c5db5b62d88119d559d",
log_index: 130,
to_address: "0xd95f7419ce0debf3a991ef9944808010928db319",
token_address: "0xcf58705e2ff8642c4276c2dd2747ec8af5bafc1d",
token_id: "281",
transaction_hash:
"0x19721cf6855f412f82de87f8c6050e6de3cc77a81388002bc24efea53bd36a0f",
transaction_index: 35,
transaction_type: "Single",
},
];

🔥 getTransaction (new)

Get the transaction by transaction hash. Returns a transaction object (asynchronous).

Options:

  • chain(optional): The blockchain to get data from. Valid values are listed on Supported Chains. Default value Eth.
  • transaction_hash (required): The transaction hash.
JS
React
curl
Unity
const options = {
chain: "eth",
transaction_hash:
"0x5e519cd5117aea6ed9d51d4f235b4badb2e3f69377a4e2f945e13feb20af4db3",
};
const transaction = await Moralis.Web3API.native.getTransaction(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchTransaction = async () => {
const options = {
chain: "eth",
transaction_hash:
"0x5e519cd5117aea6ed9d51d4f235b4badb2e3f69377a4e2f945e13feb20af4db3",
};
const transaction = await Web3Api.native.getTransaction(options);
console.log(transaction);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/transaction/0x5e519cd5117aea6ed9d51d4f235b4badb2e3f69377a4e2f945e13feb20af4db3?chain=eth' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using MoralisUnity;
using MoralisUnity.Web3Api.Models;
using UnityEngine;
public class Example
{
public async void fetchTransaction()
{
BlockTransaction blockTransaction = await Moralis.Web3Api.Native.GetTransaction(transactionHash: "0x5e519cd5117aea6ed9d51d4f235b4badb2e3f69377a4e2f945e13feb20af4db3", ChainList.eth);
Debug.Log(blockTransaction.ToJson());
}
}

Example result:

{
"hash": "0x1ed85b3757a6d31d01a4d6677fc52fd3911d649a0af21fe5ca3f886b153773ed",
"nonce": "1848059",
"transaction_index": "108",
"from_address": "0x267be1c1d684f78cb4f6a176c4911b741e4ffdc0",
"to_address": "0x003dde3494f30d861d063232c6a8c04394b686ff",
"value": "115580000000000000",
"gas": "30000",
"gas_price": "52500000000",
"input": "0x",
"receipt_cumulative_gas_used": "4923073",
"receipt_gas_used": "21000",
"receipt_contract_address": null,
"receipt_root": null,
"receipt_status": "1",
"block_timestamp": "2021-05-07T11:08:35.000Z",
"block_number": "12386788",
"block_hash": "0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171",
"logs": [
{
"log_index": "273",
"transaction_hash": "0xdd9006489e46670e0e85d1fb88823099e7f596b08aeaac023e9da0851f26fdd5",
"transaction_index": "204",
"address": "0x3105d328c66d8d55092358cf595d54608178e9b5",
"data": "0x00000000000000000000000000000000000000000000000de05239bccd4d537400000000000000000000000000024dbc80a9f80e3d5fc0a0ee30e2693781a443",
"topic0": "0x2caecd17d02f56fa897705dcc740da2d237c373f70686f4e0d9bd3bf0400ea7a",
"topic1": "0x000000000000000000000000031002d15b0d0cd7c9129d6f644446368deae391",
"topic2": "0x000000000000000000000000d25943be09f968ba740e0782a34e710100defae9",
"topic3": null,
"block_timestamp": "2021-05-07T11:08:35.000Z",
"block_number": "12386788",
"block_hash": "0x9b559aef7ea858608c2e554246fe4a24287e7aeeb976848df2b9a2531f4b9171"
}
]
}