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: 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 (p * (1 - p))^exponent uses exact decimal arithmetic, so the exponent must be a whole number.

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, the exponent is not a whole number, arithmetic overflows, or the adjusted amount truncates to zero.