Skip to content

Commit cdbf572

Browse files
authored
Merge pull request #60 from firstbatchxyz/erhant/allow-testnet
added `DKN_NETWORK` env var
1 parent 8acbaf1 commit cdbf572

File tree

6 files changed

+33
-37
lines changed

6 files changed

+33
-37
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "dkn-compute-launcher"
33
edition = "2021"
4-
version = "0.1.17"
4+
version = "0.1.18"
55
license = "Apache-2.0"
66
readme = "README.md"
77
description = "Dria Compute Node Launcher"

src/commands/points.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
use colored::Colorize;
22
use eyre::Context;
33

4-
use crate::utils::{DriaEnv, LAUNCHER_USER_AGENT};
4+
use crate::utils::{get_network_env, DriaEnv, LAUNCHER_USER_AGENT};
55

6-
const POINTS_API_BASE_URL: &str = "https://mainnet.dkn.dria.co/points/v0/total/node/";
6+
#[inline]
7+
fn get_points_api_url(address: &str) -> String {
8+
let network = get_network_env();
9+
let address = address.trim_start_matches("0x");
10+
format!("https://{network}.dkn.dria.co/points/v0/total/node/0x{address}")
11+
}
712

813
#[derive(Debug, serde::Deserialize)]
914
pub struct PointsRes {
@@ -43,11 +48,7 @@ pub async fn show_points() -> eyre::Result<()> {
4348
}
4449

4550
async fn get_points(address: &str) -> eyre::Result<PointsRes> {
46-
let url = format!(
47-
"{}/0x{}",
48-
POINTS_API_BASE_URL,
49-
address.trim_start_matches("0x")
50-
);
51+
let url = get_points_api_url(address);
5152

5253
let client = reqwest::Client::builder()
5354
.user_agent(LAUNCHER_USER_AGENT)

src/commands/referrals.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ use crate::utils::{referrals::*, DriaEnv, Selectable};
99
pub async fn handle_referrals() -> eyre::Result<()> {
1010
// ensure system is healthy
1111
let client = ReferralsClient::default();
12-
if !client.healthcheck().await {
13-
eyre::bail!("Referrals API is offline.");
14-
}
1512

1613
// get wallet secret from env
1714
let mut dria_env = DriaEnv::new_from_env();

src/utils/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,17 @@ pub const PROGRESS_BAR_CHARS: &str = "=>-";
4848
/// `UserAgent` header value for the launcher, used for HTTP requests.
4949
pub const LAUNCHER_USER_AGENT: &str =
5050
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
51+
52+
/// Returns the network type based on the `DKN_NETWORK` environment variable.
53+
///
54+
/// This is usually not used at all by the user, but rather used in testing and development.
55+
#[inline(always)]
56+
pub fn get_network_env() -> String {
57+
std::env::var("DKN_NETWORK")
58+
.map(|s| match s.as_str() {
59+
// only accept `testnet` as a valid network, otherwise default to `mainnet`
60+
"testnet" => s,
61+
_ => "mainnet".to_string(),
62+
})
63+
.unwrap_or_else(|_| "mainnet".to_string())
64+
}

src/utils/referrals.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
use crate::utils::{crypto::eip191_hash, get_network_env};
12
use eyre::{Context, Result};
23
use libsecp256k1::SecretKey;
34

4-
use crate::utils::crypto::eip191_hash;
5-
65
use super::LAUNCHER_USER_AGENT;
76

8-
const REFERRALS_API_BASE_URL: &str = "https://mainnet.dkn.dria.co/referrals/v0/";
7+
#[inline]
8+
fn get_referrals_api_base_url() -> String {
9+
let network = get_network_env();
10+
format!("https://{network}.dkn.dria.co/referral/v0")
11+
}
912

1013
pub struct ReferralsClient {
1114
base_url: String,
@@ -14,30 +17,18 @@ pub struct ReferralsClient {
1417

1518
impl Default for ReferralsClient {
1619
fn default() -> Self {
17-
Self::new(REFERRALS_API_BASE_URL)
20+
Self::new(get_referrals_api_base_url())
1821
}
1922
}
2023

2124
impl ReferralsClient {
22-
pub fn new(base_url: &str) -> Self {
25+
pub fn new(base_url: String) -> Self {
2326
let client = reqwest::Client::builder()
2427
.user_agent(LAUNCHER_USER_AGENT)
2528
.build()
2629
.expect("could not create reqwest client");
2730

28-
Self {
29-
base_url: base_url.to_string(),
30-
client,
31-
}
32-
}
33-
34-
/// Simple healthcheck for the referrals API.
35-
pub async fn healthcheck(&self) -> bool {
36-
self.client
37-
.get(format!("{}/health", self.base_url))
38-
.send()
39-
.await
40-
.is_ok_and(|r| r.status().is_success())
31+
Self { base_url, client }
4132
}
4233

4334
/// Returns a list of addresses of the users referred by the given `address`.
@@ -193,13 +184,6 @@ impl ReferralsClient {
193184
mod tests {
194185
use super::*;
195186

196-
#[tokio::test]
197-
#[ignore]
198-
async fn test_health() {
199-
let ok = ReferralsClient::default().healthcheck().await;
200-
assert!(ok);
201-
}
202-
203187
#[tokio::test]
204188
#[ignore]
205189
async fn test_get_referrals() {

0 commit comments

Comments
 (0)