Skip to main content

struct_to_data

Function struct_to_data 

Source
pub fn struct_to_data(s: &TwapStruct) -> TwapData
Expand description

Convert a TwapStruct (per-part, on-chain view) back into TwapData.

Multiplies part_sell_amount and min_part_limit by n to recover total amounts. Maps t0 and span back into TwapStartTime and DurationOfPart enums. Sets kind to OrderKind::Sell and partially_fillable to false (these fields are not encoded on-chain).

This is the inverse of data_to_struct.

ยงExample

use alloy_primitives::{Address, B256, U256};
use cow_composable::{TwapStruct, struct_to_data};

let s = TwapStruct {
    sell_token: Address::ZERO,
    buy_token: Address::ZERO,
    receiver: Address::ZERO,
    part_sell_amount: U256::from(250u64),
    min_part_limit: U256::from(200u64),
    t0: 1_000_000,
    n: 4,
    t: 3600,
    span: 0,
    app_data: B256::ZERO,
};
let data = struct_to_data(&s);
assert_eq!(data.sell_amount, U256::from(1000u64));
assert_eq!(data.buy_amount, U256::from(800u64));
assert_eq!(data.num_parts, 4);