use std::fmt::Display;
use alloy::{
primitives::{Address, U256},
sol_types::SolCall,
};
use newton_prover_core::newton_prover_task_manager::NewtonMessage;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub enum BuyOrSell {
#[default]
Buy,
Sell,
}
impl BuyOrSell {
pub fn as_str(&self) -> &'static str {
match self {
BuyOrSell::Buy => "buy",
BuyOrSell::Sell => "sell",
}
}
}
impl Display for BuyOrSell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
alloy::sol! {
interface MockUSDCSwapPool {
function buy(address buyToken, uint256 amountIn, uint32 slippage) external returns (uint256 amountOut);
function sell(address sellToken, uint256 amountIn, uint32 slippage) external returns (uint256 amountOut);
}
}
pub fn create_swap_intent(
from: Address,
to: Address,
token: Address,
amount: u64,
buy_or_sell: BuyOrSell,
slippage: u32,
chain_id: u64,
) -> eyre::Result<NewtonMessage::Intent> {
let (encoded_data, function_signature) = match buy_or_sell {
BuyOrSell::Buy => {
let call = MockUSDCSwapPool::buyCall {
buyToken: token,
amountIn: U256::from(amount),
slippage,
};
(call.abi_encode(), MockUSDCSwapPool::buyCall::SIGNATURE)
}
BuyOrSell::Sell => {
let call = MockUSDCSwapPool::sellCall {
sellToken: token,
amountIn: U256::from(amount),
slippage,
};
(call.abi_encode(), MockUSDCSwapPool::sellCall::SIGNATURE)
}
};
Ok(NewtonMessage::Intent {
from,
to,
value: U256::ZERO,
data: encoded_data.into(),
chainId: U256::from(chain_id),
functionSignature: function_signature.into(),
})
}