Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions code/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
// import session_3 module
mod week_2;

// entry point to run all topics covered
// use week_2::ebuka_task_3;

use week_2::ebuka_task_3::Network;

use week_2::ebuka_task_3::get_rpc_url;

fn main() {
// call functions from session_3 modules
week_2::function::run();
week_2::loops::run();
week_2::primitive_types::run();
}
//First Task
let btc = 1.00;
let rate = 105000.00;
let value_in_usd = week_2::ebuka_task_1::btc_value_in_usd(btc, rate);
println!("value in usd {}", value_in_usd);

//Second Task
week_2::ebuka_task_2::mine_blocks(10);

//Third Task

let selected_network = Network::Regtest;
match selected_network {
Network::Mainnet => println!("Selected: Mainnet. This is the live production network."),
Network::Testnet => println!("Selected: Testnet. Use this for testing applications."),
Network::Regtest => {
println!("Selected: Regtest. This is a private network for local development.")
}
}
let rpc_url = get_rpc_url(&selected_network);
println!("RPC URL for network is: {}", rpc_url);
}
4 changes: 4 additions & 0 deletions code/src/week_2/ebuka_task_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//- Write a function `btc_value_in_usd(btc: f64, rate: f64) -> f64` that returns btc * rate.
pub fn btc_value_in_usd(btc: f64, rate: f64) -> f64 {
btc * rate
}
20 changes: 20 additions & 0 deletions code/src/week_2/ebuka_task_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub fn mine_blocks(limit: u8) {
for height in 1..=limit {
let mut attempts = 1;
let target_difficulty = 3;

println!("\n--- Starting work for Block #{} ---", height);

while attempts <= target_difficulty {
println!("Simulating difficulty attempt #{}", attempts);
attempts += 1;
}
println!("*** Block #{} Mined! ***", height);

if height % 5 == 0 {
println!("!> Checkpoint reached at block #{} <!", height);
} else {
println!("!> Continuing chain {}", height);
}
}
}
12 changes: 12 additions & 0 deletions code/src/week_2/ebuka_task_3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub enum Network {
Mainnet,
Testnet,
Regtest,
}
pub fn get_rpc_url(network: &Network) -> &str {
match network {
Network::Mainnet => "https://api.mainnet.com/rpc",
Network::Testnet => "https://testnet-explorer.io/rpc",
Network::Regtest => "http://localhost:18443/rpc",
}
}
5 changes: 4 additions & 1 deletion code/src/week_2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// // import all the files within session_3
pub mod ebuka_task_1;
pub mod ebuka_task_2;
pub mod ebuka_task_3;
pub mod function;
pub mod loops;
pub mod primitive_types;
pub mod slices;
pub mod variables;
pub mod variables;
3 changes: 1 addition & 2 deletions code/src/week_2/task_1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

fn btc_value_in_usd(btc: f64, rate: f64) -> f64 {
btc * rate
btc * rate
}
//main run main
fn main() {
Expand Down