Aleo JS SDK 中 AleoNetworkClient 的详细介绍

Aleo JS SDK 中 AleoNetworkClient 的详细介绍

AleoNetworkClient 封装了对 Aleo 节点 RPC REST接口的调用,主要提供了对 Aleo 网络中的区块链信息的访问,方便我们直接使用 SDK 访问区块信息。

new AleoNetworkClient(host)

构建 AleoNetworkClient 的实例,参数为节点的 rpc

Example

// Connection to a local node
let connection = new AleoNetworkClient("http://localhost:3030");

// Connection to a public beacon node
let connection = new AleoNetworkClient("https://api.explorer.aleo.org/v1");

setAccount(account)

设置账户,将账户与 client 绑定

Example

let account = new Account();
connection.setAccount(account);

getAccount()

返回 client 绑定的账户信息

Example

let account = connection.getAccount();

getBlock(height)

返回指定高度的 block 信息

Example

let block = connection.getBlock(1234);

getBlockRange(start, end)

返回指定区块高度区间的区块信息

Example

let blockRange = connection.getBlockRange(2050, 2100);

getProgram(programId)

返回程序的源代码,参数为程序 ID, 在 Aleo 中程序的唯一标识就是程序 ID, 并且是唯一的

Example

let program = connection.getProgram("foo.aleo");

getProgramMappingNames(programId)

返回程序对应的 name

Example

let mappings = connection.getProgramMappingNames("credits.aleo");

getMappingValue(programId, mappingName, key)

返回指定程序中指定 Map 中对应 key 的 value 值

Example

## Get public balance of an account
let mappingValue = connection.getMappingValue("credits.aleo", "account", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");

getLatestBlock()

返回最新的区块信息

Example

let latestHeight = connection.getLatestBlock();

getLatestHash()

返回最新区块的 hash

let latestHash = connection.getLatestHash();

getLatestHeight()

返回最新的区块高度

Example

let latestHeight = connection.getLatestHeight();

getStateRoot()

返回最新的 state root

Example

let stateRoot = connection.getStateRoot();

getTransaction(id)

返回指定交易 hash 对应的交易

Example

let transaction = connection.getTransaction("at1handz9xjrqeynjrr0xay4pcsgtnczdksz3e584vfsgaz0dh0lyxq43a4wj");

getTransactions(height)

返回指定区块高度对应的所有交易信息

Example

let transactions = connection.getTransactions(654);

getTransactionsInMempool()

返回内存池中的交易

Example

let transactions = connection.getTransactionsInMempool();

getTransitionId(id)

返回指定 id 的 transition

Example

let transition = connection.getTransitionId("2429232855236830926144356377868449890830704336664550203176918782554219952323field");

findUnspentRecords()

尝试查找指定账户下面的未花费的 Records

Example

// Find all unspent records
const privateKey = "[PRIVATE_KEY]";
let records = connection.findUnspentRecords(0, undefined, privateKey);

// Find specific amounts
const startHeight = 500000;
const amounts = [600000, 1000000];
let records = connection.findUnspentRecords(startHeight, undefined, privateKey, amounts);

// Find specific amounts with a maximum number of cumulative microcredits
const maxMicrocredits = 100000;
let records = connection.findUnspentRecords(startHeight, undefined, privateKey, undefined, maxMicrocredits);

Aleo 官方链接: