feat: Add ChainId model

Remove to_chain_id helper method

--- don't change below this line ---
ENG-4081 Took 28 minutes
This commit is contained in:
Diana Carvalho
2025-01-30 15:01:53 +00:00
parent 0e70e827a0
commit 089e7d2e0f
4 changed files with 27 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
use std::{str::FromStr, sync::Arc}; use std::{str::FromStr, sync::Arc};
use alloy::{ use alloy::{
primitives::{aliases::U48, Address, Bytes as AlloyBytes, ChainId, TxKind, U160, U256}, primitives::{aliases::U48, Address, Bytes as AlloyBytes, TxKind, U160, U256},
providers::{Provider, RootProvider}, providers::{Provider, RootProvider},
rpc::types::{TransactionInput, TransactionRequest}, rpc::types::{TransactionInput, TransactionRequest},
signers::{local::PrivateKeySigner, SignerSync}, signers::{local::PrivateKeySigner, SignerSync},
@@ -20,7 +20,8 @@ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
evm::{ evm::{
approvals::protocol_approvals_manager::get_client, approvals::protocol_approvals_manager::get_client,
utils::{biguint_to_u256, bytes_to_address, encode_input, to_chain_id}, models::ChainId,
utils::{biguint_to_u256, bytes_to_address, encode_input},
}, },
}; };
@@ -62,7 +63,7 @@ sol! {
#[allow(dead_code)] #[allow(dead_code)]
impl Permit2 { impl Permit2 {
pub fn new(signer_pk: String, chain: Chain) -> Result<Self, EncodingError> { pub fn new(signer_pk: String, chain: Chain) -> Result<Self, EncodingError> {
let chain_id = to_chain_id(chain)?; let chain_id = ChainId::from(chain);
let runtime = Runtime::new() let runtime = Runtime::new()
.map_err(|_| EncodingError::FatalError("Failed to create runtime".to_string()))?; .map_err(|_| EncodingError::FatalError("Failed to create runtime".to_string()))?;
let client = runtime.block_on(get_client())?; let client = runtime.block_on(get_client())?;
@@ -145,7 +146,7 @@ impl Permit2 {
let domain = eip712_domain! { let domain = eip712_domain! {
name: "Permit2", name: "Permit2",
chain_id: self.chain_id, chain_id: self.chain_id.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);

View File

@@ -1,4 +1,5 @@
pub mod approvals; pub mod approvals;
mod models;
mod router_encoder; mod router_encoder;
mod strategy_encoder; mod strategy_encoder;
mod swap_encoder; mod swap_encoder;

View File

@@ -0,0 +1,20 @@
use tycho_core::models::Chain;
pub struct ChainId(u64);
impl ChainId {
pub fn id(&self) -> u64 {
self.0
}
}
impl From<Chain> for ChainId {
fn from(chain: Chain) -> Self {
match chain {
Chain::Ethereum => ChainId(1),
Chain::ZkSync => ChainId(324),
Chain::Arbitrum => ChainId(42161),
Chain::Starknet => ChainId(0),
}
}
}

View File

@@ -1,7 +1,7 @@
use alloy_primitives::{aliases::U24, Address, Keccak256, U256}; use alloy_primitives::{aliases::U24, Address, Keccak256, U256};
use alloy_sol_types::SolValue; use alloy_sol_types::SolValue;
use num_bigint::BigUint; use num_bigint::BigUint;
use tycho_core::{models::Chain, Bytes}; use tycho_core::Bytes;
use crate::encoding::errors::EncodingError; use crate::encoding::errors::EncodingError;
@@ -65,10 +65,3 @@ pub fn percentage_to_uint24(percentage: f64) -> U24 {
let scaled = (percentage / 100.0) * (MAX_UINT24 as f64); let scaled = (percentage / 100.0) * (MAX_UINT24 as f64);
U24::from(scaled.round()) U24::from(scaled.round())
} }
pub fn to_chain_id(chain: Chain) -> Result<u64, EncodingError> {
match chain {
Chain::Ethereum => Ok(1),
_ => Err(EncodingError::FatalError("Unsupported chain".to_string())),
}
}