chore: merge main
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
use hex;
|
||||
use num_bigint::BigUint;
|
||||
use tycho_core::{dto::ProtocolComponent, Bytes};
|
||||
use tycho_core::{
|
||||
dto::{Chain as TychoCoreChain, ProtocolComponent},
|
||||
Bytes,
|
||||
};
|
||||
|
||||
use crate::encoding::errors::EncodingError;
|
||||
|
||||
/// Represents a solution containing details describing an order, and instructions for filling
|
||||
/// the order.
|
||||
@@ -61,6 +67,17 @@ pub struct Swap {
|
||||
pub split: f64,
|
||||
}
|
||||
|
||||
impl Swap {
|
||||
pub fn new<T: Into<ProtocolComponent>>(
|
||||
component: T,
|
||||
token_in: Bytes,
|
||||
token_out: Bytes,
|
||||
split: f64,
|
||||
) -> Self {
|
||||
Self { component: component.into(), token_in, token_out, split }
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a transaction to be executed.
|
||||
///
|
||||
/// # Fields
|
||||
@@ -86,3 +103,99 @@ pub struct EncodingContext {
|
||||
pub exact_out: bool,
|
||||
pub router_address: Bytes,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct ChainId(pub u64);
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Chain {
|
||||
pub id: ChainId,
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TychoCoreChain> for Chain {
|
||||
fn from(chain: TychoCoreChain) -> Self {
|
||||
Chain { id: chain.into(), name: chain.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Chain {
|
||||
pub fn native_token(&self) -> Result<Bytes, EncodingError> {
|
||||
match self.id.id() {
|
||||
1 => Ok(Bytes::from(hex::decode("0000000000000000000000000000000000000000").map_err(
|
||||
|_| EncodingError::FatalError("Failed to decode native token".to_string()),
|
||||
)?)),
|
||||
_ => Err(EncodingError::InvalidInput(format!(
|
||||
"Native token not set for chain {:?}. Double check the chain is supported.",
|
||||
self.name
|
||||
))),
|
||||
}
|
||||
}
|
||||
pub fn wrapped_token(&self) -> Result<Bytes, EncodingError> {
|
||||
match self.id.id() {
|
||||
1 => Ok(Bytes::from(hex::decode("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").map_err(
|
||||
|_| EncodingError::FatalError("Failed to decode wrapped token".to_string()),
|
||||
)?)),
|
||||
_ => Err(EncodingError::InvalidInput(format!(
|
||||
"Wrapped token not set for chain {:?}. Double check the chain is supported.",
|
||||
self.name
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
struct MockProtocolComponent {
|
||||
id: String,
|
||||
protocol_system: String,
|
||||
}
|
||||
|
||||
impl From<MockProtocolComponent> for ProtocolComponent {
|
||||
fn from(component: MockProtocolComponent) -> Self {
|
||||
ProtocolComponent {
|
||||
id: component.id,
|
||||
protocol_system: component.protocol_system,
|
||||
tokens: vec![],
|
||||
protocol_type_name: "".to_string(),
|
||||
chain: Default::default(),
|
||||
contract_ids: vec![],
|
||||
static_attributes: Default::default(),
|
||||
change: Default::default(),
|
||||
creation_tx: Default::default(),
|
||||
created_at: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_swap_new() {
|
||||
let component = MockProtocolComponent {
|
||||
id: "i-am-an-id".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
};
|
||||
let swap = Swap::new(component, Bytes::from("0x12"), Bytes::from("34"), 0.5);
|
||||
assert_eq!(swap.token_in, Bytes::from("0x12"));
|
||||
assert_eq!(swap.token_out, Bytes::from("0x34"));
|
||||
assert_eq!(swap.component.protocol_system, "uniswap_v2");
|
||||
assert_eq!(swap.component.id, "i-am-an-id");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user