solana/programs/budget_api/src/budget_instruction.rs

154 lines
5.0 KiB
Rust
Raw Normal View History

use crate::budget_expr::BudgetExpr;
2019-03-23 04:15:15 -07:00
use crate::budget_state::BudgetState;
2019-03-02 13:23:22 -08:00
use crate::id;
2019-03-23 04:15:15 -07:00
use bincode::serialized_size;
use chrono::prelude::{DateTime, Utc};
2019-03-02 13:23:22 -08:00
use serde_derive::{Deserialize, Serialize};
2019-03-23 20:12:27 -07:00
use solana_sdk::instruction::{AccountMeta, Instruction};
2019-03-02 13:23:22 -08:00
use solana_sdk::pubkey::Pubkey;
2019-03-23 04:15:15 -07:00
use solana_sdk::system_instruction::SystemInstruction;
/// A smart contract.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Contract {
2019-03-05 17:27:25 -08:00
/// The number of lamports allocated to the `BudgetExpr` and any transaction fees.
pub lamports: u64,
2018-11-02 19:13:33 -07:00
pub budget_expr: BudgetExpr,
}
/// An instruction to progress the smart contract.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum BudgetInstruction {
2018-11-02 19:13:33 -07:00
/// Declare and instantiate `BudgetExpr`.
InitializeAccount(BudgetExpr),
/// Tell a payment plan acknowledge the given `DateTime` has past.
ApplyTimestamp(DateTime<Utc>),
/// Tell the budget that the `InitializeAccount` with `Signature` has been
/// signed by the containing transaction's `Pubkey`.
2018-09-18 18:45:44 -07:00
ApplySignature,
}
2019-02-28 03:48:44 -08:00
impl BudgetInstruction {
2019-03-25 19:57:25 -07:00
fn new_initialize_account(contract: &Pubkey, expr: BudgetExpr) -> Instruction {
2019-03-03 14:43:51 -08:00
let mut keys = vec![];
if let BudgetExpr::Pay(payment) = &expr {
keys.push(AccountMeta::new(payment.to, false));
2019-03-03 14:43:51 -08:00
}
keys.push(AccountMeta::new(*contract, false));
Instruction::new(id(), &BudgetInstruction::InitializeAccount(expr), keys)
2019-02-28 03:48:44 -08:00
}
2019-03-23 04:15:15 -07:00
pub fn new_account(
from: &Pubkey,
contract: &Pubkey,
lamports: u64,
expr: BudgetExpr,
) -> Vec<Instruction> {
if !expr.verify(lamports) {
panic!("invalid budget expression");
}
let space = serialized_size(&BudgetState::new(expr.clone())).unwrap();
vec![
SystemInstruction::new_account(&from, contract, lamports, space, &id()),
2019-03-23 04:15:15 -07:00
BudgetInstruction::new_initialize_account(contract, expr),
]
}
/// Create a new payment script.
pub fn new_payment(from: &Pubkey, to: &Pubkey, lamports: u64) -> Vec<Instruction> {
let contract = Pubkey::new_rand();
2019-03-23 04:15:15 -07:00
let expr = BudgetExpr::new_payment(lamports, to);
Self::new_account(from, &contract, lamports, expr)
}
/// Create a future payment script.
pub fn new_on_date(
from: &Pubkey,
to: &Pubkey,
contract: &Pubkey,
dt: DateTime<Utc>,
dt_pubkey: &Pubkey,
cancelable: Option<Pubkey>,
lamports: u64,
) -> Vec<Instruction> {
let expr =
BudgetExpr::new_cancelable_future_payment(dt, dt_pubkey, lamports, to, cancelable);
Self::new_account(from, contract, lamports, expr)
}
/// Create a multisig payment script.
pub fn new_when_signed(
from: &Pubkey,
to: &Pubkey,
contract: &Pubkey,
witness: &Pubkey,
cancelable: Option<Pubkey>,
lamports: u64,
) -> Vec<Instruction> {
let expr = BudgetExpr::new_cancelable_authorized_payment(witness, lamports, to, cancelable);
Self::new_account(from, contract, lamports, expr)
}
2019-03-25 19:57:25 -07:00
pub fn new_apply_timestamp(
from: &Pubkey,
contract: &Pubkey,
to: &Pubkey,
dt: DateTime<Utc>,
) -> Instruction {
let mut account_metas = vec![
AccountMeta::new(*from, true),
AccountMeta::new(*contract, false),
];
if from != to {
account_metas.push(AccountMeta::new(*to, false));
}
Instruction::new(id(), &BudgetInstruction::ApplyTimestamp(dt), account_metas)
}
pub fn new_apply_signature(from: &Pubkey, contract: &Pubkey, to: &Pubkey) -> Instruction {
let mut account_metas = vec![
AccountMeta::new(*from, true),
AccountMeta::new(*contract, false),
];
if from != to {
account_metas.push(AccountMeta::new(*to, false));
}
Instruction::new(id(), &BudgetInstruction::ApplySignature, account_metas)
}
2019-03-23 04:15:15 -07:00
}
#[cfg(test)]
mod tests {
use super::*;
use crate::budget_expr::BudgetExpr;
#[test]
fn test_budget_instruction_verify() {
let alice_pubkey = Pubkey::new_rand();
let bob_pubkey = Pubkey::new_rand();
2019-03-23 04:15:15 -07:00
BudgetInstruction::new_payment(&alice_pubkey, &bob_pubkey, 1); // No panic! indicates success.
}
#[test]
#[should_panic]
fn test_budget_instruction_overspend() {
let alice_pubkey = Pubkey::new_rand();
let bob_pubkey = Pubkey::new_rand();
let budget_pubkey = Pubkey::new_rand();
2019-03-23 04:15:15 -07:00
let expr = BudgetExpr::new_payment(2, &bob_pubkey);
BudgetInstruction::new_account(&alice_pubkey, &budget_pubkey, 1, expr);
}
#[test]
#[should_panic]
fn test_budget_instruction_underspend() {
let alice_pubkey = Pubkey::new_rand();
let bob_pubkey = Pubkey::new_rand();
let budget_pubkey = Pubkey::new_rand();
2019-03-23 04:15:15 -07:00
let expr = BudgetExpr::new_payment(1, &bob_pubkey);
BudgetInstruction::new_account(&alice_pubkey, &budget_pubkey, 2, expr);
}
2019-02-28 03:48:44 -08:00
}