solana/src/payment_plan.rs

41 lines
1.3 KiB
Rust
Raw Normal View History

//! The `plan` module provides a domain-specific language for payment plans. Users create Budget objects that
2018-05-25 14:51:41 -07:00
//! are given to an interpreter. The interpreter listens for `Witness` transactions,
2018-03-20 14:52:46 -07:00
//! which it uses to reduce the payment plan. When the plan is reduced to a
//! `Payment`, the payment is executed.
2018-03-17 13:42:43 -07:00
use chrono::prelude::*;
use signature::Pubkey;
2018-03-17 13:42:43 -07:00
2018-06-06 16:16:12 -07:00
/// The types of events a payment plan can process.
2018-05-22 20:42:04 -07:00
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
2018-03-20 14:31:28 -07:00
pub enum Witness {
2018-06-06 16:16:12 -07:00
/// The current time.
2018-03-17 13:42:43 -07:00
Timestamp(DateTime<Utc>),
2018-06-06 16:16:12 -07:00
2018-09-10 19:58:18 -07:00
/// A signature from Pubkey.
Signature,
2018-03-17 13:42:43 -07:00
}
/// Some amount of tokens that should be sent to the `to` `Pubkey`.
2018-03-17 13:42:43 -07:00
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Payment {
2018-06-06 16:16:12 -07:00
/// Amount to be paid.
2018-03-19 09:03:41 -07:00
pub tokens: i64,
2018-06-06 16:16:12 -07:00
/// The `Pubkey` that `tokens` should be paid to.
pub to: Pubkey,
2018-03-17 13:42:43 -07:00
}
2018-06-06 16:16:12 -07:00
/// Interface to smart contracts.
2018-05-29 12:00:44 -07:00
pub trait PaymentPlan {
/// Return Payment if the payment plan requires no additional Witnesses.
2018-05-29 12:00:44 -07:00
fn final_payment(&self) -> Option<Payment>;
/// Return true if the plan spends exactly `spendable_tokens`.
fn verify(&self, spendable_tokens: i64) -> bool;
/// Apply a witness to the payment plan to see if the plan can be reduced.
2018-05-29 12:00:44 -07:00
/// If so, modify the plan in-place.
fn apply_witness(&mut self, witness: &Witness, from: &Pubkey);
2018-05-29 12:00:44 -07:00
}