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
3 changes: 3 additions & 0 deletions code/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ fn main() {
week_2::function::run();
week_2::loops::run();
week_2::primitive_types::run();
week_2::soma_task_1::main();
week_2::soma_task_2::mine_blocks(10);
week_2::soma_task_3::main()
}
5 changes: 4 additions & 1 deletion code/src/week_2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ pub mod function;
pub mod loops;
pub mod primitive_types;
pub mod slices;
pub mod variables;
pub mod variables;
pub mod soma_task_1;
pub mod soma_task_2;
pub mod soma_task_3;
18 changes: 18 additions & 0 deletions code/src/week_2/soma_task_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Function that takes BTC amount and exchange rate, returns USD value
fn btc_value_in_usd(btc: f64, rate: f64) -> f64 {
{
btc * rate
}
}

pub fn main() {

let btc_amount = 1.0; // I own 1.0 BTC
let exchange_rate = 65000.0; // Current exchange_rate of BTC: $65,000 per BTC

// Calculate USD value
let usd_value = btc_value_in_usd(btc_amount, exchange_rate);

println!("{} BTC = ${:.2} USD", btc_amount, usd_value);
}

18 changes: 18 additions & 0 deletions code/src/week_2/soma_task_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub fn mine_blocks(limit: u8) {
let mut height = 1;
let mut difficulty = 1;

while height <= limit {
println!("Mining block #{}, Difficulty: {}", height, difficulty);

// Check for checkpoints every 5 blocks
if height % 5 == 0 {
println!("Checkpoint reached!");

// Increase difficulty after each checkpoint
difficulty += 1;
}

height += 1;
}
}
36 changes: 36 additions & 0 deletions code/src/week_2/soma_task_3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
enum Network {
Mainnet,
Testnet,
Regtest,
}

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",
}
}

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."),
}
}

pub 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));
}