refactor: simplify chain object and move decode_hex to utils
This commit is contained in:
@@ -23,7 +23,7 @@ use crate::encoding::{
|
|||||||
approvals::protocol_approvals_manager::get_client,
|
approvals::protocol_approvals_manager::get_client,
|
||||||
utils::{biguint_to_u256, bytes_to_address, encode_input},
|
utils::{biguint_to_u256, bytes_to_address, encode_input},
|
||||||
},
|
},
|
||||||
models::{Chain, ChainId},
|
models::Chain,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Struct for managing Permit2 operations, including encoding approvals and fetching allowance
|
/// Struct for managing Permit2 operations, including encoding approvals and fetching allowance
|
||||||
@@ -33,7 +33,7 @@ pub struct Permit2 {
|
|||||||
address: Address,
|
address: Address,
|
||||||
client: Arc<RootProvider<BoxTransport>>,
|
client: Arc<RootProvider<BoxTransport>>,
|
||||||
signer: PrivateKeySigner,
|
signer: PrivateKeySigner,
|
||||||
chain_id: ChainId,
|
chain_id: u64,
|
||||||
runtime_handle: Handle,
|
runtime_handle: Handle,
|
||||||
// Store the runtime to prevent it from being dropped before use.
|
// Store the runtime to prevent it from being dropped before use.
|
||||||
// This is required since tycho-execution does not have a pre-existing runtime.
|
// This is required since tycho-execution does not have a pre-existing runtime.
|
||||||
@@ -160,7 +160,7 @@ impl Permit2 {
|
|||||||
|
|
||||||
let domain = eip712_domain! {
|
let domain = eip712_domain! {
|
||||||
name: "Permit2",
|
name: "Permit2",
|
||||||
chain_id: self.chain_id.id(),
|
chain_id: self.chain_id,
|
||||||
verifying_contract: self.address,
|
verifying_contract: self.address,
|
||||||
};
|
};
|
||||||
let hash = permit_single.eip712_signing_hash(&domain);
|
let hash = permit_single.eip712_signing_hash(&domain);
|
||||||
|
|||||||
@@ -120,3 +120,10 @@ pub fn get_static_attribute(swap: &Swap, attribute_name: &str) -> Result<Vec<u8>
|
|||||||
})?
|
})?
|
||||||
.to_vec())
|
.to_vec())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Decodes a hex string to a `Bytes` object.
|
||||||
|
pub fn decode_hex(hex_str: &str, err_msg: &str) -> Result<Bytes, EncodingError> {
|
||||||
|
Ok(Bytes::from(
|
||||||
|
hex::decode(hex_str).map_err(|_| EncodingError::FatalError(err_msg.to_string()))?,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use hex;
|
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tycho_core::{
|
use tycho_core::{
|
||||||
@@ -6,6 +5,7 @@ use tycho_core::{
|
|||||||
Bytes,
|
Bytes,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::evm::utils::decode_hex;
|
||||||
use crate::encoding::{
|
use crate::encoding::{
|
||||||
errors::EncodingError,
|
errors::EncodingError,
|
||||||
serde_primitives::{biguint_string, biguint_string_option},
|
serde_primitives::{biguint_string, biguint_string_option},
|
||||||
@@ -119,53 +119,32 @@ pub struct EncodingContext {
|
|||||||
pub amount_out_min: BigUint,
|
pub amount_out_min: BigUint,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
||||||
pub struct ChainId(pub u64);
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct Chain {
|
pub struct Chain {
|
||||||
pub id: ChainId,
|
pub id: u64,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChainId {
|
|
||||||
pub fn id(&self) -> u64 {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<TychoCoreChain> for ChainId {
|
|
||||||
fn from(chain: TychoCoreChain) -> Self {
|
|
||||||
match chain {
|
|
||||||
TychoCoreChain::Ethereum => ChainId(1),
|
|
||||||
TychoCoreChain::ZkSync => ChainId(324),
|
|
||||||
TychoCoreChain::Arbitrum => ChainId(42161),
|
|
||||||
TychoCoreChain::Starknet => ChainId(0),
|
|
||||||
TychoCoreChain::Base => ChainId(8453),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<TychoCoreChain> for Chain {
|
impl From<TychoCoreChain> for Chain {
|
||||||
fn from(chain: TychoCoreChain) -> Self {
|
fn from(chain: TychoCoreChain) -> Self {
|
||||||
Chain { id: chain.into(), name: chain.to_string() }
|
match chain {
|
||||||
|
TychoCoreChain::Ethereum => Chain { id: 1, name: chain.to_string() },
|
||||||
|
TychoCoreChain::ZkSync => Chain { id: 324, name: chain.to_string() },
|
||||||
|
TychoCoreChain::Arbitrum => Chain { id: 42161, name: chain.to_string() },
|
||||||
|
TychoCoreChain::Starknet => Chain { id: 0, name: chain.to_string() },
|
||||||
|
TychoCoreChain::Base => Chain { id: 8453, name: chain.to_string() },
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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";
|
let decode_err_msg = "Failed to decode native token";
|
||||||
match self.id.id() {
|
match self.id {
|
||||||
1 | 8453 | 42161 => {
|
1 | 8453 | 42161 => {
|
||||||
self.decode_hex("0000000000000000000000000000000000000000", decode_err_msg)
|
decode_hex("0000000000000000000000000000000000000000", decode_err_msg)
|
||||||
}
|
}
|
||||||
324 => self.decode_hex("000000000000000000000000000000000000800A", decode_err_msg),
|
324 => 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
|
||||||
@@ -175,11 +154,11 @@ impl Chain {
|
|||||||
|
|
||||||
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";
|
let decode_err_msg = "Failed to decode wrapped token";
|
||||||
match self.id.id() {
|
match self.id {
|
||||||
1 => self.decode_hex("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", decode_err_msg),
|
1 => decode_hex("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", decode_err_msg),
|
||||||
8453 => self.decode_hex("4200000000000000000000000000000000000006", decode_err_msg),
|
8453 => decode_hex("4200000000000000000000000000000000000006", decode_err_msg),
|
||||||
324 => self.decode_hex("5AEa5775959fBC2557Cc8789bC1bf90A239D9a91", decode_err_msg),
|
324 => decode_hex("5AEa5775959fBC2557Cc8789bC1bf90A239D9a91", decode_err_msg),
|
||||||
42161 => self.decode_hex("82aF49447D8a07e3bd95BD0d56f35241523fBab1", decode_err_msg),
|
42161 => 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
|
||||||
|
|||||||
Reference in New Issue
Block a user