Moralis Web3 企业级API Moralis Web3API.account

2024-02-26 开发教程 Moralis Web3 企业级API 匿名 5

getTransactions

从当前用户或地址返回交易。 返回具有事务数和本机事务数组(异步)的对象。

选项:

  • chain​(可选):从中获取数据的区块链。有效值列在支持的链上。默认值 Eth。
  • address​(可选):用户地址(即 0x1a2b3x...)。如果指定,则附加到查询的用户将被忽略,而是使用地址。
  • from_date​(可选):获取交易的日期(momentjs 接受的任何格式)。提供参数 'from_block' 或 'from_date' 如果提供了 'from_date' 和 'from_block',则将使用 'from_block'。
  • to_date​(可选):获取到此日期的交易(momentjs 接受的任何格式)。提供参数 'to_block' 或 'to_date' 如果提供了 'to_date' 和 'to_block',将使用 'to_block'。
  • from_block​(可选):从哪里获取交易的最小块号提供参数'from_block'或'from_date'如果提供'from_date'和'from_block',将使用'from_block'。
  • to_block​(可选):从哪里获取交易的最大块号。提供参数 'to_block' 或 'to_date' 如果提供了 'to_date' 和 'to_block',将使用 'to_block'。
  • offset​(可选):偏移量。
  • limit​(可选):限制

你可以使用​JS​、​React​、​curl​、​Unity​来实现

