1use crate::address::Address;
2use std::fmt::Debug;
3use thiserror::Error;
4
5pub mod raw_policy_script;
6pub mod raw_script;
7pub mod raw_validator_script;
8
9#[derive(Clone)]
11pub struct TxContext {
12 pub signer: Address,
13}
14
15pub trait ValidatorCode<D, R>: Send + Sync {
16 fn execute(&self, datum: D, redeemer: R, ctx: TxContext) -> ScriptResult<()>;
17 fn address(&self, network: u8) -> ScriptResult<Address>;
19 fn script_hex(&self) -> ScriptResult<&str>;
20}
21
22pub trait MintingPolicy<R>: Send + Sync {
23 fn execute(&self, redeemer: R, ctx: TxContext) -> ScriptResult<()>;
24 fn id(&self) -> String;
26 fn script_hex(&self) -> ScriptResult<&str>;
27}
28
29#[derive(Debug, Error, PartialEq, Eq)]
30pub enum ScriptError {
31 #[error("Failed to execute: {0:?}")]
32 FailedToExecute(String),
33 #[error("Failed to construct: {0:?}")]
34 FailedToConstruct(String),
35 #[error("Failed to deserialize Datum")]
36 DatumDeserialization(String),
37 #[error("Failed to deserialize Redeemer")]
38 RedeemerDeserialization(String),
39}
40
41pub fn as_failed_to_execute<E: Debug>(e: E) -> ScriptError {
42 ScriptError::FailedToExecute(format!("{:?}", e))
43}
44
45pub type ScriptResult<T> = Result<T, ScriptError>;