-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added utils to help with debugging tx events
- Loading branch information
Showing
6 changed files
with
192 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { | ||
Connection, | ||
PublicKey, | ||
TransactionSignature, | ||
} from '@solana/web3.js'; | ||
import { LogProvider, logProviderCallback } from './types'; | ||
import { oneShotFetchLogs } from './fetchLogs'; | ||
|
||
export class DevOneShotLogProvider implements LogProvider { | ||
private callback?: logProviderCallback; | ||
private _isSubscribed = false; | ||
|
||
constructor( | ||
private connection: Connection, | ||
private address: PublicKey, | ||
) {} | ||
|
||
public async subscribe( | ||
callback: logProviderCallback, | ||
_skipHistory?: boolean | ||
): Promise<boolean> { | ||
if (this.isSubscribed()) { | ||
return true; | ||
} | ||
|
||
this.callback = callback; | ||
this._isSubscribed = true; | ||
return true; | ||
} | ||
|
||
public async triggerLogs(txSig: TransactionSignature): Promise<void> { | ||
if (!this.isSubscribed() || !this.callback) { | ||
console.warn('DevOneShotLogProvider: Not subscribed or no callback registered'); | ||
return; | ||
} | ||
|
||
try { | ||
// const response = await fetchLogs( | ||
// this.connection, | ||
// this.address, | ||
// 'confirmed', | ||
// undefined, | ||
// txSig, | ||
// 1 | ||
// ); | ||
|
||
const response = await oneShotFetchLogs( | ||
this.connection, | ||
this.address, | ||
'confirmed', | ||
txSig | ||
); | ||
|
||
if (!response || response.transactionLogs.length === 0) { | ||
console.warn(`DevOneShotLogProvider: No logs found for tx ${txSig}`); | ||
return; | ||
} | ||
|
||
for (const { txSig, slot, logs } of response.transactionLogs) { | ||
this.callback(txSig, slot, logs, response.mostRecentBlockTime, undefined); | ||
} | ||
} catch (e) { | ||
console.error('DevOneShotLogProvider: Error fetching logs'); | ||
console.error(e); | ||
} | ||
} | ||
|
||
public isSubscribed(): boolean { | ||
return this._isSubscribed; | ||
} | ||
|
||
public async unsubscribe(): Promise<boolean> { | ||
this.callback = undefined; | ||
this._isSubscribed = false; | ||
return true; | ||
} | ||
} |
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,58 @@ | ||
import { Connection, PublicKey, TransactionSignature, Finality } from '@solana/web3.js'; | ||
import { Program } from '@coral-xyz/anchor'; | ||
import { WrappedEvents } from './types'; | ||
import { oneShotFetchLogs } from './fetchLogs'; | ||
import { parseLogs } from './parse'; | ||
|
||
/** | ||
* Fetches and parses events for a single transaction signature | ||
* @param connection Solana connection | ||
* @param program Anchor program | ||
* @param programId Program ID to fetch events for | ||
* @param txSig Transaction signature to fetch events for | ||
* @param commitment Finality commitment level | ||
* @returns Promise containing array of parsed events, or undefined if transaction not found | ||
*/ | ||
export async function getOneShotTxEvents( | ||
connection: Connection, | ||
program: Program, | ||
programId: PublicKey, | ||
txSig: TransactionSignature, | ||
commitment: Finality = 'confirmed' | ||
): Promise<WrappedEvents | undefined> { | ||
try { | ||
// Fetch the transaction logs | ||
const response = await oneShotFetchLogs( | ||
connection, | ||
programId, | ||
commitment, | ||
txSig | ||
); | ||
|
||
if (!response || response.transactionLogs.length === 0) { | ||
return undefined; | ||
} | ||
|
||
const txLog = response.transactionLogs[0]; | ||
|
||
// Parse the events | ||
const events = parseLogs(program, txLog.logs); | ||
const records: WrappedEvents = []; | ||
|
||
let runningEventIndex = 0; | ||
for (const event of events) { | ||
event.data.txSig = txLog.txSig; | ||
event.data.slot = txLog.slot; | ||
event.data.eventType = event.name; | ||
event.data.txSigIndex = runningEventIndex; | ||
// @ts-ignore | ||
records.push(event.data); | ||
runningEventIndex++; | ||
} | ||
|
||
return records; | ||
} catch (e) { | ||
console.error(`Error fetching events for tx ${txSig}:`, e); | ||
return undefined; | ||
} | ||
} |
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