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

Method transfer_to

code/chapter09/blockchain/blockchain5/core/src/account.rs:35–58  ·  view source on GitHub ↗

交易,此处就只是转移比特币

(&mut self, to: &mut Self, amount: u64, fee: u64)

Source from the content-addressed store, hash-verified

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

Callers 1

mainFunction · 0.45

Calls 2

to_stringMethod · 0.45
set_hashMethod · 0.45

Tested by

no test coverage detected