diff --git a/src/polymarket/fund.ts b/src/polymarket/fund.ts index 526d9220..b818f7ba 100644 --- a/src/polymarket/fund.ts +++ b/src/polymarket/fund.ts @@ -17,6 +17,7 @@ import { getFundsAddress } from "./positions.js"; import { getPublicClient } from "./setup.js"; import type { ToolResult } from "./orders.js"; import { signUnderSpendPolicy, type PolymarketSpendDeps } from "./spend-policy.js"; +import { usdToBaseUnits } from "./units.js"; const FUND_FEE_USD = 0.01; const USDC_DECIMALS = 6; @@ -122,7 +123,7 @@ export async function fundVault( // goes to, so a blocked/unlisted bridge, network, or asset refuses here // before anything is signed (and before the fee leg below is charged). const privateKey = getOrCreateWalletKey(); - const amountMicro = String(Math.floor(amountUsd * 10 ** USDC_DECIMALS)); + const amountMicro = String(usdToBaseUnits(amountUsd, USDC_DECIMALS)); const network = `eip155:${BASE_CHAIN_ID}`; const depositAuthorization = await signUnderSpendPolicy( deps, diff --git a/src/polymarket/units.test.ts b/src/polymarket/units.test.ts new file mode 100644 index 00000000..d546bf2c --- /dev/null +++ b/src/polymarket/units.test.ts @@ -0,0 +1,22 @@ +import { describe, it, expect } from "vitest"; +import { usdToBaseUnits } from "./units.js"; + +describe("usdToBaseUnits", () => { + it("does not lose a base unit to float error (2.01 USDC = 2_010_000)", () => { + // 2.01 * 1e6 === 2009999.9999999998 in IEEE-754; Math.floor gave 2_009_999. + expect(usdToBaseUnits(2.01, 6)).toBe(2_010_000n); + expect(usdToBaseUnits(4.02, 6)).toBe(4_020_000n); + expect(usdToBaseUnits(8.19, 6)).toBe(8_190_000n); + }); + + it("is exact for every two-decimal amount from $0.01 to $100", () => { + for (let cents = 1; cents <= 10_000; cents++) { + expect(usdToBaseUnits(cents / 100, 6)).toBe(BigInt(cents) * 10_000n); + } + }); + + it("keeps sub-cent precision", () => { + expect(usdToBaseUnits(0.000001, 6)).toBe(1n); + expect(usdToBaseUnits(1.234567, 6)).toBe(1_234_567n); + }); +}); diff --git a/src/polymarket/units.ts b/src/polymarket/units.ts new file mode 100644 index 00000000..0c395ccc --- /dev/null +++ b/src/polymarket/units.ts @@ -0,0 +1,12 @@ +// src/polymarket/units.ts +// +// Dollar amounts arrive from the agent as JS numbers (e.g. 2.01). Multiplying +// by 10 ** decimals is not exact in IEEE-754 (2.01 * 1e6 === 2009999.9999999998), +// so Math.floor() silently drops one base unit for ~1.5% of two-decimal +// amounts. Round to the nearest base unit instead: the caller meant the +// decimal value they typed, not the float just below it. + +/** Convert a dollar amount to integer token base units (6 decimals for USDC/pUSD). */ +export function usdToBaseUnits(usd: number, decimals: number): bigint { + return BigInt(Math.round(usd * 10 ** decimals)); +} diff --git a/src/polymarket/withdraw.ts b/src/polymarket/withdraw.ts index fb425c4e..84c72dd5 100644 --- a/src/polymarket/withdraw.ts +++ b/src/polymarket/withdraw.ts @@ -32,6 +32,7 @@ import { getFundsAddress } from "./positions.js"; import { sendWalletBatch } from "./relayer.js"; import { getPublicClient } from "./setup.js"; import { signUnderSpendPolicy, type PolymarketSpendDeps } from "./spend-policy.js"; +import { usdToBaseUnits } from "./units.js"; async function rawPusdBalance(owner: Hex): Promise { return getPublicClient().readContract({ @@ -89,9 +90,7 @@ export async function withdrawFunds( }; } const amountRaw = - input.amount_usd !== undefined - ? BigInt(Math.floor(input.amount_usd * 10 ** PUSD_DECIMALS)) - : balanceRaw; + input.amount_usd !== undefined ? usdToBaseUnits(input.amount_usd, PUSD_DECIMALS) : balanceRaw; if (amountRaw > balanceRaw) { return { text: `Requested $${input.amount_usd} exceeds the pUSD balance of $${balanceUsd.toFixed(2)}.`,