the source

the code that runs inside every transfer of the token.

the program is a single rust file of roughly eight hundred lines. it is compiled with anchor against solana z's bpf target and deployed to the mainnet bpf upgradeable loader with the upgrade authority revoked in the same session. the sealed bytecode is what runs forever.

the layout

the program maintains three program derived addresses, each with a fixed account layout. the first holds the circular buffer of recent transfers and the running centroid. the second holds the running pythagorean distance estimate and welford bookkeeping. the third is the vault.

// account layouts for the pythagorean transfer hook program

const BUFFER_LEN: usize = 256;
const TUPLE_BYTES: usize = 22;          // (slot:8, src:4, dst:4, amount:4, price:2)
const FP_SCALE: i128 = 1_000_000_000_000;
const P_REFERENCE_FP: i128 = 1_414_213_562_373;  // 1.414 * 1e12 (≈ sqrt 2)

#[account]
pub struct TradeBuffer {
    pub mint: Pubkey,
    pub head: u16,                          // circular write pointer
    pub filled: u16,                        // entries (caps at BUFFER_LEN)
    pub centroid_x_fp: i128,                // running centroid x, 1e12 fixed-point
    pub centroid_y_fp: i128,                // running centroid y, 1e12 fixed-point
    pub last_update_slot: u64,
    pub entries: [u8; BUFFER_LEN * TUPLE_BYTES],
}

#[account]
pub struct CentroidState {
    pub mint: Pubkey,
    pub last_p_estimate_fp: i128,            // current P(x), 1e12 fixed-point
    pub buffer_n: u32,                       // sample count fed to welford
    pub last_compute_unit_cost: u32,         // for diagnostics
    pub last_update_slot: u64,
}

#[account]
pub struct VaultState {
    pub mint: Pubkey,
    pub accumulated_lamports: u64,
    pub last_deposit_slot: u64,
    pub total_distributed: u128,
    pub bump: u8,
}

the entry point

the transfer hook is the only entry point. it is invoked by the token-2022 program every time the pythagorean mint participates in a transfer. the hook reads the active price from the bonding curve, appends the tuple to the circular buffer, recomputes the pythagorean distance estimate, and returns the surcharge to be withheld. all of this executes atomically inside the transfer's transaction.

// the transfer hook entry point

pub fn execute(ctx: Context<Execute>, amount: u64) -> Result<u64> {
    let clock = Clock::get()?;
    let buf = &mut ctx.accounts.trade_buffer;
    let cx = &mut ctx.accounts.centroid_state;
    let vault = &mut ctx.accounts.vault;

    // 1. read active price from the bonding curve
    let price_fp = read_bonding_curve_price(&ctx.accounts.curve_state)?;

    // 2. encode the tuple and append to the circular buffer
    let tuple = encode_tuple(
        clock.slot,
        ctx.accounts.source.owner,
        ctx.accounts.destination.owner,
        amount,
        price_fp,
    );
    let head = buf.head as usize;
    let start = head * TUPLE_BYTES;
    buf.entries[start..start + TUPLE_BYTES].copy_from_slice(&tuple);
    buf.head = ((head + 1) % BUFFER_LEN) as u16;
    if (buf.filled as usize) < BUFFER_LEN {
        buf.filled += 1;
    }

    // 3. recompute the pythagorean distance estimate via incremental centroid
    let new_point = decode_point(&tuple);
    let p_fp = pythagorean_distance(buf, &new_point);
    cx.last_p_estimate_fp = p_fp;

    // 4. compute surcharge from squared deviation against the reference
    let dev = p_fp - P_REFERENCE_FP;
    let dev_sq_fp = (dev * dev) / FP_SCALE;
    let surcharge_rate_fp = (SURCHARGE_K_COEFF * dev_sq_fp) / FP_SCALE;
    let surcharge = ((amount as i128 * surcharge_rate_fp) / FP_SCALE)
        .max(0)
        .min(amount as i128) as u64;

    // 5. accumulate in the vault
    vault.accumulated_lamports = vault.accumulated_lamports.saturating_add(surcharge);
    vault.last_deposit_slot = clock.slot;

    Ok(surcharge)
}

the displacement

the pythagorean distance of the buffer is computed using welford's online algorithm for incrementally updating the centroid. the algorithm processes one new point per invocation, updates the running centroid in place without revisiting prior samples, and emits the squared euclidean displacement of the new point from the freshly updated centroid. an integer square root then yields the distance itself. the entire pass runs in constant compute units regardless of how many trades have come before, well inside the program's budget on solana z.

// incremental centroid + pythagorean distance, welford's algorithm
// returns P(x) ∈ [0, 1] as i128 fixed-point with FP_SCALE = 1e12

fn pythagorean_distance(buffer: &TradeBuffer, new_point: &Tuple) -> i128 {
    let n = buffer.filled as i128;
    if n == 0 {
        return 0;
    }
    // welford incremental update of mean
    let delta_x = new_point.x_fp - buffer.centroid_x_fp;
    let delta_y = new_point.y_fp - buffer.centroid_y_fp;
    let new_centroid_x = buffer.centroid_x_fp + delta_x / (n + 1);
    let new_centroid_y = buffer.centroid_y_fp + delta_y / (n + 1);
    // squared euclidean distance from new point to updated centroid
    let dx = new_point.x_fp - new_centroid_x;
    let dy = new_point.y_fp - new_centroid_y;
    let sq_dist_fp = ((dx * dx) + (dy * dy)) / FP_SCALE;
    // pythagorean distance is the integer square root of the squared distance
    isqrt_fp(sq_dist_fp.max(0))
}

the three functions above are the entire protocol. the rest of the file is account setup, error definitions, and the math for the bonding curve price read. there is nothing else hidden in the binary.