#stack #bitcoin #stacks-blockchain #api-bindings

stacks-rs

与 Stacks 区块链交互的最小化 Rust 工具包

3 个不稳定版本

0.3.3 2024 年 3 月 7 日
0.3.2 2024 年 3 月 6 日
0.2.8 2024 年 2 月 27 日
0.1.12 2024 年 2 月 7 日
0.1.1 2023 年 4 月 28 日

#1579 in 魔法豆

Download history 134/week @ 2024-07-26

134 个月下载量

MIT/Apache

180KB
4.5K SLoC

Stacks.rs - 开发中

一个与 Stacks 区块链 交互的最小化依赖 Rust 工具包

警告:尚未准备好用于生产使用 - 预计会有破坏性更改

用法

构建并签署一个 STXTokenTransfer 交易

use stacks_rs::clarity;
use stacks_rs::transaction::STXTokenTransfer;
use stacks_rs::transaction::StacksMainnet;
use stacks_rs::wallet::StacksWallet;

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

    let account = wallet.get_account(0)?;
    let sender_key = account.private_key()?;

    let transaction = STXTokenTransfer::builder()
        .recipient(clarity!(PrincipalStandard, "ST000000000000000000002AMW42H"))
        .amount(100_000)
        .sender(sender_key)
        .network(StacksMainnet::new())
        .build()
        .transaction();

    let signed = transaction.sign(sender_key)?;

    Ok(())
}

构建并签署一个 STXContractCall 交易

use stacks_rs::clarity;
use stacks_rs::transaction::STXContractCall;
use stacks_rs::transaction::StacksMainnet;
use stacks_rs::wallet::StacksWallet;

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

    let account = wallet.get_account(0)?;
    let sender_key = account.private_key()?;

    let transaction = STXContractCall::builder()
        .address("ST000000000000000000002AMW42H")
        .contract("pox")
        .fn_name("make-pox")
        .fn_args(clarity!(
            FnArguments,
            clarity!(UInt, 123),
            clarity!(True),
            clarity!(Buffer, [0x01, 0x02, 0x03, 0x04]),
            clarity!(
                List,
                clarity!(StringAscii, "foo"),
                clarity!(StringAscii, "bar")
            )
        ))
        .sender(sender_key)
        .network(StacksMainnet::new())
        .build()
        .transaction();

    let signed = transaction.sign(sender_key)?;

    Ok(())
}

使用 clarity!(...) 宏创建复杂类型

use stacks_rs::clarity;
use stacks_rs::clarity::Buffer;
use stacks_rs::clarity::False;
use stacks_rs::clarity::Int;
use stacks_rs::clarity::List;
use stacks_rs::clarity::OptionalNone;
use stacks_rs::clarity::OptionalSome;
use stacks_rs::clarity::PrincipalContract;
use stacks_rs::clarity::PrincipalStandard;
use stacks_rs::clarity::ResponseErr;
use stacks_rs::clarity::ResponseOk;
use stacks_rs::clarity::StringAscii;
use stacks_rs::clarity::StringUtf8;
use stacks_rs::clarity::True;
use stacks_rs::clarity::Tuple;
use stacks_rs::clarity::UInt;

fn main() -> Result<(), stacks_rs::Error> {
    let list = clarity!(
        List,
        clarity!(Int, 3),
        clarity!(Int, -4),
        clarity!(UInt, 1),
        clarity!(True),
        clarity!(False),
        clarity!(PrincipalStandard, "ST000000000000000000002AMW42H"),
        clarity!(PrincipalContract, "ST000000000000000000002AMW42H", "pox"),
        clarity!(OptionalSome, clarity!(Int, 1)),
        clarity!(OptionalNone),
        clarity!(ResponseOk, clarity!(OptionalSome, clarity!(Int, 1))),
        clarity!(ResponseErr, clarity!(OptionalNone)),
        clarity!(Tuple, ("foo", clarity!(Int, 1)), ("bar", clarity!(UInt, 2))),
        clarity!(Buffer, vec![0xde, 0xad, 0xbe, 0xef]),
        clarity!(StringAscii, "Hello, world!"),
        clarity!(StringUtf8, "🌾!")
    );

    for item in list {
        println!("Item: {}", item.hex()?)
    }

    Ok(())
}

使用 post_condition!(...) 宏创建后置条件


// create a collection of conditions:
post_condition!(
    (
        STXCondition,
        clarity!(PrincipalStandard, "ST000000000000002AMW42H"),
        1_000_000,
        ConditionCode::GTE
    ),
    (
        STXCondition,
        clarity!(PrincipalContract, "ST000000000000000000002AMW42H", "pox"),
        1_000_000,
        ConditionCode::EQ
    ),
    (
        NonFungibleCondition,
        clarity!(PrincipalStandard, "ST000000000000002AMW42H"),
        clarity!(UInt, 60149),
        ConditionCode::Has,
        AssetInfo::new()
    ),
    (
        FungibleCondition,
        clarity!(PrincipalContract, "ST000000000000000000002AMW42H", "pox"),
        1_000_000,
        ConditionCode::LTE,
        AssetInfo::new()
    )
);

// ...or create a singular condition:
post_condition!(
    STXCondition,
    clarity!(PrincipalStandard, "ST000000000000002AMW42H"),
    1_000_000,
    ConditionCode::GTE
);

依赖项

~11–20MB
~340K SLoC