Skip to content

Commit

Permalink
Merge pull request #73 from starknet-id/feat/add_ccip_support
Browse files Browse the repository at this point in the history
feat: add ccip support to domain_to_addr
  • Loading branch information
Th0rgal authored Apr 18, 2024
2 parents eebd3b8 + f4711ad commit a1be2f6
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 127 deletions.
13 changes: 13 additions & 0 deletions config.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ old_verifier = "0xXXXXXXXXXXXX"
pop_verifier = "0xXXXXXXXXXXXX"
pp_verifier = "0xXXXXXXXXXXXX"

[variables]
rpc_url = "xxxxxx"
refresh_delay = 60 # in seconds

[starkscan]
api_url = "https://api-testnet.starkscan.co/api/v0"
api_key = "xxxxxx"
Expand Down Expand Up @@ -60,3 +64,12 @@ min_price = 2000
max_price = 10000
decimals = 18
max_quote_validity = 600

[offchain_resolvers]

[offchain_resolvers.NOTION]
root_domain = "notion.stark"
resolver_address = "0x153be68cf8fc71138610811dd2b4fa481eb99f3eedcb3fce7369569055be275"
uri = [
"https://sepolia.api.ccip-demo.starknet.id/resolve?domain=",
]
69 changes: 69 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::de::{MapAccess, Visitor};
use serde::{Deserialize, Deserializer};
use starknet::core::types::FieldElement;
use std::collections::HashMap;
Expand Down Expand Up @@ -66,26 +67,50 @@ pub_struct!(Clone, Debug; Altcoins {
data: HashMap<FieldElement, AltcoinData>,
});

pub_struct!(Clone, Debug, Deserialize; Variables {
rpc_url: String,
refresh_delay: f64,
});

#[derive(Deserialize)]
struct TempOffchainResolver {
root_domain: String,
resolver_address: String,
uri: Vec<String>,
}

pub_struct!(Clone, Debug, Deserialize; OffchainResolver {
resolver_address: String,
uri: Vec<String>,
});

#[derive(Debug, Clone)]
pub struct OffchainResolvers(HashMap<String, OffchainResolver>);

#[derive(Deserialize)]
struct RawConfig {
server: Server,
databases: Databases,
variables: Variables,
contracts: Contracts,
starkscan: Starkscan,
custom_resolvers: HashMap<String, Vec<String>>,
solana: Solana,
altcoins: Altcoins,
offchain_resolvers: OffchainResolvers,
}

pub_struct!(Clone, Deserialize; Config {
server: Server,
databases: Databases,
variables: Variables,
contracts: Contracts,
starkscan: Starkscan,
custom_resolvers: HashMap<String, Vec<String>>,
reversed_resolvers: HashMap<String, String>,
solana: Solana,
altcoins: Altcoins,
offchain_resolvers: OffchainResolvers,
});

impl Altcoins {
Expand Down Expand Up @@ -123,6 +148,48 @@ impl<'de> Deserialize<'de> for Altcoins {
}
}

impl OffchainResolvers {
pub fn get(&self, key: &str) -> Option<&OffchainResolver> {
self.0.get(key)
}
}

impl<'de> Deserialize<'de> for OffchainResolvers {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct OffchainResolversVisitor;

impl<'de> Visitor<'de> for OffchainResolversVisitor {
type Value = OffchainResolvers;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a map of resolver addresses to OffchainResolvers")
}

fn visit_map<V>(self, mut map: V) -> Result<OffchainResolvers, V::Error>
where
V: MapAccess<'de>,
{
let mut hash_map = HashMap::new();
while let Some((_, temp_resolver)) =
map.next_entry::<String, TempOffchainResolver>()?
{
let resolver = OffchainResolver {
resolver_address: temp_resolver.resolver_address,
uri: temp_resolver.uri,
};
hash_map.insert(temp_resolver.root_domain, resolver);
}
Ok(OffchainResolvers(hash_map))
}
}

deserializer.deserialize_map(OffchainResolversVisitor)
}
}

impl From<RawConfig> for Config {
fn from(raw: RawConfig) -> Self {
let mut reversed_resolvers = HashMap::new();
Expand All @@ -134,12 +201,14 @@ impl From<RawConfig> for Config {
Config {
server: raw.server,
databases: raw.databases,
variables: raw.variables,
contracts: raw.contracts,
starkscan: raw.starkscan,
custom_resolvers: raw.custom_resolvers,
reversed_resolvers,
solana: raw.solana,
altcoins: raw.altcoins,
offchain_resolvers: raw.offchain_resolvers,
}
}
}
Expand Down
Loading

0 comments on commit a1be2f6

Please sign in to comment.