Skip to content

Commit

Permalink
chore: add WIP examples (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Sep 20, 2024
1 parent bb50b61 commit abe48a7
Show file tree
Hide file tree
Showing 23 changed files with 6,089 additions and 0 deletions.
3,038 changes: 3,038 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

[workspace]
resolver = "2"
members = [
"balius",
"examples/basic",
"examples/catalyst-demo",
"examples/sundae-stop-loss-strategy",
"examples/ticket-vending-machine",
]
14 changes: 14 additions & 0 deletions examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "my-bod"
version = "0.1.0"
edition = "2021"

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

[dependencies]
balius = { path = "../../balius" }
pallas-primitives = "0.30.0"
serde = { version = "1.0.204", features = ["derive"] }

[lib]
crate-type = ["cdylib"]
72 changes: 72 additions & 0 deletions examples/basic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// fn track_utxo(utxo: Utxo) -> Result<()> {
// bindings::balius::odk::kv::set_value("balance",
// &handler.as_bytes().to_vec()).unwrap();

// Ok(())
// }

// fn clear_utxo(utxo: Utxo, state: Saved<State>) -> Result<()> {
// let state = state.get_mut();

// state.lovelace_balance -= utxo.value.lovelace;

// Ok(())
// }

// fn read_state(state: Saved<State>) -> Result<State> {
// let state = state.get();

// Ok(state)
// }

use balius::handler_wrapper;
use balius::{
bindings::balius::odk::driver::{Event, HandleError, Response},
Json,
};

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Param {
my_name: String,
}

#[derive(Serialize, Deserialize)]
struct Reply {
message: String,
}

fn say_hello(param: Json<Param>) -> Result<Json<Reply>, HandleError> {
Ok(Json(Reply {
message: format!("Hello, {}!", param.0.my_name),
}))
}

fn read_state(evt: Event) -> Result<Json<State>, HandleError> {
Ok(Json(State {
lovelace_balance: 3,
}))
}

#[derive(Serialize, Deserialize)]
struct State {
lovelace_balance: u64,
}

#[derive(Default)]
struct Config {
address: String,
}

//#[balius::main]
fn main(config: Config) -> balius::Worker {
balius::Worker::new()
//.watch_utxo(with_address(config.address), track_utxo)
//.watch_utxo_spent(with_address(config.address), clear_utxo)
//.watch_utxo_undo(with_address(config.address), clear_utxo)
.handle_request("read-state", read_state)
.handle_request("say-hello", say_hello)
}

balius::entrypoint!(main);
14 changes: 14 additions & 0 deletions examples/catalyst-demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "catalyst-demo"
version = "0.1.0"
edition = "2021"

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

[dependencies]
balius = { path = "../../balius" }
pallas-primitives = "0.30.0"
serde = { version = "1.0.204", features = ["derive"] }

[lib]
crate-type = ["cdylib"]
47 changes: 47 additions & 0 deletions examples/catalyst-demo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use balius::{
bindings::balius::odk::driver::{Event, HandleError, Response},
Json,
};

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Param {
my_name: String,
}

#[derive(Serialize, Deserialize)]
struct Reply {
message: String,
}

fn say_hello(param: Json<Param>) -> Result<Json<Reply>, HandleError> {
Ok(Json(Reply {
message: format!("Hello, {}!", param.0.my_name),
}))
}

fn read_state(evt: Event) -> Result<Json<State>, HandleError> {
Ok(Json(State {
lovelace_balance: 3,
}))
}

#[derive(Serialize, Deserialize)]
struct State {
lovelace_balance: u64,
}

#[derive(Default)]
struct Config {
address: String,
}

//#[balius::main]
fn main(config: Config) -> balius::Worker {
balius::Worker::new()
.handle_request("read-state", read_state)
.handle_request("say-hello", say_hello)
}

balius::entrypoint!(main);
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions examples/sundae-stop-loss-strategy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "sundae-stop-loss-strategy"
version = "0.1.0"
edition = "2021"

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

[dependencies]
hex = "0.4.3"
pallas = { version = "0.23.0" }
rocket = "0.5.0"
rocket_dyn_templates = { version = "0.1.0", features = ["handlebars"] }
34 changes: 34 additions & 0 deletions examples/sundae-stop-loss-strategy/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
mod ddk;
mod sundae;

use crate::ddk::*;

struct MyParams {
sell_price: f32,
sell_quantity: u64,
max_slippage: f32,
}

struct MyDatum {}

fn on_some_utxo(utxo: Utxo<sundae::PoolDatum>, config: Config<MyParams>) {
// This is where your logic goes. Do whatever you need with the
}

wit_bindgen::generate!({
world: "bod",
});

struct MyBod;

impl Guest for MyBod {
fn handle(evt: Event) -> Result<(), HandleError> {
Router::on_utxo()
.at_address("addr1xxx")
.holding_token("asset1xxxx")
.handle_with(on_some_utxo)
.bind(evt)
}
}

export!(MyBod);
104 changes: 104 additions & 0 deletions examples/sundae-stop-loss-strategy/src/sundae.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use balius_ddk::prelude::*;
use serde::Serialize;

pub type Bytes = Vec<u8>;

#[derive(Serialize, Clone, PartialEq)]
pub struct AssetClass {
pub policy: Bytes,
pub name: Bytes,
}

#[derive(Serialize, Clone)]
pub enum Credential {
VKeyCredential(Bytes),
SCredential(Bytes),
}

impl Into<Cbor> for &Credential {
fn into(self) -> crate::submit::Cbor {
todo!()
}
}

#[derive(Serialize)]
pub struct MultiSigScriptSchema {
pub signature: Bytes,
}

#[derive(Serialize)]
pub struct SingletonValue {
pub policy: Bytes,
pub name: Bytes,
pub amount: u64,
}

#[derive(Serialize)]
pub enum Extension {
NoExtension,
Foo,
}

#[derive(Serialize, Clone)]
pub struct Address {
pub payment_credential: Credential,
pub stake_credential: Option<Credential>,
}

#[derive(Serialize)]
pub struct Destination {
pub address: Credential,
pub datum: Option<Credential>,
}

#[derive(Serialize)]
pub struct PoolDatum {
pub identifier: Bytes,
pub assets: (AssetClass, AssetClass),
pub circulating_lp: u64,
pub fees_per10_thousand: (u64, u64),
pub market_open: u64,
pub fee_finalized: u64,
pub protocol_fees: u64,
}

#[derive(Serialize)]
pub struct SwapOrderSpec {
pub offer: SingletonValue,
pub min_received: SingletonValue,
}

#[derive(Serialize)]
pub struct OrderDatum {
pub pool_ident: Bytes,
pub owner: MultiSigScriptSchema,
pub scooper_fee: u64,
pub destination: Destination,
pub order: SwapOrderSpec,
pub extension: Extension,
}

pub fn compute_abl_price(pool: &Utxo<PoolDatum>, asset: &AssetClass, gives: u64) -> f32 {
todo!()
}

pub fn pool_has_assets(asset_a: &AssetClass, asset_b: &AssetClass) -> bool {
todo!()
}

pub fn compute_received_with_slippage(current_price: f32, gives: u64, max_slippage: f32) -> u64 {
//current_price * gives as f32 * (1f32 - max_slippage)
todo!()
}

pub struct SwapOrderTx {
pub pool: Utxo<PoolDatum>,
pub desination: Address,
pub order: SwapOrderSpec,
}

impl SwapOrderTx {
pub fn finalize(self) {
todo!()
}
}
20 changes: 20 additions & 0 deletions examples/ticket-vending-machine/.github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Tests

on:
push:
branches: ["main"]
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: aiken-lang/[email protected]
with:
version: v1.0.24-alpha

- run: aiken fmt --check
- run: aiken check -D
- run: aiken build
6 changes: 6 additions & 0 deletions examples/ticket-vending-machine/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Aiken compilation artifacts
artifacts/
# Aiken's project working directory
build/
# Aiken's default documentation export
docs/
Loading

0 comments on commit abe48a7

Please sign in to comment.