Moralis 账户

2022-05-13 10:35 更新

所有方法都扩展自 ​Moralis.SolanaAPI.account

balance

返回地址的 SOL 余额。

选项:

network​:要从中获取数据的网络集群。 有效值列在支持的网络上。 默认值主网。

address​:用户地址(即 6XU36wCxWobLx5Rtsb58kmgAJKVYmmVqy4SHXxENAyAe)。 如果指定,则附加到查询的用户将被忽略,而是使用地址。

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

// get mainnet SOL balance for the current user
const solBalance = await Moralis.SolanaAPI.account.balance();

// get devnet SOL balance for a given address
const options = {
  network: "devnet",
  address: "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe",
};
const solBalance = await Moralis.SolanaAPI.account.balance(options);
import { useMoralisSolanaApi, useMoralisSolanaCall } from "react-moralis";

const { account } = useMoralisSolanaApi();

// get mainnet SOL balance for the current user
const { fetch, data, isLoading } = useMoralisSolanaCall(account.balance);

// get devnet SOL balance for a given address
const options = {
  network: "devnet",
  address: "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe",
};
const { fetch, data, isLoading } = useMoralisSolanaCall(
  account.balance,
  options
);
curl -X 'GET' \
  'https://solana-gateway.moralis.io/account/devnet/6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe/balance' \
  -H 'accept: application/json' \
  -H 'X-API-Key: MY-API-KEY'
using System.Collections.Generic;
using Moralis.SolanaApi.Models;
using Moralis.SolanaApi;
using MoralisWeb3ApiSdk;

  public async void GetSolNativeBalance()
  {
    NativeBalance solBalance = await MoralisSolanaClient.SolanaApi.Account.Balance(NetworkTypes.mainnet, "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe");
    print(solBalance);
  }

示例结果

{
  "lamports": "500000000",
  "solana": "0.5"
}

getSPL

返回地址的 SPL 代币余额。

选项:

  • network​:要从中获取数据的网络集群。 有效值列在支持的网络上。 默认值主网。
  • address​:用户地址(即 HsXZnAba2...)。 如果指定,则附加到查询的用户将被忽略,而是使用地址。

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

// get mainnet SPL token balance for the current user
const tokenBalance = await Moralis.SolanaAPI.account.getSPL();

// get devnet SPL token balance for a given address
const options = {
  network: "devnet",
  address: "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe",
};
const tokenBalance = await Moralis.SolanaAPI.account.getSPL(options);
import { useMoralisSolanaApi, useMoralisSolanaCall } from "react-moralis";

const { account } = useMoralisSolanaApi();

// get mainnet SPL token balance for the current user
const { fetch, data, isLoading } = useMoralisSolanaCall(account.getSPL);

// get devnet SPL token balance for a given address
const options = {
  network: "devnet",
  address: "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe",
};
const { fetch, data, isLoading } = useMoralisSolanaCall(
  account.getSPL,
  options
);
curl -X 'GET' \
  'https://solana-gateway.moralis.io/account/devnet/6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe/tokens' \
  -H 'accept: application/json' \
  -H 'X-API-Key: MY-API-KEY'
using System.Collections.Generic;
using Moralis.SolanaApi.Models;
using Moralis.SolanaApi;
using MoralisWeb3ApiSdk;

  public async void GetSPLTokens()
  {
    List<SplTokenBalanace> spltokens = await MoralisSolanaClient.SolanaApi.Account.GetSplTokens(NetworkTypes.mainnet, "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe");
    foreach(SplTokenBalanace spltoken in spltokens){
      print(spltoken);
   }
  }

示例结果

[
  {
    associatedTokenAddress: "Dpmpwm93Amvj4uEFpYhjv8ZzfpgARq6zxKTi6mrj97gW",
    mint: "BXWuzb3jEuGsGUe29xdApu8Z3jVgrFbr3wWdsZmLWYk9",
    amountRaw: "100000000000",
    amount: "100",
    decimals: "9",
  },
];

getNFTs

返回地址的 SPL NFT 余额。

选项:

  • network​:要从中获取数据的网络集群。 有效值列在支持的网络上。 默认值主网。
  • address​:用户地址(即 HsXZnAba2...)。 如果指定,则附加到查询的用户将被忽略,而是使用地址。

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

// get mainnet SPL NFT balance for the current user
const nftBalance = await Moralis.SolanaAPI.account.getNFTs();

