Skip to content
Merged
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
11 changes: 11 additions & 0 deletions agent/server/dryRun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ describe("dry-run tools", () => {
expect(quotes).toEqual([]);
});

it("the buys of one message together stay within the practice limit per day", async () => {
const { tools, quotes, record } = setup();
const buy = async (usd: string) => (JSON.parse(await tools.run("buy_now", { asset: "bitcoin", usd, reason: "asked" }, [])) as { status: string }).status;
expect(await buy("20")).toBe("dry_run");
expect(await buy("20")).toBe("dry_run");
expect(await buy("20")).toBe("refused");
expect(quotes).toHaveLength(2);
expect(record.log.at(-1)?.answer).toEqual({ status: "refused", code: "over_day_limit" });
expect(await buy("10")).toBe("dry_run");
});

it("asks the trading service for at most a few quotes per message", async () => {
const { tools, quotes } = setup();
for (let i = 0; i < QUOTES_PER_MESSAGE; i++) await tools.run("get_quote", { asset: "bitcoin", usd: "5" }, []);
Expand Down
8 changes: 8 additions & 0 deletions agent/server/dryRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ const answerOf = (q: SwapQuote): Record<string, string> => ({

/** The tools of one dry-run message; every call is written to `record` for the app to show. */
export function dryRunTools(deps: DryRunDeps, record: DryRunRecord): Toolset & { prompt: string } {
// Each message starts a fresh practice day: its buys together stay within the daily limit.
let spentMicro = 0n;
const prices = async () => {
const list = await deps.tokens();
const byId = new Map(list.map((t) => [t.asset_id, t]));
Expand Down Expand Up @@ -194,10 +196,16 @@ export function dryRunTools(deps: DryRunDeps, record: DryRunRecord): Toolset & {
events.push({ type: "swap_denied", from: "NEAR", amountIn: input.usd, code: "over_order_limit", reason: "above the practice limit per order" });
return JSON.stringify({ status: "refused", reason: `the amount is above the practice limit of ${PRACTICE_PER_ORDER_USD} USD per order` });
}
if (name === "buy_now" && spentMicro + usdStringToMicro(input.usd) > usdStringToMicro(PRACTICE_PER_DAY_USD)) {
record.log.push({ tool: name, asked, request: null, answer: { status: "refused", code: "over_day_limit" } });
events.push({ type: "swap_denied", from: "NEAR", amountIn: input.usd, code: "over_day_limit", reason: "above the practice limit per day" });
return JSON.stringify({ status: "refused", reason: `together with the earlier buys this is above the practice limit of ${PRACTICE_PER_DAY_USD} USD per day` });
}
const q = await quote(name, asked, a.assetId, wnear, token, nearMicro);
if (!q) return JSON.stringify({ status: "refused", reason: "there is no way to trade this right now, often because the amount is too small for this asset's market" });
const view = quoteView(input.usd, wnear, q, token.decimals, priceToMicro(token.price ?? 0));
if (name === "get_quote") return JSON.stringify({ status: "quoted", asset: a.name, ...view });
spentMicro += usdStringToMicro(input.usd);
const kept = staysOnAgent(a.assetId);
record.steps = [
{ place: "wallet", text: `${view.pay_near} NEAR (${input.usd} USD) waits in your wallet, set aside for orders. Your Spending Limits contract caps each order and each day.` },
Expand Down
Loading