跳转到内容

View 函数

View 函数可用于查询区块链上的智能合约。它们在智能合约中定义为带有 view 修饰符的入口函数。本指南提供 View 函数、其类型和用法的代码片段。

如果不关心 View 函数的返回类型,可在不提供任何类型参数的情况下使用 View 函数。

我们将调用的 Move 函数:

public fun balance<CoinType>(owner: address): u64

要调用此 View 函数,将使用 ContractClient 的 View 函数。

using Aptos;
class Program
{
static void Main(string[] args)
{
var client = new AptosClient(Networks.Mainnet);
// Call the view function by specifying the function name, arguments, and type arguments
var values = await client.Contract.View(
new GenerateViewFunctionPayloadData(
function: "0x1::coin::balance",
functionArguments: ["0x1"],
typeArguments: ["0x1::aptos_coin::AptosCoin"]
)
);
// Returns a list of return values: ["100"]
ulong balance = ulong.Parse(values[0]);
}
}

对于具有常见返回类型的 View 函数,可以通过传入类型参数为返回值指定类型。

我们将调用的 Move 函数:

public fun get_current_epoch_proposal_counts(validator_index: u64): (u64, u64)

要调用此 View 函数,将使用 ContractClient 的 View 函数并传入类型参数。

using Aptos;
class Program
{
static void Main(string[] args)
{
var client = new AptosClient(Networks.Mainnet);
// Call the view function by specifying the function name, arguments, and type arguments
var values = await client.Contract.View<List<ulong>>(
new GenerateViewFunctionPayloadData(
function: "0x1::stake::get_current_epoch_proposal_counts",
functionArguments: [(ulong)0],
typeArguments: []
)
);
// Returns a list of return values: ["100", "100"]
ulong successfulProposals = values[0];
ulong failedProposals = values[1];
}
}

对于具有复杂返回类型的 View 函数,可使用 Newtonson.Json 反序列化返回值。默认情况下,传入 View 函数的所有类型都会使用 Newtonson.JsonJsonConvert.DeserializeObject<T>() 反序列化返回值。也可通过创建自定义 JsonConverter 覆盖反序列化行为。

我们将调用的 Move 函数:

public fun supply<CoinType>(): Option<u128>

创建自己的 JsonConverter 以反序列化返回值。

using Aptos;
using Newtonsoft.Json;
[JsonConverter(typeof(CoinSupplyConverter))]
class CoinSupply(ulong value) {
public ulong Value;
}
class CoinSupplyConverter : JsonConverter<CoinSupply> {
public override CoinSupply ReadJson(JsonReader reader, Type objectType, CoinSupply existingValue, bool hasExistingValue, JsonSerializer serializer) {
// The return type of the view function is an Option<u128> -> [{ "vec": [] }] or [{ "vec": ["100"] }]
JArray array = JArray.Load(reader);
var option = array[0];
// If the Option is None
if (option["vec"].Count == 0) return null;
// If the Option is Some
ulong value = ulong.Parse(option["vec"][0]);
return new CoinSupply(value);
}
}

要调用此 View 函数,将使用 ContractClient 的 View 函数并传入类型参数。

using Aptos;
using Newtonsoft.Json;
class Program
{
static void Main(string[] args)
{
var client = new AptosClient(Networks.Mainnet);
// Call the view function by specifying the function name, arguments, and type arguments
CoinSupply coinSupply = await client.Contract.View<CoinSupply>(
new GenerateViewFunctionPayloadData(
function: "0x1::coin::supply",
functionArguments: [],
typeArguments: ["0x1::aptos_coin::AptosCoin"]
)
);
ulong coinSupply = coinSupply.Value;
}
}