Parse chain ID into CAIP-2 ChainId type during blockchain config deserialization
This commit is contained in:
parent
0e8943a24b
commit
c4e5438ac2
4 changed files with 27 additions and 13 deletions
|
@ -4,6 +4,7 @@ use std::path::PathBuf;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::ethereum::utils::{parse_caip2_chain_id, ChainIdError};
|
use crate::ethereum::utils::{parse_caip2_chain_id, ChainIdError};
|
||||||
|
use crate::utils::caip2::ChainId;
|
||||||
|
|
||||||
fn default_chain_sync_step() -> u64 { 1000 }
|
fn default_chain_sync_step() -> u64 { 1000 }
|
||||||
|
|
||||||
|
@ -11,8 +12,8 @@ fn default_chain_reorg_max_depth() -> u64 { 10 }
|
||||||
|
|
||||||
#[derive(Clone, Deserialize)]
|
#[derive(Clone, Deserialize)]
|
||||||
pub struct BlockchainConfig {
|
pub struct BlockchainConfig {
|
||||||
// CAIP-2 chain ID (https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md)
|
// CAIP-2 chain ID
|
||||||
pub chain_id: String,
|
pub chain_id: ChainId,
|
||||||
// Additional information for clients
|
// Additional information for clients
|
||||||
pub chain_info: Option<HashMap<String, String>>,
|
pub chain_info: Option<HashMap<String, String>>,
|
||||||
pub contract_address: String,
|
pub contract_address: String,
|
||||||
|
|
|
@ -10,9 +10,6 @@ use crate::utils::caip2::ChainId;
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum ChainIdError {
|
pub enum ChainIdError {
|
||||||
#[error("invalid chain ID")]
|
|
||||||
InvalidChainId,
|
|
||||||
|
|
||||||
#[error("unsupported chain")]
|
#[error("unsupported chain")]
|
||||||
UnsupportedChain,
|
UnsupportedChain,
|
||||||
|
|
||||||
|
@ -21,9 +18,7 @@ pub enum ChainIdError {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses CAIP-2 chain ID
|
/// Parses CAIP-2 chain ID
|
||||||
pub fn parse_caip2_chain_id(chain_id: &str) -> Result<u32, ChainIdError> {
|
pub fn parse_caip2_chain_id(chain_id: &ChainId) -> Result<u32, ChainIdError> {
|
||||||
let chain_id = chain_id.parse::<ChainId>()
|
|
||||||
.map_err(|_| ChainIdError::InvalidChainId)?;
|
|
||||||
if chain_id.namespace != "eip155" {
|
if chain_id.namespace != "eip155" {
|
||||||
return Err(ChainIdError::UnsupportedChain);
|
return Err(ChainIdError::UnsupportedChain);
|
||||||
};
|
};
|
||||||
|
@ -54,15 +49,15 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_caip2_chain_id() {
|
fn test_parse_caip2_chain_id() {
|
||||||
let chain_id = "eip155:1";
|
let chain_id: ChainId = "eip155:1".parse().unwrap();
|
||||||
let result = parse_caip2_chain_id(chain_id).unwrap();
|
let result = parse_caip2_chain_id(&chain_id).unwrap();
|
||||||
assert_eq!(result, 1);
|
assert_eq!(result, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_caip2_chain_id_unsupported() {
|
fn test_parse_caip2_chain_id_unsupported() {
|
||||||
let chain_id = "bip122:000000000019d6689c085ae165831e93";
|
let chain_id: ChainId = "bip122:000000000019d6689c085ae165831e93".parse().unwrap();
|
||||||
let error = parse_caip2_chain_id(chain_id).err().unwrap();
|
let error = parse_caip2_chain_id(&chain_id).err().unwrap();
|
||||||
assert!(matches!(error, ChainIdError::UnsupportedChain));
|
assert!(matches!(error, ChainIdError::UnsupportedChain));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ impl InstanceInfo {
|
||||||
login_message: config.login_message.clone(),
|
login_message: config.login_message.clone(),
|
||||||
post_character_limit: config.post_character_limit,
|
post_character_limit: config.post_character_limit,
|
||||||
blockchain_id: config.blockchain.as_ref()
|
blockchain_id: config.blockchain.as_ref()
|
||||||
.map(|val| val.chain_id.clone()),
|
.map(|val| val.chain_id.to_string()),
|
||||||
blockchain_explorer_url: config.blockchain.as_ref()
|
blockchain_explorer_url: config.blockchain.as_ref()
|
||||||
.and_then(|val| val.explorer_url.clone()),
|
.and_then(|val| val.explorer_url.clone()),
|
||||||
blockchain_contract_address: config.blockchain.as_ref()
|
blockchain_contract_address: config.blockchain.as_ref()
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use serde::{Deserialize, Deserializer, de::Error as DeserializerError};
|
||||||
|
|
||||||
const CAIP2_RE: &str = r"(?P<namespace>[-a-z0-9]{3,8}):(?P<reference>[-a-zA-Z0-9]{1,32})";
|
const CAIP2_RE: &str = r"(?P<namespace>[-a-z0-9]{3,8}):(?P<reference>[-a-zA-Z0-9]{1,32})";
|
||||||
|
|
||||||
|
@ -29,6 +30,21 @@ impl FromStr for ChainId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ToString for ChainId {
|
||||||
|
fn to_string(&self) -> String {
|
||||||
|
format!("{}:{}", self.namespace, self.reference)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for ChainId {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where D: Deserializer<'de>
|
||||||
|
{
|
||||||
|
String::deserialize(deserializer)?
|
||||||
|
.parse().map_err(DeserializerError::custom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -39,6 +55,7 @@ mod tests {
|
||||||
let chain_id = value.parse::<ChainId>().unwrap();
|
let chain_id = value.parse::<ChainId>().unwrap();
|
||||||
assert_eq!(chain_id.namespace, "bip122");
|
assert_eq!(chain_id.namespace, "bip122");
|
||||||
assert_eq!(chain_id.reference, "000000000019d6689c085ae165831e93");
|
assert_eq!(chain_id.reference, "000000000019d6689c085ae165831e93");
|
||||||
|
assert_eq!(chain_id.to_string(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -47,6 +64,7 @@ mod tests {
|
||||||
let chain_id = value.parse::<ChainId>().unwrap();
|
let chain_id = value.parse::<ChainId>().unwrap();
|
||||||
assert_eq!(chain_id.namespace, "eip155");
|
assert_eq!(chain_id.namespace, "eip155");
|
||||||
assert_eq!(chain_id.reference, "1");
|
assert_eq!(chain_id.reference, "1");
|
||||||
|
assert_eq!(chain_id.to_string(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue