Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
swap parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu committed Jul 7, 2023
1 parent 10fa75f commit f5de1e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
11 changes: 11 additions & 0 deletions crates/poirot-core/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub struct Action {
#[derive(Debug, Clone)]
pub enum ActionType {
Transfer(Transfer),

PoolCreation(PoolCreation),
Swap(Swap),

WethDeposit(Deposit),
WethWithdraw(Withdrawal),
Expand Down Expand Up @@ -49,6 +51,15 @@ pub struct PoolCreation {
pub fee: u32,
}

#[derive(Debug, Clone)]
pub struct Swap {
recipient: (),
direction: bool,
amount_specified: (),
price_limit: (),
data: (),
}

impl Transfer {
/// Public constructor function to instantiate a [`Transfer`].
pub fn new(to: Address, amount: ruint2::Uint<256, 4>, token: H160) -> Self {
Expand Down
35 changes: 34 additions & 1 deletion crates/poirot-core/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::action::{Action, ActionType, Deposit, PoolCreation, Transfer, Withdrawal};
use crate::action::{Action, ActionType, Deposit, PoolCreation, Swap, Transfer, Withdrawal};

use reth_rpc_types::trace::parity::{Action as RethAction, LocalizedTransactionTrace};

Expand Down Expand Up @@ -31,6 +31,13 @@ sol! {
}
}

sol! {
#[derive(Debug, PartialEq)]
interface IUniswapV3Pool {
function swap(address recipient, bool zeroForOne, int256 amountSpecified, uint160 sqrtPriceLimitX96, bytes data) external override returns (int256, int256);
}
}

pub struct Parser {
block_trace: Vec<LocalizedTransactionTrace>,
}
Expand Down Expand Up @@ -65,6 +72,32 @@ impl Parser {
self.parse_transfer(curr)
.or_else(|| self.parse_pool_creation(curr))
.or_else(|| self.parse_weth(curr))
.or_else(|| self.parse_swap(curr))
}

pub fn parse_swap(&self, curr: &LocalizedTransactionTrace) -> Option<Action> {
match &curr.trace.action {
RethAction::Call(call) => {
let mut decoded = match IUniswapV3Pool::swapCall::decode(&call.input.to_vec(), true)
{
Ok(decoded) => decoded,
Err(_) => return None,
};

return Some(Action {
ty: ActionType::Swap(Swap {
recipient: decoded.recipient,
direction: decoded.zeroForOne,
amount_specified: decoded.amountSpecified,
price_limit: decoded.sqrtPriceLimitX96,
data: decoded.data,
}),
hash: curr.transaction_hash.unwrap(),
block: curr.block_number.unwrap(),
})
}
_ => None,
}
}

pub fn parse_weth(&self, curr: &LocalizedTransactionTrace) -> Option<Action> {
Expand Down

0 comments on commit f5de1e7

Please sign in to comment.