import { resolve } from 'path' ; import { app , BrowserWindow , ipcMain , IpcMainEvent } from 'electron' ; import TonPoolClient from './client' ; import readConfig from './client/config' ; import readGPUs , { GPU } from './client/read-gpus' ; const isDev = process . env . NODE_ENV === 'development' ; const config = readConfig ( ) ; const createWindow = ( ) => { const mainWindow = new BrowserWindow ( { height : 480 , width : 350 , resizable : false , maximizable : false , webPreferences : { preload : resolve ( __dirname , 'preload.js' ) } } ) ; mainWindow . setMenuBarVisibility ( false ) ; void mainWindow . loadURL ( isDev ? 'http://127.0.0.1:5000' : `file://${ resolve ( __dirname , '..' , 'index.html' ) }` ) ; TonPoolClient . on ( 'connect' , ( ) => ! mainWindow . isDestroyed ( ) && ! mainWindow . webContents . isDestroyed ( ) ? mainWindow . webContents . send ( 'connect' ) : undefined ) ; TonPoolClient . on ( 'error' , ( error ) => ! mainWindow . isDestroyed ( ) && ! mainWindow . webContents . isDestroyed ( ) ? mainWindow . webContents . send ( 'error' , error ) : undefined ) ; TonPoolClient . on ( 'hashrate' , ( gpuId , hashrate ) => ! mainWindow . isDestroyed ( ) && ! mainWindow . webContents . isDestroyed ( ) ? mainWindow . webContents . send ( 'hashrate' , gpuId , hashrate ) : undefined ) ; TonPoolClient . on ( 'reconnect' , ( ) => ! mainWindow . isDestroyed ( ) && ! mainWindow . webContents . isDestroyed ( ) ? mainWindow . webContents . send ( 'reconnect' ) : undefined ) ; TonPoolClient . on ( 'stop' , ( ) => ! mainWindow . isDestroyed ( ) && ! mainWindow . webContents . isDestroyed ( ) ? mainWindow . webContents . send ( 'stop' ) : undefined ) ; TonPoolClient . on ( 'submit' , ( ) => ! mainWindow . isDestroyed ( ) && ! mainWindow . webContents . isDestroyed ( ) ? mainWindow . webContents . send ( 'submit' ) : undefined ) ; TonPoolClient . on ( 'submitDuplicate' , ( ) => ! mainWindow . isDestroyed ( ) && ! mainWindow . webContents . isDestroyed ( ) ? mainWindow . webContents . send ( 'submitDuplicate' ) : undefined ) ; TonPoolClient . on ( 'submitInvalid' , ( ) => ! mainWindow . isDestroyed ( ) && ! mainWindow . webContents . isDestroyed ( ) ? mainWindow . webContents . send ( 'submitInvalid' ) : undefined ) ; TonPoolClient . on ( 'submitStale' , ( ) => ! mainWindow . isDestroyed ( ) && ! mainWindow . webContents . isDestroyed ( ) ? mainWindow . webContents . send ( 'submitStale' ) : undefined ) ; if ( isDev ) { mainWindow . webContents . openDevTools ( ) ; } mainWindow . webContents . on ( 'new-window' , ( e , url ) => { e . preventDefault ( ) ; void require ( 'electron' ) . shell . openExternal ( url ) ; } ) ; } ; app . on ( 'ready' , ( ) => { if ( config . headless ) { readGPUs ( config . baseBinaryPath , config . boost , config . excludeGPUs , config . binary ) . then ( ( gpus ) => { TonPoolClient . on ( 'stop' , ( ) => app . quit ( ) ) ; TonPoolClient . start ( config , gpus ) ; } , ( ) => { } ) ; } else { createWindow ( ) ; app . on ( 'activate' , function ( ) { if ( BrowserWindow . getAllWindows ( ) . length === 0 ) createWindow ( ) ; } ) ; } } ) ; app . on ( 'window-all-closed' , ( ) => { if ( process . platform !== 'darwin' ) { TonPoolClient . stop ( ) . finally ( ( ) => app . quit ( ) ) ; } } ) ; interface GUIConfig { gpus : string [ ] ; pool : string ; wallet : string ; rig : string ; binary : string ; } ipcMain . on ( 'miningStart' , async ( event , guiConfig ) => { const gpus = [ ] ; for ( const bin of guiConfig . binary . split ( ',' ) ) { const binGPUs = await readGPUs ( config . baseBinaryPath , config . boost , config . excludeGPUs , bin ) ; for ( const binGPU of binGPUs ) { gpus . push ( binGPU ) ; } } config . binary = guiConfig . binary . split ( ',' ) . length > 1 ? '' : guiConfig . binary ; config . pool = guiConfig . pool ; config . rig = guiConfig . rig ; config . wallet = guiConfig . wallet ; const selectedGpus = guiConfig . gpus . map ( ( idString ) => Number . parseInt ( idString , 10 ) ) ; const filteredGpus = gpus . filter ( ( gpu , index ) => selectedGpus . includes ( index ) ) ; TonPoolClient . start ( config , filteredGpus ) ; event . reply ( 'miningStart' , true ) ; } ) ; ipcMain . on ( 'miningStop' , ( event ) => { TonPoolClient . stop ( ) . finally ( ( ) => void event . reply ( 'miningStop' , true ) ) ; } ) ; ipcMain . on ( 'getDevices' , async ( event , binary ) => { try { const gpus = [ ] ; for ( const bin of binary . split ( ',' ) ) { const binGPUs = await readGPUs ( config . baseBinaryPath , config . boost , config . excludeGPUs , bin ) ; for ( const binGPU of binGPUs ) { gpus . push ( binGPU ) ; } } const gpuNames = gpus . map < string > ( ( gpu ) => { const id = gpu . type === 'CUDA' ? gpu . deviceId . toString ( 10 ) : `${ gpu . platformId }:${ gpu . deviceId }` ; return `${ id }. ${ gpu . name }` ; } ) ; event . reply ( 'getDevices' , null , gpuNames ) ; } catch ( error ) { event . reply ( 'getDevices' , error , [ ] ) ; } } ) ;