Skip to main content

encode_twap_struct

Function encode_twap_struct 

Source
pub fn encode_twap_struct(s: &TwapStruct) -> Vec<u8> 
Expand description

ABI-encode a TwapStruct into the 320-byte staticInput bytes expected by the on-chain TWAP handler.

Encoding layout (each word is 32 bytes, big-endian):

  • Words 0-2: sell_token, buy_token, receiver (address, left-padded)
  • Words 3-4: part_sell_amount, min_part_limit (uint256)
  • Words 5-8: t0, n, t, span (uint32, right-aligned in 32 bytes)
  • Word 9: app_data (bytes32)

Total: 10 x 32 = 320 bytes.

This is the symmetric counterpart to decode_twap_struct.

use alloy_primitives::{Address, U256};
use cow_composable::{
    TwapData, TwapStruct, data_to_struct, decode_twap_struct, encode_twap_struct,
};

let data = TwapData::sell(Address::ZERO, Address::ZERO, U256::from(1000u64), 4, 3600)
    .with_buy_amount(U256::from(800u64));
let s = data_to_struct(&data).unwrap();
let bytes = encode_twap_struct(&s);
assert_eq!(bytes.len(), 320); // 10 × 32-byte ABI words
let decoded = decode_twap_struct(&bytes).unwrap();
assert_eq!(decoded.n, s.n);