Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["sapio", "sapio-ws", "sapio-front", "sapio-contrib", "ctv_emulators", "sapio-base", "cli", "tools", "plugins", 'emulator-trait', 'examples/dcf_mining_pool']
members = ["sapio", "sapio-ws", "sapio-front", "sapio-contrib", "ctv_emulators", "sapio-base", "cli", "tools", "plugins", 'emulator-trait', 'examples/dcf_mining_pool', 'sapio-trait']
exclude = ["plugin-example", "integration_tests"]
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ schemars = "0.8.0"
serde_json = "1.0"
serde = "1.0"
serde_derive = "1.0"
clap = "3.0.0-beta.2"
clap = "=3.0.0-beta.2"
base64 = "0.13.0"
lazy_static = "1.4.0"
bitcoincore-rpc-async = "3.0.1"
Expand Down
4 changes: 3 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
(version: "0.1.0 Beta")
(author: "Jeremy Rubin <j@rubin.io>")
(about: "Sapio CLI for Bitcoin Smart Contracts")
(@arg config: -c --config +takes_value #{1,2} {check_file} "Sets a custom config file")
(@arg config: -c --config +takes_value #{1,1} {check_file} "Sets a custom config file")
(@arg debug: -d ... "Sets the level of debugging information")
(@subcommand emulator =>
(@setting SubcommandRequiredElseHelp)
(about: "Make Requests to Emulator Servers")
(@subcommand sign =>
(about: "Sign a PSBT")
Expand All @@ -71,6 +72,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
)
)
(@subcommand contract =>
(@setting SubcommandRequiredElseHelp)
(about: "Create or Manage a Contract")
(@subcommand bind =>
(about: "Bind Contract to a specific UTXO")
Expand Down
2 changes: 1 addition & 1 deletion plugin-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["treepay"]
members = ["treepay", "trampolinepay", "batching-trait"]
44 changes: 44 additions & 0 deletions plugin-example/batching-trait/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[package]
name = "batching-trait"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
schemars = "0.8.0"
serde_json = "1.0"
serde = "1.0"
serde_derive = "1.0"


[dependencies.wasm-bindgen]
version = "0.2.69"

[dependencies.bitcoin]
package = "sapio-bitcoin"
version = "^0.26.0"
features = ['use-serde', 'rand']
[dependencies.sapio]
path = "../../sapio"
version = "0.1.0"

[dependencies.sapio-base]
path = "../../sapio-base"
version = "0.1.0"


[dependencies.sapio-trait]
path="../../sapio-trait"
version = "0.1.0"

[dependencies.sapio-ctv-emulator-trait]
path="../../emulator-trait"
version = "0.1.0"

[dependencies.miniscript]
package = "sapio-miniscript"
version = "^5.1.0"
features = ['compiler', 'use-serde', 'rand', 'use-schemars', 'serde']
optional = true

40 changes: 40 additions & 0 deletions plugin-example/batching-trait/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use sapio::contract::*;
use sapio::util::amountrange::*;
use sapio::*;
use sapio_trait::SapioJSONTrait;
use schemars::*;
use serde::*;
use serde_json::Value;
use std::collections::VecDeque;

/// A payment to a specific address
#[derive(JsonSchema, Serialize, Deserialize, Clone)]
pub struct Payment {
/// The amount to send
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
#[schemars(with = "f64")]
pub amount: bitcoin::util::amount::Amount,
/// # Address
/// The Address to send to
pub address: bitcoin::Address,
}
#[derive(Serialize, JsonSchema, Deserialize, Clone)]
pub struct BatchingTraitVersion0_1_1 {
pub payments: Vec<Payment>,
#[serde(with = "bitcoin::util::amount::serde::as_sat")]
#[schemars(with = "u64")]
pub feerate_per_byte: bitcoin::util::amount::Amount,
}

impl SapioJSONTrait for BatchingTraitVersion0_1_1 {
fn get_example_for_api_checking() -> Value {
#[derive(Serialize)]
enum Versions {
BatchingTraitVersion0_1_1(BatchingTraitVersion0_1_1),
}
serde_json::to_value(Versions::BatchingTraitVersion0_1_1(BatchingTraitVersion0_1_1 {
payments: vec![],
feerate_per_byte: bitcoin::util::amount::Amount::from_sat(0),
})).unwrap()
}
}
59 changes: 59 additions & 0 deletions plugin-example/trampolinepay/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[package]
name = "trampolinepay"
version = "0.1.0"
license = "MPL-2.0"
authors = ["Jeremy Rubin <j@rubin.io>"]
edition = "2018"
repository = "https://github.com/sapio-lang/sapio"
homepage = "https://sapio-lang.org"
description = "An Example Sapio Application"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html



[dependencies]
schemars = "0.8.0"
serde_json = "1.0"
serde = "1.0"
serde_derive = "1.0"

[dependencies.bitcoin]
package = "sapio-bitcoin"
version = "^0.26.0"
features = ['use-serde', 'rand']
[dependencies.sapio]
path = "../../sapio"
version = "0.1.0"

[dependencies.batching-trait]
path = "../batching-trait"
version = "0.1.0"

[dependencies.sapio-base]
path = "../../sapio-base"
version = "0.1.0"
[lib]
crate-type = ["cdylib", "rlib"]
path = "src/plugin.rs"



[dependencies.sapio-ctv-emulator-trait]
path="../../emulator-trait"
version = "0.1.0"

[dependencies.miniscript]
package = "sapio-miniscript"
version = "^5.1.0"
features = ['compiler', 'use-serde', 'rand', 'use-schemars', 'serde']
optional = true

[package.metadata.wasm-pack.profile.release]
wasm-opt = false

[dependencies.sapio-wasm-plugin]
path = "../../plugins"
version = "0.1.0"
features = ["client"]

8 changes: 8 additions & 0 deletions plugin-example/trampolinepay/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Trampoline Pay

This crate can be compiled with `wasm-pack build`. The `*.wasm` artifact will
be created in the `pkg` directory, not in `target`.

Feel free to modify this code to experiment with creating your own Sapio plugins.

Trampoline Pay demonstrates using the Traits API.
Binary file added plugin-example/trampolinepay/src/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions plugin-example/trampolinepay/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright Judica, Inc 2021
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#[deny(missing_docs)]
use batching_trait::BatchingTraitVersion0_1_1;
use bitcoin::util::amount::Amount;
use sapio::contract::*;
use sapio::util::amountrange::*;
use sapio::*;
use sapio_wasm_plugin::client::*;
use sapio_wasm_plugin::*;
use schemars::*;
use serde::*;
use std::collections::VecDeque;

/// Documentation placed here will be visible to users!
#[derive(JsonSchema, Serialize, Deserialize)]
pub struct TrampolinePay {
/// # Which Plugin to Use
/// Specify which contract plugin to call out to.
handle: SapioHostAPI<BatchingTraitVersion0_1_1>,
/// # Data for the Contract
// Just do this to get the data... not always necessary (could be computed any way)
data: BatchingTraitVersion0_1_1,
}

#[derive(Serialize, Deserialize, JsonSchema)]
enum Versions {
/// # Batching Trait API
BatchingTraitVersion0_1_1(BatchingTraitVersion0_1_1),
}
impl TrampolinePay {
then! {
fn expand(self, ctx) {
let compiled = create_contract_by_key(
&self.handle.key,
serde_json::to_value(CreateArgs {
amount: ctx.funds(),
network: ctx.network,
arguments: Versions::BatchingTraitVersion0_1_1(self.data.clone()),
})
.map_err(|_| CompilationError::TerminateCompilation)?,
Amount::from_sat(0),
);
if let Some(contract) = compiled {
let mut builder = ctx.template();
builder = builder.add_output(contract.amount_range.max(), &contract, None)?;
builder.into()
} else {
Err(CompilationError::TerminateCompilation)
}
}
}
}
impl Contract for TrampolinePay {
declare! {then, Self::expand}
declare! {non updatable}
}

REGISTER![TrampolinePay, "logo.png"];
4 changes: 4 additions & 0 deletions plugin-example/treepay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ features = ['use-serde', 'rand']
path = "../../sapio"
version = "0.1.0"

[dependencies.batching-trait]
path = "../batching-trait"
version = "0.1.0"

[dependencies.sapio-base]
path = "../../sapio-base"
version = "0.1.0"
Expand Down
Empty file removed plugin-example/treepay/LOG
Empty file.
Binary file modified plugin-example/treepay/src/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 25 additions & 18 deletions plugin-example/treepay/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use batching_trait::{BatchingTraitVersion0_1_1, Payment};
#[deny(missing_docs)]
use sapio::contract::*;
use sapio::util::amountrange::*;
Expand All @@ -14,17 +15,6 @@ use schemars::*;
use serde::*;
use std::collections::VecDeque;

/// A payment to a specific address
#[derive(JsonSchema, Serialize, Deserialize, Clone)]
pub struct Payment {
/// The amount to send
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
#[schemars(with = "f64")]
pub amount: bitcoin::util::amount::Amount,
/// # Address
/// The Address to send to
pub address: bitcoin::Address,
}
/// Documentation placed here will be visible to users!
#[derive(JsonSchema, Serialize, Deserialize)]
pub struct TreePay {
Expand Down Expand Up @@ -100,17 +90,34 @@ impl Contract for TreePay {
}

#[derive(Serialize, Deserialize, JsonSchema)]
enum Versions{
Basic(TreePay),
Advanced(TreePay,
/// # A Ranomd Field For Example
u8),
enum Versions {
/// # Tree Pay
TreePay(TreePay),
/// # Advanced Tree Pay
Advanced(
TreePay,
/// # A Random Field For Example
u8,
),
/// # Batching Trait API
BatchingTraitVersion0_1_1(BatchingTraitVersion0_1_1),
}
impl From<BatchingTraitVersion0_1_1> for TreePay {
fn from(args: BatchingTraitVersion0_1_1) -> Self {
TreePay {
participants: args.payments,
radix: 4,
// estimate fees to be 4 outputs and 1 input + change
fee_sats_per_tx: args.feerate_per_byte * ((4 * 41) + 41 + 10),
}
}
}
impl From<Versions> for TreePay {
fn from(v:Versions) -> TreePay {
fn from(v: Versions) -> TreePay {
match v {
Versions::Basic(v) => v,
Versions::TreePay(v) => v,
Versions::Advanced(v, _) => v,
Versions::BatchingTraitVersion0_1_1(v) => v.into(),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions plugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ serde_json = "1.0"
serde = "1.0"
serde_derive = "1.0"
base64 = "0.13.0"
hex = "0.4.3"

[dependencies.wasm-bindgen]
version = "0.2.69"
optional = true

[dependencies.sapio-trait]
version = "0.1.0"
path = "../sapio-trait"

[dependencies.directories]
version = "3.0.1"
optional = true
Expand Down
Loading