summaryrefslogtreecommitdiff
path: root/src/ui.rs
blob: 697c7f3e067c32960240069534b8ab72727e4a51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::sync::Arc;

use bitcoin::{blocks::BlockChain, client::Client, network::Request, transaction::Transaction};
use tokio::sync::Mutex;

pub struct Ui {
    blockchain: Arc<Mutex<BlockChain>>,
    me: Client,
}

impl Ui {
    pub fn new(blockchain: Arc<Mutex<BlockChain>>, me: Client) -> Self {
        Self {
            blockchain,
            me,
        }
    }

    // TODO: typed command and result propagation
    pub async fn cmd(&mut self, cmd: &str) -> Option<Request> {
        let cmd: Vec<&str> = cmd
            .split_whitespace()
            .collect();
        if cmd[0] == "trans" {
            let trans = Transaction::new(
                self.me,
                Client::new(
                    cmd[1]
                        .parse()
                        .unwrap(),
                ),
                cmd[2]
                    .parse()
                    .unwrap(),
            );
            return Some(Request::Block(
                self.blockchain
                    .lock()
                    .await
                    .new_block(trans),
            ));
        }

        if cmd[0] == "bc" {
            println!(
                "{:#?}",
                self.blockchain
                    .lock()
                    .await
            );
        }
        None
    }
}