Skip to content

Commit

Permalink
Integrate SpaceID name provider
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Sep 2, 2023
1 parent f1a0eb1 commit ff4f9d1
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ url = "https://tonapi.io"
[name.tree]
url = "https://api.eths.center"

[name.spaceid]
url = "https://api.prd.space.id"

[metrics]
path = "/metrics"

Expand Down
1 change: 1 addition & 0 deletions api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async fn rocket(settings: Settings) -> Rocket<Build> {
settings.name.sns.url,
settings.name.ton.url,
settings.name.tree.url,
settings.name.spaceid.url,
);
let plausible_client = PlausibleClient::new(&settings.plausible.url);
let request_client = FiatClient::request_client(settings.fiat.timeout);
Expand Down
29 changes: 19 additions & 10 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, tree::TreeClient};
use crate::{ens::ENSClient, ud::UDClient, sns::SNSClient, ton::TONClient, tree::TreeClient, spaceid::SpaceIdClient};

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

impl Client {
Expand All @@ -31,14 +32,16 @@ impl Client {
ud_api_key: String,
sns_url: String,
ton_url: String,
tree_api_url: String
tree_api_url: String,
space_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);
let spaceid_client: SpaceIdClient = SpaceIdClient::new(space_api_url);

Self {
domains_mapping,
Expand All @@ -47,17 +50,13 @@ impl Client {
sns_client,
ton_client,
tree_client,
spaceid_client,
}
}

pub async fn resolve(&self, name: &str, chain: Chain) -> Result<NameRecord, Box<dyn Error>> {
let name_parts = name.split('.');
let name_prefix = name_parts.clone().last();

println!("name_parts {}", name_parts.count());
println!("name_prefix {:?}", name_prefix.clone());

let provider = self.domains_mapping.get(name_prefix.unwrap()).unwrap();
let name_prefix = name.split('.').clone().last().unwrap_or_default();
let provider = self.domains_mapping.get(name_prefix).expect("unable to get provider");

match provider {
NameProvider::Ens => {
Expand Down Expand Up @@ -89,7 +88,13 @@ impl Client {
return Err("not supported chain".to_string().into())
}
self.tree_client.resolve(name, chain).await
}
},
NameProvider::SpaceId => {
if !SpaceIdClient::chains().contains(&chain) {
return Err("not supported chain".to_string().into())
}
self.spaceid_client.resolve(name, chain).await
},
}
}

Expand All @@ -116,6 +121,10 @@ impl Client {
result.insert(domain, NameProvider::Tree);
}

for domain in SpaceIdClient::domains() {
result.insert(domain, NameProvider::SpaceId);
}

result
}
}
1 change: 1 addition & 0 deletions name_resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod ud;
pub mod sns;
pub mod ton;
pub mod tree;
pub mod spaceid;
63 changes: 63 additions & 0 deletions name_resolver/src/spaceid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 code: i32,
pub address: String,
}

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

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

#[async_trait]
impl NameClient for SpaceIdClient {

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

async fn resolve(&self, name: &str, chain: Chain) -> Result<NameRecord, Box<dyn Error>> {
let tld = name.split('.').clone().last().unwrap_or_default();
let url = format!("{}/v1/getAddress?tld={}&domain={}", self.api_url, tld, name);
let record: ResolveRecord = self.client.get(&url).send().await?.json().await?;
if record.code != 0 {
return Err("SpaceIdClient: code != 0".into());
}
let address = record.address;

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

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

fn chains() -> Vec<Chain> {
vec![
Chain::SmartChain,
Chain::Arbitrum,
]
}
}
2 changes: 1 addition & 1 deletion name_resolver/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use primitives::chain::Chain;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use async_trait::async_trait;
use std::{error::Error};
use std::error::Error;

use primitives::name::{NameRecord, NameProvider};
use crate::client::NameClient;
Expand Down
2 changes: 2 additions & 0 deletions primitives/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum NameProvider {
Sns,
Ton,
Tree,
SpaceId
}

impl NameProvider {
Expand All @@ -31,6 +32,7 @@ impl NameProvider {
Self::Sns => "sns",
Self::Ton => "ton",
Self::Tree => "tree",
Self::SpaceId => "spaceid"
}
}
}
8 changes: 8 additions & 0 deletions settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct Name {
pub sns: SNS,
pub ton: TON,
pub tree: TREE,
pub spaceid: SpaceId,
}

#[derive(Debug, Deserialize)]
Expand All @@ -120,6 +121,7 @@ pub struct TON {
pub url: String,
}


#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct UD {
Expand All @@ -133,6 +135,12 @@ pub struct TREE {
pub url: String,
}

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

#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Metrics {
Expand Down

0 comments on commit ff4f9d1

Please sign in to comment.