-
-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathgenerate-python-exchanges.js
More file actions
348 lines (311 loc) · 13.2 KB
/
Copy pathgenerate-python-exchanges.js
File metadata and controls
348 lines (311 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Generates sdks/python/pmxt/_exchanges.py from core/src/server/exchange-factory.ts
// Run via: npm run generate:python-exchanges --workspace=pmxt-core
//
// The createExchange() function in exchange-factory.ts is the single source of
// truth for which exchanges exist and which credentials they require. This
// script reads that function and produces the corresponding Python wrapper classes.
const fs = require('fs');
const path = require('path');
const APP_TS_PATH = path.join(__dirname, '../src/server/exchange-factory.ts');
const OUTPUT_PATH = path.join(__dirname, '../../sdks/python/pmxt/_exchanges.py');
const INIT_PATH = path.join(__dirname, '../../sdks/python/pmxt/__init__.py');
// Python-specific overrides that cannot be derived from app.ts alone.
// Keep this list minimal — only add entries when the generated default is wrong.
const OVERRIDES = {
polymarket: {
// The Python SDK defaults to gnosis-safe to match historical behaviour.
defaults: { signature_type: '"gnosis-safe"' },
methods: [
[
' def init_auth(self) -> None:',
' """Initialize L2 API credentials for Polymarket implicit API signing."""',
' self._call_method("initAuth")',
' return None',
].join('\n'),
],
},
myriad: {
// Myriad uses privateKey as the wallet address, not a signing key.
paramAliases: { private_key: 'wallet_address' },
paramDocs: {
wallet_address: 'Wallet address (required for positions and balance)',
},
},
suibets: {
// Exchange name has no separator; force the canonical PascalCase used
// by the TypeScript SDK and core engine. The default-derived "Suibets"
// is emitted as a backwards-compatible alias.
className: 'SuiBets',
},
};
function toClassName(name) {
return name
.split(/[-_]/)
.map(part => part.toLowerCase() === 'us' ? 'US' : part.charAt(0).toUpperCase() + part.slice(1))
.join('');
}
// Resolve the canonical Python class name for an exchange wire key, honoring
// any explicit override in OVERRIDES[name].className. Use this instead of
// calling toClassName() directly so overrides apply everywhere consistently.
function classNameFor(name) {
const ov = OVERRIDES[name] || {};
return ov.className || toClassName(name);
}
function toLegacyClassName(name) {
return name
.split('-')
.map(part => part.charAt(0).toUpperCase() + part.slice(1))
.join('');
}
function parseExchanges(content) {
const startIdx = content.indexOf('function createExchange(');
if (startIdx === -1) throw new Error('createExchange not found in exchange-factory.ts');
// Grab everything from createExchange to the next top-level function/end
const tail = content.slice(startIdx);
// Find the closing brace of createExchange by tracking brace depth
let depth = 0;
let bodyEnd = 0;
for (let i = tail.indexOf('{'); i < tail.length; i++) {
if (tail[i] === '{') depth++;
else if (tail[i] === '}') {
depth--;
if (depth === 0) { bodyEnd = i + 1; break; }
}
}
const funcBody = tail.slice(0, bodyEnd);
const exchanges = [];
const lines = funcBody.split('\n');
let currentName = null;
let currentBlock = '';
for (const line of lines) {
const caseMatch = line.match(/^\s*case "([^"]+)":/);
if (caseMatch) {
if (currentName) exchanges.push(build(currentName, currentBlock));
currentName = caseMatch[1];
currentBlock = '';
continue;
}
if (/^\s*default:/.test(line) && currentName) {
exchanges.push(build(currentName, currentBlock));
currentName = null;
currentBlock = '';
continue;
}
if (currentName) currentBlock += line + '\n';
}
if (currentName) exchanges.push(build(currentName, currentBlock));
return exchanges;
}
function build(name, block) {
return {
name,
creds: {
apiKey: /credentials\?\.apiKey/.test(block),
apiToken: /credentials\?\.apiToken/.test(block),
apiSecret: /credentials\?\.apiSecret/.test(block),
passphrase: /credentials\?\.passphrase/.test(block),
privateKey: /credentials\?\.privateKey/.test(block),
funderAddress: /credentials\?\.funderAddress/.test(block),
signatureType: /credentials\?\.signatureType/.test(block),
},
};
}
function generateClass(exchange) {
const { name, creds } = exchange;
const className = classNameFor(name);
const ov = OVERRIDES[name] || {};
const aliases = ov.paramAliases || {};
const defaults = ov.defaults || {};
const paramDocs = ov.paramDocs || {};
const methods = ov.methods || [];
const constructorParams = [];
const superArgs = [`exchange_name="${name}"`];
const extraAttrs = [];
const credOverrideLines = [];
// Track which params have been added to avoid duplicates
const addedParams = new Set();
if (creds.apiKey) {
constructorParams.push('api_key: Optional[str] = None');
superArgs.push('api_key=api_key');
addedParams.add('api_key');
}
if (creds.apiToken) {
constructorParams.push('api_token: Optional[str] = None');
superArgs.push('api_token=api_token');
addedParams.add('api_token');
}
if (creds.apiSecret) {
constructorParams.push('api_secret: Optional[str] = None');
extraAttrs.push('self.api_secret = api_secret');
credOverrideLines.push(' if self.api_secret:', ' creds["apiSecret"] = self.api_secret');
addedParams.add('api_secret');
}
if (creds.passphrase) {
constructorParams.push('passphrase: Optional[str] = None');
extraAttrs.push('self.passphrase = passphrase');
credOverrideLines.push(' if self.passphrase:', ' creds["passphrase"] = self.passphrase');
addedParams.add('passphrase');
}
if (creds.privateKey) {
const pyParam = aliases['private_key'] || 'private_key';
const defaultVal = defaults['private_key'] || 'None';
constructorParams.push(`${pyParam}: Optional[str] = ${defaultVal}`);
superArgs.push(`private_key=${pyParam}`);
addedParams.add(pyParam);
}
if (creds.funderAddress) {
constructorParams.push('proxy_address: Optional[str] = None');
superArgs.push('proxy_address=proxy_address');
addedParams.add('proxy_address');
}
if (creds.signatureType) {
const defaultVal = defaults['signature_type'] || 'None';
constructorParams.push(`signature_type: Optional[str] = ${defaultVal}`);
superArgs.push('signature_type=signature_type');
addedParams.add('signature_type');
}
constructorParams.push('base_url: Optional[str] = None');
constructorParams.push('auto_start_server: Optional[bool] = None');
constructorParams.push('pmxt_api_key: Optional[str] = None');
superArgs.push('base_url=base_url');
superArgs.push('auto_start_server=auto_start_server');
superArgs.push('pmxt_api_key=pmxt_api_key');
addedParams.add('base_url');
addedParams.add('auto_start_server');
addedParams.add('pmxt_api_key');
// 👇 ONLY ADD wallet_address IF NOT ALREADY ADDED VIA ALIAS (e.g., Myriad)
if (!addedParams.has('wallet_address')) {
constructorParams.push('wallet_address: Optional[str] = None');
superArgs.push('wallet_address=wallet_address');
addedParams.add('wallet_address');
}
// 👇 ONLY ADD signer IF NOT ALREADY ADDED
if (!addedParams.has('signer')) {
constructorParams.push('signer: Optional[object] = None');
superArgs.push('signer=signer');
addedParams.add('signer');
}
// 👇 ONLY ADD websocket IF NOT ALREADY ADDED
if (!addedParams.has('websocket')) {
constructorParams.push('websocket: Optional[dict] = None');
superArgs.push('websocket=websocket');
addedParams.add('websocket');
}
const docLines = [];
if (creds.apiKey) docLines.push(' api_key: API key for authentication (optional)');
if (creds.apiToken) docLines.push(' api_token: API token for authentication (optional; required for Metaculus API access)');
if (creds.apiSecret) docLines.push(' api_secret: API secret for authentication (optional)');
if (creds.passphrase) docLines.push(' passphrase: Passphrase for authentication (optional)');
if (creds.privateKey) {
const pyParam = aliases['private_key'] || 'private_key';
const doc = paramDocs[pyParam] || 'Private key for authentication (optional)';
docLines.push(` ${pyParam}: ${doc}`);
}
if (creds.funderAddress) docLines.push(' proxy_address: Proxy/smart wallet address (optional)');
if (creds.signatureType) docLines.push(' signature_type: Signature type (optional)');
docLines.push(' base_url: Base URL of the PMXT sidecar server');
docLines.push(' auto_start_server: Automatically start server if not running (default: True)');
docLines.push(' pmxt_api_key: Hosted PMXT API key (optional; enables hosted mode)');
// 👇 ONLY ADD DOCS IF THE PARAM EXISTS
if (addedParams.has('wallet_address')) {
docLines.push(' wallet_address: Wallet address for hosted operations (optional)');
}
if (addedParams.has('signer')) {
docLines.push(' signer: Custom signer for hosted operations (optional)');
}
if (addedParams.has('websocket')) {
docLines.push(' websocket: WebSocket configuration dict (optional)');
}
const indent4 = s => ` ${s}`;
const indent8 = s => ` ${s}`;
const indent12 = s => ` ${s}`;
const lines = [
`class ${className}(Exchange):`,
indent4(`"""${className} exchange client."""`),
'',
indent4('def __init__('),
indent8('self,'),
...constructorParams.map(p => indent8(`${p},`)),
indent4(') -> None:'),
indent8('"""'),
indent8(`Initialize ${className} client.`),
'',
indent8('Args:'),
...docLines,
indent8('"""'),
indent8('super().__init__('),
...superArgs.map(a => indent12(`${a},`)),
indent8(')'),
];
if (extraAttrs.length) {
lines.push('', ...extraAttrs.map(indent8));
}
if (credOverrideLines.length) {
lines.push(
'',
indent4('def _get_credentials_dict(self) -> Optional[Dict[str, Any]]:'),
indent8('creds = super()._get_credentials_dict() or {}'),
...credOverrideLines,
indent8('return creds if creds else None'),
);
}
if (methods.length) {
lines.push('', ...methods);
}
return lines.join('\n');
}
const appTs = fs.readFileSync(APP_TS_PATH, 'utf8');
const exchanges = parseExchanges(appTs);
const legacyAliases = exchanges
.map(ex => ({ legacyName: toLegacyClassName(ex.name), className: classNameFor(ex.name) }))
.filter(alias => alias.legacyName !== alias.className);
const header = [
'# This file is auto-generated by core/scripts/generate-python-exchanges.js',
'# Do not edit manually.',
'# To regenerate: npm run generate:sdk:all --workspace=pmxt-core',
'# Source of truth: core/src/server/exchange-factory.ts (createExchange function)',
'',
'from typing import Any, Dict, Optional',
'',
'from .client import Exchange',
'',
'',
].join('\n');
const body = exchanges.map(generateClass).join('\n\n\n');
const aliasBlock = legacyAliases.length
? [
'',
'',
'# Backwards-compatible aliases for exchange classes generated before underscore handling.',
...legacyAliases.map(alias => `${alias.legacyName} = ${alias.className}`),
'',
].join('\n')
: '\n';
fs.writeFileSync(OUTPUT_PATH, header + body + aliasBlock);
console.log(`Generated ${exchanges.length} exchange classes -> ${path.relative(process.cwd(), OUTPUT_PATH)}`);
for (const ex of exchanges) {
console.log(` ${classNameFor(ex.name)} (exchange_name="${ex.name}")`);
}
// ---------------------------------------------------------------------------
// Update __init__.py imports and __all__ to match generated exchanges
// ---------------------------------------------------------------------------
const classNames = exchanges.flatMap(ex => {
const className = classNameFor(ex.name);
const legacyName = toLegacyClassName(ex.name);
return legacyName === className ? [className] : [className, legacyName];
});
const importList = classNames.join(', ');
let init = fs.readFileSync(INIT_PATH, 'utf8');
// Update the import line from ._exchanges
init = init.replace(
/^from \._exchanges import .+$/m,
`from ._exchanges import ${importList}`
);
// Update the # Exchanges section in __all__
const allEntries = classNames.map(n => ` "${n}",`).join('\n');
init = init.replace(
/( # Exchanges\r?\n)([\s\S]*?)( "Exchange",)/,
`$1${allEntries}\n$3`
);
fs.writeFileSync(INIT_PATH, init);
console.log(`Updated __init__.py imports and __all__`);