From 7b48154ab327610e36f0354c6e76fc14c5803489 Mon Sep 17 00:00:00 2001 From: Fabian Habil Ramdhan Date: Sat, 10 Aug 2024 15:45:54 +0700 Subject: [PATCH 1/4] discord webhook integration --- src/main.rs | 7 +++++++ src/mine.rs | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/main.rs b/src/main.rs index 56c69014..f6ccd1b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ struct Miner { pub dynamic_fee: bool, pub rpc_client: Arc, pub fee_payer_filepath: Option, + pub discord_webhook: Option } #[derive(Subcommand, Debug)] @@ -137,6 +138,9 @@ struct Args { #[arg(long, help = "Enable dynamic priority fees", global = true)] dynamic_fee: bool, + #[arg(long, value_name = "DISCORD_WEBHOOK", global = true)] + discord_webhook: Option, + #[command(subcommand)] command: Commands, } @@ -170,6 +174,7 @@ async fn main() { args.dynamic_fee_url, args.dynamic_fee, Some(fee_payer_filepath), + args.discord_webook, )); // Execute user command. @@ -225,6 +230,7 @@ impl Miner { dynamic_fee_url: Option, dynamic_fee: bool, fee_payer_filepath: Option, + discord_webhook: Option, ) -> Self { Self { rpc_client, @@ -233,6 +239,7 @@ impl Miner { dynamic_fee_url, dynamic_fee, fee_payer_filepath, + discord_webhook, } } diff --git a/src/mine.rs b/src/mine.rs index 5c369e3e..7f8c23ee 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -14,6 +14,9 @@ use rand::Rng; use solana_program::pubkey::Pubkey; use solana_rpc_client::spinner; use solana_sdk::signer::Signer; +use reqwest::Client; +use serde_json::json; +use std::error::Error; use crate::{ args::MineArgs, @@ -36,6 +39,10 @@ impl Miner { // Start mining loop let mut last_hash_at = 0; let mut last_balance = 0; + + let discord_webhook_url = &self.discord_webhook; + let client = Client::new(); + loop { // Fetch proof let config = get_config(&self.rpc_client).await; @@ -86,6 +93,14 @@ impl Miner { self.send_and_confirm(&ixs, ComputeBudget::Fixed(compute_budget), false) .await .ok(); + + let payload = json!({ + "content": format!("Ore Gained: {}, Current Balance: {}", amount_u64_to_string(proof.balance.saturating_sub(last_balance)), amount_u64_to_string(proof.balance)), + }); + let _ = http_client.post(&discord_webhook_url) + .json(&payload) + .send() + .await; } } @@ -175,6 +190,15 @@ impl Miner { nonce += 1; } + let payload = json!({ + "content": format!("Difficulty: {}", best_difficulty), + }); + + let _ = http_client.post(&discord_webhook_url) + .json(&payload) + .send() + .await; + // Return the best nonce (best_nonce, best_difficulty, best_hash) } From b69b6a42782a9a08c6a4dccc249b3e11cd4496a0 Mon Sep 17 00:00:00 2001 From: Fabian Habil Ramdhan Date: Sat, 10 Aug 2024 15:54:10 +0700 Subject: [PATCH 2/4] fix --- src/main.rs | 2 +- src/mine.rs | 19 ++++--------------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index f6ccd1b1..11b5f5c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -174,7 +174,7 @@ async fn main() { args.dynamic_fee_url, args.dynamic_fee, Some(fee_payer_filepath), - args.discord_webook, + args.discord_webhook, )); // Execute user command. diff --git a/src/mine.rs b/src/mine.rs index 7f8c23ee..53e9e345 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -16,7 +16,6 @@ use solana_rpc_client::spinner; use solana_sdk::signer::Signer; use reqwest::Client; use serde_json::json; -use std::error::Error; use crate::{ args::MineArgs, @@ -40,9 +39,6 @@ impl Miner { let mut last_hash_at = 0; let mut last_balance = 0; - let discord_webhook_url = &self.discord_webhook; - let client = Client::new(); - loop { // Fetch proof let config = get_config(&self.rpc_client).await; @@ -97,7 +93,10 @@ impl Miner { let payload = json!({ "content": format!("Ore Gained: {}, Current Balance: {}", amount_u64_to_string(proof.balance.saturating_sub(last_balance)), amount_u64_to_string(proof.balance)), }); - let _ = http_client.post(&discord_webhook_url) + + let http_client = Client::new(); + + let _ = http_client.post(&self.discord_webhook) .json(&payload) .send() .await; @@ -189,16 +188,6 @@ impl Miner { // Increment nonce nonce += 1; } - - let payload = json!({ - "content": format!("Difficulty: {}", best_difficulty), - }); - - let _ = http_client.post(&discord_webhook_url) - .json(&payload) - .send() - .await; - // Return the best nonce (best_nonce, best_difficulty, best_hash) } From eb2111a565f169c06125b1b3cc820d1f27ce8ebb Mon Sep 17 00:00:00 2001 From: Fabian Habil Ramdhan Date: Sat, 10 Aug 2024 15:56:23 +0700 Subject: [PATCH 3/4] fix --- src/mine.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mine.rs b/src/mine.rs index 53e9e345..dfc38122 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -90,13 +90,15 @@ impl Miner { .await .ok(); + let http_client = Client::new(); + let payload = json!({ "content": format!("Ore Gained: {}, Current Balance: {}", amount_u64_to_string(proof.balance.saturating_sub(last_balance)), amount_u64_to_string(proof.balance)), }); - let http_client = Client::new(); + let discord_webhook_url = self.discord_webhook.as_deref().expect("Discord webhook URL must be set"); - let _ = http_client.post(&self.discord_webhook) + let _ = http_client.post(discord_webhook_url) .json(&payload) .send() .await; From 2c76fb4ce67360499b5338aef1097d2a37c73706 Mon Sep 17 00:00:00 2001 From: Fabian Habil Ramdhan Date: Sat, 10 Aug 2024 16:18:25 +0700 Subject: [PATCH 4/4] fix ore gained --- src/mine.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/mine.rs b/src/mine.rs index dfc38122..06f731f2 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -45,6 +45,9 @@ impl Miner { let proof = get_updated_proof_with_authority(&self.rpc_client, signer.pubkey(), last_hash_at) .await; + + let ore_gained = proof.balance.saturating_sub(last_balance); + println!( "\n\nStake: {} ORE\n{} Multiplier: {:12}x", amount_u64_to_string(proof.balance), @@ -59,6 +62,7 @@ impl Miner { calculate_multiplier(proof.balance, config.top_balance) ); last_hash_at = proof.last_hash_at; + last_balance = proof.balance; // Calculate cutoff time @@ -93,7 +97,7 @@ impl Miner { let http_client = Client::new(); let payload = json!({ - "content": format!("Ore Gained: {}, Current Balance: {}", amount_u64_to_string(proof.balance.saturating_sub(last_balance)), amount_u64_to_string(proof.balance)), + "content": format!("Ore Gained: {}, Current Balance: {}", amount_u64_to_string(ore_gained), amount_u64_to_string(proof.balance)), }); let discord_webhook_url = self.discord_webhook.as_deref().expect("Discord webhook URL must be set");