Skip to main content

adjust_market_buy_amount

Function adjust_market_buy_amount 

Source
pub fn adjust_market_buy_amount(
    amount: Decimal,
    user_pusd_balance: Decimal,
    price: Decimal,
    fee_rate: Decimal,
    fee_exponent: f64,
    builder_taker_fee_rate: Decimal,
) -> Result<Decimal>
Expand description

Adjusts a market-BUY pUSD amount to fit within the user’s pUSD balance once platform and builder taker fees are deducted. Mirrors adjust_market_buy_amount in polymarket-rs-clob-client-v2’s clob/utilities.rs.

Returns amount unchanged when the balance already covers amount + fees. Otherwise solves for the principal that, with fees, exactly consumes the balance, then truncates to USDC_DECIMALS (the on-chain pUSD scale).

The fee-curve step (p * (1 - p))^exponent is the only computation that crosses into f64, matching the reference SDK so we agree with the venue’s authoritative match-time fee calculation regardless of whether Polymarket ships a fractional exponent in the future.

price must be strictly inside (0, 1). The SDK relies on its order-builder pipeline to enforce this; this helper is public so we repeat the precondition here.

§Errors

Returns an error if price is outside the open (0, 1) interval, or if the balance is too small to cover even one pUSD-unit of fees and the adjusted amount truncates to zero.