visionclaw 0.1.220-dev.fix-linux-rdp-openbox.1 → 0.1.220-dev.fix-linux-rdp-openbox.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/engines/claude/session.d.ts.map +1 -1
- package/dist/agent/engines/claude/session.js +1 -0
- package/dist/agent/engines/claude/session.js.map +1 -1
- package/dist/agent/engines/pi/tools.d.ts +2 -0
- package/dist/agent/engines/pi/tools.d.ts.map +1 -1
- package/dist/agent/engines/pi/tools.js +3 -0
- package/dist/agent/engines/pi/tools.js.map +1 -1
- package/dist/agent/engines/pi-coding/session.d.ts.map +1 -1
- package/dist/agent/engines/pi-coding/session.js +1 -0
- package/dist/agent/engines/pi-coding/session.js.map +1 -1
- package/dist/agent/engines/pi-coding/tools.d.ts +1 -0
- package/dist/agent/engines/pi-coding/tools.d.ts.map +1 -1
- package/dist/agent/engines/pi-coding/tools.js.map +1 -1
- package/dist/agent/runtime-surface.js +2 -2
- package/dist/agent/runtime-surface.js.map +1 -1
- package/dist/builtin-skills/rethinka-fund/SKILL.md +120 -5
- package/dist/builtin-skills/rethinka-fund/core-contract.md +25 -23
- package/dist/channels/rethinka-fund.d.ts +6 -2
- package/dist/channels/rethinka-fund.d.ts.map +1 -1
- package/dist/channels/rethinka-fund.js +125 -14
- package/dist/channels/rethinka-fund.js.map +1 -1
- package/dist/rethinka-fund/hyperliquid-signing.d.ts +56 -0
- package/dist/rethinka-fund/hyperliquid-signing.d.ts.map +1 -0
- package/dist/rethinka-fund/hyperliquid-signing.js +159 -0
- package/dist/rethinka-fund/hyperliquid-signing.js.map +1 -0
- package/dist/rethinka-fund/hyperliquid-trading.d.ts +131 -0
- package/dist/rethinka-fund/hyperliquid-trading.d.ts.map +1 -0
- package/dist/rethinka-fund/hyperliquid-trading.js +516 -0
- package/dist/rethinka-fund/hyperliquid-trading.js.map +1 -0
- package/dist/rethinka-fund/polymarket-trading.d.ts +125 -0
- package/dist/rethinka-fund/polymarket-trading.d.ts.map +1 -0
- package/dist/rethinka-fund/polymarket-trading.js +374 -0
- package/dist/rethinka-fund/polymarket-trading.js.map +1 -0
- package/dist/rethinka-fund/trading-common.d.ts +82 -0
- package/dist/rethinka-fund/trading-common.d.ts.map +1 -0
- package/dist/rethinka-fund/trading-common.js +87 -0
- package/dist/rethinka-fund/trading-common.js.map +1 -0
- package/dist/service/systemd.d.ts +0 -10
- package/dist/service/systemd.d.ts.map +1 -1
- package/dist/service/systemd.js +2 -49
- package/dist/service/systemd.js.map +1 -1
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +2 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/rethinka-fund-trading.d.ts +75 -0
- package/dist/tools/rethinka-fund-trading.d.ts.map +1 -0
- package/dist/tools/rethinka-fund-trading.js +60 -0
- package/dist/tools/rethinka-fund-trading.js.map +1 -0
- package/dist-agent/bundle.cjs +2 -2
- package/package.json +3 -1
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Polymarket order placement for the Rethinka Fund agent.
|
|
3
|
+
*
|
|
4
|
+
* This is deterministic code that ships with the agent. The model calls a
|
|
5
|
+
* tool with a decision id, a price and a size; everything else happens here:
|
|
6
|
+
*
|
|
7
|
+
* 1. The decision is read from the Rethinka Fund server, which also says
|
|
8
|
+
* whether an order may be placed under it now (the owner's Fully Auto
|
|
9
|
+
* switch, an approval, the size still unused). No decision, no order.
|
|
10
|
+
* 2. The order is checked against the decision: same instrument and side,
|
|
11
|
+
* at or inside its limit price, within what is left of its size.
|
|
12
|
+
* 3. The order is signed on this host with the wallet key from a local
|
|
13
|
+
* settings file. The key is never sent anywhere and never returned to
|
|
14
|
+
* the model.
|
|
15
|
+
* 4. The signed order goes to Polymarket through poly-service, which applies
|
|
16
|
+
* its own per-order limit and geoblock check.
|
|
17
|
+
* 5. What happened is reported back to the Rethinka Fund server, before and
|
|
18
|
+
* after submission, so an order whose fate is unknown still counts
|
|
19
|
+
* against the decision. The server takes the first report only while an
|
|
20
|
+
* order may be placed and it fits in the decision's size; if it says no,
|
|
21
|
+
* nothing is signed.
|
|
22
|
+
*
|
|
23
|
+
* The settings file is written by the owner on this host, not through chat.
|
|
24
|
+
* It is off until that file exists.
|
|
25
|
+
*/
|
|
26
|
+
import { z } from "zod";
|
|
27
|
+
import { type FetchLike, type FundAccess, type KeyFileRead, type TradingResult } from "./trading-common.js";
|
|
28
|
+
export declare const POLYMARKET_SETTINGS_FILE = "rethinka-fund-polymarket.json";
|
|
29
|
+
export declare const PolymarketSettingsSchema: z.ZodObject<{
|
|
30
|
+
privateKey: z.ZodString;
|
|
31
|
+
wallet: z.ZodString;
|
|
32
|
+
polyServiceUrl: z.ZodString;
|
|
33
|
+
polyServiceKey: z.ZodString;
|
|
34
|
+
builderCode: z.ZodOptional<z.ZodString>;
|
|
35
|
+
}, z.core.$strict>;
|
|
36
|
+
export type PolymarketSettings = z.infer<typeof PolymarketSettingsSchema>;
|
|
37
|
+
export declare function polymarketSettingsPath(): string;
|
|
38
|
+
export type PolymarketSettingsRead = KeyFileRead<PolymarketSettings>;
|
|
39
|
+
/** Reads the settings. A failure names the fields at fault, never their values. */
|
|
40
|
+
export declare function readPolymarketSettings(file?: string): PolymarketSettingsRead;
|
|
41
|
+
export type { FetchLike, TradingResult } from "./trading-common.js";
|
|
42
|
+
/** The order was definitely not placed: poly-service or the exchange said no. */
|
|
43
|
+
export declare class OrderRefusedError extends Error {
|
|
44
|
+
constructor(message: string, options?: {
|
|
45
|
+
cause?: unknown;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Does poly-service's error reply to `/v1/orders` prove the order is not live?
|
|
50
|
+
*
|
|
51
|
+
* The HTTP status cannot say: poly-service answers 502 both for an order the
|
|
52
|
+
* exchange turned down and for one it sent whose answer never came back. Its
|
|
53
|
+
* body can. An order is refused when poly-service never forwarded it, or
|
|
54
|
+
* forwarded it and the exchange answered with a client error. A timeout, an
|
|
55
|
+
* exchange 5xx, a duplicate of an earlier attempt, or a reply that cannot be
|
|
56
|
+
* read leaves the order's fate unknown.
|
|
57
|
+
*/
|
|
58
|
+
export declare function orderReplyProvesRefusal(status: number, body: unknown): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* A fetch for the Polymarket client that sends every authenticated exchange
|
|
61
|
+
* request through poly-service instead of straight to the exchange. The
|
|
62
|
+
* client still signs each request here; poly-service relays the exact body and
|
|
63
|
+
* headers, after its own checks. Public reads and other hosts pass through.
|
|
64
|
+
*/
|
|
65
|
+
export declare function createPolyServiceFetch(options: {
|
|
66
|
+
baseUrl: string;
|
|
67
|
+
key: string;
|
|
68
|
+
fetchImpl?: FetchLike;
|
|
69
|
+
}): FetchLike;
|
|
70
|
+
/** The part of the Polymarket client this module uses. */
|
|
71
|
+
export interface PolymarketClient {
|
|
72
|
+
createLimitOrder(request: {
|
|
73
|
+
assetId: string;
|
|
74
|
+
price: number;
|
|
75
|
+
size: number;
|
|
76
|
+
side: "BUY" | "SELL";
|
|
77
|
+
builderCode?: string;
|
|
78
|
+
}): Promise<unknown>;
|
|
79
|
+
postOrder(order: unknown): Promise<unknown>;
|
|
80
|
+
cancelOrder(request: {
|
|
81
|
+
orderId: string;
|
|
82
|
+
}): Promise<unknown>;
|
|
83
|
+
listOpenOrders(): {
|
|
84
|
+
firstPage(): Promise<{
|
|
85
|
+
items: unknown[];
|
|
86
|
+
hasMore?: boolean;
|
|
87
|
+
}>;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export type PolymarketClientFactory = (settings: PolymarketSettings) => Promise<PolymarketClient>;
|
|
91
|
+
/**
|
|
92
|
+
* One signed-in client per wallet key for the life of the process, so the
|
|
93
|
+
* exchange API credentials are created once and reused. The libraries are
|
|
94
|
+
* loaded on first use: no other agent pays for them.
|
|
95
|
+
*/
|
|
96
|
+
export declare const defaultPolymarketClient: PolymarketClientFactory;
|
|
97
|
+
/**
|
|
98
|
+
* The builder code poly-service requires on every order it relays, asked from
|
|
99
|
+
* poly-service itself so the owner does not have to copy it into the settings
|
|
100
|
+
* file. Null when it has none. A failed lookup is not remembered.
|
|
101
|
+
*/
|
|
102
|
+
export declare function lookupBuilderCode(settings: PolymarketSettings, fetchImpl?: FetchLike): Promise<string | null>;
|
|
103
|
+
export interface TradingDeps extends FundAccess {
|
|
104
|
+
/** Override for the builder-code lookup; tests only. */
|
|
105
|
+
builderCode?: (settings: PolymarketSettings) => Promise<string | null>;
|
|
106
|
+
settingsFile?: string;
|
|
107
|
+
client?: PolymarketClientFactory;
|
|
108
|
+
newId?: () => string;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Place one limit order under a recorded decision. Never throws: every outcome
|
|
112
|
+
* is a result the model can read, with the reason when nothing was placed.
|
|
113
|
+
*/
|
|
114
|
+
export declare function placePolymarketOrder(input: {
|
|
115
|
+
decisionId: number;
|
|
116
|
+
price: number;
|
|
117
|
+
size: number;
|
|
118
|
+
}, deps: TradingDeps): Promise<TradingResult>;
|
|
119
|
+
export declare function cancelPolymarketOrder(input: {
|
|
120
|
+
orderId: string;
|
|
121
|
+
}, deps: TradingDeps): Promise<TradingResult>;
|
|
122
|
+
export declare function listPolymarketOpenOrders(deps: TradingDeps): Promise<TradingResult>;
|
|
123
|
+
/** Test-only. */
|
|
124
|
+
export declare function resetPolymarketClientsForTest(): void;
|
|
125
|
+
//# sourceMappingURL=polymarket-trading.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polymarket-trading.d.ts","sourceRoot":"","sources":["../../src/rethinka-fund/polymarket-trading.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAKH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAgE,KAAK,SAAS,EAAE,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,KAAK,aAAa,EAAE,MAA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E,WAAW,GAAG,OAAO,CAAC,aAAa,CAAC,CAUxF;AAED,iBAAiB;AACjB,wBAAgB,6BAA6B,IAAI,IAAI,CAGpD"}
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Polymarket order placement for the Rethinka Fund agent.
|
|
3
|
+
*
|
|
4
|
+
* This is deterministic code that ships with the agent. The model calls a
|
|
5
|
+
* tool with a decision id, a price and a size; everything else happens here:
|
|
6
|
+
*
|
|
7
|
+
* 1. The decision is read from the Rethinka Fund server, which also says
|
|
8
|
+
* whether an order may be placed under it now (the owner's Fully Auto
|
|
9
|
+
* switch, an approval, the size still unused). No decision, no order.
|
|
10
|
+
* 2. The order is checked against the decision: same instrument and side,
|
|
11
|
+
* at or inside its limit price, within what is left of its size.
|
|
12
|
+
* 3. The order is signed on this host with the wallet key from a local
|
|
13
|
+
* settings file. The key is never sent anywhere and never returned to
|
|
14
|
+
* the model.
|
|
15
|
+
* 4. The signed order goes to Polymarket through poly-service, which applies
|
|
16
|
+
* its own per-order limit and geoblock check.
|
|
17
|
+
* 5. What happened is reported back to the Rethinka Fund server, before and
|
|
18
|
+
* after submission, so an order whose fate is unknown still counts
|
|
19
|
+
* against the decision. The server takes the first report only while an
|
|
20
|
+
* order may be placed and it fits in the decision's size; if it says no,
|
|
21
|
+
* nothing is signed.
|
|
22
|
+
*
|
|
23
|
+
* The settings file is written by the owner on this host, not through chat.
|
|
24
|
+
* It is off until that file exists.
|
|
25
|
+
*/
|
|
26
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
27
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
28
|
+
import path from "node:path";
|
|
29
|
+
import { z } from "zod";
|
|
30
|
+
import { getSharedProfileDir } from "../config/index.js";
|
|
31
|
+
import { errorText as message, readDecision, readKeyFile, reportOrder } from "./trading-common.js";
|
|
32
|
+
export const POLYMARKET_SETTINGS_FILE = "rethinka-fund-polymarket.json";
|
|
33
|
+
const CLOB_HOST = "clob.polymarket.com";
|
|
34
|
+
export const PolymarketSettingsSchema = z.object({
|
|
35
|
+
/** The signing key of the dedicated wallet. Read by this module only. */
|
|
36
|
+
privateKey: z.string().trim().regex(/^0x[0-9a-fA-F]{64}$/),
|
|
37
|
+
/**
|
|
38
|
+
* The Polymarket account the orders trade for: the address added in the
|
|
39
|
+
* console. The same as the key's own address for a plain wallet; the proxy
|
|
40
|
+
* or deposit wallet address when the account was created on polymarket.com.
|
|
41
|
+
*/
|
|
42
|
+
wallet: z.string().trim().regex(/^0x[0-9a-fA-F]{40}$/),
|
|
43
|
+
polyServiceUrl: z.string().trim().regex(/^https:\/\/[^\s/?#]+(?::\d+)?(?:\/[^\s?#]*)?$/),
|
|
44
|
+
polyServiceKey: z.string().trim().regex(/^poly_[!-~]+$/),
|
|
45
|
+
/** The builder code poly-service requires on orders. Optional: it is asked from poly-service when left out. */
|
|
46
|
+
builderCode: z.string().trim().regex(/^0x[0-9a-fA-F]{64}$/).optional(),
|
|
47
|
+
}).strict();
|
|
48
|
+
export function polymarketSettingsPath() {
|
|
49
|
+
return path.join(getSharedProfileDir(), POLYMARKET_SETTINGS_FILE);
|
|
50
|
+
}
|
|
51
|
+
/** Reads the settings. A failure names the fields at fault, never their values. */
|
|
52
|
+
export function readPolymarketSettings(file = polymarketSettingsPath()) {
|
|
53
|
+
return readKeyFile(file, PolymarketSettingsSchema);
|
|
54
|
+
}
|
|
55
|
+
function jsonResponse(status, body) {
|
|
56
|
+
return new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
|
|
57
|
+
}
|
|
58
|
+
/** USD at stake in a signed order body: what a buy pays, what a sell receives. */
|
|
59
|
+
function orderNotionalUsd(body) {
|
|
60
|
+
try {
|
|
61
|
+
const order = JSON.parse(body).order ?? {};
|
|
62
|
+
const units = Number(order.side === "BUY" ? order.makerAmount : order.takerAmount);
|
|
63
|
+
return Number.isFinite(units) && units > 0 ? units / 1_000_000 : 0;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return 0;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Carries the trace from `postOrder` down to the fetch the client library calls. */
|
|
70
|
+
const orderTrace = new AsyncLocalStorage();
|
|
71
|
+
/** The order was definitely not placed: poly-service or the exchange said no. */
|
|
72
|
+
export class OrderRefusedError extends Error {
|
|
73
|
+
constructor(message, options) {
|
|
74
|
+
super(message, options);
|
|
75
|
+
this.name = "OrderRefusedError";
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Does poly-service's error reply to `/v1/orders` prove the order is not live?
|
|
80
|
+
*
|
|
81
|
+
* The HTTP status cannot say: poly-service answers 502 both for an order the
|
|
82
|
+
* exchange turned down and for one it sent whose answer never came back. Its
|
|
83
|
+
* body can. An order is refused when poly-service never forwarded it, or
|
|
84
|
+
* forwarded it and the exchange answered with a client error. A timeout, an
|
|
85
|
+
* exchange 5xx, a duplicate of an earlier attempt, or a reply that cannot be
|
|
86
|
+
* read leaves the order's fate unknown.
|
|
87
|
+
*/
|
|
88
|
+
export function orderReplyProvesRefusal(status, body) {
|
|
89
|
+
const error = body?.error;
|
|
90
|
+
if (error?.code === "ORDER_ATTEMPT_RECORDED") {
|
|
91
|
+
if (error.forwarded === false)
|
|
92
|
+
return true;
|
|
93
|
+
const reason = typeof error.attempt?.reasonCode === "string" ? error.attempt.reasonCode : "";
|
|
94
|
+
// 408 is a timeout on the exchange's side: the order may have been taken.
|
|
95
|
+
return error.forwarded === true && error.status === "rejected_remote" && /^CLOB_HTTP_4\d\d$/.test(reason) && reason !== "CLOB_HTTP_408";
|
|
96
|
+
}
|
|
97
|
+
// Turned away by poly-service before it recorded an attempt: bad request, bad key, rate limit.
|
|
98
|
+
return status >= 400 && status < 500 && status !== 408;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* A fetch for the Polymarket client that sends every authenticated exchange
|
|
102
|
+
* request through poly-service instead of straight to the exchange. The
|
|
103
|
+
* client still signs each request here; poly-service relays the exact body and
|
|
104
|
+
* headers, after its own checks. Public reads and other hosts pass through.
|
|
105
|
+
*/
|
|
106
|
+
export function createPolyServiceFetch(options) {
|
|
107
|
+
const direct = options.fetchImpl ?? fetch;
|
|
108
|
+
const base = options.baseUrl.replace(/\/+$/, "");
|
|
109
|
+
const relay = async (route, payload) => {
|
|
110
|
+
const reply = await direct(`${base}${route}`, {
|
|
111
|
+
method: "POST",
|
|
112
|
+
headers: { "content-type": "application/json", authorization: `Bearer ${options.key}` },
|
|
113
|
+
body: JSON.stringify(payload),
|
|
114
|
+
signal: AbortSignal.timeout(30_000),
|
|
115
|
+
});
|
|
116
|
+
const body = (await reply.json().catch(() => ({})));
|
|
117
|
+
if (route === "/v1/orders") {
|
|
118
|
+
const trace = orderTrace.getStore();
|
|
119
|
+
if (trace)
|
|
120
|
+
trace.outcome = reply.ok ? "accepted" : orderReplyProvesRefusal(reply.status, body) ? "refused" : "unknown";
|
|
121
|
+
}
|
|
122
|
+
if (reply.ok)
|
|
123
|
+
return jsonResponse(200, body);
|
|
124
|
+
// The client reads an exchange-style error; keep poly-service's reason in it.
|
|
125
|
+
// A 5xx is handed over as 400 so the client does not send the request
|
|
126
|
+
// again. That status says nothing about whether an order went in: the
|
|
127
|
+
// order's outcome is the trace above, never the client's error type.
|
|
128
|
+
const message = typeof body.error?.message === "string" ? body.error.message : `poly-service returned HTTP ${reply.status}`;
|
|
129
|
+
return jsonResponse(reply.status >= 500 ? 400 : reply.status, { error: message });
|
|
130
|
+
};
|
|
131
|
+
return async (input, init) => {
|
|
132
|
+
const request = input instanceof Request ? input : new Request(input, init);
|
|
133
|
+
const url = new URL(request.url);
|
|
134
|
+
const header = (name) => request.headers.get(name) ?? "";
|
|
135
|
+
const signature = header("poly_signature");
|
|
136
|
+
if (url.hostname !== CLOB_HOST || !signature)
|
|
137
|
+
return await direct(request);
|
|
138
|
+
const apiKey = header("poly_api_key");
|
|
139
|
+
if (!apiKey) {
|
|
140
|
+
// Wallet-signed sign-in: create (POST) or re-derive (GET) the API credentials.
|
|
141
|
+
return await relay("/v1/clob/auth", {
|
|
142
|
+
poly_address: header("poly_address"),
|
|
143
|
+
signature,
|
|
144
|
+
timestamp: header("poly_timestamp"),
|
|
145
|
+
nonce: header("poly_nonce") || "0",
|
|
146
|
+
derive: request.method === "GET",
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const body = request.method === "GET" ? "" : await request.text();
|
|
150
|
+
const credentials = { poly_address: header("poly_address"), api_key: apiKey, passphrase: header("poly_passphrase"), signature, timestamp: header("poly_timestamp") };
|
|
151
|
+
if (request.method === "POST" && url.pathname === "/order") {
|
|
152
|
+
return await relay("/v1/orders", {
|
|
153
|
+
...credentials,
|
|
154
|
+
body,
|
|
155
|
+
// poly-service's idempotency key: one signed body is submitted once.
|
|
156
|
+
order_hash: `0x${createHash("sha256").update(body).digest("hex")}`,
|
|
157
|
+
estimated_usd: orderNotionalUsd(body),
|
|
158
|
+
confirm: true,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return await relay("/v1/clob/request", {
|
|
162
|
+
...credentials,
|
|
163
|
+
method: request.method,
|
|
164
|
+
path: `${url.pathname}${url.search}`,
|
|
165
|
+
body,
|
|
166
|
+
...(request.method === "GET" ? {} : { confirm: true }),
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const clients = new Map();
|
|
171
|
+
/**
|
|
172
|
+
* One signed-in client per wallet key for the life of the process, so the
|
|
173
|
+
* exchange API credentials are created once and reused. The libraries are
|
|
174
|
+
* loaded on first use: no other agent pays for them.
|
|
175
|
+
*/
|
|
176
|
+
export const defaultPolymarketClient = (settings) => {
|
|
177
|
+
const cacheKey = createHash("sha256").update(JSON.stringify(settings)).digest("hex");
|
|
178
|
+
let client = clients.get(cacheKey);
|
|
179
|
+
if (!client) {
|
|
180
|
+
client = (async () => {
|
|
181
|
+
const [{ createSecureClient }, { Address, PersonalMessage, Secp256k1, Signature, TypedData }] = await Promise.all([import("@polymarket/client"), import("ox")]);
|
|
182
|
+
const privateKey = settings.privateKey;
|
|
183
|
+
// Checksummed, the form the exchange and the console show the address in.
|
|
184
|
+
const address = Address.checksum(Address.fromPublicKey(Secp256k1.getPublicKey({ privateKey })));
|
|
185
|
+
const sign = (payload) => Signature.toHex(Secp256k1.sign({ payload, privateKey }));
|
|
186
|
+
const signer = {
|
|
187
|
+
getAddress: () => Promise.resolve(address),
|
|
188
|
+
signTypedData: (payload) => Promise.resolve(sign(TypedData.getSignPayload(payload))),
|
|
189
|
+
signMessage: (message) => Promise.resolve(sign(PersonalMessage.getSignPayload(message))),
|
|
190
|
+
// Placing and cancelling orders sends no chain transaction. Anything that would is not this tool's job.
|
|
191
|
+
sendTransaction: () => Promise.reject(new Error("this tool does not send chain transactions")),
|
|
192
|
+
};
|
|
193
|
+
const secure = await createSecureClient({
|
|
194
|
+
signer: signer,
|
|
195
|
+
wallet: settings.wallet,
|
|
196
|
+
fetch: createPolyServiceFetch({ baseUrl: settings.polyServiceUrl, key: settings.polyServiceKey }),
|
|
197
|
+
});
|
|
198
|
+
const sdk = secure;
|
|
199
|
+
return {
|
|
200
|
+
createLimitOrder: (request) => sdk.createLimitOrder(request),
|
|
201
|
+
cancelOrder: (request) => sdk.cancelOrder(request),
|
|
202
|
+
listOpenOrders: () => sdk.listOpenOrders(),
|
|
203
|
+
postOrder: async (order) => {
|
|
204
|
+
const trace = { outcome: "unknown" };
|
|
205
|
+
try {
|
|
206
|
+
return await orderTrace.run(trace, () => sdk.postOrder(order));
|
|
207
|
+
}
|
|
208
|
+
catch (err) {
|
|
209
|
+
if (trace.outcome === "refused")
|
|
210
|
+
throw new OrderRefusedError(err instanceof Error ? err.message : String(err), { cause: err });
|
|
211
|
+
throw err;
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
})();
|
|
216
|
+
clients.set(cacheKey, client);
|
|
217
|
+
// A failed sign-in must not be remembered: the next call tries again.
|
|
218
|
+
client.catch(() => clients.delete(cacheKey));
|
|
219
|
+
}
|
|
220
|
+
return client;
|
|
221
|
+
};
|
|
222
|
+
const builderCodes = new Map();
|
|
223
|
+
/**
|
|
224
|
+
* The builder code poly-service requires on every order it relays, asked from
|
|
225
|
+
* poly-service itself so the owner does not have to copy it into the settings
|
|
226
|
+
* file. Null when it has none. A failed lookup is not remembered.
|
|
227
|
+
*/
|
|
228
|
+
export async function lookupBuilderCode(settings, fetchImpl = fetch) {
|
|
229
|
+
const cached = builderCodes.get(settings.polyServiceUrl);
|
|
230
|
+
if (cached !== undefined)
|
|
231
|
+
return cached;
|
|
232
|
+
const reply = await fetchImpl(`${settings.polyServiceUrl.replace(/\/+$/, "")}/v1/builder/status`, {
|
|
233
|
+
headers: { authorization: `Bearer ${settings.polyServiceKey}` },
|
|
234
|
+
signal: AbortSignal.timeout(15_000),
|
|
235
|
+
});
|
|
236
|
+
if (!reply.ok)
|
|
237
|
+
throw new Error(`poly-service /v1/builder/status returned HTTP ${reply.status}`);
|
|
238
|
+
const code = (await reply.json()).builderCode;
|
|
239
|
+
const value = typeof code === "string" && /^0x[0-9a-fA-F]{64}$/.test(code) ? code : null;
|
|
240
|
+
builderCodes.set(settings.polyServiceUrl, value);
|
|
241
|
+
return value;
|
|
242
|
+
}
|
|
243
|
+
function settingsOrReason(deps) {
|
|
244
|
+
const read = readPolymarketSettings(deps.settingsFile);
|
|
245
|
+
if (read.status === "absent")
|
|
246
|
+
return { reason: `Polymarket trading is not set up on this agent: the owner has not created ${deps.settingsFile ?? polymarketSettingsPath()} on this host yet.` };
|
|
247
|
+
if (read.status === "invalid")
|
|
248
|
+
return { reason: `Polymarket trading settings are not usable: ${read.reason}` };
|
|
249
|
+
return { settings: read.settings };
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* True only when the order is known not to be live. Any other failure of a
|
|
253
|
+
* submission, whatever the client library calls it, leaves the order's fate
|
|
254
|
+
* unknown: it may be resting on the exchange.
|
|
255
|
+
*/
|
|
256
|
+
function wasRefused(err) {
|
|
257
|
+
return err instanceof OrderRefusedError;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Place one limit order under a recorded decision. Never throws: every outcome
|
|
261
|
+
* is a result the model can read, with the reason when nothing was placed.
|
|
262
|
+
*/
|
|
263
|
+
export async function placePolymarketOrder(input, deps) {
|
|
264
|
+
const configured = settingsOrReason(deps);
|
|
265
|
+
if ("reason" in configured)
|
|
266
|
+
return { ok: false, reason: configured.reason };
|
|
267
|
+
const read = await readDecision(deps, input.decisionId, "polymarket");
|
|
268
|
+
if ("reason" in read)
|
|
269
|
+
return { ok: false, reason: read.reason, ...read.extra };
|
|
270
|
+
const { permission } = read;
|
|
271
|
+
const { decision } = permission;
|
|
272
|
+
const side = decision.action === "sell" ? "SELL" : "BUY";
|
|
273
|
+
const limit = Number(decision.limit_price);
|
|
274
|
+
if (!Number.isFinite(limit) || limit <= 0)
|
|
275
|
+
return { ok: false, reason: `Decision #${decision.id} has no limit price.` };
|
|
276
|
+
if (!(input.price > 0 && input.price < 1))
|
|
277
|
+
return { ok: false, reason: "A Polymarket price is between 0 and 1, exclusive." };
|
|
278
|
+
if (!(input.size > 0))
|
|
279
|
+
return { ok: false, reason: "The size must be a positive number of shares." };
|
|
280
|
+
if (side === "BUY" ? input.price > limit : input.price < limit) {
|
|
281
|
+
return { ok: false, reason: `Price ${input.price} is worse than the decision's limit of ${limit} for a ${decision.action}. Record a new decision if the plan changed.` };
|
|
282
|
+
}
|
|
283
|
+
const notional = Number((input.price * input.size).toFixed(6));
|
|
284
|
+
if (notional > permission.remaining_usd + 1e-9) {
|
|
285
|
+
return { ok: false, reason: `This order is $${notional}, but only $${permission.remaining_usd} of decision #${decision.id}'s size is left.` };
|
|
286
|
+
}
|
|
287
|
+
// Sign-in and the builder code come first: a failure here has sent nothing.
|
|
288
|
+
let client;
|
|
289
|
+
let builderCode;
|
|
290
|
+
try {
|
|
291
|
+
client = await (deps.client ?? defaultPolymarketClient)(configured.settings);
|
|
292
|
+
builderCode = configured.settings.builderCode ?? (await (deps.builderCode ?? lookupBuilderCode)(configured.settings));
|
|
293
|
+
}
|
|
294
|
+
catch (err) {
|
|
295
|
+
return { ok: false, reason: `Could not get ready to trade, and nothing was placed: ${message(err)}` };
|
|
296
|
+
}
|
|
297
|
+
const clientId = (deps.newId ?? randomUUID)();
|
|
298
|
+
const report = (status, detail, venueOrderId) => reportOrder(deps, decision.id, { client_id: clientId, venue_order_id: venueOrderId, side: decision.action === "sell" ? "sell" : "buy", price: input.price, size: input.size, notional_usd: notional, status, detail });
|
|
299
|
+
// Recorded before anything is signed: if this process dies mid-submission,
|
|
300
|
+
// the order still counts against the decision and the owner sees it.
|
|
301
|
+
const preReport = await report("unknown", "submitting", null);
|
|
302
|
+
if (preReport !== null)
|
|
303
|
+
return { ok: false, reason: `The Rethinka Fund server did not take this order (${preReport}), so it was not placed.` };
|
|
304
|
+
let signed;
|
|
305
|
+
try {
|
|
306
|
+
signed = await client.createLimitOrder({ assetId: decision.symbol, price: input.price, size: input.size, side, ...(builderCode ? { builderCode } : {}) });
|
|
307
|
+
}
|
|
308
|
+
catch (err) {
|
|
309
|
+
// Building and signing happen here; nothing has been submitted yet.
|
|
310
|
+
await report("rejected", `not submitted: ${message(err)}`, null);
|
|
311
|
+
return { ok: false, reason: `The order could not be built, and nothing was placed: ${message(err)}` };
|
|
312
|
+
}
|
|
313
|
+
let response;
|
|
314
|
+
try {
|
|
315
|
+
response = ((await client.postOrder(signed)) ?? {});
|
|
316
|
+
}
|
|
317
|
+
catch (err) {
|
|
318
|
+
const refused = wasRefused(err);
|
|
319
|
+
await report(refused ? "rejected" : "unknown", message(err), null);
|
|
320
|
+
return refused
|
|
321
|
+
? { ok: false, reason: `The order was refused and nothing was placed: ${message(err)}` }
|
|
322
|
+
: { ok: false, reason: `The order's fate is unknown: ${message(err)}. It may or may not be live. Check open orders and the account before doing anything else; do not simply retry.`, unknown: true };
|
|
323
|
+
}
|
|
324
|
+
const orderId = typeof response.orderID === "string" ? response.orderID : typeof response.orderId === "string" ? response.orderId : null;
|
|
325
|
+
const accepted = response.success !== false && response.ok !== false && orderId !== null;
|
|
326
|
+
const venueStatus = typeof response.status === "string" ? response.status : "";
|
|
327
|
+
const detail = accepted ? venueStatus : typeof response.errorMsg === "string" && response.errorMsg ? response.errorMsg : JSON.stringify(response).slice(0, 300);
|
|
328
|
+
const reportError = await report(accepted ? "submitted" : "rejected", detail, orderId);
|
|
329
|
+
if (!accepted)
|
|
330
|
+
return { ok: false, reason: `Polymarket did not accept the order: ${detail}` };
|
|
331
|
+
return {
|
|
332
|
+
ok: true,
|
|
333
|
+
order_id: orderId,
|
|
334
|
+
venue_status: venueStatus,
|
|
335
|
+
decision_id: decision.id,
|
|
336
|
+
side: decision.action,
|
|
337
|
+
price: input.price,
|
|
338
|
+
size: input.size,
|
|
339
|
+
notional_usd: notional,
|
|
340
|
+
note: "The order is placed. A limit order can rest unfilled or fill only in part: read the fills and open orders before assuming a position."
|
|
341
|
+
+ (reportError ? ` The result could not be written back to the Rethinka Fund server (${reportError}); tell the owner.` : ""),
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
export async function cancelPolymarketOrder(input, deps) {
|
|
345
|
+
const configured = settingsOrReason(deps);
|
|
346
|
+
if ("reason" in configured)
|
|
347
|
+
return { ok: false, reason: configured.reason };
|
|
348
|
+
try {
|
|
349
|
+
const client = await (deps.client ?? defaultPolymarketClient)(configured.settings);
|
|
350
|
+
return { ok: true, result: await client.cancelOrder({ orderId: input.orderId }) };
|
|
351
|
+
}
|
|
352
|
+
catch (err) {
|
|
353
|
+
return { ok: false, reason: `The cancel did not go through: ${message(err)}. The order may still be live.` };
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
export async function listPolymarketOpenOrders(deps) {
|
|
357
|
+
const configured = settingsOrReason(deps);
|
|
358
|
+
if ("reason" in configured)
|
|
359
|
+
return { ok: false, reason: configured.reason };
|
|
360
|
+
try {
|
|
361
|
+
const client = await (deps.client ?? defaultPolymarketClient)(configured.settings);
|
|
362
|
+
const page = await client.listOpenOrders().firstPage();
|
|
363
|
+
return { ok: true, wallet: configured.settings.wallet, orders: page.items, more: page.hasMore === true };
|
|
364
|
+
}
|
|
365
|
+
catch (err) {
|
|
366
|
+
return { ok: false, reason: `Open orders could not be read: ${message(err)}` };
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
/** Test-only. */
|
|
370
|
+
export function resetPolymarketClientsForTest() {
|
|
371
|
+
clients.clear();
|
|
372
|
+
builderCodes.clear();
|
|
373
|
+
}
|
|
374
|
+
//# sourceMappingURL=polymarket-trading.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polymarket-trading.js","sourceRoot":"","sources":["../../src/rethinka-fund/polymarket-trading.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,SAAS,IAAI,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAyE,MAAM,qBAAqB,CAAC;AAE1K,MAAM,CAAC,MAAM,wBAAwB,GAAG,+BAA+B,CAAC;AACxE,MAAM,SAAS,GAAG,qBAAqB,CAAC;AAExC,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,CAAC,MAAM,CAAC;IAC/C,yEAAyE;IACzE,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,qBAAqB,CAAC;IAC1D;;;;OAIG;IACH,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAA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,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;IAC3G,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,kCAAkC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;IACjF,CAAC;AACH,CAAC;AAED,iBAAiB;AACjB,MAAM,UAAU,6BAA6B;IAC3C,OAAO,CAAC,KAAK,EAAE,CAAC;IAChB,YAAY,CAAC,KAAK,EAAE,CAAC;AACvB,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the Rethinka Fund agent's trading tools share, whatever the venue:
|
|
3
|
+
* reading a key file the owner placed on this host, and talking to the
|
|
4
|
+
* Rethinka Fund server about the decision an order is placed under.
|
|
5
|
+
*/
|
|
6
|
+
import type { z } from "zod";
|
|
7
|
+
export type FetchLike = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
8
|
+
/** Every tool outcome is a result the model can read, with the reason when nothing was done. */
|
|
9
|
+
export type TradingResult = {
|
|
10
|
+
ok: true;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
reason: string;
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
};
|
|
17
|
+
export type KeyFileRead<T> = {
|
|
18
|
+
status: "absent";
|
|
19
|
+
} | {
|
|
20
|
+
status: "invalid";
|
|
21
|
+
reason: string;
|
|
22
|
+
} | {
|
|
23
|
+
status: "ok";
|
|
24
|
+
settings: T;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Reads a settings file that holds a wallet key. Refused when other accounts
|
|
28
|
+
* on the host can read it. A failure names the fields at fault, never their
|
|
29
|
+
* values.
|
|
30
|
+
*/
|
|
31
|
+
export declare function readKeyFile<T>(file: string, schema: z.ZodType<T>): KeyFileRead<T>;
|
|
32
|
+
export interface FundAccess {
|
|
33
|
+
/** The Rethinka Fund server and this agent's key for it, or null when the agent has none. */
|
|
34
|
+
fund: () => {
|
|
35
|
+
baseUrl: string;
|
|
36
|
+
apiKey: string;
|
|
37
|
+
} | null;
|
|
38
|
+
fetchImpl?: FetchLike;
|
|
39
|
+
}
|
|
40
|
+
/** A decision as the Rethinka Fund server returns it to the trading tool, with whether an order may be placed now. */
|
|
41
|
+
export interface FundDecision {
|
|
42
|
+
decision: {
|
|
43
|
+
id: number;
|
|
44
|
+
venue: string;
|
|
45
|
+
symbol: string;
|
|
46
|
+
title: string;
|
|
47
|
+
action: string;
|
|
48
|
+
reduce_only?: boolean;
|
|
49
|
+
size_usd: string | null;
|
|
50
|
+
limit_price: string | null;
|
|
51
|
+
status: string;
|
|
52
|
+
};
|
|
53
|
+
may_trade: boolean;
|
|
54
|
+
reason: string;
|
|
55
|
+
approval: string;
|
|
56
|
+
remaining_usd: number;
|
|
57
|
+
}
|
|
58
|
+
export declare function fundCall(deps: FundAccess, method: string, route: string, body?: unknown): Promise<{
|
|
59
|
+
status: number;
|
|
60
|
+
body: Record<string, unknown>;
|
|
61
|
+
}>;
|
|
62
|
+
export declare function errorText(err: unknown): string;
|
|
63
|
+
/** The decision an order names, or the reason no order can be placed under it. */
|
|
64
|
+
export declare function readDecision(deps: FundAccess, decisionId: number, venue: string): Promise<{
|
|
65
|
+
permission: FundDecision;
|
|
66
|
+
} | {
|
|
67
|
+
reason: string;
|
|
68
|
+
extra?: Record<string, unknown>;
|
|
69
|
+
}>;
|
|
70
|
+
export interface OrderReport {
|
|
71
|
+
client_id: string;
|
|
72
|
+
venue_order_id: string | null;
|
|
73
|
+
side: "buy" | "sell";
|
|
74
|
+
price: number;
|
|
75
|
+
size: number;
|
|
76
|
+
notional_usd: number;
|
|
77
|
+
status: string;
|
|
78
|
+
detail: string;
|
|
79
|
+
}
|
|
80
|
+
/** Writes one order's state to the decision. Null on success, otherwise why the server did not take it. */
|
|
81
|
+
export declare function reportOrder(deps: FundAccess, decisionId: number, order: OrderReport): Promise<string | null>;
|
|
82
|
+
//# sourceMappingURL=trading-common.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trading-common.d.ts","sourceRoot":"","sources":["../../src/rethinka-fund/trading-common.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEjG,gGAAgG;AAChG,MAAM,MAAM,aAAa,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAC;AAEzH,MAAM,MAAM,WAAW,CAAC,CAAC,IACrB;IAAE,MAAM,EAAE,QAAQ,CAAA;CAAE,GACpB;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACrC;IAAE,MAAM,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,CAAC,CAAA;CAAE,CAAC;AAElC;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAuBjF;AAED,MAAM,WAAW,UAAU;IACzB,6FAA6F;IAC7F,IAAI,EAAE,MAAM;QAAE,OAAO,EAAE,MA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