Last active
December 31, 2024 15:37
-
-
Save ngundotra/2798de564f8ec5df21e283380152b40c to your computer and use it in GitHub Desktop.
Parse Lighthouse Instructions
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 { | |
address, | |
createSolanaRpc, | |
getBase58Decoder, | |
getBase58Encoder, | |
signature, | |
} from "@solana/web3.js"; | |
import { parseInstructionData } from "@codama/dynamic-parsers"; | |
import { readFileSync } from "fs"; | |
async function main() { | |
const rawRoot = readFileSync("lighthouse.codama.json").toString(); | |
const rootNode = JSON.parse(rawRoot); | |
const baseUrl = | |
"api.mainnet-beta.solana.com"; | |
const rpc = createSolanaRpc(`https://${baseUrl}`, {}); | |
const tx = await rpc | |
.getTransaction( | |
signature( // Lighthouse Transactions | |
// "2dW5WMPeoBBrVHuRsyJAs6iyC2VuDMirAvL3YKTNMtKnRrL3hkwTYwJisYP9vEojVymxjF8JXFRN8EUCtdZUNLq6" | |
// "5dakXwp5QTySbvc6P1Wp9MLZubnnG4R1Dh6cWSgNv6w1xt2JMsTp7EZvWEUxk9YLbJZHG97TT3jMVJ4yMTXKjM2L" | |
// "6LHBhFVLwuqiH93znCkyzMBZCkye5eHSBBeNZsz7m7M4SmJny9PQkiWtzdquEQvPfHVmn6bT6AeMa4pjNCVbefA" | |
// "43PnzYerXr5b4LNf8A1j8kqztt8Voa7oiL9pzDTmWwSKCeLdZbLLJRd9A2XebiJvRP6kjNW6pF4mnGYnbSsRNXoU" | |
// "5ZtLCLaUGDVXyCZhKLiiNTFqasqUAwahpEeFNHM86amL2awLDByWkuWAdz6C7gdt1GRmfDbjUooh8ozL6a5LUyeZ" | |
// "5WA6DR6vBbyk6wsyxfFAQcsyFLLFamPFKWwgYMSpWbFdUBCCP2WweVggGKtrnJmUa8yyZE5ykqeaQe97daxpPMKZ" | |
"2PmrAtG26M6g8YiokmgbqrJYT4Rhe3kRN3AY3WCcu7NPuX4Jc4aiXoNb5aZuFK48vYhm8pDmwZhVZ9sP6KMAdsKw" | |
), | |
{ maxSupportedTransactionVersion: 0 } | |
) | |
.send(); | |
if (!tx) { | |
console.log("tx not found"); | |
return; | |
} | |
const ACCOUNTS = [ | |
...tx.transaction.message.accountKeys, | |
...(tx.meta?.loadedAddresses.readonly ?? []), | |
...(tx.meta?.loadedAddresses.writable ?? []), | |
]; | |
tx.transaction.message.instructions.forEach((k) => { | |
if ( | |
ACCOUNTS[k.programIdIndex] === | |
address("L2TExMFKdjpN9kozasaurPirfHy9P8sbXoAN1qA3S95") | |
) { | |
const parsedData = parseInstructionData( | |
rootNode, | |
getBase58Encoder().encode(k.data) | |
); | |
console.log(parsedData); | |
const accounts = k.accounts.map((index) => ACCOUNTS[index]); | |
const data = getBase58Encoder().encode(k.data); | |
console.log( | |
"{ keys: [" + | |
accounts | |
.map( | |
(account) => | |
`{ pubkey: new PublicKey("${account}"), isWritable: false, isSigner: false }` | |
) | |
.join(",") + | |
"], data: Buffer.from([" + | |
data.join(",") + | |
"]), programId: new PublicKey('" + | |
ACCOUNTS[k.programIdIndex] + | |
"') }" | |
); | |
} | |
}); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment