use serde::{Serialize, Deserialize}; use rust_util::XResult; use crate::credit::{CreditContract, load_credit_contract}; use crate::tx::{Transaction, TransactionBody}; #[derive(Debug, Serialize, Deserialize)] pub struct CreditContractCreateParameters { pub name: String, pub credit_limit: u32, } #[derive(Debug, Serialize, Deserialize)] pub struct CreditContractIssueParameters { pub name: String, pub receiver: String, pub credit: u32, } #[derive(Debug, Serialize, Deserialize)] pub struct CreditContractTransferParameters { pub name: String, pub receiver: String, pub credit: u32, } #[derive(Debug, Serialize, Deserialize)] pub struct CreditContractQueryParameters { pub name: String, pub account: String, } #[derive(Debug, Serialize, Deserialize)] pub struct CreditContractQueryAllParameters { pub name: String, } pub struct ContractEngineCredit { } impl ContractEngineCredit { pub fn new() -> Self { Self{} } pub fn execute(&self, tx: &Transaction, tx_body: &TransactionBody) -> XResult<()> { let action = tx_body.action.as_str(); debugging!("Creidt contract, action: {}", action); match action { "create" => { let params: CreditContractCreateParameters = serde_json::from_str(&tx_body.parameters)?; CreditContract::new(tx, ¶ms.name, params.credit_limit)?; }, "issue" => { let params: CreditContractIssueParameters = serde_json::from_str(&tx_body.parameters)?; let mut c = load_credit_contract(¶ms.name)?; c.issue(tx, ¶ms.receiver, params.credit)?; }, "transfer" => { let params: CreditContractTransferParameters = serde_json::from_str(&tx_body.parameters)?; let mut c =load_credit_contract(¶ms.name)?; c.transfer(tx, ¶ms.receiver, params.credit)?; }, "query" => { let params: CreditContractQueryParameters = serde_json::from_str(&tx_body.parameters)?; let c = load_credit_contract(¶ms.name)?; information!("Query: {}, credit: {}", ¶ms.account, c.query(tx, ¶ms.account)); }, "query_all" => { let params: CreditContractQueryAllParameters = serde_json::from_str(&tx_body.parameters)?; let c = load_credit_contract(¶ms.name)?; let map = c.query_all(tx); map.iter().for_each(|(k , v)| { information!("Query: {}, credit: {}", k, v); }); }, _ => return simple_error!("Unknown action: {}", action), } Ok(()) } }