Last active
December 5, 2024 16:41
-
-
Save iatomic1/8f58b201a73ffe574f9fc7ae5c79b4a5 to your computer and use it in GitHub Desktop.
Get Recent Stx Transactions
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
const { bot, forwardOrBroadCast } = require('../lib/'); | |
bot( | |
{ | |
pattern: 'transactions ?(.*)', | |
desc: 'Gets latest 3 STX transactions for the provided address', | |
type: 'all', | |
}, | |
async (message, match) => { | |
try { | |
if (!match) { | |
return await message.send('Please provide a valid STX address.'); | |
} | |
const arr = match.split(" ") | |
const stx_addr = arr[0] | |
const limit = arr[1] | |
const res = await fetch(`https://api.hiro.so/extended/v2/addresses/${stx_addr}/transactions?limit=${limit}&offset=0`); | |
const json = await res.json(); | |
const latestTxs = json.results; | |
let output = ''; | |
for (let i = 0; i < latestTxs.length; i++) { | |
const tx = latestTxs[i]; | |
output += `Transaction ${i + 1}: | |
**Type:** ${tx.tx.tx_type}`; | |
if (tx.tx.tx_type === 'token_transfer') { | |
output += ` | |
**Receiver:** ${tx.tx.token_transfer.recipient_address} | |
**Sender:** ${tx.tx.sender_address}`; | |
} | |
output += ` | |
**Stx Sent:** ${(tx.stx_sent / 1000000).toFixed(6)} | |
**Stx Received:** ${(tx.stx_received / 1000000).toFixed(2)}\n`; | |
} | |
await message.send(output.trim()); | |
} catch (error) { | |
return await message.send(error.message, { quoted: message.data }); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment