use arete_sdk::protocol;
pub use arete_sdk::protocol::{
verify_webhook, ProtocolClient, ProtocolClientOptions, ProtocolError, ProtocolInteger,
ProtocolPrepareOptions, ProtocolPrepareResult, ProtocolPrepared, ProtocolRejection,
ProtocolRejectionCode,
};
pub mod stacks {}
pub mod programs {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum LocalTipJarInstanceState {
#[serde(rename = "closed")]
Closed,
#[serde(rename = "open")]
Open,
}
impl LocalTipJarInstanceState {
pub const ALL: [Self; 2] = [Self::Closed, Self::Open];
pub fn as_wire(self) -> &'static str {
match self {
Self::Closed => "closed",
Self::Open => "open",
}
}
pub fn from_wire(wire: &str) -> Option<Self> {
match wire {
"closed" => Some(Self::Closed),
"open" => Some(Self::Open),
_ => None,
}
}
}
impl LocalTipJarInstanceState {
pub fn of(instance: &protocol::ProtocolInstanceSummary) -> Option<Self> {
Self::from_wire(&instance.state)
}
pub fn is_terminal(self) -> bool {
matches!(self, Self::Closed)
}
}
#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize)]
pub struct LocalTipJarParams {
pub amount: ProtocolInteger,
}
#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize)]
pub struct LocalTipJarCloseArgs {
pub amount: ProtocolInteger,
}
#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize)]
pub struct LocalTipJarOpenArgs {
pub amount: ProtocolInteger,
pub recipient: String,
}
#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize)]
pub struct LocalTipJarInstanceMetadata {}
#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize)]
pub struct LocalTipJarParticipantMetadata {}
impl LocalTipJarInstanceMetadata {
pub fn from_map(
metadata: &serde_json::Map<String, serde_json::Value>,
) -> Result<Self, serde_json::Error> {
serde_json::from_value(serde_json::Value::Object(metadata.clone()))
}
pub fn to_patch(&self) -> serde_json::Map<String, serde_json::Value> {
match serde_json::to_value(self) {
Ok(serde_json::Value::Object(patch)) => patch,
_ => serde_json::Map::new(),
}
}
}
impl LocalTipJarParticipantMetadata {
pub fn from_map(
metadata: &serde_json::Map<String, serde_json::Value>,
) -> Result<Self, serde_json::Error> {
serde_json::from_value(serde_json::Value::Object(metadata.clone()))
}
pub fn to_patch(&self) -> serde_json::Map<String, serde_json::Value> {
match serde_json::to_value(self) {
Ok(serde_json::Value::Object(patch)) => patch,
_ => serde_json::Map::new(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LocalTipJarCloseGuardCode {
NotRecipient,
NotOpen,
}
impl LocalTipJarCloseGuardCode {
pub const ALL: [Self; 2] = [Self::NotRecipient, Self::NotOpen];
pub fn as_wire(self) -> &'static str {
match self {
Self::NotRecipient => "not_recipient",
Self::NotOpen => "not_open",
}
}
pub fn from_wire(wire: &str) -> Option<Self> {
match wire {
"not_recipient" => Some(Self::NotRecipient),
"not_open" => Some(Self::NotOpen),
_ => None,
}
}
}
impl protocol::ProtocolGuardCode for LocalTipJarCloseGuardCode {
fn from_wire(wire: &str) -> Option<Self> {
Self::from_wire(wire)
}
fn as_wire(&self) -> &str {
(*self).as_wire()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LocalTipJarOpenGuardCode {
AmountZero,
}
impl LocalTipJarOpenGuardCode {
pub const ALL: [Self; 1] = [Self::AmountZero];
pub fn as_wire(self) -> &'static str {
match self {
Self::AmountZero => "amount_zero",
}
}
pub fn from_wire(wire: &str) -> Option<Self> {
match wire {
"amount_zero" => Some(Self::AmountZero),
_ => None,
}
}
}
impl protocol::ProtocolGuardCode for LocalTipJarOpenGuardCode {
fn from_wire(wire: &str) -> Option<Self> {
Self::from_wire(wire)
}
fn as_wire(&self) -> &str {
(*self).as_wire()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LocalTipJarEvent {
TipJarClosed,
}
impl LocalTipJarEvent {
pub const ALL: [Self; 1] = [Self::TipJarClosed];
pub fn as_wire(self) -> &'static str {
match self {
Self::TipJarClosed => "tipJar.closed",
}
}
pub fn from_wire(wire: &str) -> Option<Self> {
match wire {
"tipJar.closed" => Some(Self::TipJarClosed),
_ => None,
}
}
}
impl LocalTipJarEvent {
pub fn machine(self) -> protocol::ProtocolMachineKind {
match self {
Self::TipJarClosed => protocol::ProtocolMachineKind::Instance,
}
}
pub fn of(payload: &protocol::ProtocolEventPayloadV1) -> Option<Self> {
Self::from_wire(&payload.event)
}
}
pub const LOCAL_TIP_JAR_PROTOCOL: protocol::ProtocolDefinition = protocol::ProtocolDefinition {
name: "localTipJar",
version: "0.1.0",
package: "local-tip-jar",
package_release_hash: "arete:h1:protocol-release:sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
protocol_spec_hash: "arete:h1:protocol-spec:sha256:f1a6580e1d2259ca7da870e33e0a1920ea0704cf2014efccf29116c4abc06032",
protocol_release_hash: "arete:h1:protocol-release:sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
deployment: protocol::ProtocolDeploymentDescriptor {
deployment_id: "dep-local-1",
access: protocol::ProtocolDeploymentAccess::Private,
api_url: "https://api.example.test/api",
session_endpoint: "https://api.example.test/ws/sessions",
},
instance_visibility: protocol::ProtocolInstanceVisibility::Shared,
steps: &[
protocol::ProtocolStepDefinition {
name: "close",
scope: protocol::ProtocolStepScope::Instance,
access: protocol::ProtocolStepAccess::Secret,
guards: &["not_recipient", "not_open"],
args: &["amount"],
},
protocol::ProtocolStepDefinition {
name: "open",
scope: protocol::ProtocolStepScope::Create,
access: protocol::ProtocolStepAccess::Secret,
guards: &["amount_zero"],
args: &["amount", "recipient"],
},
],
events: &[
protocol::ProtocolEventDefinition { name: "tipJar.closed", machine: protocol::ProtocolMachineKind::Instance },
],
instance_metadata: &[],
participant_metadata: &[],
live_stack: None,
};
#[derive(Debug, Clone)]
pub struct LocalTipJarPublishableClient {
client: ProtocolClient,
}
impl LocalTipJarPublishableClient {
pub fn new(publishable_key: impl Into<String>) -> Result<Self, ProtocolError> {
Self::with_options(ProtocolClientOptions::default().with_publishable_key(publishable_key))
}
pub fn with_options(mut options: ProtocolClientOptions) -> Result<Self, ProtocolError> {
options.key_class = Some(protocol::ProtocolKeyClass::Publishable);
Ok(Self {
client: ProtocolClient::new(&LOCAL_TIP_JAR_PROTOCOL, options)?,
})
}
pub fn from_session(
session: &arete_sdk::Session,
mut options: ProtocolClientOptions,
) -> Result<Self, ProtocolError> {
options.key_class = Some(protocol::ProtocolKeyClass::Publishable);
Ok(Self {
client: session.protocol_client(&LOCAL_TIP_JAR_PROTOCOL, options)?,
})
}
pub fn client(&self) -> &ProtocolClient {
&self.client
}
pub async fn instance_handle(
&self,
instance: &str,
) -> Result<protocol::ProtocolInstanceHandle, ProtocolError> {
self.client.instance_handle(instance).await
}
}
impl std::ops::Deref for LocalTipJarPublishableClient {
type Target = ProtocolClient;
fn deref(&self) -> &ProtocolClient {
&self.client
}
}
#[derive(Debug, Clone)]
pub struct LocalTipJarSecretClient {
client: ProtocolClient,
}
impl LocalTipJarSecretClient {
pub fn new(secret_key: impl Into<String>) -> Result<Self, ProtocolError> {
Self::with_options(ProtocolClientOptions::default().with_secret_key(secret_key))
}
pub fn from_env() -> Result<Self, ProtocolError> {
Self::with_options(ProtocolClientOptions::default())
}
pub fn with_options(mut options: ProtocolClientOptions) -> Result<Self, ProtocolError> {
options.key_class = Some(protocol::ProtocolKeyClass::Secret);
Ok(Self {
client: ProtocolClient::new(&LOCAL_TIP_JAR_PROTOCOL, options)?,
})
}
pub fn from_session(
session: &arete_sdk::Session,
mut options: ProtocolClientOptions,
) -> Result<Self, ProtocolError> {
options.key_class = Some(protocol::ProtocolKeyClass::Secret);
Ok(Self {
client: session.protocol_client(&LOCAL_TIP_JAR_PROTOCOL, options)?,
})
}
pub fn client(&self) -> &ProtocolClient {
&self.client
}
pub async fn instance_handle(
&self,
instance: &str,
) -> Result<protocol::ProtocolInstanceHandle, ProtocolError> {
self.client.instance_handle(instance).await
}
pub async fn prepare_close(
&self,
instance: &str,
wallet: &str,
args: &LocalTipJarCloseArgs,
options: ProtocolPrepareOptions,
) -> Result<ProtocolPrepareResult<LocalTipJarCloseGuardCode>, ProtocolError> {
self.client
.prepare("close", Some(instance), wallet, args, options)
.await
}
pub async fn prepare_open(
&self,
wallet: &str,
args: &LocalTipJarOpenArgs,
options: ProtocolPrepareOptions,
) -> Result<ProtocolPrepareResult<LocalTipJarOpenGuardCode>, ProtocolError> {
self.client
.prepare("open", None, wallet, args, options)
.await
}
}
impl std::ops::Deref for LocalTipJarSecretClient {
type Target = ProtocolClient;
fn deref(&self) -> &ProtocolClient {
&self.client
}
}
pub type LocalTipJarWebhookPayload = protocol::ProtocolEventPayloadV1;
pub fn verify_local_tip_jar_webhook(
secret: impl AsRef<[u8]>,
body: impl AsRef<[u8]>,
headers: &(impl protocol::WebhookHeaders + ?Sized),
options: protocol::VerifyWebhookOptions,
) -> Result<protocol::VerifiedWebhook<LocalTipJarWebhookPayload>, protocol::WebhookVerificationError> {
protocol::verify_webhook(secret, body, headers, options)
}