newton-testing-utils 0.1.35

newton provero testing utils
Documentation
use std::fmt::Display;

use alloy::{
    primitives::{Address, U256},
    sol_types::SolCall,
};
use newton_prover_core::newton_prover_task_manager::NewtonMessage;

/// Buy or sell enum
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub enum BuyOrSell {
    /// Buy
    #[default]
    Buy,
    /// Sell
    Sell,
}

impl BuyOrSell {
    /// Returns the string representation of the enum
    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())
    }
}

// Mock swapper contract interface
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);
    }
}

/// Create a swap intent for the demo
///
/// This is a mock swap contract that is used to swap USDC for a token or sell a token for USDC
/// It is hardcoded to the mock swap contract address for the demo
///
/// The intent is created for the demo policy
/// contracts/demo/policy_params.json
///
/// @param from The address of the sender
/// @param to The address of the receiver
/// @param token The address of the token to swap
/// @param amount The amount of the token to swap
/// @param buy_or_sell Whether to buy or sell the token
/// @param slippage The slippage for the swap
/// @param chain_id The chain id of the chain that the swap is on
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> {
    // encode the swap function call: swap(uint256 _amount, uint256 _swapNumber)
    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)
        }
    };

    // get the encoded data and function signature
    Ok(NewtonMessage::Intent {
        from,
        to,
        value: U256::ZERO,
        data: encoded_data.into(),
        chainId: U256::from(chain_id),
        functionSignature: function_signature.into(),
    })
}