2018-05-29 12:08:59 -07:00
|
|
|
//! 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::*;
|
2018-08-09 08:13:57 -07:00
|
|
|
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-08-09 08:13:57 -07:00
|
|
|
/// A siganture from Pubkey.
|
2018-07-08 20:04:55 -07:00
|
|
|
Signature,
|
2018-03-17 13:42:43 -07:00
|
|
|
}
|
|
|
|
|
2018-08-09 08:13:57 -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)]
|
2018-03-17 13:42:50 -07:00
|
|
|
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
|
|
|
|
2018-08-09 08:13:57 -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 {
|
2018-05-29 16:11:24 -07:00
|
|
|
/// 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;
|
|
|
|
|
2018-05-29 16:11:24 -07:00
|
|
|
/// 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.
|
2018-08-09 08:13:57 -07:00
|
|
|
fn apply_witness(&mut self, witness: &Witness, from: &Pubkey);
|
2018-05-29 12:00:44 -07:00
|
|
|
}
|