@@ -100,12 +100,16 @@ export class LighterAdapter implements ExchangeAdapter {
100100 }
101101
102102 // Auto-generate API key if we have PK but no API key and account exists
103+ let signerReady = false ;
103104 if ( ! this . _apiKey && this . _accountIndex >= 0 ) {
104105 try {
105106 const autoKeyIndex = 2 ; // default for auto-setup
106107 const { privateKey : apiKey } = await this . setupApiKey ( autoKeyIndex ) ;
107108 this . _apiKey = apiKey ;
108109 this . _apiKeyIndex = autoKeyIndex ;
110+ // setupApiKey already configured the static WASM client — reuse it
111+ this . _signer = LighterAdapter . _wasmClient ! ;
112+ signerReady = true ;
109113 // Save to .env for future use
110114 try {
111115 const { setEnvVar } = await import ( "../commands/init.js" ) ;
@@ -120,18 +124,19 @@ export class LighterAdapter implements ExchangeAdapter {
120124 }
121125 }
122126
123- // Initialize signer for trading if we have an API key
124- if ( this . _apiKey ) {
125- const { WasmSignerClient } = await import ( "lighter-ts-sdk" ) ;
126- this . _signer = new WasmSignerClient ( { } ) ;
127- await this . _signer . initialize ( ) ;
128- await this . _signer . createClient ( {
127+ // Initialize signer for trading if we have an API key (reuse singleton WASM client)
128+ if ( this . _apiKey && ! signerReady ) {
129+ const client = await LighterAdapter . getWasmClient ( ) ;
130+ await client . createClient ( {
129131 url : this . _baseUrl ,
130132 privateKey : this . _apiKey ,
131133 chainId : this . _chainId ,
132134 apiKeyIndex : this . _apiKeyIndex ,
133135 accountIndex : this . _accountIndex ,
134136 } ) ;
137+ this . _signer = client ;
138+ }
139+ if ( this . _apiKey ) {
135140 this . _readOnly = false ;
136141 }
137142
@@ -872,11 +877,21 @@ export class LighterAdapter implements ExchangeAdapter {
872877
873878 private static async getWasmClient ( ) : Promise < WasmSignerClientType > {
874879 if ( LighterAdapter . _wasmClient ) return LighterAdapter . _wasmClient ;
875- const { WasmSignerClient } = await import ( "lighter-ts-sdk" ) ;
876- const client = new WasmSignerClient ( { } ) ;
877- await client . initialize ( ) ;
878- LighterAdapter . _wasmClient = client ;
879- return client ;
880+ // Prevent Go WASM runtime from killing the process on panic
881+ const origExit = process . exit ;
882+ process . exit = ( ( code ?: number ) => {
883+ process . exit = origExit ; // restore immediately
884+ throw new Error ( `Lighter WASM runtime exited with code ${ code } ` ) ;
885+ } ) as typeof process . exit ;
886+ try {
887+ const { WasmSignerClient } = await import ( "lighter-ts-sdk" ) ;
888+ const client = new WasmSignerClient ( { } ) ;
889+ await client . initialize ( ) ;
890+ LighterAdapter . _wasmClient = client ;
891+ return client ;
892+ } finally {
893+ process . exit = origExit ;
894+ }
880895 }
881896
882897 /**
0 commit comments