跳转到内容

通过 Kotlin SDK 获取数据

Kaptos 提供了覆盖 fullnode REST 端点、索引器端点以及 Move view 函数的数据读取 API。

网络调用返回 Result<Ok, Err>,你可以选择显式分支处理,或者使用 fail-fast 解包。

val ledgerInfoResult = aptos.getLedgerInfo()
when (ledgerInfoResult) {
is Result.Ok -> println("Ledger version: ${ledgerInfoResult.value.ledgerVersion}")
is Result.Err -> println("Failed to read ledger info: ${ledgerInfoResult.error.message}")
}

如果你更偏好 fail-fast:

val ledgerInfo = aptos.getLedgerInfo().expect("Failed to fetch ledger info")

更多处理模式,请参见 处理 SDK 数据读取响应

val chainId = aptos.getChainId().expect("Failed to fetch chain id")
val latestBlock = aptos.getBlockByHeight(ledgerHeight = 1, withTransactions = true).expect("Failed to fetch block info")
val address = AccountAddress.fromString("0x1")
val accountInfo = aptos.getAccountInfo(accountAddress = address).expect("Failed to fetch account info")
val modules = aptos.getAccountModules(accountAddress = address) { limit = 20 }.expect("Failed to fetch modules")
val resources = aptos.getAccountResources(accountAddress = address) { limit = 50 }.expect("Failed to fetch resources")

通过索引器读取 APT 余额:

val aptBalance = aptos.getAccountAPTAmount(accountAddress = address).expect("Failed to fetch APT balance")

通过智能合约 view 路径读取指定 coin type:

val aptBalanceFromView =
aptos
.getAccountCoinAmountFromSmartContract(
accountAddress = address,
coinType = MoveValue.MoveStructId("0x1::aptos_coin::AptosCoin"),
)
.expect("Failed to fetch coin balance from smart contract")

使用 view<T>() 发起自定义只读 Move 调用。

val payload =
InputViewFunctionData(
function = "0x1::chain_id::get",
typeArguments = emptyList(),
functionArguments = emptyList(),
)
val viewResult =
aptos
.view<List<MoveValue.MoveUint8Type>>(payload)
.expect("Failed to execute view")
val chainIdFromView = viewResult.first().value