From 5a661ab6caa0ee19fed0fbac470476927ac26a2d Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Tue, 14 Jan 2025 18:26:26 +0000 Subject: [PATCH] feat: Add Transaction as output of encoding --- src/encoding/models.rs | 8 +++++++- src/encoding/router_encoder.rs | 22 ++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 09439f7..7c3870b 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -42,7 +42,7 @@ pub struct Solution { pub native_action: Option, } -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub enum NativeAction { Wrap, Unwrap, @@ -60,6 +60,12 @@ pub struct Swap { pub split: f64, } +pub struct Transaction { + pub data: Vec, + // ETH value to be sent with the transaction. + pub value: BigUint, +} + pub struct EncodingContext { pub receiver: Bytes, pub exact_out: bool, diff --git a/src/encoding/router_encoder.rs b/src/encoding/router_encoder.rs index a6d5bf5..a655f8d 100644 --- a/src/encoding/router_encoder.rs +++ b/src/encoding/router_encoder.rs @@ -1,14 +1,17 @@ use crate::encoding::approvals::interface::{Approval, UserApprovalsManager}; -use crate::encoding::models::{Solution, PROPELLER_ROUTER_ADDRESS}; +use crate::encoding::models::{NativeAction, Solution, Transaction, PROPELLER_ROUTER_ADDRESS}; use crate::encoding::strategy_selector::StrategySelector; use crate::encoding::utils::{encode_input, ple_encode}; use alloy_sol_types::SolValue; use anyhow::Error; +use num_bigint::BigUint; +use std::cmp::PartialEq; struct RouterEncoder { strategy_selector: S, approvals_manager: A, } + impl RouterEncoder { pub fn new(strategy_selector: S, approvals_manager: A) -> Self { RouterEncoder { @@ -16,10 +19,11 @@ impl RouterEncoder { approvals_manager, } } - pub fn encode_router_calldata(&self, solutions: Vec) -> Result, Error> { + pub fn encode_router_calldata(&self, solutions: Vec) -> Result { let approvals_calldata = self.handle_approvals(&solutions)?; // TODO: where should we append this? let mut calldata_list: Vec> = Vec::new(); let encode_for_batch_execute = solutions.len() > 1; + let mut value = BigUint::ZERO; for solution in solutions.iter() { let exact_out = solution.exact_out.clone(); let straight_to_pool = solution.straight_to_pool.clone(); @@ -38,13 +42,19 @@ impl RouterEncoder { } }; calldata_list.push(contract_interaction); + + if solution.native_action.clone().unwrap() == NativeAction::Wrap { + value += solution.given_amount.clone(); + } } - if encode_for_batch_execute { + let data = if encode_for_batch_execute { let args = (false, ple_encode(calldata_list)); - Ok(encode_input("batchExecute(bytes)", args.abi_encode())) + encode_input("batchExecute(bytes)", args.abi_encode()) } else { - Ok(calldata_list[0].clone()) - } + calldata_list[0].clone() + }; + + Ok(Transaction { data, value }) } fn handle_approvals(&self, solutions: &Vec) -> Result, Error> {