chore: fix encoding

This commit is contained in:
pedrobergamini
2025-08-10 17:24:29 -03:00
parent 97a961ecb8
commit 29dc960a63
4 changed files with 207 additions and 153 deletions

View File

@@ -948,33 +948,67 @@ impl SwapEncoder for BebopSwapEncoder {
if selector == SWAP_SINGLE_SELECTOR {
// For swapSingle, only care about taker_amount; receiver comes from context
// Single struct layout (after 4-byte selector):
// expiry: 0..32, taker_address: 32..64, maker_address: 64..96,
// maker_nonce: 96..128, taker_token: 128..160, maker_token: 160..192,
// taker_amount: 192..224, maker_amount: 224..256, receiver: 256..288,
// packed_commands: 288..320, flags: 320..352
// So taker_amount is at bytes 196..228 (4 + 192..4 + 224)
// The bebop_calldata can come in different formats:
// 1. Selector + inline params (from integration test via build_bebop_calldata):
// - Bytes 0-4: selector (0x4dcebcba)
// - Bytes 4-356: order struct inline (352 bytes)
// - Bytes 356-388: signature offset (32 bytes)
// - Bytes 388-420: filledTakerAmount (32 bytes)
// - Bytes 420+: signature data
// - taker_amount is at bytes 196-228 (4 + 192)
//
// 2. Selector + offsets + data (from unit test):
// - Bytes 0-4: selector
// - Bytes 4-36: order offset (value = 96)
// - Bytes 36-68: signature offset
// - Bytes 68-100: filledTakerAmount
// - Bytes 100+: order data
// - taker_amount is at bytes 292-324 (100 + 192)
let taker_amount = if filled_taker_amount != U256::ZERO {
filled_taker_amount
} else if bebop_calldata.len() >= 228 {
U256::from_be_slice(&bebop_calldata[196..228])
} else {
U256::ZERO
// Check if we have a selector (starts with 0x4dcebcba)
if bebop_calldata.len() >= 4 && bebop_calldata[0..4] == [0x4d, 0xce, 0xbc, 0xba] {
// We have a selector, need to determine which format
// Check if bytes 4-36 look like an offset (should be 0x60 = 96 for offset format)
if bebop_calldata.len() >= 36 {
let potential_offset = U256::from_be_slice(&bebop_calldata[4..36]);
if potential_offset == U256::from(96) {
// Format with offsets - taker_amount is at 292-324
if bebop_calldata.len() >= 324 {
U256::from_be_slice(&bebop_calldata[292..324])
} else {
U256::ZERO
}
} else {
// Inline format with selector - taker_amount is at 196-228
if bebop_calldata.len() >= 228 {
U256::from_be_slice(&bebop_calldata[196..228])
} else {
U256::ZERO
}
}
} else {
U256::ZERO
}
} else {
// No selector, pure inline format - taker_amount is at 192-224
if bebop_calldata.len() >= 224 {
U256::from_be_slice(&bebop_calldata[192..224])
} else {
U256::ZERO
}
}
};
taker_amount
} else if selector == SWAP_AGGREGATE_SELECTOR {
println!("DEBUG: Processing SWAP_AGGREGATE_SELECTOR");
// For swapAggregate, compute taker_amount from calldata if needed; receiver from
// context
let taker_amount = if filled_taker_amount != U256::ZERO {
println!("DEBUG: Using filled_taker_amount: {}", filled_taker_amount);
filled_taker_amount
} else {
println!("DEBUG: Calling extract_aggregate_taker_amount");
let extracted =
extract_aggregate_taker_amount(&bebop_calldata).unwrap_or(U256::ZERO);
println!("DEBUG: Extracted taker amount: {}", extracted);
extracted
extract_aggregate_taker_amount(&bebop_calldata).unwrap_or(U256::ZERO)
};
taker_amount
} else {
@@ -2214,15 +2248,19 @@ mod tests {
hex_swap.contains("0000000000000000000000000000000000000000000000000000000bebc200")
); // 200000000 in hex
// Verify the partialFillOffset byte (02 = 2) appears in the right place
// The packed data format is: tokens | transfer_type | partialFillOffset |
// original_filled_taker_amount | approval_needed | receiver | bebop_calldata
// Looking at the hex output, we can see that partialFillOffset
// (02) is followed by the original filledTakerAmount
assert!(
hex_swap
.contains("02000000000000000000000000000000000000000000000000000000000bebc200"),
"partialFillOffset byte (02) should be followed by original filledTakerAmount"
// The packed data format is:
// token_in (20) | token_out (20) | transfer_type (1) | partial_fill_offset (1) |
// original_filled_taker_amount (32) | approval_needed (1) | receiver (20) |
// bebop_calldata Verify partialFillOffset and original_filled_taker_amount
// are correct
let offset_pos = 41 * 2; // 41 bytes * 2 hex chars per byte
let partial_fill_offset_hex = &hex_swap[offset_pos..offset_pos + 2];
let amount_hex = &hex_swap[offset_pos + 2..offset_pos + 2 + 64];
assert_eq!(partial_fill_offset_hex, "02", "partialFillOffset should be 02");
assert_eq!(
amount_hex, "000000000000000000000000000000000000000000000000000000000bebc200",
"original_filled_taker_amount should be 200000000 (0xbebc200)"
);
write_calldata_to_file("test_encode_bebop_single", hex_swap.as_str());