// get mainnet transactions for the current user
const transactions = await Moralis.Web3API.account.getTransactions();
// get BSC transactions for a given address
// with most recent transactions appearing first
const options = {
chain: "bsc",
address: "0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e",
order: "desc",
from_block: "0",
};
const transactions = await Moralis.Web3API.account.getTransactions(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchTransactions = async () => {
// get mainnet transactions for the current user
const transactions = await Web3Api.account.getTransactions();
console.log(transactions);
// get BSC transactions for a given address
// with most recent transactions appearing first
const options = {
chain: "bsc",
address: "0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e",
order: "desc",
from_block: "0",
};
const bscTransactions = await Web3Api.account.getTransactions(options);
console.log(bscTransactions);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e?chain=bsc&from_block=0' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using System.Collections.Generic;
using Moralis.Web3Api.Models;
using MoralisWeb3ApiSdk;
public async void GetTransactions()
{
// get BSC transactions for a given address
TransactionCollection BSCtransactions = await MoralisInterface.GetClient().Web3Api.Account.GetTransactions("0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e".ToLower(), ChainList.bsc);
print(BSCtransactions.ToJson());
}

示例结果;

[
{
hash: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
nonce: "326595425",
transaction_index: "25",
from_address: "0xd4a3BebD824189481FC45363602b83C9c7e9cbDf",
to_address: "0xa71db868318f0a0bae9411347cd4a6fa23d8d4ef",
value: "650000000000000000",
gas: "6721975",
gas_price: "20000000000",
input: "string",
receipt_cumulative_gas_used: "1340925",
receipt_gas_used: "1340925",
receipt_contract_address: "0x1d6a4cf64b52f6c73f201839aded7379ce58059c",
receipt_root: "string",
receipt_status: "1",
block_timestamp: "2021-04-02T10:07:54.000Z",
block_number: "12526958",
block_hash:
"0x0372c302e3c52e8f2e15d155e2c545e6d802e479236564af052759253b20fd86",
},
];

getNativeBalance

返回特定地址的本机余额(异步)。

选项:

  • chain​(可选):从中获取数据的区块链。 有效值列在支持的链上。 默认值 Eth。
  • to_block​(可选):应检查余额的块号。
  • address​(可选):将检查本机余额的地址。 如果指定,则附加到查询的用户将被忽略,而是使用地址。 如果用户未通过钱包进行身份验证,则必须指定地址。

你可以使用​JS​、​React​、​curl​、​Unity​来实现

// get mainnet native balance for the current user
const balance = await Moralis.Web3API.account.getNativeBalance();
// get BSC native balance for a given address
const options = {
chain: "bsc",
address: "0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e",
to_block: "1234",
};
const balance = await Moralis.Web3API.account.getNativeBalance(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchNativeBalance = async () => {
// get mainnet native balance for the current user
const balance = await Web3Api.account.getNativeBalance();
console.log(balance);
// get BSC native balance for a given address
const options = {
chain: "bsc",
address: "0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e",
to_block: "1234",
};
const bscBalance = await Web3Api.account.getNativeBalance(options);
console.log(bscBalance);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e/balance?chain=bsc' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using System.Collections.Generic;
using Moralis.Web3Api.Models;
using MoralisWeb3ApiSdk;
public async void GetNativeBalance()
{
// get BSC native balance for a given address
NativeBalance BSCbalance = await MoralisInterface.GetClient().Web3Api.Account.GetNativeBalance("0x4c6Ec2448C243B39Cd1e9E6db0F9bF7436c0c93f".ToLower(), ChainList.bsc);
print(BSCbalance.ToJson());
}

示例结果;

{
"balance": "1234567890"
}

getTokenBalances

检索当前用户或指定地址的所有代币余额。 返回一个包含令牌数量和令牌对象数组的对象(异步)。

选项:

  • chain​(可选):从中获取数据的区块链。 有效值列在支持的链上。 默认值 Eth。
  • address​(可选):用户地址(即 0x1a2b3x...)。 如果指定,则附加到查询的用户将被忽略,而是使用地址。
  • to_block​(可选):应检查余额的块号

你可以使用​JS​、​React​、​curl​、​Unity​来实现

const balances = await Moralis.Web3API.account.getTokenBalances();
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchTokenBalances = async () => {
const balances = await Web3Api.account.getTokenBalances();
console.log(balances);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e/erc20' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using System.Collections.Generic;
using Moralis.Web3Api.Models;
using MoralisWeb3ApiSdk;
public async void fetchTokenBalance()
{
List<Erc20TokenBalance> balance = await MoralisInterface.GetClient().Web3Api.Account.GetTokenBalances("0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e".ToLower(), ChainList.bsc);
foreach (Erc20TokenBalance erc20bal in balance)
{
print(erc20bal.ToJson());
}
}

如果没有指定任何参数,它默认为 '​Eth​' 作为链和当前用户,但您也可以在选项对象中指定​chain​和​address​(可使用​JS​、​React​、​curl​实现):

const options = {
chain: "bsc",
address: "0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e",
to_block: "10253391",
};
const balances = await Moralis.Web3API.account.getTokenBalances(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchTokenBalances = async () => {
const options = {
chain: "bsc",
address: "0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e",
to_block: "10253391",
};
const balances = await Web3Api.account.getTokenBalances(options);
console.log(balances);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e/erc20?chain=bsc&to_block=10253391' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'

示例结果:

[
{
token_address:
"0x2d30ca6f024dbc1307ac8a1a44ca27de6f797ec22ef20627a1307243b0ab7d09",
name: "Kylin Network",
symbol: "KYL",
logo: "https://cdn.moralis.io/eth/0x67b6d479c7bb412c54e03dca8e1bc6740ce6b99c.png",
thumbnail:
"https://cdn.moralis.io/eth/0x67b6d479c7bb412c54e03dca8e1bc6740ce6b99c_thumb.png",
decimals: "18",
balance: "123456789",
},
];

getTokenTransfers

从当前用户或地址获取 ERC20 代币转账。 返回一个包含令牌传输次数和令牌传输数组(异步)的对象。

选项:

  • chain​(可选):从中获取数据的区块链。有效值列在支持的链上。默认值 Eth。
  • address​(可选):用户地址(即 0x1a2b3x...)。如果指定,则附加到查询的用户将被忽略,而是使用地址。
  • from_date​(可选):获取交易的日期(momentjs 接受的任何格式)。提供参数 'from_block' 或 'from_date' 如果提供了 'from_date' 和 'from_block',则将使用 'from_block'。
  • to_date​(可选):获取到此日期的交易(momentjs 接受的任何格式)。提供参数 'to_block' 或 'to_date' 如果提供了 'to_date' 和 'to_block',将使用 'to_block'。
  • from_block​(可选):从哪里获取交易的最小块号提供参数'from_block'或'from_date'如果提供'from_date'和'from_block',将使用'from_block'。
  • to_block​(可选):从哪里获取交易的最大块号。提供参数 'to_block' 或 'to_date' 如果提供了 'to_date' 和 'to_block',将使用 'to_block'。
  • offset​(可选):偏移量。
  • limit​(可选):限制

你可以使用​JS​、​React​、​curl​、​Unity​来实现

// get mainnet transfers for the current user
const userTrans = await Moralis.Web3API.account.getTokenTransfers();
// get BSC transfers for a given address
// with most recent transfers appearing first
const options = {
chain: "bsc",
address: "0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e",
from_block: "0",
};
const transfers = await Moralis.Web3API.account.getTokenTransfers(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchTokenTransfers = async () => {
// get mainnet transfers for the current user
const userTrans = await Web3Api.account.getTokenTransfers();
console.log(userTrans);
// get BSC transfers for a given address
// with most recent transfers appearing first
const options = {
chain: "bsc",
address: "0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e",
from_block: "0",
};
const transfers = await Web3Api.account.getTokenTransfers(options);
console.log(transfers);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e/erc20/transfers?chain=bsc&from_block=0' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using System.Collections.Generic;
using Moralis.Web3Api.Models;
using MoralisWeb3ApiSdk;
public async void fetchTokenTransfers()
{
Erc20TransactionCollection transfers = await MoralisInterface.GetClient().Web3Api.Account.GetTokenTransfers("0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e".ToLower(), ChainList.bsc);
print(transfers.ToJson());
}

示例结果:

[
{
transaction_hash:
"0x2d30ca6f024dbc1307ac8a1a44ca27de6f797ec22ef20627a1307243b0ab7d09",
address: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
block_timestamp: "2021-04-02T10:07:54.000Z",
block_number: "12526958",
block_hash:
"0x0372c302e3c52e8f2e15d155e2c545e6d802e479236564af052759253b20fd86",
to_address: "0x62AED87d21Ad0F3cdE4D147Fdcc9245401Af0044",
from_address: "0xd4a3BebD824189481FC45363602b83C9c7e9cbDf",
value: "650000000000000000",
},
];

getNFTs

从当前用户或地址获取所有 NFT。 支持 ERC721 和 ERC1155。 返回具有 NFT 对象数量和 NFT 对象数组(异步)的对象。

选项:

  • chain​(可选):从中获取数据的区块链。 有效值列在支持的链上。 默认值 Eth。
  • address​(可选):用户地址(即 0x1a2b3x...)。 如果指定,则附加到查询的用户将被忽略,而是使用地址。

你可以使用​JS​、​React​、​curl​、​Unity​来实现

// get NFTs for current user on Mainnet
const userEthNFTs = await Moralis.Web3API.account.getNFTs();
// get testnet NFTs for user
const testnetNFTs = await Moralis.Web3API.account.getNFTs({ chain: "ropsten" });
// get polygon NFTs for address
const options = {
chain: "polygon",
address: "0x75e3e9c92162e62000425c98769965a76c2e387a",
};
const polygonNFTs = await Moralis.Web3API.account.getNFTs(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchNFTs = async () => {
// get NFTs for current user on Mainnet
const userEthNFTs = await Web3Api.account.getNFTs();
console.log(userEthNFTs);
// get testnet NFTs for user
const testnetNFTs = await Web3Api.Web3API.account.getNFTs({
chain: "ropsten",
});
console.log(testnetNFTs);
// get polygon NFTs for address
const options = {
chain: "polygon",
address: "0x75e3e9c92162e62000425c98769965a76c2e387a",
};
const polygonNFTs = await Web3Api.account.getNFTs(options);
};
console.log(polygonNFTs);
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/0x75e3e9c92162e62000425c98769965a76c2e387a/nft?chain=polygon&format=decimal' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using System.Collections.Generic;
using Moralis.Web3Api.Models;
using MoralisWeb3ApiSdk;
public async void fetchNFTs()
{
NftOwnerCollection polygonNFTs = await MoralisInterface.GetClient().Web3Api.Account.GetNFTs("0x75e3e9c92162e62000425c98769965a76c2e387a".ToLower(), ChainList.polygon);
print(polygonNFTs.ToJson());
}

示例结果:

[
{
token_address: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
token_id: "15",
contract_type: "ERC721",
owner_of: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
block_number: "88256",
block_number_minted: "88256",
token_uri: "string",
metadata: "string",
synced_at: "string",
amount: "1",
name: "CryptoKitties",
symbol: "RARI",
},
];

getNFTTransfers

获取 NFT 转账。 返回具有 NFT 传输次数和 NFT 传输数组(异步)的对象。

选项:

  • chain​(可选):从中获取数据的区块链。 有效值列在支持的链上。 默认值 Eth。
  • format​(可选):token id 的格式。 可用值:十进制、十六进制。 默认值:十进制。
  • offset​(可选):偏移量。
  • direction​(可选):传输方向。 可用值:both、to、from 。 默认值:两者。
  • limit​(可选):限制。
  • order​(可选):要排序的字段以及是否应按升序或降序排序。
  • address​(可选):用户地址(即 0x1a2b3x...)。 如果指定,则附加到查询的用户将被忽略,而是使用地址。

你可以使用​JS​、​React​、​curl​、​Unity​来实现

// get mainnet NFT transfers for the current user
const transfersNFT = await Moralis.Web3API.account.getNFTTransfers();
// get BSC NFT transfers for a given address
// with most recent transactions appearing first
const options = {
chain: "polygon",
address: "0x75e3e9c92162e62000425c98769965a76c2e387a",
limit: "5",
};
const transfersNFT = await Moralis.Web3API.account.getNFTTransfers(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchNFTTransfers = async () => {
// get mainnet NFT transfers for the current user
const transfersNFT = await Web3Api.account.getNFTTransfers();
console.log(transfersNFT);
// get BSC NFT transfers for a given address
// with most recent transactions appearing first
const options = {
chain: "polygon",
address: "0x75e3e9c92162e62000425c98769965a76c2e387a",
limit: "5",
};
const bscTransfersNFT = await Web3Api.account.getNFTTransfers(options);
console.log(bscTransfersNFT);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/0x75e3e9c92162e62000425c98769965a76c2e387a/nft/transfers?chain=polygon&format=decimal&direction=both&limit=5' \
-H 'accept: application/json' \
-H 'X-API-Key: MY-API-KEY'
using System.Collections.Generic;
using Moralis.Web3Api.Models;
using MoralisWeb3ApiSdk;
public async void fetchNFTTransfers()
{
NftTransferCollection BSCnfttransfers = await MoralisInterface.GetClient().Web3Api.Account.GetNFTTransfers("0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e".ToLower(), ChainList.bsc);
print(BSCnfttransfers.ToJson());
}

使用 ​token_address​ 参数仅获取特定合约的结果。
注意结果将包括所有索引的 ​NFT​。
任何包含 ​token_address​ 参数的请求都将在第一次被请求时启动该 ​NFT ​集合的索引过程。

示例结果:

[
{
token_address: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
token_id: "15",
from_address: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
to_address: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
amount: "1",
contract_type: "ERC721",
block_number: "88256",
block_timestamp: "2021-06-04T16:00:15",
block_hash: "string",
transaction_hash: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
transaction_type: "string",
transaction_index: "string",
log_index: 0,
},
];

getNFTsForContract

返回具有指定合约的 NFT 计数的对象和属于指定合约的给定地址的 NFT 数组(异步)。

选项:

  • chain​(可选):从中获取数据的区块链。 有效值列在支持的链上。 默认值 Eth。
  • format​(可选):token id 的格式。 可用值:十进制、十六进制。 默认值:十进制。
  • offset​(可选):偏移量。
  • limit​(可选):限制。
  • order​(可选):要排序的字段以及是否应按升序或降序排序。
  • address​(可选):给定令牌的所有者(即 0x1a2b3x...)。 如果指定,则附加到查询的用户将被忽略,而是使用地址。
  • token_address​(必选): 合约地址

你可以使用​JS​、​React​、​curl​、​Unity​来实现

const options = {
chain: "polygon",
address: "0x75e3e9c92162e62000425c98769965a76c2e387a",
token_address: "0x2953399124F0cBB46d2CbACD8A89cF0599974963",
};
const polygonNFTs = await Moralis.Web3API.account.getNFTsForContract(options);
import React from "react";
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
const fetchNFTsForContract = async () => {
const options = {
chain: "polygon",
address: "0x75e3e9c92162e62000425c98769965a76c2e387a",
token_address: "0x2953399124F0cBB46d2CbACD8A89cF0599974963",
};
const polygonNFTs = await Web3Api.account.getNFTsForContract(options);
console.log(polygonNFTs);
};
curl -X 'GET' \
'https://deep-index.moralis.io/api/v2/0x75e3e9c92162e62000425c98769965a76c2e387a/nft/0x2953399124F0cBB46d2CbACD8A89cF0599974963?chain=polygon&format=decimal' \
-H 'accept: application/json' \
-H 'X-API-Key: My-API-KEY'
using System.Collections.Generic;
using Moralis.Web3Api.Models;
using MoralisWeb3ApiSdk;
public async void fetchNFTsForContract()
{
NftOwnerCollection polygonNFTs = await MoralisInterface.GetClient().Web3Api.Account.GetNFTsForContract("0x3d6c0e79a1239df0039ec16Cc80f7A343b6C530e".ToLower(),"0x2953399124F0cBB46d2CbACD8A89cF0599974963", ChainList.polygon);
print(polygonNFTs.ToJson());
}

使用 ​token_address​ 参数仅获取特定合约的结果。
注意结果将包括所有索引的 ​NFT​。
任何包含 ​token_address ​参数的请求都将在第一次被请求时启动该 ​NFT ​集合的索引过程。

示例结果:

{
total: 5,
page: 0,
page_size: 1,
cursor: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ3aGVyZSI6eyJ0b2tlbl9hZGRyZXNzIjoiMHgyOTUzMzk5MTI0ZjBjYmI0NmQyY2JhY2Q4YTg5Y2YwNTk5OTc0OTYzIiwib3duZXJfb2YiOiIweDc1ZTNlOWM5MjE2MmU2MjAwMDQyNWM5ODc2OTk2NWE3NmMyZTM4N2EifSwibGltaXQiOjEsIm9mZnNldCI6MSwib3JkZXIiOltbInRyYW5zZmVyX2luZGV4IiwiREVTQyJdXSwicGFnZSI6MSwiaWF0IjoxNjQ2NDkxMzYzfQ.2emXTUoQYAV5dcC-05fkX5bHuuCHgL8aSQ2P9nqJPs0",
result: [
{
"token_address": "0x2953399124f0cbb46d2cbacd8a89cf0599974963",
"token_id": "54882136101329053367331551663964422650505490251725779255107807133728757514309",
"block_number_minted": "22364568",
"owner_of": "0x75e3e9c92162e62000425c98769965a76c2e387a",
"block_number": "22728905",
"amount": "1",
"contract_type": "ERC1155",
"name": "OpenSea Collections",
"symbol": "OPENSTORE",
"token_uri": "https://api.opensea.io/api/v2/metadata/matic/0x2953399124F0cBB46d2CbACD8A89cF0599974963/0x7956302fe62df98c5c7f35354a2d03eb8b160f0e000000000000020000000045",
"metadata": null,
"synced_at": "2021-12-10T17:04:09.775Z",
"is_valid": 0,
"syncing": 2,
"frozen": 0
}
],
status: "SYNCED"
}