Skip to content

Commit

Permalink
feat(lib): added rpc module (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
52 authored Feb 21, 2024
1 parent bcce04f commit 9015969
Show file tree
Hide file tree
Showing 15 changed files with 628 additions and 287 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "2"
members = ["stacks", "stacks_derive", "tests"]

[workspace.package]
version = "0.2.4"
version = "0.2.5"
edition = "2021"
license = "MIT OR Apache-2.0"
authors = ["Max Karou <maxkarou@protonmail.com>"]
Expand Down
53 changes: 18 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ A minimal dependency Rust toolkit to interact with the [Stacks Blockchain](https

```rust
use stacks_rs::clarity;
use stacks_rs::transaction::AnchorMode;
use stacks_rs::transaction::PostConditionMode;
use stacks_rs::transaction::PostConditions;
use stacks_rs::transaction::STXTokenTransfer;
use stacks_rs::transaction::StacksMainnet;
use stacks_rs::wallet::StacksWallet;
Expand All @@ -24,21 +21,15 @@ fn main() -> Result<(), stacks_rs::Error> {
let account = wallet.get_account(0)?;
let sender_key = account.private_key()?;

let tx = STXTokenTransfer::new(
clarity!(PrincipalStandard, "ST000000000000000000002AMW42H"),
sender_key,
100_000,
1000,
69,
&StacksMainnet::new(),
AnchorMode::Any,
"test-memo",
PostConditionMode::Allow,
PostConditions::default(),
false,
);
let transaction = STXTokenTransfer::builder()
.recipient(clarity!(PrincipalStandard, "ST000000000000000000002AMW42H"))
.amount(100_000)
.sender(sender_key)
.network(StacksMainnet::new())
.build()
.transaction();

let signed = tx.sign()?;
let signed = transaction.sign(sender_key)?;

Ok(())
}
Expand All @@ -48,9 +39,6 @@ fn main() -> Result<(), stacks_rs::Error> {

```rust
use stacks_rs::clarity;
use stacks_rs::transaction::AnchorMode;
use stacks_rs::transaction::PostConditionMode;
use stacks_rs::transaction::PostConditions;
use stacks_rs::transaction::STXContractCall;
use stacks_rs::transaction::StacksMainnet;
use stacks_rs::wallet::StacksWallet;
Expand All @@ -61,10 +49,10 @@ fn main() -> Result<(), stacks_rs::Error> {
let account = wallet.get_account(0)?;
let sender_key = account.private_key()?;

let tx = STXContractCall::new(
clarity!(PrincipalContract, "ST000000000000000000002AMW42H", "pox"),
"make-pox",
clarity!(
let transaction = STXContractCall::builder()
.contract(("ST000000000000000000002AMW42H", "pox"))
.fn_name("make-pox")
.fn_args(clarity!(
FnArguments,
clarity!(UInt, 123),
clarity!(True),
Expand All @@ -74,18 +62,13 @@ fn main() -> Result<(), stacks_rs::Error> {
clarity!(StringAscii, "foo"),
clarity!(StringAscii, "bar")
)
),
sender_key,
1000,
69,
&StacksMainnet::new(),
AnchorMode::Strict,
PostConditionMode::Allow,
PostConditions::default(),
false,
);
))
.sender(sender_key)
.network(StacksMainnet::new())
.build()
.transaction();

let signed = tx.sign()?;
let signed = transaction.sign(sender_key)?;

Ok(())
}
Expand Down
21 changes: 19 additions & 2 deletions stacks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ dyn-clone = "1.0.16"
ripemd = "0.1.3"
thiserror = "1.0.29"

[dependencies.typed-builder]
version = "0.18.1"
optional = true

[dependencies.secp256k1]
version = "0.28.2"
features = ["recovery"]
Expand All @@ -29,11 +33,21 @@ path = "../stacks_derive"
version = "0.17.7"
features = ["std"]

[dependencies.serde]
version = "1.0.197"
optional = true
features = ["derive"]

[dependencies.ureq]
version = "2.9.6"
optional = true
features = ["json"]

[dev-dependencies]
rand = "0.8.5"

[features]
default = ["clarity", "transaction", "wallet-sdk"]
default = ["clarity", "transaction", "wallet-sdk", "rpc"]

# Provide convenience derive(...) macros.
derive = ["stacks_derive"]
Expand All @@ -42,7 +56,10 @@ derive = ["stacks_derive"]
clarity = []

# Provide transaction builders. (transfer, call etc.)
transaction = ["clarity"]
transaction = ["clarity", "typed-builder"]

# Provide a wallet-sdk
wallet-sdk = []

# Provide rpc methods.
rpc = ["clarity", "transaction", "ureq", "serde"]
72 changes: 72 additions & 0 deletions stacks/src/clarity/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ impl Clone for Int {

impl Copy for Int {}

impl From<i128> for Int {
fn from(int: i128) -> Self {
Self::new(int)
}
}

impl From<Int> for i128 {
fn from(int: Int) -> Self {
int.__value
}
}

impl Codec for UInt {
fn encode(&self) -> Result<Vec<u8>, Error> {
let mut buff = vec![Self::id()];
Expand Down Expand Up @@ -117,6 +129,18 @@ impl Clone for UInt {

impl Copy for UInt {}

impl From<u128> for UInt {
fn from(int: u128) -> Self {
Self::new(int)
}
}

impl From<UInt> for u128 {
fn from(int: UInt) -> Self {
int.__value
}
}

impl Codec for Buffer {
fn encode(&self) -> Result<Vec<u8>, Error> {
let mut buff = vec![Self::id()];
Expand Down Expand Up @@ -174,6 +198,24 @@ impl IntoIterator for Buffer {
}
}

impl From<Vec<u8>> for Buffer {
fn from(bytes: Vec<u8>) -> Self {
Self::new(bytes)
}
}

impl From<Buffer> for Vec<u8> {
fn from(buf: Buffer) -> Self {
buf.__value
}
}

impl From<&[u8]> for Buffer {
fn from(bytes: &[u8]) -> Self {
Self::new(bytes.to_vec())
}
}

impl Codec for True {
fn encode(&self) -> Result<Vec<u8>, Error> {
Ok(vec![Self::id()])
Expand Down Expand Up @@ -287,6 +329,24 @@ impl Clone for PrincipalStandard {
}
}

impl From<&str> for PrincipalStandard {
fn from(addr: &str) -> Self {
Self::new(addr.to_string())
}
}

impl From<String> for PrincipalStandard {
fn from(addr: String) -> Self {
Self::new(addr)
}
}

impl From<PrincipalStandard> for String {
fn from(principal: PrincipalStandard) -> Self {
principal.__value
}
}

impl Codec for PrincipalContract {
fn encode(&self) -> Result<Vec<u8>, Error> {
let (addr, ver) = c32_address_decode(&self.__value.0)?;
Expand Down Expand Up @@ -329,6 +389,18 @@ impl Clone for PrincipalContract {
}
}

impl From<(String, String)> for PrincipalContract {
fn from((addr, name): (String, String)) -> Self {
Self::new((addr, name))
}
}

impl From<(&str, &str)> for PrincipalContract {
fn from((addr, name): (&str, &str)) -> Self {
Self::new((addr.to_string(), name.to_string()))
}
}

impl Codec for ResponseOk {
fn encode(&self) -> Result<Vec<u8>, Error> {
let mut buff = vec![Self::id()];
Expand Down
13 changes: 11 additions & 2 deletions stacks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
clippy::missing_errors_doc,
clippy::must_use_candidate,
clippy::upper_case_acronyms,
clippy::too_many_arguments
clippy::too_many_arguments,
clippy::large_enum_variant,
clippy::result_large_err,
clippy::similar_names
)]

#[cfg(feature = "clarity")]
pub mod clarity;
pub mod crypto;
#[cfg(feature = "rpc")]
pub mod rpc;
#[cfg(feature = "transaction")]
pub mod transaction;
#[cfg(feature = "wallet-sdk")]
Expand All @@ -30,7 +35,7 @@ pub mod derive {
pub use stacks_derive::*;
}

#[derive(Debug, Clone, thiserror::Error)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// `crypto::b58` crate errors.
#[error(transparent)]
Expand All @@ -49,6 +54,10 @@ pub enum Error {
/// `transaction` crate errors.
#[error(transparent)]
Transaction(#[from] transaction::Error),
#[cfg(feature = "rpc")]
/// `rpc` crate errors.
#[error(transparent)]
RPC(#[from] rpc::Error),
#[cfg(feature = "wallet-sdk")]
/// `wallet` crate errors.
#[error(transparent)]
Expand Down
Loading

0 comments on commit 9015969

Please sign in to comment.