pub fn adjust_market_buy_amount(
amount: Decimal,
user_pusd_balance: Decimal,
price: Decimal,
fee_rate: Decimal,
fee_exponent: Decimal,
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. Because adjust_market_buy_amount
is public, it repeats the precondition here.
§Errors
Returns an error if price is outside the open (0, 1) interval, or if
amount or balance is non-positive, a fee input is negative, arithmetic overflows,
or the adjusted amount truncates to zero.