Crates.io | kadena |
lib.rs | kadena |
version | 0.1.0 |
source | src |
created_at | 2024-10-27 00:41:10.125879 |
updated_at | 2024-10-27 00:41:10.125879 |
description | A comprehensive Rust library for interacting with Pact smart contracts and the Kadena blockchain. |
homepage | |
repository | https://github.com/ledger-things/kadena-rust-lib |
max_upload_size | |
id | 1424253 |
size | 138,614 |
A comprehensive Rust library for interacting with Pact smart contracts and the Kadena blockchain. This library provides a type-safe, ergonomic interface for creating and signing Pact commands, managing cryptographic operations, and interacting with Kadena nodes.
Add this to your Cargo.toml
:
[dependencies]
kadena = "0.1.0"
use kadena::{
crypto::PactKeypair,
pact::{
meta::Meta,
cap::Cap,
command::Cmd,
},
fetch::{ApiClient, ApiConfig}
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create keypair from known keys
let keypair = PactKeypair::generate();
// Format sender account with "k:" prefix
let sender_account = format!("k:{}", keypair.public_key);
let network = "testnet04";
let chain_id = "0";
// Create metadata using the new constructor and builder pattern
let meta = Meta::new(chain_id, &sender_account)
.with_gas_limit(1500)
.with_gas_price(0.00000001)
.with_ttl(3600);
// Create capabilities using the new constructors
let caps = vec![
Cap::new("coin.GAS"), // Using the convenience constructor for GAS capability
Cap::transfer(&sender_account, "Bob", 10.0), // Using the convenience constructor for TRANSFER
];
// Create Pact code for the transaction
let pact_code = format!("(coin.transfer \"{}\" \"Bob\" 10.0)", sender_account);
// Prepare the execution command using the new method
let transaction_cmd = Cmd::prepare_exec(
&[(&keypair, caps)],
None, // Let it generate a random nonce
&pact_code,
None,
meta,
Some(network.to_string()),
)?;
println!("Transaction Payload:");
println!("{}", serde_json::to_string_pretty(&transaction_cmd)?);
// Send the transaction to the Kadena testnet node
// Create the client
let client = ApiClient::new(
ApiConfig::new("https://api.testnet.chainweb.com", network, chain_id).with_timeout(60),
);
let result = client.local(&transaction_cmd).await?;
println!("\nTransaction Response:");
println!("{}", serde_json::to_string_pretty(&result)?);
Ok(())
}
For detailed documentation and examples, visit docs.rs/kadena.
See the examples directory for more detailed examples.
This library includes comprehensive benchmarks that you can run with:
cargo bench
View the benchmark results in target/criterion/report/index.html
.
This library uses well-audited cryptographic implementations:
ed25519-dalek
blake2
rand
We welcome contributions! See CONTRIBUTING.md for guidelines.
Clone the repository:
git clone https://github.com/ledger-things/kadena-rust-lib
cd kadena
Run tests:
cargo test
Run benchmarks:
cargo bench
This project is licensed under the MIT License - see the LICENSE file for details.
See CHANGELOG.md for a list of changes.