import Big from 'big.js'; import { Result } from './result.js'; /** * Decimal-safe helpers for converting between human-readable amounts and * on-chain integer wei amounts, and for truncating display-precision. * * Background: encoding `2933.0 * 10**18` via float arithmetic produces * `2932999999999999737856`, which the trading contract rejects. All * write paths must route through {@link toWei} (or equivalent) so the * intended value reaches the contract exactly. * * Big.js operates on arbitrary-precision decimal strings; only the * final conversion to wei narrows to `bigint`. */ /** * Accepted scalar inputs for the helpers below. `number` is convenient * for human callers; `string`, `bigint`, and `Big` preserve precision * exactly and are the safer choice for amounts that may have many * fractional digits. */ export type DecimalInput = number | string | bigint | Big; /** * Coerce a numeric value to Big preserving the user's intended decimal * representation. Floats route through `String(value)` (so `0.1` stays * `"0.1"` rather than picking up binary-noise tail digits). */ export declare function toDecimal(value: DecimalInput): Big; /** * Convert a human-readable amount to integer wei: floor(value * 10^decimals). * * Truncation toward zero (Big.roundDown) matches the Python reference's * `int(Decimal(str(value)) * Decimal(10)**decimals)` semantics. Inputs * whose precision exceeds `decimals` are truncated silently here; the * display-precision REJECT-with-tolerance gate (introduced separately * by the CLOB write paths) is responsible for refusing over-precise * order inputs before they reach this helper. */ export declare function toWei(value: DecimalInput, decimals: number): bigint; /** * Convert an integer wei value to a human-readable `Big` via * precision-safe decimal division. Inverse of {@link toWei}. * * Used by the order-format path so the wei → display conversion is * exact through the entire arithmetic chain; callers that need a * `number` (e.g. for the public `Order` shape) take `.toNumber()` at * the boundary, accepting the standard IEEE-754 rounding only at the * final step rather than at every intermediate operation. */ export declare function fromWei(wei: bigint | string | number, decimals: number): Big; /** * Truncate `value` to `displayDecimals` fractional digits using ROUND_DOWN. * * Intended for callers that have already validated precision (so this * never silently rounds away meaningful significant digits). Use the * REJECT-with-tolerance precision gate in the write paths for inputs * that may exceed display precision. */ export declare function quantizeToDisplay(value: DecimalInput, displayDecimals: number): Big; /** * Tolerance band that absorbs binary-float-representation noise when * validating display-precision. Float arithmetic like `0.1 + 0.2` produces * residuals on the order of `1e-17`; a user typing genuinely extra digits * produces residuals `>= 1e-6`. `1e-10` sits comfortably in the gap. */ export declare const DISPLAY_PRECISION_TOLERANCE: Big.Big; /** * Validate that `value` fits in `displayDecimals` fractional digits. * * Returns `Result.ok(snapped)` when the input is at display precision — * or differs from a representable value only by float noise (within * {@link DISPLAY_PRECISION_TOLERANCE}). Returns `Result.fail` when the * input has genuinely more decimals than the pair allows; callers must * round explicitly rather than have the SDK silently slip the order. * * Silent rounding would be dangerous in a trading SDK — a stop-loss * at `99.99` quietly becoming `99.9` is silent slippage. */ export declare function checkDisplayPrecision(value: DecimalInput, displayDecimals: number, paramName: string): Result; /** * Enforce a pair's min/max trade-amount bounds, computed against the * quote-token notional `price * amount`. A bound of `0` is treated as * "no bound" (some pairs legitimately omit a cap). When `price` is * `null` the bounds check is skipped — market orders have no * client-side notional to check. */ export declare function checkTradeAmountBounds(price: Big | null, amount: Big, minTradeAmount: DecimalInput, maxTradeAmount: DecimalInput, pairName: string): Result; export { Big }; //# sourceMappingURL=decimal.d.ts.map