// get devnet SPL NFT balance for a given address
const options = {
  network: "devnet",
  address: "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe",
};
const nftBalance = await Moralis.SolanaAPI.account.getNFTs(options);
import { useMoralisSolanaApi, useMoralisSolanaCall } from "react-moralis";

const { account } = useMoralisSolanaApi();

// get mainnet SPL NFT balance for the current user
const { fetch, data, isLoading } = useMoralisSolanaCall(account.getNFTs);

// get devnet SPL NFT balance for a given address
const options = {
  network: "devnet",
  address: "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe",
};
const { fetch, data, isLoading } = useMoralisSolanaCall(
  account.getNFTs,
  options
);
curl -X 'GET' \
  'https://solana-gateway.moralis.io/account/devnet/6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe/nft' \
  -H 'accept: application/json' \
  -H 'X-API-Key: MY-API-KEY'
using System.Collections.Generic;
using Moralis.SolanaApi.Models;
using Moralis.SolanaApi;
using MoralisWeb3ApiSdk;

  public async void GetSPLNft()
  {
    List<SplNft> SplNFTbal = await MoralisSolanaClient.SolanaApi.Account.GetNFTs(NetworkTypes.mainnet, "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe");
    foreach(SplNft splnft in SplNFTbal){
      print(splnft);
    }
  }

示例结果

[
  {
    associatedTokenAddress: "Dpmpwm93Amvj4uEFpYhjv8ZzfpgARq6zxKTi6mrj97gW",
    mint: "BXWuzb3jEuGsGUe29xdApu8Z3jVgrFbr3wWdsZmLWYk9",
    amountRaw: "100000000000",
    amount: "100",
    decimals: "9",
  },
];

getPortfolio

返回地址的投资组合(SOL 余额、SPL 代币余额、SPL NFT 余额)。

选项:

  • network​:要从中获取数据的网络集群。 有效值列在支持的网络上。 默认值主网。
  • address​:用户地址(即 HsXZnAba2...)。 如果指定,则附加到查询的用户将被忽略,而是使用地址。

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

// get mainnet NFT balance for the current user
const portfolio = await Moralis.SolanaAPI.account.getPortfolio();

// get devnet NFT balance for a given address
const options = {
  network: "devnet",
  address: "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe",
};
const portfolio = await Moralis.SolanaAPI.account.getPortfolio(options);
import { useMoralisSolanaApi, useMoralisSolanaCall } from "react-moralis";

const { account } = useMoralisSolanaApi();

// get mainnet SPL NFT balance for the current user
const { fetch, data, isLoading } = useMoralisSolanaCall(account.getPortfolio);

// get devnet SPL NFT balance for a given address
const options = {
  network: "devnet",
  address: "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe",
};
const { fetch, data, isLoading } = useMoralisSolanaCall(
  account.getPortfolio,
  options
);
curl -X 'GET' \
  'https://solana-gateway.moralis.io/account/devnet/6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe/portfolio' \
  -H 'accept: application/json' \
  -H 'X-API-Key: MY-API-KEY'
using System.Collections.Generic;
using Moralis.SolanaApi.Models;
using Moralis.SolanaApi;
using MoralisWeb3ApiSdk;

  public async void GetSPLPortfolioBal()
  {
    Portfolio PortfolioBal = await MoralisSolanaClient.SolanaApi.Account.GetPortfolio(NetworkTypes.mainnet, "6XU36wCxWobLx5Rtsb58kmgAJKVYmMVqy4SHXxENAyAe");
    print(PortfolioBal);
  }

示例结果

{
  "nativeBalance": {
    "lamports": "6000000000",
    "solana": "6"
  },
  "nfts": [
    {
      "associatedTokenAddress": "6Fk96aAvPhV9gZvGXp9mMw1YwiScPX3pm1RwWn11eGrQ",
      "mint": "BpCcCVU4pK2rcvgduCzScaBhqKEKaMukcejkDhJWRyPv"
    }
  ],
  "tokens": [
    {
      "associatedTokenAddress": "Dpmpwm93Amvj4uEFpYhjv8ZzfpgARq6zxKTi6mrj97gW",
      "mint": "BXWuzb3jEuGsGUe29xdApu8Z3jVgrFbr3wWdsZmLWYk9",
      "amountRaw": "100000000000",
      "amount": "100",
      "decimals": "9"
    }
  ]
}


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号