Skip to content

Commit

Permalink
feat: add multicall decoding
Browse files Browse the repository at this point in the history
Introduce `decode_multicall` function to decode encoded multicall data. Reorganized tests into `encode` and `decode` modules for clarity and thorough testing.
  • Loading branch information
shuhuiluo committed Oct 12, 2024
1 parent 61a7d22 commit 39304d9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ mod tests;

pub mod prelude {
pub use crate::{
abi::*, constants::*, entities::*, error::*, multicall::encode_multicall,
nonfungible_position_manager::*, payments::*, quoter::*, self_permit::*, staker::*,
swap_router::*, utils::*,
abi::*, constants::*, entities::*, error::*, multicall::*, nonfungible_position_manager::*,
payments::*, quoter::*, self_permit::*, staker::*, swap_router::*, utils::*,
};
pub use alloc::{
string::{String, ToString},
Expand Down
60 changes: 46 additions & 14 deletions src/multicall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,58 @@ pub fn encode_multicall(data: Vec<Bytes>) -> Bytes {
}
}

#[inline]
#[must_use]
pub fn decode_multicall(encoded: &Bytes) -> Vec<Bytes> {
IMulticall::multicallCall::abi_decode(encoded.as_ref(), true)
.unwrap()
.data
}

#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::hex;

#[test]
fn test_encode_multicall_string_array_len_1() {
let calldata = encode_multicall(vec![vec![0x01].into()]);
assert_eq!(calldata, vec![0x01]);
mod encode {
use super::*;

#[test]
fn test_string_array_len_1() {
let calldata = encode_multicall(vec![vec![0x01].into()]);
assert_eq!(calldata, vec![0x01]);
}

#[test]
fn test_string_array_len_2() {
let calldata = encode_multicall(vec![
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").into(),
]);
assert_eq!(
calldata.to_vec(),
hex!("ac9650d800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000020aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000020bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
);
}
}

#[test]
fn test_encode_multicall_string_array_len_2() {
let calldata = encode_multicall(vec![
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").into(),
]);
assert_eq!(
calldata.to_vec(),
hex!("ac9650d800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000020aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000020bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
);
mod decode {
use super::*;

#[test]
fn test_string_array_len_2() {
let calldatas = vec![
hex!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").into(),
hex!("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").into(),
];
let multicall = encode_multicall(calldatas.clone());
assert_eq!(
multicall.to_vec(),
hex!("ac9650d800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000020aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000020bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
);

let decoded_calldata = decode_multicall(&multicall);
assert_eq!(decoded_calldata, calldatas);
}
}
}

0 comments on commit 39304d9

Please sign in to comment.