Skip to content

Instantly share code, notes, and snippets.

@sarpdorukaslan
Created October 5, 2024 12:19
Show Gist options
  • Select an option

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

Select an option

Save sarpdorukaslan/2aed502f278560284779b28d992ca7ae 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 { log } = require('console');
const bs58 = require('bs58');
const connection = new web3.Connection(web3.clusterApiUrl('mainnet-beta'));
function getInstructionDiscriminator(name) {
return Buffer.from(crypto.createHash('sha256').update(`global:${name}`).digest()).slice(0, 8);
}
function decodeBuyOrSellInstruction(innerInstrucions) {
const decodedArgs = [];
//Son iç instruction TradeEvent Datası veriyor
const instruction = innerInstrucions.instructions.at(-1);
const instructionData = instruction.data
const dataHex = bs58.decode(instructionData.toString());
let offset = 16;
decodedArgs.tokenAddres = new PublicKey(dataHex.slice(offset, offset + 32)).toString();
offset += 32;
decodedArgs.amountSol = dataHex.readBigUInt64LE(offset).toString();
offset += 8;
decodedArgs.amountToken = dataHex.readBigUInt64LE(offset).toString();
const decodedEventData = decodeTradeEvent(instructionData);
// return decodedEventData;
return decodedArgs;
}
async function getBuyAndSellInstructions(signature, programId, idlFilename) {
try {
const 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 buyDiscriminator = getInstructionDiscriminator('buy');
const sellDiscriminator = getInstructionDiscriminator('sell');
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 = Buffer.from(ix.data);
const instructionDiscriminator = data.slice(0, 8);
const innerInstrucions = transaction.meta.innerInstructions.find( ins => ins.index === index);
if (instructionDiscriminator.equals(buyDiscriminator)) {
const decodedBuy = decodeBuyOrSellInstruction(innerInstrucions);
if (decodedBuy) {
buyInstructions.push({ ...decodedBuy });
}
} else if (instructionDiscriminator.equals(sellDiscriminator)) {
const decodedSell = decodeBuyOrSellInstruction(innerInstrucions);
if (decodedSell) {
sellInstructions.push({ ...decodedSell });
}
} else {
console.log(`Instruction ${index} için eşleşme bulunamadı. Discriminator: ${instructionDiscriminator.toString('hex')}`);
}
}
});
console.log('Buy Instructions:', buyInstructions);
console.log('Sell Instructions:', sellInstructions);
} catch (error) {
console.error('Hata oluştu:', error);
}
}
function decodeTradeEvent(base58String) {
const buffer = bs58.decode(base58String);
let offset = 0;
function readPublicKey() {
const key = new PublicKey(buffer.slice(offset, offset + 32));
offset += 32;
return key;
}
function readU64() {
const value = buffer.readBigUInt64LE(offset);
offset += 8;
return value;
}
function readI64() {
const value = buffer.readBigInt64LE(offset);
offset += 8;
return value;
}
function readBool() {
const value = buffer.readUInt8(offset);
offset += 1;
return value !== 0;
}
// Event discriminator'ı atla (ilk 16 byte)
offset += 16;
const event = {
mint: {
type: "publicKey",
data: readPublicKey().toBase58()
},
solAmount: {
type: "u64",
data: readU64().toString()
},
tokenAmount: {
type: "u64",
data: readU64().toString()
},
isBuy: {
type: "bool",
data: readBool()
},
user: {
type: "publicKey",
data: readPublicKey().toBase58()
},
timestamp: {
type: "i64",
data: readI64().toString()
},
virtualSolReserves: {
type: "u64",
data: readU64().toString()
},
virtualTokenReserves: {
type: "u64",
data: readU64().toString()
}
};
return event;
}
const transactionSignature = 'pGKbTE7J4muvN7hRp1NCcn4ti9n1Wc8B52FrsU4jhpSp819hSzqSwMmeinswWM4aRuVbMqupKSoxVPg1Ntdz2tD';
const programId = '6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P';
getBuyAndSellInstructions(transactionSignature, programId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment