cotyledon 0.1.0

Framework for writing sprouts — sandboxed, event-driven on-chain trading bots.
Documentation
//! Core value types used by handlers.

use serde::{Deserialize, Serialize};

/// A US-dollar amount.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Usd(pub f64);

/// Trade direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Side {
    Buy,
    Sell,
}

/// A trade to record on the sprout's activity feed. Build with [`Trade::buy`] /
/// [`Trade::sell`] and pass to `Ctx::emit`.
#[derive(Debug, Clone, Serialize)]
pub struct Trade {
    pub side: Side,
    pub token: String,
    pub amount: Usd,
    pub signature: Option<String>,
}

impl Trade {
    pub fn buy(token: &str, amount: Usd) -> Self {
        Self { side: Side::Buy, token: token.to_owned(), amount, signature: None }
    }

    pub fn sell(token: &str, amount: Usd) -> Self {
        Self { side: Side::Sell, token: token.to_owned(), amount, signature: None }
    }

    /// Attach the on-chain transaction signature.
    pub fn tx(mut self, signature: String) -> Self {
        self.signature = Some(signature);
        self
    }
}

/// The result of submitting a transaction.
#[derive(Debug, Clone, Deserialize)]
pub struct Receipt {
    pub signature: String,
}

/// A trigger event delivered to a handler, carrying the typed payload `T`.
#[derive(Debug, Clone)]
pub struct Event<T> {
    pub data: T,
}