-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for streaming transaction logs (#31)
* Stream transaction logs in addition to program changes * Make logs subscribe work across multiple nets * Add Program Changes back in as a tab Signed-off-by: Nathan LeClaire <[email protected]> * Import relatively (CI thing)
- Loading branch information
1 parent
903da2d
commit 4722889
Showing
6 changed files
with
154 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as sol from '@solana/web3.js'; | ||
import Electron from 'electron'; | ||
import { LogSubscriptionMap } from 'types/types'; | ||
import { netToURL } from '../common/strings'; | ||
|
||
const logSubscriptions: LogSubscriptionMap = {}; | ||
const subscribeTransactionLogs = async ( | ||
event: Electron.IpcMainEvent, | ||
msg: any | ||
) => { | ||
const solConn = new sol.Connection(netToURL(msg.net)); | ||
const subscriptionID = solConn.onLogs( | ||
'all', | ||
(logsInfo) => { | ||
event.reply('transaction-logs', logsInfo); | ||
}, | ||
'processed' | ||
); | ||
logSubscriptions[msg.net] = { subscriptionID, solConn }; | ||
}; | ||
|
||
const unsubscribeTransactionLogs = async ( | ||
_event: Electron.IpcMainEvent, | ||
msg: any | ||
) => { | ||
const sub = logSubscriptions[msg.net]; | ||
await sub.solConn.removeOnLogsListener(sub.subscriptionID); | ||
delete logSubscriptions[msg.net]; | ||
}; | ||
|
||
export { unsubscribeTransactionLogs, subscribeTransactionLogs }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useEffect, useState } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { useSelector } from 'react-redux'; | ||
import { RootState } from 'renderer/slices/mainSlice'; | ||
|
||
const LogView = () => { | ||
const [logs, setLogs] = useState<string[]>([]); | ||
const validator = useSelector((state: RootState) => state.validator); | ||
const { net } = validator; | ||
|
||
useEffect(() => { | ||
const listener = (logsResp: any) => { | ||
ReactDOM.unstable_batchedUpdates(() => { | ||
setLogs((prevLogs: string[]) => { | ||
const newLogs = [...logsResp.logs.reverse(), ...prevLogs]; | ||
|
||
// utter pseudo-science -- determine max log lines from window size | ||
const MAX_DISPLAYED_LOG_LINES = window.innerHeight / 22; | ||
if (newLogs.length > MAX_DISPLAYED_LOG_LINES) { | ||
return newLogs.slice(0, MAX_DISPLAYED_LOG_LINES); | ||
} | ||
return newLogs; | ||
}); | ||
}); | ||
}; | ||
setLogs([]); | ||
window.electron.ipcRenderer.on('transaction-logs', listener); | ||
window.electron.ipcRenderer.subscribeTransactionLogs({ | ||
net, | ||
}); | ||
|
||
return () => { | ||
window.electron.ipcRenderer.removeAllListeners('transaction-logs'); | ||
window.electron.ipcRenderer.unsubscribeTransactionLogs({ | ||
net, | ||
}); | ||
}; | ||
}, [net]); | ||
|
||
return ( | ||
<div> | ||
{logs.length > 0 ? ( | ||
<pre> | ||
<code>{logs.join('\n')}</code> | ||
</pre> | ||
) : ( | ||
<p className="text-secondary"> | ||
Logs will appear here once transactions are processed. | ||
</p> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default LogView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters