feat: add simple contract

This commit is contained in:
2021-01-01 18:35:28 +08:00
parent 6386371a69
commit 54856504a2
6 changed files with 684 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
use std::{collections::HashMap, fs, fs::File};
use serde::{Serialize, Deserialize};
use crate::{tx::Transaction, util::{SimpleError, XResult}};
#[derive(Debug, Serialize, Deserialize)]
pub struct CreditContract {
pub credit_limit: u32,
pub issue_amount: u32,
pub admin: String,
pub credit: HashMap<String, u32>,
}
impl CreditContract {
pub fn new(tx: &Transaction, name: &str, credit_limit: u32) -> XResult<Self> {
let c = Self {
credit_limit,
issue_amount: 0,
admin: tx.sender.clone().unwrap(),
credit: HashMap::new(),
};
Self::save(name, &c)?;
Ok(c)
}
pub fn issue(&mut self, tx: &Transaction, receiver: &str, credit: u32) -> XResult<()> {
if &self.admin != tx.sender.as_ref().ok_or_else(|| SimpleError::new("Sender is not provided".into()))? {
return Err(SimpleError::new(format!("Current user is not admin, {} vs {:?}", self.admin, tx.sender)).into())
}
if self.issue_amount + credit > self.credit_limit {
return Err(SimpleError::new(format!("Issue too much credit, current: {}, issue: {}, limit: {}", self.issue_amount, credit, self.credit_limit)).into());
}
match self.credit.get_mut(receiver) {
None => { self.credit.insert(receiver.to_owned(), credit); },
Some(cr) => *cr += credit,
}
self.issue_amount += credit;
Ok(())
}
pub fn transfer(&mut self, tx: &Transaction, receiver: &str, credit: u32) -> XResult<()> {
match self.credit.get_mut(tx.sender.as_ref().ok_or_else(|| SimpleError::new("Sender is not provided".into()))?) {
None => return Err(SimpleError::new(format!("Have not enough credit: {:?}", tx.sender)).into()),
Some(cr) => {
if *cr >= credit {
*cr -= credit;
match self.credit.get_mut(receiver) {
None => { self.credit.insert(receiver.to_owned(), credit); },
Some(receiver_credit) => *receiver_credit += credit,
}
} else {
return Err(SimpleError::new(format!("Have not enough credit: {:?}", tx.sender)).into());
}
},
}
Ok(())
}
pub fn get(&self, _tx: &Transaction, account: &str) -> u32 {
match self.credit.get(account) {
None => 0,
Some(receiver_credit) => *receiver_credit,
}
}
pub fn load(name: &str) -> XResult<Self> {
let json = fs::read_to_string(name)?;
serde_json::from_str(&json).map_err(|e| e.into())
}
pub fn save(name: &str, c: &CreditContract) -> XResult<()> {
if let Ok(_) = File::open(name) {
return Err(SimpleError::new(format!("File exists: {}", name)).into());
}
fs::write(name, serde_json::to_string(c)?.as_bytes())?;
Ok(())
}
}