Skip to content

Commit

Permalink
Add .tree domain support (#13)
Browse files Browse the repository at this point in the history
* Update client.rs

* Create tree.rs

* Update lib.rs

* Update Cargo.toml

* Update Settings.toml

* Update lib.rs

* Complete tree integration

* Update tree.rs

---------

Co-authored-by: timdegen <104884878+timdegen@users.noreply.github.com>
  • Loading branch information
ludomain and gemcoder21 committed Sep 2, 2023
1 parent b71302c commit f1a0eb1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ url = "https://sns-sdk-proxy.bonfida.workers.dev"
[name.ton]
url = "https://tonapi.io"

[name.tree]
url = "https://api.eths.center"

[metrics]
path = "/metrics"

[assets]
url = "https://raw.githubusercontent.com/gemwalletcom/assets/master"
url = "https://raw.githubusercontent.com/gemwalletcom/assets/master"
1 change: 1 addition & 0 deletions api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async fn rocket(settings: Settings) -> Rocket<Build> {
settings.name.ud.key.secret,
settings.name.sns.url,
settings.name.ton.url,
settings.name.tree.url,
);
let plausible_client = PlausibleClient::new(&settings.plausible.url);
let request_client = FiatClient::request_client(settings.fiat.timeout);
Expand Down
18 changes: 16 additions & 2 deletions name_resolver/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_trait::async_trait;
use primitives::chain::Chain;
use primitives::name::{NameRecord, NameProvider};

use crate::{ens::ENSClient, ud::UDClient, sns::SNSClient, ton::TONClient};
use crate::{ens::ENSClient, ud::UDClient, sns::SNSClient, ton::TONClient, tree::TreeClient};

#[async_trait]
pub trait NameClient {
Expand All @@ -20,6 +20,7 @@ pub struct Client {
ud_client: UDClient,
sns_client: SNSClient,
ton_client: TONClient,
tree_client: TreeClient,
}

impl Client {
Expand All @@ -30,19 +31,22 @@ impl Client {
ud_api_key: String,
sns_url: String,
ton_url: String,
tree_api_url: String
) -> Self {
let domains_mapping = Self::domains_mapping();
let ens_client = ENSClient::new(ens_url);
let ud_client = UDClient::new(ud_url, ud_api_key);
let sns_client = SNSClient::new(sns_url);
let ton_client: TONClient = TONClient::new(ton_url);
let tree_client: TreeClient = TreeClient::new(tree_api_url);

Self {
domains_mapping,
ens_client,
ud_client,
sns_client,
ton_client,
tree_client,
}
}

Expand Down Expand Up @@ -79,6 +83,12 @@ impl Client {
return Err("not supported chain".to_string().into())
}
self.ton_client.resolve(name, chain).await
},
NameProvider::Tree => {
if !TreeClient::chains().contains(&chain) {
return Err("not supported chain".to_string().into())
}
self.tree_client.resolve(name, chain).await
}
}
}
Expand All @@ -102,6 +112,10 @@ impl Client {
result.insert(domain, NameProvider::Ton);
}

for domain in TreeClient::domains() {
result.insert(domain, NameProvider::Tree);
}

result
}
}
}
1 change: 1 addition & 0 deletions name_resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod ens;
pub mod ud;
pub mod sns;
pub mod ton;
pub mod tree;
58 changes: 58 additions & 0 deletions name_resolver/src/tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use primitives::chain::Chain;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use async_trait::async_trait;
use std::{error::Error};

use primitives::name::{NameRecord, NameProvider};
use crate::client::NameClient;

#[derive(Debug, Deserialize, Serialize)]
pub struct ResolveRecord {
pub owner: String,
}

pub struct TreeClient {
api_url: String,
client: Client,
}

impl TreeClient {
pub fn new(api_url: String) -> Self {
let client = Client::new();
Self {
api_url,
client,
}
}
}

#[async_trait]
impl NameClient for TreeClient {

fn provider() -> NameProvider {
NameProvider::Tree
}

async fn resolve(&self, name: &str, chain: Chain) -> Result<NameRecord, Box<dyn Error>> {
let url = format!("{}/resolve/{}", self.api_url, name);
let record: ResolveRecord = self.client.get(&url).send().await?.json().await?;
let address = record.owner;

Ok(NameRecord { name: name.to_string(), chain, address, provider: Self::provider() })
}

fn domains() -> Vec<&'static str> {
vec![
"tree"
]
}

fn chains() -> Vec<Chain> {
vec![
Chain::Ethereum,
Chain::Polygon,
Chain::SmartChain,
]
}
}
2 changes: 2 additions & 0 deletions primitives/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum NameProvider {
Ens,
Sns,
Ton,
Tree,
}

impl NameProvider {
Expand All @@ -29,6 +30,7 @@ impl NameProvider {
Self::Ens => "ens",
Self::Sns => "sns",
Self::Ton => "ton",
Self::Tree => "tree",
}
}
}
9 changes: 8 additions & 1 deletion settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub struct Name {
pub ud: UD,
pub sns: SNS,
pub ton: TON,
pub tree: TREE,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -126,6 +127,12 @@ pub struct UD {
pub key: KeySecret,
}

#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct TREE {
pub url: String,
}

#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Metrics {
Expand All @@ -146,4 +153,4 @@ impl Settings {
.build()?;
s.try_deserialize()
}
}
}

0 comments on commit f1a0eb1

Please sign in to comment.