MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / transfer_to

Method transfer_to

code/chapter09/blockchain/blockchain6/core/src/account.rs:33–57  ·  view source on GitHub ↗
(&mut self, to: &mut Self, amount: u64, fee: u64)

Source from the content-addressed store, hash-verified

31 }
32
33 pub fn transfer_to(&mut self, to: &mut Self, amount: u64, fee: u64)
34 -> Result<Transaction, String>
35 {
36 if amount + fee > self.balance {
37 return Err("Error: not enough amount!".to_string());
38 }
39
40 self.balance -= amount;
41 self.balance -= fee;
42 self.nonce += 1;
43 self.set_hash();
44
45 to.balance += amount;
46 to.nonce += 1;
47 to.set_hash();
48
49 let sign = format!("{} -> {}: {} btc",
50 self.address.clone(),
51 to.address.clone(),
52 amount);
53 let tx = Transaction::new(self.address.clone(),
54 to.address.clone(),
55 amount, fee, self.nonce, sign);
56 Ok(tx)
57 }
58
59 pub fn account_info(&self) {
60 println!("{:#?}", &self);

Callers 1

mainFunction · 0.45

Calls 2

to_stringMethod · 0.45
set_hashMethod · 0.45

Tested by

no test coverage detected