style: style
This commit is contained in:
81
__crypto/simple_contract/src/engine_plugin_credit.rs
Normal file
81
__crypto/simple_contract/src/engine_plugin_credit.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
use rust_util::XResult;
|
||||
use crate::credit::CreditContract;
|
||||
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 = CreditContract::load(¶ms.name)?;
|
||||
c.issue(tx, ¶ms.receiver, params.credit)?;
|
||||
},
|
||||
"transfer" => {
|
||||
let params: CreditContractTransferParameters = serde_json::from_str(&tx_body.parameters)?;
|
||||
let mut c = CreditContract::load(¶ms.name)?;
|
||||
c.transfer(tx, ¶ms.receiver, params.credit)?;
|
||||
},
|
||||
"query" => {
|
||||
let params: CreditContractQueryParameters = serde_json::from_str(&tx_body.parameters)?;
|
||||
let c = CreditContract::load(¶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 = CreditContract::load(¶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(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user