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
2 changes: 1 addition & 1 deletion code/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion code/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "rust_for_bitcoin"
name = "Tosin_week2_assignment"
version = "0.1.0"
edition = "2024"

Expand Down
36 changes: 29 additions & 7 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;
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}");


}
8 changes: 1 addition & 7 deletions code/src/week_2/task_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

23 changes: 11 additions & 12 deletions code/src/week_2/task_2.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
37 changes: 7 additions & 30 deletions code/src/week_2/task_3.rs
Original file line number Diff line number Diff line change
@@ -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));
}
}