Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sarpdorukaslan/d99679deb88881d2be68eb2ebfdcb85f to your computer and use it in GitHub Desktop.

Select an option

Save sarpdorukaslan/d99679deb88881d2be68eb2ebfdcb85f to your computer and use it in GitHub Desktop.
const web3 = require('@solana/web3.js');
const { PublicKey } = require('@solana/web3.js');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const bs58 = require('bs58');
const connection = new web3.Connection(web3.clusterApiUrl('mainnet-beta'));
let transaction;
function getInstructionDiscriminator(name) {
return Buffer.from(crypto.createHash('sha256').update(`global:${name}`).digest()).slice(0, 8);
}
function decodeCreateInstuction(innerInstrucions) {
const accountKeys = transaction.transaction.message.accountKeys??transaction.transaction.message.staticAccountKeys
return {
solAmount : Number(bs58.decode(innerInstrucions.instructions[9].data).readBigUInt64LE(4)) / web3.LAMPORTS_PER_SOL,
tokenAmount : Number(bs58.decode(innerInstrucions.instructions[12].data).readBigUInt64LE(1)),
tokenAddress : accountKeys[innerInstrucions.instructions[12].accounts[0]].toString()
}
}
function decodeBuyOrSellInstruction(innerInstrucions) {
const decodedArgs = {};
const instruction = innerInstrucions.instructions.at(-1);
const instructionData = instruction.data;
const dataHex = bs58.decode(instructionData);
let offset = 16;
decodedArgs.tokenAddress = new PublicKey(dataHex.slice(offset, offset + 32)).toString();
offset += 32;
decodedArgs.amountSol = dataHex.readBigUInt64LE(offset).toString();
offset += 8;
decodedArgs.amountToken = dataHex.readBigUInt64LE(offset).toString();
return decodedArgs;
}
async function getBuyAndSellInstructions(signature, programId) {
try {
transaction = await connection.getTransaction(signature, {
maxSupportedTransactionVersion: 0,
});
if (!transaction) {
console.log('İşlem bulunamadı.');
return;
}
const message = transaction.transaction.message;
const programPubkey = new web3.PublicKey(programId);
const instructions = message.instructions || message.compiledInstructions;
const buyInstructions = [];
const sellInstructions = [];
const createInstructions = [];
const buyDiscriminator = getInstructionDiscriminator('buy');
const sellDiscriminator = getInstructionDiscriminator('sell');
const createDiscriminator = getInstructionDiscriminator('create');
instructions.forEach((ix, index) => {
if (!ix.data) {
console.log(`Instruction ${index} için data bulunamadı.`);
return;
}
const accountKey = message.accountKeys ? message.accountKeys[ix.programIdIndex] : message.staticAccountKeys[ix.programIdIndex];
if (accountKey.equals(programPubkey)) {
const data = typeof ix.data === "string" ? bs58.decode(ix.data) : Buffer.from(ix.data);
const instructionDiscriminator = data.slice(0, 8);
const innerInstructions = transaction.meta.innerInstructions.find(ins => ins.index === index);
if (instructionDiscriminator.equals(buyDiscriminator)) {
const decodedBuy = decodeBuyOrSellInstruction(innerInstructions);
if (decodedBuy) {
buyInstructions.push(decodedBuy);
}
} else if (instructionDiscriminator.equals(sellDiscriminator)) {
const decodedSell = decodeBuyOrSellInstruction(innerInstructions);
if (decodedSell) {
sellInstructions.push(decodedSell);
}
} else if (instructionDiscriminator.equals(createDiscriminator)) {
const decodedCreate = decodeCreateInstuction(innerInstructions);
if (decodedCreate) {
createInstructions.push(decodedCreate);
}
} else {
console.log(`Instruction ${index} için eşleşme bulunamadı.`);
}
}
});
console.log('Buy Instructions:', buyInstructions);
console.log('Sell Instructions:', sellInstructions);
console.log('Create Instructions:', createInstructions);
} catch (error) {
console.error('Hata oluştu:', error);
}
}
const transactionSignature = '7F5t4nvgJnKdbxpFf8PMB6gZMqT6xLWyFFZVPbKB9JHbLCLuLGh2sgKJhZsNH22y84bUPvzwMrXWEagKxFVxtJo';
const programId = '6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P';
getBuyAndSellInstructions(transactionSignature, programId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment