diff --git a/code/Cargo.lock b/code/Cargo.lock index a4ee902..dc11edc 100644 --- a/code/Cargo.lock +++ b/code/Cargo.lock @@ -3,5 +3,5 @@ version = 4 [[package]] -name = "rust_for_bitcoin" +name = "Tosin_week2_assignment" version = "0.1.0" diff --git a/code/Cargo.toml b/code/Cargo.toml index 4f9a0c1..b6e1549 100644 --- a/code/Cargo.toml +++ b/code/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rust_for_bitcoin" +name = "Tosin_week2_assignment" version = "0.1.0" edition = "2024" diff --git a/code/src/main.rs b/code/src/main.rs index 4308031..a96ead8 100644 --- a/code/src/main.rs +++ b/code/src/main.rs @@ -1,10 +1,32 @@ -// import session_3 module -mod week_2; +mod task1; +mod task2; +mod task3; + +use task1::btc_value_in_usd; +use task2::mine_blocks; +use task3::{get_rpc_url, Network }; + -// entry point to run all topics covered fn main() { - // call functions from session_3 modules - week_2::function::run(); - week_2::loops::run(); - week_2::primitive_types::run(); + println!("Hello, world!"); + + println!("Task 1"); + let btc_val = 0.5; + let rate = 115_194.75; + let btc_usd = btc_value_in_usd(btc_val, rate); + println!("The price of {btc_val} in usd is {btc_usd}"); + + println!("Task 2"); + mine_blocks(15); + + println!("Task 3"); + let network = Network::Mainnet; + let rpc = get_rpc_url(&network); + println!("The rpc of mainnet is {rpc}"); + + let network = Network::Testnet; + let rpc = get_rpc_url(&network); + println!("The rpc of testnet is {rpc}"); + + } \ No newline at end of file diff --git a/code/src/week_2/task_1.rs b/code/src/week_2/task_1.rs index f47db00..7c8f641 100644 --- a/code/src/week_2/task_1.rs +++ b/code/src/week_2/task_1.rs @@ -2,10 +2,4 @@ fn btc_value_in_usd(btc: f64, rate: f64) -> f64 { btc * rate } -//main run main -fn main() { - let btc = 0.25; //a dream amount - let rate = 65_000.0; // example rate here - let value = btc_value_in_usd(btc, rate); - println!("{} BTC is worth ${} USD", btc, value); -} + diff --git a/code/src/week_2/task_2.rs b/code/src/week_2/task_2.rs index aca8ad6..fd7baa3 100644 --- a/code/src/week_2/task_2.rs +++ b/code/src/week_2/task_2.rs @@ -1,21 +1,20 @@ -fn mine_blocks(limit: u8) { +pub fn mine_blocks(limit: u8) { let mut difficulty = 1; - for height in 1..=limit { - println!("Mining block #{}", height); + for height in 0..limit { + let block_no = height + 1; + println!("Mining block #{block_no}"); - // Simulate difficulty - while difficulty < 3 { - println!("Simulating difficulty level {}", difficulty); - difficulty += 1; // the number of starting zero's + while difficulty < height { + difficulty += 1; + println!("Simluating difficulty {difficulty}"); + println!(" "); } if height % 5 == 0 { - println!("Checkpoint reached"); + println!("Checkpoint reached ✅"); + println!(" "); } - } -} -fn main() { - mine_blocks(10); + } } diff --git a/code/src/week_2/task_3.rs b/code/src/week_2/task_3.rs index 0b0b29a..aabe3c4 100644 --- a/code/src/week_2/task_3.rs +++ b/code/src/week_2/task_3.rs @@ -1,36 +1,13 @@ -enum Network { +pub enum Network { Mainnet, Testnet, - Regtest, + Regtest } -fn get_rpc_url(network: &Network) -> &str { +pub fn get_rpc_url(network : &Network) -> &str { match network { - Network::Mainnet => "https://mainnet.example.com", - Network::Testnet => "https://testnet.example.com", - Network::Regtest => "http://localhost:8332", + Network::Mainnet => "https://a_mainnet_rpc_url.com", + Network::Testnet => "https://a_testnet_rpc_url.com", + Network::Regtest => "https://a_regtest_rpc_url.com", } -} - -fn print_network_details(network: &Network) { - match network { - Network::Mainnet => println!("This is the main Bitcoin network."), - Network::Testnet => println!("This is the test Bitcoin network."), - Network::Regtest => println!("This is the regtest Bitcoin network."), - } -} - -fn main() { - let network = Network::Regtest; - - print_network_details(&network); - println!("RPC URL: {}", get_rpc_url(&network)); - let network = Network::Mainnet; - - print_network_details(&network); - println!("RPC URL: {}", get_rpc_url(&network)); - let network = Network::Testnet; - - print_network_details(&network); - println!("RPC URL: {}", get_rpc_url(&network)); -} +} \ No newline at end of file