21 个版本
0.6.8 | 2024年2月13日 |
---|---|
0.6.5 | 2023年11月29日 |
0.5.1 | 2023年7月20日 |
0.3.6 | 2023年3月30日 |
#913 in 魔法豆
用于 2 crates
165KB
2.5K SLoC
Aleo Rust SDK
Aleo Rust SDK 提供了一套用于部署和执行程序以及与 Aleo 网络通信的工具。
Aleo 网络交互
SDK 用户可以通过 AleoAPIClient 结构与 Aleo 网络交互。
Aleo 网络内部有节点,提供 REST API 以与网络交互。AleoAPIClient 结构提供了这些 REST API 端点的 1:1 映射,以及一些方便与网络交互的方法。
Aleo API 客户端的几个关键用途包括:
- 查找用于价值转移、程序执行和程序部署的记录
- 定位网络上的程序
- 向网络发送交易
- 检查链数据,如区块内容、交易内容等
示例用法
use aleo_rust::AleoAPIClient;
use snarkvm_console::{
account::PrivateKey,
network::Testnet3,
};
use rand::thread_rng;
// Create a client that interacts with the testnet3 program
let api_client = AleoAPIClient::<Testnet3>testnet3();
// FIND A PROGRAM ON THE ALEO NETWORK
let hello = api_client.get_program("hello.aleo").unwrap();
println!("Hello program: {hello:?}");
// FIND RECORDS THAT BELONG TO A PRIVATE KEY
let mut rng = thread_rng();
// Create a private key (in practice, this would be an existing user's private key)
let private_key = PrivateKey::new(&mut rng).unwrap();
// Get the latest block height
let end_height = api_client.latest_height().unwrap();
// Look back 1000 blocks
let start_height = end_height - 1000u32;
// Find records with these gate amounts (requires an account with a balance)
let amounts_to_find = vec![100u64, 200u64];
let records = api_client.get_unspent_records(&private_key, (start_height..end_height), None, Some(&amounts_to_find)).unwrap();
程序执行和部署
Aleo程序管理器提供了一套工具,用于在本地和Aleo网络上部署和执行程序。RecordFinder结构体与程序管理器配合使用,用于查找用于价值转移和程序执行/部署费用的记录。
以下示例显示了程序部署和执行流程。
示例用法
use aleo_rust::{AleoAPIClient, Encryptor, ProgramManager, RecordFinder};
use snarkvm_console::{
account::{Address, PrivateKey},
network::Testnet3,
};
use snarkvm_synthesizer::Program;
use rand::thread_rng;
use std::str::FromStr;
// Create the necessary components to create the program manager
let mut rng = thread_rng();
// Create an api client to query the network state
let api_client = AleoAPIClient::<Testnet3>::testnet3();
// Create a private key (in practice, this would be a user's private key)
let private_key = PrivateKey::<Testnet3>::new(&mut rng).unwrap();
// Encrypt the private key with a password
let private_key_ciphertext = Encryptor::<Testnet3>::encrypt_private_key_with_secret(&private_key, "password").unwrap();
// Create the program manager
// (Note: An optional local directory can be provided to manage local program data)
let mut program_manager = ProgramManager::<Testnet3>::new(None, Some(private_key_ciphertext), Some(api_client), None, false).unwrap();
// ------------------
// EXECUTE PROGRAM STEPS
// ------------------
// Execute the function `hello` of the hello.aleo program with the arguments 5u32 and 3u32.
// Specify 0 for the fee and provide a password to decrypt the private key stored in the program manager
program_manager.execute_program("hello.aleo", "hello", ["5u32", "3u32"].into_iter(), 0, None, Some("password")).unwrap();
// ------------------
// DEPLOY PROGRAM STEPS
// ------------------
// Note - Deployment requires a mandatory deployment fee, so an account with an existing
// balance is required to deploy a program
// Create a program name (note: change this to something unique)
let program_name = "yourownprogram.aleo";
// Create a test program
let test_program = format!("program {};\n\nfunction hello:\n input r0 as u32.public;\n input r1 as u32.private;\n add r0 r1 into r2;\n output r2 as u32.private;\n", program_name);
// Create a program object from the program string
let program = Program::from_str(&test_program).unwrap();
// Add the program to the program manager (this can also be done by providing a path to
// the program on disk when the program manager is created)
program_manager.add_program(&program).unwrap();
// Create a record finder to find records to fund the deployment fee
let record_finder = RecordFinder::<Testnet3>::new(AleoAPIClient::testnet3());
// Set the fee for the deployment transaction (in units of gates)
let fee_gates = 600000;
// Find a record to fund the deployment fee (requires an account with a balance)
let record = record_finder.find_one_record(&private_key, fee_gates).unwrap();
// Deploy the program to the network
program_manager.deploy_program(program_name, fee_gates, record, Some("password")).unwrap();
// Wait several minutes.. then check the program exists on the network
let api_client = AleoAPIClient::<Testnet3>::testnet3();
let program_on_chain = api_client.get_program(program_name).unwrap();
let program_on_chain_name = program_on_chain.id().to_string();
assert_eq!(&program_on_chain_name, program_name);
// ------------------
// TRANSFER STEPS
// ------------------
// Create a recipient (in practice, the recipient would send their address to the sender)
let recipient_key = PrivateKey::<Testnet3>::new(&mut rng).unwrap();
let recipient_address = Address::try_from(recipient_key).unwrap();
// Create amount and fee (both in units of gates)
let amount = 30000;
let fee = 100;
// Find records to fund the transfer
let (amount_record, fee_record) = record_finder.find_amount_and_fee_records(amount, fee, &private_key).unwrap();
// Create a transfer
program_manager.transfer(amount, fee, recipient_address, Some("password"), amount_record, Some(fee_record)).unwrap();
该API目前正在积极开发中,预计未来将进行更改,以便为程序执行和部署提供更流畅的体验。
依赖项
~32–46MB
~538K SLoC