Stacks.rs - IN DEVELOPMENT

A Rust port of existing Javascript/Typescript tooling to interact with the Stacks blockchain.
Disclaimer: Not ready for production use - breaking changes expected.

This project is inspired by micro-stacks[^micro-stacks] & Stacks.js[^stacks.js]

Usage

Build a token-transfer transaction:

```rust use stacksrs::transaction::AnchorMode; use stacksrs::transaction::PostConditionMode; use stacksrs::transaction::PostConditions; use stacksrs::transaction::STXTokenTransfer; use stacksrs::AddressVersion; use stacksrs::Error; use stacksrs::StacksTestnet; use stacksrs::StacksWallet;

[tokio::main]

async fn main() -> Result<(), Error> { let mut wallet = StacksWallet::fromsecretkey(SECRET_KEY)?;

let account = wallet.get_account(0)?;
let address = account.get_address(AddressVersion::TestnetP2PKH)?;

let tx = STXTokenTransfer::new(
    "ST2G0KVR849MZHJ6YB4DCN8K5TRDVXF92A664PHXT",
    account.private_key,
    1337,
    0,
    0,
    StacksTestnet::new(),
    AnchorMode::Any,
    "test memo",
    PostConditionMode::Deny,
    PostConditions::empty(),
    false,
)?;

let signed_tx = tx.sign()?;

Ok(())

} ```

Build a contract-call transaction:

```rust use stacksrs::clarity::IntCV; use stacksrs::clarity::StandardPrincipalCV; use stacksrs::clarity::TupleCV; use stacksrs::transaction::AnchorMode; use stacksrs::transaction::PostConditionMode; use stacksrs::transaction::PostConditions; use stacksrs::transaction::STXContractCall; use stacksrs::Error; use stacksrs::StacksMainnet; use stacksrs::StacksWallet;

[tokio::main]

async fn main() -> Result<(), Error> { let mut wallet = StacksWallet::fromsecretkey(SECRETKEY)?; let account = wallet.getaccount(0)?;

let tx = STXContractCall::new(
    "SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159",
    "example-contract",
    "example-function",
    [
        IntCV::new(1),
        StandardPrincipalCV::new("SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159"),
        TupleCV::new(&[
            ("a", IntCV::new(1)),
            ("b", IntCV::new(2)),
            ("c", IntCV::new(3)),
        ]),
    ],
    account.private_key,
    0,
    0,
    StacksMainnet::new(),
    AnchorMode::Any,
    PostConditionMode::Deny,
    PostConditions::empty(),
    false,
)?;

let signed_tx = tx.sign()?;

Ok(())

} ```

Set nonce + fee & broadcast transfer:

```rust use stacksrs::transaction::broadcasttransaction; use stacksrs::transaction::estimatetransactionfee; use stacksrs::transaction::getnonce; use stacksrs::transaction::AnchorMode; use stacksrs::transaction::PostConditionMode; use stacksrs::transaction::PostConditions; use stacksrs::transaction::STXTokenTransfer; use stacksrs::AddressVersion; use stacksrs::Error; use stacksrs::StacksTestnet; use stacks_rs::StacksWallet;

[tokio::main]

async fn main() -> Result<(), Error> { let mut wallet = StacksWallet::fromsecretkey(SECRET_KEY)?;

let account = wallet.get_account(0)?;
let address = account.get_address(AddressVersion::TestnetP2PKH)?;
let network = StacksTestnet::new();

let mut tx = STXTokenTransfer::new(
    "ST21HQTGHGJ3DDWM8BC1E00TYZPD3DF31NSK0Y1JS",
    account.private_key,
    1337,
    0,
    0,
    network,
    AnchorMode::Any,
    "test memo",
    PostConditionMode::Deny,
    PostConditions::empty(),
    false,
)?;

let bytes = tx.byte_length()?;
let nonce = get_nonce(&address, network).await?;
let fee = estimate_transaction_fee(bytes, network).await?;

tx.set_nonce(nonce);
tx.set_fee(fee);

let signed_tx = tx.sign()?;
let tx_id = broadcast_transaction(&signed_tx, network).await?;

Ok(())

} ```