Skip to content

Commit

Permalink
more deposit bindings tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hardyjosh committed Jan 6, 2024
1 parent 3b845b9 commit 05e9d4a
Showing 1 changed file with 62 additions and 7 deletions.
69 changes: 62 additions & 7 deletions crates/bindings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use alloy_sol_types::sol;

sol!(IOrderBookV3, "../../out/IOrderBookV3.sol/IOrderBookV3.json");
sol!(
#![sol(all_derives = true)]
IOrderBookV3, "../../out/IOrderBookV3.sol/IOrderBookV3.json"
);

#[cfg(test)]
pub mod test {
use crate::IOrderBookV3::depositCall;
use alloy_sol_types::{SolCall, SolError};
use crate::IOrderBookV3::*;
use alloy_sol_types::{SolCall, SolError, SolEvent, abi::token::WordToken};
use alloy_primitives::{hex, Address, U256, keccak256};

#[test]
fn test_deposit() {
fn test_deposit_function() {
assert_call_signature::<depositCall>("deposit(address,uint256,uint256)");

let call = depositCall {
Expand All @@ -21,9 +24,9 @@ pub mod test {

let expected_call_data = hex! (
"0efe6a8b"
"0000000000000000000000001111111111111111111111111111111111111111"
"0000000000000000000000000000000000000000000000000000000000000001"
"0000000000000000000000000000000000000000000000000000000000000001"
"0000000000000000000000001111111111111111111111111111111111111111" // token
"0000000000000000000000000000000000000000000000000000000000000001" // amount
"0000000000000000000000000000000000000000000000000000000000000001" // vaultId
);

assert_eq!(
Expand All @@ -32,6 +35,53 @@ pub mod test {
);
}

#[test]
fn test_deposit_error() {
assert_error_signature::<ZeroDepositAmount>("ZeroDepositAmount(address,address,uint256)");
let call_data = hex!(
"0000000000000000000000001111111111111111111111111111111111111111" // sender
"0000000000000000000000002222222222222222222222222222222222222222" // token
"0000000000000000000000000000000000000000000000000000000000000001" // vaultId
);
assert_eq!(
ZeroDepositAmount::abi_decode_raw(&call_data, true),
Ok(
ZeroDepositAmount {
sender: Address::repeat_byte(0x11),
token: Address::repeat_byte(0x22),
vaultId: U256::from(1),
}
)
);
}

#[test]
fn test_deposit_event() {
assert_event_signature::<Deposit>("Deposit(address,address,uint256,uint256)");
assert!(!Deposit::ANONYMOUS);
let deposit_event = Deposit {
sender: Address::repeat_byte(0x11),
token: Address::repeat_byte(0x22),
vaultId: U256::from(1),
amount: U256::from(1),
};
assert_eq!(
deposit_event.encode_topics_array::<1>(),
[
WordToken(Deposit::SIGNATURE_HASH)
]
);
assert_eq!(
deposit_event.encode_data(),
hex!(
"0000000000000000000000001111111111111111111111111111111111111111" // sender
"0000000000000000000000002222222222222222222222222222222222222222" // token
"0000000000000000000000000000000000000000000000000000000000000001" // vaultId
"0000000000000000000000000000000000000000000000000000000000000001" // amount
)
)
}

fn assert_call_signature<T: SolCall>(expected: &str) {
assert_eq!(T::SIGNATURE, expected);
assert_eq!(T::SELECTOR, keccak256(expected)[..4]);
Expand All @@ -41,4 +91,9 @@ pub mod test {
assert_eq!(T::SIGNATURE, expected);
assert_eq!(T::SELECTOR, keccak256(expected)[..4]);
}

fn assert_event_signature<T: SolEvent>(expected: &str) {
assert_eq!(T::SIGNATURE, expected);
assert_eq!(T::SIGNATURE_HASH, keccak256(expected));
}
}

0 comments on commit 05e9d4a

Please sign in to comment.