Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/polymarket/fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions src/polymarket/units.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
12 changes: 12 additions & 0 deletions src/polymarket/units.ts
Original file line number Diff line number Diff line change
@@ -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));
}
5 changes: 2 additions & 3 deletions src/polymarket/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<bigint> {
return getPublicClient().readContract({
Expand Down Expand Up @@ -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)}.`,
Expand Down