@blockrun/franklin 3.29.16 → 3.30.1
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/tools/index.js +3 -1
- package/dist/tools/polymarket/client.d.ts +61 -0
- package/dist/tools/polymarket/client.js +236 -0
- package/dist/tools/polymarket/constants.d.ts +192 -0
- package/dist/tools/polymarket/constants.js +205 -0
- package/dist/tools/polymarket/creds.d.ts +33 -0
- package/dist/tools/polymarket/creds.js +96 -0
- package/dist/tools/polymarket/fund.d.ts +5 -0
- package/dist/tools/polymarket/fund.js +141 -0
- package/dist/tools/polymarket/l1-auth-1271.d.ts +29 -0
- package/dist/tools/polymarket/l1-auth-1271.js +198 -0
- package/dist/tools/polymarket/orders.d.ts +44 -0
- package/dist/tools/polymarket/orders.js +455 -0
- package/dist/tools/polymarket/positions.d.ts +26 -0
- package/dist/tools/polymarket/positions.js +88 -0
- package/dist/tools/polymarket/redeem.d.ts +5 -0
- package/dist/tools/polymarket/redeem.js +121 -0
- package/dist/tools/polymarket/relayer.d.ts +37 -0
- package/dist/tools/polymarket/relayer.js +140 -0
- package/dist/tools/polymarket/setup.d.ts +9 -0
- package/dist/tools/polymarket/setup.js +385 -0
- package/dist/tools/polymarket/wallet-key.d.ts +14 -0
- package/dist/tools/polymarket/wallet-key.js +69 -0
- package/dist/tools/polymarket/withdraw.d.ts +8 -0
- package/dist/tools/polymarket/withdraw.js +108 -0
- package/dist/tools/polymarket-bet.d.ts +4 -0
- package/dist/tools/polymarket-bet.js +164 -0
- package/dist/tools/tool-categories.js +4 -0
- package/package.json +7 -2
- package/dist/agent/media-router.d.ts +0 -98
- package/dist/agent/media-router.js +0 -328
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// src/utils/polymarket/l1-auth-1271.ts
|
|
2
|
+
//
|
|
3
|
+
// Workaround for https://github.com/Polymarket/clob-client-v2/issues/65
|
|
4
|
+
// (open as of v1.0.8, 2026-07): the SDK's createApiKey()/createL1Headers()
|
|
5
|
+
// signs the L1 ClobAuth attestation as a PLAIN EOA signature bound to the EOA
|
|
6
|
+
// address, while POLY_1271 orders set order.signer = deposit wallet — so the
|
|
7
|
+
// CLOB rejects every order with 400 "the order signer address has to be the
|
|
8
|
+
// address of the API KEY". The SDK's ORDER signing does wrap correctly
|
|
9
|
+
// (ExchangeOrderBuilderV2.buildOrderSignature); only L1 auth lacks the wrap.
|
|
10
|
+
//
|
|
11
|
+
// This module applies the SAME ERC-7739 TypedDataSign envelope the SDK uses
|
|
12
|
+
// for orders to the L1 ClobAuth message, with POLY_ADDRESS = the deposit
|
|
13
|
+
// wallet, so the derived API creds are bound to the deposit wallet:
|
|
14
|
+
//
|
|
15
|
+
// contentsHash = hashStruct(ClobAuth message) (app = ClobAuthDomain v1)
|
|
16
|
+
// innerSig = eth_signTypedData(TypedDataSign{contents, DepositWallet domain})
|
|
17
|
+
// envelope = innerSig ‖ appDomainSeparator ‖ contentsHash
|
|
18
|
+
// ‖ typeString(ClobAuth) ‖ uint16(len(typeString))
|
|
19
|
+
//
|
|
20
|
+
// Re-check issue #65 when bumping @polymarket/clob-client-v2 — if fixed
|
|
21
|
+
// upstream, delete this module and use client.createOrDeriveApiKey().
|
|
22
|
+
import axios from "axios";
|
|
23
|
+
import { encodeAbiParameters, hashDomain, keccak256, toHex, zeroHash, } from "viem";
|
|
24
|
+
import { CLOB_HOST, POLYGON_CHAIN_ID } from "./constants.js";
|
|
25
|
+
// Must match the SDK byte-for-byte (dist/signing/{constants,eip712}.js).
|
|
26
|
+
const MSG_TO_SIGN = "This message attests that I control the given wallet";
|
|
27
|
+
const CLOB_AUTH_DOMAIN = {
|
|
28
|
+
name: "ClobAuthDomain",
|
|
29
|
+
version: "1",
|
|
30
|
+
chainId: POLYGON_CHAIN_ID,
|
|
31
|
+
};
|
|
32
|
+
const CLOB_AUTH_FIELDS = [
|
|
33
|
+
{ name: "address", type: "address" },
|
|
34
|
+
{ name: "timestamp", type: "string" },
|
|
35
|
+
{ name: "nonce", type: "uint256" },
|
|
36
|
+
{ name: "message", type: "string" },
|
|
37
|
+
];
|
|
38
|
+
const CLOB_AUTH_TYPE_STRING = "ClobAuth(address address,string timestamp,uint256 nonce,string message)";
|
|
39
|
+
const CLOB_AUTH_TYPE_HASH = keccak256(toHex(CLOB_AUTH_TYPE_STRING));
|
|
40
|
+
// TypedDataSign layout mirrors TYPED_DATA_SIGN_STRUCT in the SDK's
|
|
41
|
+
// exchangeOrderBuilderV2, with `contents` retyped to ClobAuth. The embedded
|
|
42
|
+
// domain fields describe the DEPOSIT WALLET (the ERC-1271 validator), which is
|
|
43
|
+
// what binds the signature to that one wallet and prevents cross-wallet replay.
|
|
44
|
+
const TYPED_DATA_SIGN_FIELDS = [
|
|
45
|
+
{ name: "contents", type: "ClobAuth" },
|
|
46
|
+
{ name: "name", type: "string" },
|
|
47
|
+
{ name: "version", type: "string" },
|
|
48
|
+
{ name: "chainId", type: "uint256" },
|
|
49
|
+
{ name: "verifyingContract", type: "address" },
|
|
50
|
+
{ name: "salt", type: "bytes32" },
|
|
51
|
+
];
|
|
52
|
+
/** hashStruct(ClobAuth) — string fields keccak-hashed per EIP-712. */
|
|
53
|
+
function clobAuthContentsHash(address, timestamp, nonce) {
|
|
54
|
+
return keccak256(encodeAbiParameters([
|
|
55
|
+
{ type: "bytes32" },
|
|
56
|
+
{ type: "address" },
|
|
57
|
+
{ type: "bytes32" },
|
|
58
|
+
{ type: "uint256" },
|
|
59
|
+
{ type: "bytes32" },
|
|
60
|
+
], [
|
|
61
|
+
CLOB_AUTH_TYPE_HASH,
|
|
62
|
+
address,
|
|
63
|
+
keccak256(toHex(timestamp)),
|
|
64
|
+
nonce,
|
|
65
|
+
keccak256(toHex(MSG_TO_SIGN)),
|
|
66
|
+
]));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Build 7739-wrapped L1 headers binding the attestation to `depositWallet`,
|
|
70
|
+
* signed by the local EOA account. Exported for the golden-vector unit test.
|
|
71
|
+
*/
|
|
72
|
+
export async function buildWrapped1271Headers(account, depositWallet, timestampSec) {
|
|
73
|
+
const ts = String(timestampSec ?? Math.floor(Date.now() / 1000));
|
|
74
|
+
const nonce = 0n;
|
|
75
|
+
const innerSig = await account.signTypedData({
|
|
76
|
+
domain: CLOB_AUTH_DOMAIN,
|
|
77
|
+
types: {
|
|
78
|
+
TypedDataSign: [...TYPED_DATA_SIGN_FIELDS],
|
|
79
|
+
ClobAuth: [...CLOB_AUTH_FIELDS],
|
|
80
|
+
},
|
|
81
|
+
primaryType: "TypedDataSign",
|
|
82
|
+
message: {
|
|
83
|
+
contents: {
|
|
84
|
+
address: depositWallet,
|
|
85
|
+
timestamp: ts,
|
|
86
|
+
nonce,
|
|
87
|
+
message: MSG_TO_SIGN,
|
|
88
|
+
},
|
|
89
|
+
name: "DepositWallet",
|
|
90
|
+
version: "1",
|
|
91
|
+
chainId: BigInt(POLYGON_CHAIN_ID),
|
|
92
|
+
verifyingContract: depositWallet,
|
|
93
|
+
salt: zeroHash,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
const appDomainSep = hashDomain({
|
|
97
|
+
domain: { ...CLOB_AUTH_DOMAIN, chainId: BigInt(POLYGON_CHAIN_ID) },
|
|
98
|
+
types: {
|
|
99
|
+
EIP712Domain: [
|
|
100
|
+
{ name: "name", type: "string" },
|
|
101
|
+
{ name: "version", type: "string" },
|
|
102
|
+
{ name: "chainId", type: "uint256" },
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
const contentsHash = clobAuthContentsHash(depositWallet, ts, nonce);
|
|
107
|
+
const typeHex = toHex(CLOB_AUTH_TYPE_STRING).slice(2);
|
|
108
|
+
const lenHex = (typeHex.length / 2).toString(16).padStart(4, "0");
|
|
109
|
+
const envelope = `0x${innerSig.slice(2)}${appDomainSep.slice(2)}${contentsHash.slice(2)}${typeHex}${lenHex}`;
|
|
110
|
+
return {
|
|
111
|
+
POLY_ADDRESS: depositWallet,
|
|
112
|
+
POLY_SIGNATURE: envelope,
|
|
113
|
+
POLY_TIMESTAMP: ts,
|
|
114
|
+
POLY_NONCE: "0",
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** Plain (unwrapped) L1 headers for EOA mode — matches the SDK's own path. */
|
|
118
|
+
async function buildPlainL1Headers(account) {
|
|
119
|
+
const ts = String(Math.floor(Date.now() / 1000));
|
|
120
|
+
const signature = await account.signTypedData({
|
|
121
|
+
domain: CLOB_AUTH_DOMAIN,
|
|
122
|
+
types: { ClobAuth: [...CLOB_AUTH_FIELDS] },
|
|
123
|
+
primaryType: "ClobAuth",
|
|
124
|
+
message: {
|
|
125
|
+
address: account.address,
|
|
126
|
+
timestamp: ts,
|
|
127
|
+
nonce: 0n,
|
|
128
|
+
message: MSG_TO_SIGN,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
POLY_ADDRESS: account.address,
|
|
133
|
+
POLY_SIGNATURE: signature,
|
|
134
|
+
POLY_TIMESTAMP: ts,
|
|
135
|
+
POLY_NONCE: "0",
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Create-or-derive CLOB L2 API creds. sigType 3 binds them to the deposit
|
|
140
|
+
* wallet via the wrapped headers; sigType 0 binds them to the EOA. Mirrors the
|
|
141
|
+
* SDK's createOrDeriveApiKey flow (POST /auth/api-key, falling back to
|
|
142
|
+
* GET /auth/derive-api-key when the key already exists).
|
|
143
|
+
*/
|
|
144
|
+
export async function deriveApiCreds(account, opts) {
|
|
145
|
+
const headers = opts.sigType === 3
|
|
146
|
+
? await buildWrapped1271Headers(account, requireDepositWallet(opts.depositWallet))
|
|
147
|
+
: await buildPlainL1Headers(account);
|
|
148
|
+
// POST creates a key; if one already exists it 4xxs and we fall back to
|
|
149
|
+
// deriving. Keep the POST failure reason (redacted) so that when the derive
|
|
150
|
+
// ALSO fails — the common cause is the server rejecting the wrapped 1271
|
|
151
|
+
// signature (e.g. deposit wallet not yet deployed) — we can surface it
|
|
152
|
+
// instead of a misleading "not registered" message.
|
|
153
|
+
let createError;
|
|
154
|
+
const create = await axios
|
|
155
|
+
.post(`${CLOB_HOST}/auth/api-key`, undefined, { headers, timeout: 15_000 })
|
|
156
|
+
.then((r) => r.data)
|
|
157
|
+
.catch((e) => {
|
|
158
|
+
createError = summarizeAxiosError(e);
|
|
159
|
+
return null;
|
|
160
|
+
});
|
|
161
|
+
if (create?.apiKey && create.secret && create.passphrase) {
|
|
162
|
+
return { key: create.apiKey, secret: create.secret, passphrase: create.passphrase };
|
|
163
|
+
}
|
|
164
|
+
const derive = await axios
|
|
165
|
+
.get(`${CLOB_HOST}/auth/derive-api-key`, { headers, timeout: 15_000 })
|
|
166
|
+
.catch((e) => {
|
|
167
|
+
throw new Error(`CLOB credential derivation failed: ${summarizeAxiosError(e)}` +
|
|
168
|
+
(createError ? ` (create attempt: ${createError})` : "") +
|
|
169
|
+
`. If the deposit wallet is not yet deployed, run action:"setup" first.`);
|
|
170
|
+
});
|
|
171
|
+
const d = derive.data;
|
|
172
|
+
if (!d?.apiKey || !d.secret || !d.passphrase) {
|
|
173
|
+
// Never echo the raw body — a partial response could contain a live secret.
|
|
174
|
+
// Report only which fields were present.
|
|
175
|
+
const present = d && typeof d === "object" ? Object.keys(d).join(", ") || "none" : typeof d;
|
|
176
|
+
throw new Error(`CLOB did not return complete API credentials (fields present: ${present}). ` +
|
|
177
|
+
`If this persists, the account may not be registered — run action:"setup".`);
|
|
178
|
+
}
|
|
179
|
+
return { key: d.apiKey, secret: d.secret, passphrase: d.passphrase };
|
|
180
|
+
}
|
|
181
|
+
/** Status + short error text from an axios failure, WITHOUT echoing any body
|
|
182
|
+
* (which for these auth endpoints can carry credential material). */
|
|
183
|
+
function summarizeAxiosError(e) {
|
|
184
|
+
const ax = e;
|
|
185
|
+
if (ax?.response?.status) {
|
|
186
|
+
const body = ax.response.data;
|
|
187
|
+
const reason = body && typeof body === "object"
|
|
188
|
+
? (body.error ?? body.message ?? "")
|
|
189
|
+
: typeof body === "string" ? body : "";
|
|
190
|
+
return `HTTP ${ax.response.status}${reason ? ` (${String(reason).slice(0, 120)})` : ""}`;
|
|
191
|
+
}
|
|
192
|
+
return ax?.message ? ax.message.slice(0, 120) : "network error";
|
|
193
|
+
}
|
|
194
|
+
function requireDepositWallet(addr) {
|
|
195
|
+
if (!addr)
|
|
196
|
+
throw new Error("deposit wallet address required for POLY_1271 credential derivation");
|
|
197
|
+
return addr;
|
|
198
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export declare function getSessionLedger(): {
|
|
2
|
+
totalUsd: number;
|
|
3
|
+
count: number;
|
|
4
|
+
perAgent: Record<string, number>;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Round a probability price onto the market's tick grid, CONSERVATIVELY by side:
|
|
8
|
+
* a BUY rounds DOWN and a SELL rounds UP, so the signed order is never priced
|
|
9
|
+
* worse than the user's stated limit. (Plain Math.round could round a buy at
|
|
10
|
+
* 0.556 up to 0.56 — signing above the limit; near the grid floor a buy at
|
|
11
|
+
* 0.0051 would round up to a full tick, doubling notional.) A sub-tick buy
|
|
12
|
+
* floors to 0, which the caller's range check then rejects with a clear error
|
|
13
|
+
* rather than silently lifting it to the first tick.
|
|
14
|
+
*/
|
|
15
|
+
export declare function roundToTick(price: number, tickSize: string, side?: "buy" | "sell"): number;
|
|
16
|
+
/** Map CLOB errors to actionable guidance. Exported for unit tests. */
|
|
17
|
+
export declare function mapClobError(err: unknown): Promise<string>;
|
|
18
|
+
export interface TradeInput {
|
|
19
|
+
action: "buy" | "sell";
|
|
20
|
+
token_id?: string;
|
|
21
|
+
condition_id?: string;
|
|
22
|
+
outcome?: string;
|
|
23
|
+
price?: number;
|
|
24
|
+
size?: number;
|
|
25
|
+
amount_usd?: number;
|
|
26
|
+
order_type?: "GTC" | "GTD" | "FOK" | "FAK";
|
|
27
|
+
expires_at?: number;
|
|
28
|
+
post_only?: boolean;
|
|
29
|
+
confirm?: boolean;
|
|
30
|
+
agent_id?: string;
|
|
31
|
+
}
|
|
32
|
+
export interface ToolResult {
|
|
33
|
+
text: string;
|
|
34
|
+
structured?: Record<string, unknown>;
|
|
35
|
+
isError?: boolean;
|
|
36
|
+
}
|
|
37
|
+
export declare function executeTrade(input: TradeInput): Promise<ToolResult>;
|
|
38
|
+
export declare function listOpenOrders(input: {
|
|
39
|
+
condition_id?: string;
|
|
40
|
+
}): Promise<ToolResult>;
|
|
41
|
+
export declare function cancelOrdersAction(input: {
|
|
42
|
+
order_id?: string;
|
|
43
|
+
all?: boolean;
|
|
44
|
+
}): Promise<ToolResult>;
|