-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
146 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule AlephiumMinerBot.Kaspa.KaspaSupervisor do | ||
use Supervisor | ||
|
||
alias AlephiumMinerBot.Kaspa.Worker.{WorkerReward, WorkerHashrate} | ||
|
||
def start_link() do | ||
Supervisor.start_link(__MODULE__, nil, name: __MODULE__) | ||
end | ||
|
||
@impl true | ||
def init(_) do | ||
children = [ | ||
%{ | ||
id: WorkerReward, | ||
start: {WorkerReward, :start_link, []}, | ||
type: :worker | ||
}, | ||
%{ | ||
id: WorkerHashrate, | ||
start: {WorkerHashrate, :start_link, []}, | ||
type: :worker | ||
} | ||
] | ||
Supervisor.init(children, strategy: :one_for_one) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
defmodule AlephiumMinerBot.Kaspa.Worker.WorkerHashrate do | ||
use GenServer | ||
alias AlephiumMinerBot.Telegram | ||
|
||
def start_link() do | ||
IO.puts "[Kaspa][Worker.WorkerHashrate] started." | ||
GenServer.start_link(__MODULE__, :kaspa_worker_hashrate) | ||
end | ||
|
||
@impl true | ||
def init(_) do | ||
Process.send_after(self(), :check_hashrate, 2_000) | ||
{:ok, nil} | ||
end | ||
|
||
@impl true | ||
def handle_info(:check_hashrate, _state) do | ||
case check_hashrate() do | ||
{:ok, hashrate} -> | ||
message = generate_message(hashrate) | ||
IO.puts(message) | ||
Telegram.send_message(message) | ||
schedule_check_hashrate() | ||
{:noreply, nil} | ||
:error -> | ||
schedule_check_hashrate() | ||
{:noreply, nil} | ||
end | ||
end | ||
|
||
defp schedule_check_hashrate do | ||
interval = Application.get_env(:alephium_miner_bot, :kaspa_worker_hashrate_interval) | ||
Process.send_after(self(), :check_hashrate, interval) | ||
end | ||
|
||
def generate_message(hashrate) do | ||
new_timestamp = NaiveDateTime.utc_now() | ||
new_timestamp_string = new_timestamp | ||
|> NaiveDateTime.add(7*60*60, :second) | ||
|> NaiveDateTime.truncate(:second) | ||
|> NaiveDateTime.to_string() | ||
|> String.slice(0..-4) | ||
|
||
"#{new_timestamp_string} [Kaspa] Network Hashrate: #{hashrate} TH/s" | ||
end | ||
|
||
def check_hashrate do | ||
kaspactl_path = Application.get_env(:alephium_miner_bot, :kaspa_ctl_path) | ||
case System.cmd(kaspactl_path, ["GetBlockDagInfo"]) do | ||
{message, 0} -> | ||
hashrate = extract_hashrate_from_message(message) | ||
{:ok, hashrate} | ||
{"", 1} -> | ||
IO.puts "[Kaspa][Worker.WorkerHashrate][check_hashrate/0] Ensure that you can run `kaspactl GetBlockDagInfo` manually." | ||
:error | ||
end | ||
end | ||
|
||
def extract_hashrate_from_message(message) do | ||
Jason.decode!(message) | ||
|> Map.get("getBlockDagInfoResponse") | ||
|> Map.get("difficulty") | ||
|> Decimal.from_float() | ||
|> Decimal.mult(2) | ||
|> Decimal.div(1_000_000_000_000) | ||
|> Number.Currency.number_to_currency([format: "%n", precision: 2]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters