Created
May 31, 2023 03:50
-
-
Save wphan/5f1bd99f5a12d0c69dd9935d6970663d to your computer and use it in GitHub Desktop.
Decode an anchor log from Drift V2 with Typescript SDK
This file contains hidden or 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
import { Connection, Keypair, PublicKey } from '@solana/web3.js'; | |
import { | |
configs, | |
DriftClient, | |
Wallet, | |
} from "@drift-labs/sdk"; | |
async function main() { | |
const driftConfig = configs['mainnet-beta']; | |
const connection = new Connection('https://api.mainnet-beta.solana.com'); | |
const driftClient = new DriftClient({ | |
connection: connection, | |
wallet: new Wallet(new Keypair()), | |
programID: new PublicKey(driftConfig.DRIFT_PROGRAM_ID), | |
userStats: true, | |
env: 'mainnet-beta', | |
}); | |
console.log(`driftClientSubscribed: ${await driftClient.subscribe()}`); | |
const txHash = "3gvGQufckXGHrFDv4dNWEXuXKRMy3NZkKHMyFrAhLoYScaXXTGCp9vq58kWkfyJ8oDYZrz4bTyGayjUy9PKigeLS"; | |
const tx = await driftClient.connection.getParsedTransaction(txHash, { | |
commitment: "confirmed", | |
maxSupportedTransactionVersion: 0, | |
}); | |
let logIdx = 0; | |
// @ts-ignore | |
for (const event of driftClient.program._events._eventParser.parseLogs(tx!.meta!.logMessages)) { | |
console.log("----------------------------------------"); | |
console.log(`Log ${logIdx++}`); | |
console.log("----------------------------------------"); | |
console.log(`${JSON.stringify(event, null, 2)}`); | |
} | |
console.log("========================================"); | |
console.log("Raw transaction logs"); | |
console.log("========================================"); | |
console.log(JSON.stringify(tx!.meta!.logMessages, null, 2)); | |
process.exit(0); | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment