-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2fe87d
commit 8f22058
Showing
26 changed files
with
318 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
[package] | ||
name = "sapio-wasm-ordinal-example" | ||
version = "0.1.0" | ||
license = "MPL-2.0" | ||
authors = ["Jeremy Rubin <[email protected]>"] | ||
edition = "2021" | ||
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 | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
path = "src/plugin.rs" | ||
[package.metadata.wasm-pack.profile.release] | ||
wasm-opt = false | ||
|
||
[dependencies] | ||
serde_json = "1.0" | ||
serde = "1.0" | ||
serde_derive = "1.0" | ||
|
||
|
||
[dependencies.schemars] | ||
version = "0.8.0" | ||
features = ['impl_json_schema'] | ||
[dependencies.bitcoin] | ||
package = "sapio-bitcoin" | ||
version = "0.28.0" | ||
features = ['use-serde'] | ||
[dependencies.sapio] | ||
path = "../../sapio" | ||
version = "0.2.0" | ||
|
||
[dependencies.batching-trait] | ||
path = "../batching-trait" | ||
version = "0.1.0" | ||
|
||
[dependencies.sapio-base] | ||
path = "../../sapio-base" | ||
version = "0.2.0" | ||
[dependencies.sapio-contrib] | ||
path = "../../sapio-contrib" | ||
version = "0.2.0" | ||
|
||
|
||
[dependencies.sapio-ctv-emulator-trait] | ||
path = "../../emulator-trait" | ||
version = "0.2.0" | ||
|
||
[dependencies.miniscript] | ||
package = "sapio-miniscript" | ||
version = "^7.0.0" | ||
features = ['compiler', 'use-serde', 'use-schemars', 'serde'] | ||
optional = true | ||
|
||
|
||
[dependencies.sapio-wasm-plugin] | ||
path = "../../plugins" | ||
version = "0.2.0" | ||
features = ["client"] | ||
|
||
[dependencies.sapio-wasm-nft-trait] | ||
path = "../nft-trait" | ||
version = "0.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Sapio Vault Example | ||
|
||
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. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use bitcoin::hashes::hex::ToHex; | ||
use bitcoin::hashes::sha256::Hash as Sha256; | ||
use bitcoin::hashes::Hash; | ||
use bitcoin::util::amount::Amount; | ||
use bitcoin::XOnlyPublicKey; | ||
use sapio::contract::empty; | ||
use sapio::contract::object::ObjectMetadata; | ||
use sapio::contract::CompilationError; | ||
use sapio::contract::Compiled; | ||
use sapio::contract::Contract; | ||
use sapio::contract::StatefulArgumentsTrait; | ||
use sapio::util::amountrange::AmountF64; | ||
use sapio::*; | ||
use sapio_base::Clause; | ||
use sapio_wasm_nft_trait::*; | ||
use sapio_wasm_plugin::client::*; | ||
use sapio_wasm_plugin::plugin_handle::PluginHandle; | ||
use sapio_wasm_plugin::*; | ||
use schemars::*; | ||
use serde::*; | ||
use std::convert::TryFrom; | ||
use std::convert::TryInto; | ||
use std::sync::Arc; | ||
/// # SimpleOrdinal | ||
/// A really Ordinal Bearing Contract | ||
#[derive(JsonSchema, Serialize, Deserialize)] | ||
pub struct SimpleOrdinal { | ||
ordinal: u64, | ||
#[schemars(with = "bitcoin::hashes::sha256::Hash")] | ||
owner: XOnlyPublicKey, | ||
} | ||
#[derive(JsonSchema, Serialize, Deserialize)] | ||
pub struct Sell { | ||
purchaser: bitcoin::Address, | ||
amount: AmountF64, | ||
change: AmountF64, | ||
fee: AmountF64, | ||
} | ||
|
||
#[derive(JsonSchema, Serialize, Deserialize, Default)] | ||
pub struct Sale(Option<Sell>); | ||
// ASSUMES 500 sats after Ord are "dust" | ||
impl SimpleOrdinal { | ||
#[continuation(guarded_by = "[Self::signed]", web_api, coerce_args = "default_coerce")] | ||
fn sell(self, ctx: Context, opt_sale: Sale) { | ||
if let Sale(Some(sale)) = opt_sale { | ||
let o = ctx | ||
.get_ordinals() | ||
.as_ref() | ||
.ok_or_else(|| CompilationError::OrdinalsError("Missing Ordinals Info".into()))?; | ||
let mut index = 0; | ||
for (a, b) in o.iter() { | ||
if (*a..*b).contains(&self.ordinal) { | ||
index += (self.ordinal) - a; | ||
break; | ||
} else { | ||
index += b - a | ||
} | ||
} | ||
let mut t = ctx.template(); | ||
// TODO: Check Index calculation | ||
if index != 0 { | ||
t = t.add_output(Amount::from_sat(index - 1), &self.owner, None)?; | ||
} | ||
let buyer = Compiled::from_address(sale.purchaser, None); | ||
t = t.add_output(Amount::from_sat(501), &buyer, None)?; | ||
let remaining = t.ctx().funds(); | ||
t = t.add_amount(sale.amount.into()); | ||
t = t.add_output(remaining + sale.amount.into(), &self.owner, None)?; | ||
t = t.add_sequence(); | ||
t = t.add_amount(sale.change.into()); | ||
t = t.add_output(sale.change.into(), &buyer, None)?; | ||
t = t.add_amount(sale.fee.into()); | ||
t.add_fees(sale.fee.into())?.into() | ||
} else { | ||
empty() | ||
} | ||
} | ||
} | ||
impl StatefulArgumentsTrait for Sale {} | ||
|
||
/// # The SimpleNFT Contract | ||
impl Contract for SimpleOrdinal { | ||
// Ordinals... only good for selling? | ||
declare! {updatable<Sale>, Self::sell} | ||
|
||
fn ensure_amount(&self, ctx: Context) -> Result<Amount, CompilationError> { | ||
let ords = ctx | ||
.get_ordinals() | ||
.as_ref() | ||
.ok_or_else(|| CompilationError::OrdinalsError("Missing Ordinals Info".into()))?; | ||
if ords.iter().any(|(a, b)| (*a..*b).contains(&self.ordinal)) { | ||
Ok(Amount::from_sat(1 + 500)) | ||
} else { | ||
Err(CompilationError::OrdinalsError( | ||
"Missing Intended Ordinal".into(), | ||
)) | ||
} | ||
} | ||
} | ||
|
||
impl SimpleOrdinal { | ||
/// # signed | ||
/// Get the current owners signature. | ||
#[guard] | ||
fn signed(self, _ctx: Context) { | ||
Clause::Key(self.owner.clone()) | ||
} | ||
} | ||
fn default_coerce( | ||
k: <SimpleOrdinal as sapio::contract::Contract>::StatefulArguments, | ||
) -> Result<Sale, CompilationError> { | ||
Ok(k) | ||
} | ||
|
||
REGISTER![SimpleOrdinal, "logo.png"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.