@@ -43,6 +43,23 @@ const SKIP_GENERATE = new Set([
4343 'filterEvents' , // pure local computation, no sidecar
4444] ) ;
4545
46+ // Hosted-mode trading/read methods carry hand-maintained dispatch in client.ts.
47+ // Keep these sections inside the generated region stable when regenerating the
48+ // sidecar pass-through surface from BaseExchange.ts. Without this preservation,
49+ // a focused SDK/doc PR that runs this generator drops hosted-mode guards/helpers
50+ // unrelated to the PR and fails generated-sync checks with broad drift.
51+ const PRESERVE_GENERATED_METHODS = [
52+ 'submitOrder' ,
53+ 'cancelOrder' ,
54+ 'fetchOrder' ,
55+ 'fetchOpenOrders' ,
56+ 'fetchMyTrades' ,
57+ 'fetchClosedOrders' ,
58+ 'fetchAllOrders' ,
59+ 'fetchPositions' ,
60+ 'fetchBalance' ,
61+ ] ;
62+
4663// ---------------------------------------------------------------------------
4764// TypeScript type name -> SDK type info
4865//
@@ -460,6 +477,54 @@ function generateMethod(name, params, config, sf) {
460477 ] . join ( '\n' ) ;
461478}
462479
480+ function extractGeneratedRegion ( client ) {
481+ const beginIdx = client . indexOf ( MARKER_BEGIN ) ;
482+ const endIdx = client . indexOf ( MARKER_END ) ;
483+ if ( beginIdx === - 1 || endIdx === - 1 || endIdx <= beginIdx ) return '' ;
484+ return client . slice ( beginIdx + MARKER_BEGIN . length , endIdx ) ;
485+ }
486+
487+ function methodPrefix ( methodName ) {
488+ return ` async ${ methodName } (` ;
489+ }
490+
491+ function findMethodSegment ( region , methodName ) {
492+ const start = region . indexOf ( methodPrefix ( methodName ) ) ;
493+ if ( start === - 1 ) return null ;
494+
495+ const nextMethod = region . indexOf ( '\n async ' , start + methodPrefix ( methodName ) . length ) ;
496+ const end = nextMethod === - 1 ? region . length : nextMethod + 1 ;
497+ return region . slice ( start , end ) . replace ( / \n + $ / u, '' ) ;
498+ }
499+
500+ function replaceMethodSegment ( region , methodName , replacement ) {
501+ const start = region . indexOf ( methodPrefix ( methodName ) ) ;
502+ if ( start === - 1 ) {
503+ throw new Error ( `Generated ${ methodName } not found while preserving hosted overrides` ) ;
504+ }
505+
506+ const nextMethod = region . indexOf ( '\n async ' , start + methodPrefix ( methodName ) . length ) ;
507+ const end = nextMethod === - 1 ? region . length : nextMethod + 1 ;
508+ return `${ region . slice ( 0 , start ) } ${ replacement } \n\n${ region . slice ( end ) . replace ( / ^ \n + / u, '' ) } ` ;
509+ }
510+
511+ function preserveHostedMethodOverrides ( existingClient , generated ) {
512+ const existingRegion = extractGeneratedRegion ( existingClient ) ;
513+ let nextGenerated = generated ;
514+
515+ for ( const methodName of PRESERVE_GENERATED_METHODS ) {
516+ const existingSegment = findMethodSegment ( existingRegion , methodName ) ;
517+ if ( ! existingSegment ) continue ;
518+
519+ // Only preserve methods that actually contain hosted-mode dispatch. This
520+ // avoids freezing unrelated generated methods if the source file changes.
521+ if ( ! existingSegment . includes ( 'isHostedTradingMode()' ) ) continue ;
522+ nextGenerated = replaceMethodSegment ( nextGenerated , methodName , existingSegment ) ;
523+ }
524+
525+ return nextGenerated ;
526+ }
527+
463528// ---------------------------------------------------------------------------
464529// Main
465530// ---------------------------------------------------------------------------
@@ -470,13 +535,14 @@ function main() {
470535
471536 const methods = extractMethods ( sf ) ;
472537
473- const generated = methods . map ( m => {
538+ let generated = methods . map ( m => {
474539 const name = m . name . text ;
475540 const config = inferReturnConfig ( m . type , name , sf ) ;
476541 return generateMethod ( name , m . parameters , config , sf ) ;
477542 } ) . join ( '\n\n' ) ;
478543
479544 let client = fs . readFileSync ( CLIENT_PATH , 'utf-8' ) ;
545+ generated = preserveHostedMethodOverrides ( client , generated ) ;
480546
481547 const beginIdx = client . indexOf ( MARKER_BEGIN ) ;
482548 const endIdx = client . indexOf ( MARKER_END ) ;
0 commit comments