feat: add native and weth addresses for supported networks

This commit is contained in:
royvardhan
2025-02-20 18:24:46 +05:30
parent a5624be0e7
commit 83f1955094
2 changed files with 20 additions and 16 deletions

View File

@@ -26,16 +26,9 @@ impl EVMTychoEncoder {
strategy_encoder: Box<dyn StrategyEncoder>, strategy_encoder: Box<dyn StrategyEncoder>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
let chain: Chain = Chain::from(chain); let chain: Chain = Chain::from(chain);
if chain.name != *"ethereum" { let native_address = chain.native_token()?;
return Err(EncodingError::InvalidInput( let wrapped_address = chain.wrapped_token()?;
"Currently only Ethereum is supported".to_string(), Ok(EVMTychoEncoder { strategy_encoder, native_address, wrapped_address })
));
}
Ok(EVMTychoEncoder {
strategy_encoder,
native_address: chain.native_token()?,
wrapped_address: chain.wrapped_token()?,
})
} }
} }

View File

@@ -153,22 +153,33 @@ impl From<TychoCoreChain> for Chain {
} }
impl Chain { impl Chain {
fn decode_hex(&self, hex_str: &str, err_msg: &str) -> Result<Bytes, EncodingError> {
Ok(Bytes::from(
hex::decode(hex_str).map_err(|_| EncodingError::FatalError(err_msg.to_string()))?,
))
}
pub fn native_token(&self) -> Result<Bytes, EncodingError> { pub fn native_token(&self) -> Result<Bytes, EncodingError> {
let decode_err_msg = "Failed to decode native token";
match self.id.id() { match self.id.id() {
1 => Ok(Bytes::from(hex::decode("0000000000000000000000000000000000000000").map_err( 1 | 8453 | 42161 => {
|_| EncodingError::FatalError("Failed to decode native token".to_string()), self.decode_hex("0000000000000000000000000000000000000000", decode_err_msg)
)?)), }
324 => self.decode_hex("000000000000000000000000000000000000800A", decode_err_msg),
_ => Err(EncodingError::InvalidInput(format!( _ => Err(EncodingError::InvalidInput(format!(
"Native token not set for chain {:?}. Double check the chain is supported.", "Native token not set for chain {:?}. Double check the chain is supported.",
self.name self.name
))), ))),
} }
} }
pub fn wrapped_token(&self) -> Result<Bytes, EncodingError> { pub fn wrapped_token(&self) -> Result<Bytes, EncodingError> {
let decode_err_msg = "Failed to decode wrapped token";
match self.id.id() { match self.id.id() {
1 => Ok(Bytes::from(hex::decode("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").map_err( 1 => self.decode_hex("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", decode_err_msg),
|_| EncodingError::FatalError("Failed to decode wrapped token".to_string()), 8453 => self.decode_hex("4200000000000000000000000000000000000006", decode_err_msg),
)?)), 324 => self.decode_hex("5AEa5775959fBC2557Cc8789bC1bf90A239D9a91", decode_err_msg),
42161 => self.decode_hex("82aF49447D8a07e3bd95BD0d56f35241523fBab1", decode_err_msg),
_ => Err(EncodingError::InvalidInput(format!( _ => Err(EncodingError::InvalidInput(format!(
"Wrapped token not set for chain {:?}. Double check the chain is supported.", "Wrapped token not set for chain {:?}. Double check the chain is supported.",
self.name self.name