Crates.io | onemoney-protocol |
lib.rs | onemoney-protocol |
version | 0.3.0 |
created_at | 2025-08-18 05:10:55.42968+00 |
updated_at | 2025-08-26 02:55:43.702476+00 |
description | Official Rust SDK for OneMoney Protocol - L1 blockchain network client |
homepage | https://1money.network |
repository | https://github.com/1Money-Co/onemoney-protocol |
max_upload_size | |
id | 1799976 |
size | 681,538 |
Official Rust SDK for the OneMoney L1 blockchain REST API.
Add this to your Cargo.toml
:
[dependencies]
onemoney-protocol = "0.1.0"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
use onemoney_protocol::{Client, ClientBuilder, Network};
use alloy_primitives::{Address, U256};
use std::str::FromStr;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create clients for different networks
let mainnet_client = Client::mainnet(); // Mainnet
let testnet_client = Client::testnet(); // Testnet
let local_client = Client::local(); // Local development
// Or use the builder pattern
let client = ClientBuilder::new()
.network(Network::Testnet)
.build()?;
// Get account nonce
let address = Address::from_str("0x742d35Cc6634C0532925a3b8D91D6F4A81B8Cbc0")?;
let nonce = client.get_account_nonce(address).await?;
println!("Account nonce: {}", nonce.nonce);
// Get latest blockchain state
let state = client.get_latest_epoch_checkpoint().await?;
println!("Current epoch: {}, checkpoint: {}", state.epoch, state.checkpoint);
Ok(())
}
use onemoney_protocol::{Client, ClientBuilder};
use std::time::Duration;
// Basic clients
let client = Client::mainnet(); // Mainnet
let client = Client::testnet(); // Testnet
// Custom configuration
let client = ClientBuilder::new()
.base_url("https://custom.api.endpoint.com")
.timeout(Duration::from_secs(30))
.build()?;
// Get account nonce
let nonce = client.get_account_nonce(address).await?;
// Get token account balance
let token_account = client.get_associated_token_account(owner, mint_address).await?;
println!("Balance: {}", token_account.amount);
// List all token accounts for an address
let (accounts, total) = client.list_token_accounts(owner, Some(10), Some(0)).await?;
// Derive token account address
let token_account_addr = client.derive_token_account_address(wallet, mint);
use onemoney_protocol::{TokenMintPayload, Authority};
// Mint tokens
let mint_payload = TokenMintPayload {
recent_epoch: state.epoch,
recent_checkpoint: state.checkpoint,
chain_id: 1212101,
nonce: 1,
token: token_address,
recipient: recipient_address,
value: U256::from(1000000000000000000u64), // 1 token
};
let result = client.mint_token(mint_payload, private_key).await?;
use onemoney_protocol::PaymentPayload;
// Send a payment
let payment = PaymentPayload {
recent_epoch: state.epoch,
recent_checkpoint: state.checkpoint,
chain_id: 1212101,
nonce: 2,
recipient: recipient_address,
value: U256::from(500000000000000000u64), // 0.5 tokens
token: token_address,
};
let result = client.send_payment(payment, private_key).await?;
println!("Payment sent: {}", result.hash);
// Get transaction details
let tx = client.get_transaction_by_hash(&result.hash).await?;
println!("Transaction status: {:?}", tx.status);
// Wait for confirmation
let confirmed_tx = client.wait_for_transaction(
&result.transaction_hash,
30, // max attempts
Duration::from_secs(2) // polling interval
).await?;
// Get latest state (for transaction construction)
let state = client.get_latest_epoch_checkpoint().await?;
// Get chain information
let chain = client.get_chain_info().await?;
println!("Chain ID: {}", chain.chain_id);
// Get network statistics
let stats = client.get_network_stats().await?;
println!("Total transactions: {}", stats.total_transactions);
use onemoney_protocol::crypto;
// Derive address from private key
let address = crypto::private_key_to_address(private_key)?;
// Sign a message
let signature = crypto::sign_message(&payload, private_key)?;
// Verify signature
let is_valid = crypto::verify_signature(&payload, &signature, signer_address)?;
The SDK provides comprehensive error handling:
use onemoney_protocol::{Error, Result};
match client.get_account_nonce(address).await {
Ok(nonce) => println!("Nonce: {}", nonce.nonce),
Err(Error::Api { status_code: 404, .. }) => {
println!("Account not found");
},
Err(Error::Http(e)) => {
println!("Network error: {}", e);
},
Err(Error::Json(e)) => {
println!("JSON parsing error: {}", e);
},
Err(e) => {
println!("Other error: {}", e);
}
}
Run the included examples:
# Transaction management example
cargo run --example transactions_example
# Network configuration examples
cargo run --example network_examples
# Token operations example
cargo run --example tokens_example
# Account management example
cargo run --example accounts_example
The SDK is organized into several modules:
accounts
- Account queries, nonce management, token account operationstokens
- Token minting, burning, authority management, pause/blacklist/whitelist controlstransactions
- Payment sending, transaction queries, fee estimationchains
- Chain information and network detailscheckpoints
- Checkpoint queries and blockchain statestates
- Latest state queries and network statisticscrypto
- Cryptographic utilities for signing and verificationONEMONEY_API_URL
- Override the default API endpointONEMONEY_TIMEOUT
- Set request timeout in secondsONEMONEY_LOG_LEVEL
- Set logging level (trace, debug, info, warn, error)https://api.1money.network
https://api.testnet.1money.network
cargo test
# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run specific test module
cargo test accounts::tests
# Run tests with coverage (requires cargo-llvm-cov)
cargo install cargo-llvm-cov
cargo llvm-cov --all-features --workspace --html
# Run integration tests
cargo test --test '*'
# Run unit tests only
cargo test --lib
This project is licensed under the Apache License, Version 2.0.
See LICENSE for details or visit http://www.apache.org/licenses/LICENSE-2.0.
Built by the OneMoney team.