Skip to content

Instantly share code, notes, and snippets.

@jongan69
Last active June 14, 2024 08:12
Show Gist options
  • Save jongan69/c74407c1b09c9e5bce16024c6c585df4 to your computer and use it in GitHub Desktop.
Save jongan69/c74407c1b09c9e5bce16024c6c585df4 to your computer and use it in GitHub Desktop.
Version 2 of the buy wallet tracker , still testing for memechan
// A server that receives POST data that is printed to the console for debugging purposes
const http = require('http');
const server = http.createServer((req, res) => {
if (req.method === 'POST') {
let data = '';
req.on('data', (chunk) => {
data += chunk;
});
req.on('end', () => {
console.log(data);
res.end('Data received');
});
}
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
const TELEGRAM_BOT_TOKEN = BOT_TOKEN;
const TELEGRAM_CHAT_ID = CHAT_ID;
const HELIUS_API_KEY = API_KEY;
const HELIUS_RPC_URL = `https://mainnet.helius-rpc.com/?api-key=${HELIUS_API_KEY}`;
// Add your external API URL here for debugging
const EXTERNAL_API_URL = 'https://368542cd-5f12-4f94-bbe4-46f770c25130-00-10amryngux4xm.janeway.replit.dev/';
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
if (request.method === 'POST') {
const requestBody = await request.json();
console.log('Received POST request with body:', requestBody);
// Post the received request body to the external API
await postToExternalAPI(requestBody);
// Extract transaction description, timestamp, signature for transfer updates
const Tokentransferfrom = requestBody[0].tokenTransfers[0]?.fromUserAccount;
const memechanprofile = Tokentransferfrom ? `https://solana.memechan.gg/profile/${Tokentransferfrom}` : '';
const Transferdescription = requestBody[0].description;
const Transfertimestamp = new Date(requestBody[0].timestamp * 1000).toLocaleString(); // Convert Unix timestamp to readable date-time
const Transfersignature = `https://xray.helius.xyz/tx/${requestBody[0].signature}`;
const fromTokenAccount = requestBody[0].tokenTransfers[0]?.fromTokenAccount;
// Function to find the mint address dynamically
function findMintAddress(data) {
for (const accountData of data.accountData) {
for (const tokenChange of accountData.tokenBalanceChanges) {
if (tokenChange.mint) {
return tokenChange.mint;
}
}
}
for (const tokenTransfer of data.tokenTransfers) {
if (tokenTransfer.mint) {
return tokenTransfer.mint;
}
}
return null;
}
const foundMintAddress = findMintAddress(requestBody[0]);
// Determine if it's a swap (buy) or sell
const isSwap = requestBody[0].type === "SWAP";
const isSell = requestBody[0].tokenTransfers[requestBody[0].tokenTransfers.length - 1]?.mint === 'So11111111111111111111111111111111111111112';
let transactionType = '';
if (isSwap && !isSell) {
transactionType = '-- NEW BUY --';
} else if (isSell) {
transactionType = '-- NEW SELL --';
}
let messageToSendTransfer =
`${transactionType}\n` +
`Description:\n${Transferdescription}\n` +
`Signature:\n${Transfersignature}\n` +
`Timestamp:\n${Transfertimestamp}\n` +
`Mint Address: <code>${foundMintAddress ? foundMintAddress : 'N/A'}</code>\n`;
if (isSwap && !isSell && foundMintAddress) {
messageToSendTransfer += `\n<a href="https://t.me/bonkbot_bot?start=ref_jyzn2_ca_${foundMintAddress}">Bonk Buy</a>`;
}
// Check if the contract mint address is the specific one
if (foundMintAddress === '7BgBvyjrZX1YKz4oh9mjb8ZScatkkwb8DzFx7LoiVkM3') {
messageToSendTransfer =
`${transactionType}\n` +
`Contract Interacted with was the $slerf token at\n` +
`Mint Address: <code>${foundMintAddress}</code>\n` +
`This purchase was likely on memechan. You can find all my memechan buys here:\n` +
`<a href="${memechanprofile}">Memechan Profile</a>` +
`\nSignature:\n${Transfersignature}\n` +
`Timestamp:\n${Transfertimestamp}\n` +
`\n<a href="https://t.me/bonkbot_bot?start=ref_jyzn2_ca_${foundMintAddress}">Bonk Buy Slerf</a>`;
}
if(foundMintAddress){
await sendToTelegramTransfer(messageToSendTransfer); // Send to Telegram
return new Response('Logged POST request body.', {status: 200});
}
} else {
return new Response('Method not allowed.', {status: 405});
}
}
async function postToExternalAPI(requestBody) {
const response = await fetch(EXTERNAL_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});
if (!response.ok) {
console.error('Failed to post to external API:', await response.text());
} else {
console.log('Successfully posted to external API');
}
}
// This function is used to send NFT Updates to the bot
async function sendToTelegramNFT(message, imageUrl) {
const telegramUrl = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPhoto`;
const response = await fetch(telegramUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
chat_id: TELEGRAM_CHAT_ID,
photo: imageUrl,
caption: message,
parse_mode: "HTML"
}),
});
const responseData = await response.json();
if (!response.ok) {
console.error('Failed to send photo to Telegram:', responseData);
}
}
// This function is used to send Transfer Updates to the Bot
async function sendToTelegramTransfer(message) {
const telegramUrl = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`;
const response = await fetch(telegramUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
chat_id: TELEGRAM_CHAT_ID,
text: message,
parse_mode: "HTML"
}),
});
const responseData = await response.json();
if (!response.ok) {
console.error('Failed to send message to Telegram:', responseData);
}
}
// This function gets images associated with NFTs that are featured in updates
async function getAssetImageUrl(mintAddress) {
const response = await fetch(HELIUS_RPC_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 'my-id',
method: 'getAsset',
params: {
id: mintAddress,
},
}),
});
const { result } = await response.json();
return result.content.links.image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment