Merge pull request #14 from propeller-heads/encoding/dc/remove-expects
chore: Do not use expect
This commit is contained in:
@@ -3,7 +3,10 @@ use std::str::FromStr;
|
||||
use alloy_primitives::U256;
|
||||
use tycho_core::Bytes;
|
||||
|
||||
use crate::encoding::user_approvals_manager::{Approval, UserApprovalsManager};
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
user_approvals_manager::{Approval, UserApprovalsManager},
|
||||
};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct Permit2 {
|
||||
@@ -12,12 +15,13 @@ pub struct Permit2 {
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Permit2 {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pub fn new() -> Result<Self, EncodingError> {
|
||||
Ok(Self {
|
||||
address: Bytes::from_str("0x000000000022D473030F116dDEE9F6B43aC78BA3")
|
||||
.expect("Permit2 address not valid"),
|
||||
}
|
||||
.map_err(|_| EncodingError::FatalError("Permit2 address not valid".to_string()))?,
|
||||
})
|
||||
}
|
||||
|
||||
fn get_allowance_data(
|
||||
&self,
|
||||
_user: Bytes,
|
||||
|
||||
@@ -18,10 +18,11 @@ pub struct ProtocolApprovalsManager {
|
||||
runtime: Runtime,
|
||||
}
|
||||
impl ProtocolApprovalsManager {
|
||||
pub fn new() -> Self {
|
||||
let runtime = Runtime::new().expect("Failed to create runtime");
|
||||
let client = runtime.block_on(get_client());
|
||||
Self { client, runtime }
|
||||
pub fn new() -> Result<Self, EncodingError> {
|
||||
let runtime = Runtime::new()
|
||||
.map_err(|_| EncodingError::FatalError("Failed to create runtime".to_string()))?;
|
||||
let client = runtime.block_on(get_client())?;
|
||||
Ok(Self { client, runtime })
|
||||
}
|
||||
pub fn approval_needed(
|
||||
&self,
|
||||
@@ -56,14 +57,15 @@ impl ProtocolApprovalsManager {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_client() -> Arc<RootProvider<BoxTransport>> {
|
||||
pub async fn get_client() -> Result<Arc<RootProvider<BoxTransport>>, EncodingError> {
|
||||
dotenv().ok();
|
||||
let eth_rpc_url = env::var("ETH_RPC_URL").expect("Missing ETH_RPC_URL in environment");
|
||||
let eth_rpc_url = env::var("ETH_RPC_URL")
|
||||
.map_err(|_| EncodingError::FatalError("Missing ETH_RPC_URL in environment".to_string()))?;
|
||||
let client = ProviderBuilder::new()
|
||||
.on_builtin(ð_rpc_url)
|
||||
.await
|
||||
.expect("Failed to build provider");
|
||||
Arc::new(client)
|
||||
.map_err(|_| EncodingError::FatalError("Failed to build provider".to_string()))?;
|
||||
Ok(Arc::new(client))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -85,7 +87,7 @@ mod tests {
|
||||
true
|
||||
)]
|
||||
fn test_approval_needed(#[case] spender: &str, #[case] owner: &str, #[case] expected: bool) {
|
||||
let manager = ProtocolApprovalsManager::new();
|
||||
let manager = ProtocolApprovalsManager::new().unwrap();
|
||||
|
||||
let token = Address::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
|
||||
let spender = Address::from_str(spender).unwrap();
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use num_bigint::BigUint;
|
||||
use tycho_core::Bytes;
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
evm::utils::encode_input,
|
||||
models::{NativeAction, Solution, Transaction, PROPELLER_ROUTER_ADDRESS},
|
||||
models::{NativeAction, Solution, Transaction},
|
||||
router_encoder::RouterEncoder,
|
||||
strategy_encoder::StrategySelector,
|
||||
user_approvals_manager::{Approval, UserApprovalsManager},
|
||||
@@ -13,12 +16,13 @@ use crate::encoding::{
|
||||
pub struct EVMRouterEncoder<S: StrategySelector, A: UserApprovalsManager> {
|
||||
strategy_selector: S,
|
||||
approvals_manager: A,
|
||||
router_address: String,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl<S: StrategySelector, A: UserApprovalsManager> EVMRouterEncoder<S, A> {
|
||||
pub fn new(strategy_selector: S, approvals_manager: A) -> Self {
|
||||
EVMRouterEncoder { strategy_selector, approvals_manager }
|
||||
pub fn new(strategy_selector: S, approvals_manager: A, router_address: String) -> Self {
|
||||
EVMRouterEncoder { strategy_selector, approvals_manager, router_address }
|
||||
}
|
||||
}
|
||||
impl<S: StrategySelector, A: UserApprovalsManager> RouterEncoder<S, A> for EVMRouterEncoder<S, A> {
|
||||
@@ -61,7 +65,9 @@ impl<S: StrategySelector, A: UserApprovalsManager> RouterEncoder<S, A> for EVMRo
|
||||
spender: solution
|
||||
.router_address
|
||||
.clone()
|
||||
.unwrap_or(PROPELLER_ROUTER_ADDRESS.clone()),
|
||||
.unwrap_or(Bytes::from_str(&self.router_address).map_err(|_| {
|
||||
EncodingError::FatalError("Invalid router address".to_string())
|
||||
})?),
|
||||
amount: solution.given_amount.clone(),
|
||||
owner: solution.sender.clone(),
|
||||
});
|
||||
|
||||
@@ -36,15 +36,14 @@ impl SwapEncoder for UniswapV2SwapEncoder {
|
||||
|
||||
pub struct BalancerV2SwapEncoder {
|
||||
executor_address: String,
|
||||
vault_address: Address,
|
||||
vault_address: String,
|
||||
}
|
||||
|
||||
impl SwapEncoder for BalancerV2SwapEncoder {
|
||||
fn new(executor_address: String) -> Self {
|
||||
Self {
|
||||
executor_address,
|
||||
vault_address: Address::from_str("0xba12222222228d8ba445958a75a0704d566bf2c8")
|
||||
.expect("Invalid string for balancer vault address"),
|
||||
vault_address: "0xba12222222228d8ba445958a75a0704d566bf2c8".to_string(),
|
||||
}
|
||||
}
|
||||
fn encode_swap(
|
||||
@@ -52,11 +51,15 @@ impl SwapEncoder for BalancerV2SwapEncoder {
|
||||
swap: Swap,
|
||||
encoding_context: EncodingContext,
|
||||
) -> Result<Vec<u8>, EncodingError> {
|
||||
let token_approvals_manager = ProtocolApprovalsManager::new();
|
||||
let token_approvals_manager = ProtocolApprovalsManager::new()?;
|
||||
let token = bytes_to_address(&swap.token_in)?;
|
||||
let router_address = bytes_to_address(&encoding_context.address_for_approvals)?;
|
||||
let approval_needed =
|
||||
token_approvals_manager.approval_needed(token, router_address, self.vault_address)?;
|
||||
let approval_needed = token_approvals_manager.approval_needed(
|
||||
token,
|
||||
router_address,
|
||||
Address::from_str(&self.vault_address)
|
||||
.map_err(|_| EncodingError::FatalError("Invalid vault address".to_string()))?,
|
||||
)?;
|
||||
// should we return gas estimation here too?? if there is an approval needed, gas will be
|
||||
// higher.
|
||||
let args = (
|
||||
|
||||
@@ -9,6 +9,7 @@ use tycho_core::dto::Chain;
|
||||
|
||||
use crate::encoding::evm::swap_encoder::registry::{Config, SwapEncoderRegistry};
|
||||
|
||||
// TODO: init this at the higher level at some point
|
||||
lazy_static! {
|
||||
pub static ref SWAP_ENCODER_REGISTRY: RwLock<SwapEncoderRegistry> = {
|
||||
let config = Config::from_file("src/encoding/config/executor_addresses.json")
|
||||
|
||||
@@ -1,16 +1,6 @@
|
||||
use std::{env, str::FromStr};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use num_bigint::BigUint;
|
||||
use tycho_core::{dto::ProtocolComponent, Bytes};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref PROPELLER_ROUTER_ADDRESS: Bytes = Bytes::from_str(
|
||||
&env::var("ROUTER_ADDRESS").expect("Missing ROUTER_ADDRESS in environment"),
|
||||
)
|
||||
.expect("Invalid ROUTER_ADDRESS");
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(dead_code)]
|
||||
pub struct Solution {
|
||||
|
||||
Reference in New Issue
Block a user