solana/programs/budget_api/src/budget_instruction.rs

42 lines
1.4 KiB
Rust
Raw Normal View History

2018-12-14 20:39:10 -08:00
use crate::budget_expr::BudgetExpr;
2019-03-02 13:23:22 -08:00
use crate::id;
use chrono::prelude::{DateTime, Utc};
2019-03-02 13:23:22 -08:00
use serde_derive::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::transaction_builder::BuilderInstruction;
/// 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 {
pub fn new_initialize_account(contract: Pubkey, expr: BudgetExpr) -> BuilderInstruction {
2019-03-03 14:43:51 -08:00
let mut keys = vec![];
if let BudgetExpr::Pay(payment) = &expr {
keys.push((payment.to, false));
} else {
panic!("unsupported Budget instruction");
}
keys.push((contract, false));
BuilderInstruction::new(id(), &BudgetInstruction::InitializeAccount(expr), keys)
2019-02-28 03:48:44 -08:00
}
}