feat: works
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::{collections::HashMap, fs, fs::File};
|
||||
use rust_util::{SimpleError, XResult, simple_error};
|
||||
use rust_util::{XResult, information, simple_error};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crate::{tx::Transaction};
|
||||
use crate::tx::Transaction;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CreditContract {
|
||||
@@ -18,9 +18,10 @@ impl CreditContract {
|
||||
name: name.into(),
|
||||
credit_limit,
|
||||
issue_amount: 0,
|
||||
admin: tx.sender.clone().unwrap(),
|
||||
admin: tx.sender.clone(),
|
||||
credit: HashMap::new(),
|
||||
};
|
||||
save_credit_contract(&c, false)?;
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
@@ -29,23 +30,29 @@ impl CreditContract {
|
||||
}
|
||||
|
||||
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()))? {
|
||||
if &self.admin != &tx.sender {
|
||||
return simple_error!("Current user is not admin, {} vs {:?}", self.admin, tx.sender);
|
||||
}
|
||||
if credit == 0 {
|
||||
return simple_error!("Cannot issue 0 credit!");
|
||||
}
|
||||
if self.issue_amount + credit > self.credit_limit {
|
||||
return simple_error!("Issue too much credit, current: {}, issue: {}, limit: {}", self.issue_amount, credit, self.credit_limit);
|
||||
return simple_error!("Issue too much credit, current: {}, issue: {}, limit: {}, left: {}", self.issue_amount, credit, self.credit_limit, self.credit_limit - self.issue_amount);
|
||||
}
|
||||
match self.credit.get_mut(receiver) {
|
||||
None => { self.credit.insert(receiver.to_owned(), credit); },
|
||||
Some(cr) => *cr += credit,
|
||||
}
|
||||
self.issue_amount += credit;
|
||||
save_credit_contract(self)?;
|
||||
save_credit_contract(self, true)?;
|
||||
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()))?) {
|
||||
if credit == 0 {
|
||||
return simple_error!("Cannot transfer 0 credit!");
|
||||
}
|
||||
match self.credit.get_mut(&tx.sender) {
|
||||
None => return simple_error!("Have not enough credit: {:?}", tx.sender),
|
||||
Some(cr) => {
|
||||
if *cr >= credit {
|
||||
@@ -59,7 +66,7 @@ impl CreditContract {
|
||||
}
|
||||
},
|
||||
}
|
||||
save_credit_contract(self)?;
|
||||
save_credit_contract(self, true)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -80,11 +87,14 @@ fn load_credit_contract(name: &str) -> XResult<CreditContract> {
|
||||
serde_json::from_str(&json).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
fn save_credit_contract(c: &CreditContract) -> XResult<()> {
|
||||
fn save_credit_contract(c: &CreditContract, overwrite: bool) -> XResult<()> {
|
||||
let name = &c.name;
|
||||
if let Ok(_) = File::open(name) {
|
||||
return simple_error!("File exists: {}", name);
|
||||
if !overwrite {
|
||||
if let Ok(_) = File::open(name) {
|
||||
return simple_error!("File exists: {}", name);
|
||||
}
|
||||
}
|
||||
information!("Write file: {}", name);
|
||||
fs::write(name, serde_json::to_string(c)?.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user