Skip to content

Instantly share code, notes, and snippets.

@iatomic1
Last active December 5, 2024 16:41
Show Gist options
  • Save iatomic1/8f58b201a73ffe574f9fc7ae5c79b4a5 to your computer and use it in GitHub Desktop.
Save iatomic1/8f58b201a73ffe574f9fc7ae5c79b4a5 to your computer and use it in GitHub Desktop.

Revisions

  1. iatomic1 revised this gist Dec 5, 2024. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion transactions.js
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,11 @@ bot(
    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/${match}/transactions?limit=3&offset=0`);
    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;

  2. iatomic1 revised this gist Dec 5, 2024. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions transactions.js
    Original file line number Diff line number Diff line change
    @@ -19,10 +19,17 @@ bot(
    let output = '';
    for (let i = 0; i < latestTxs.length; i++) {
    const tx = latestTxs[i];

    output += `Transaction ${i + 1}:
    **Type:** ${tx.tx.tx_type}
    **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}
    **Sender:** ${tx.tx.sender_address}`;
    }

    output += `
    **Stx Sent:** ${(tx.stx_sent / 1000000).toFixed(6)}
    **Stx Received:** ${(tx.stx_received / 1000000).toFixed(2)}\n`;
    }
    @@ -32,4 +39,4 @@ bot(
    return await message.send(error.message, { quoted: message.data });
    }
    }
    );
    );
  3. iatomic1 revised this gist Dec 5, 2024. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions transactions.js
    Original file line number Diff line number Diff line change
    @@ -20,16 +20,16 @@ bot(
    for (let i = 0; i < latestTxs.length; i++) {
    const tx = latestTxs[i];
    output += `Transaction ${i + 1}:
    Type: ${tx.tx.tx_type}
    Receiver: ${tx.tx.token_transfer.recipient_address}
    Sender: ${tx.tx.sender_address}
    Stx Sent: ${(tx.stx_sent / 1000000).toFixed(6)}
    Stx Received: ${(tx.stx_received / 1000000).toFixed(2)}\n`;
    **Type:** ${tx.tx.tx_type}
    **Receiver:** ${tx.tx.token_transfer.recipient_address}
    **Sender:** ${tx.tx.sender_address}
    **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 });
    }
    }
    );
    );
  4. iatomic1 revised this gist Dec 5, 2024. 1 changed file with 14 additions and 10 deletions.
    24 changes: 14 additions & 10 deletions transactions.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ const { bot, forwardOrBroadCast } = require('../lib/');
    bot(
    {
    pattern: 'transactions ?(.*)',
    desc: 'Gets latest STX transaction for the provided address',
    desc: 'Gets latest 3 STX transactions for the provided address',
    type: 'all',
    },
    async (message, match) => {
    @@ -12,18 +12,22 @@ bot(
    return await message.send('Please provide a valid STX address.');
    }

    const res = await fetch(`https://api.hiro.so/extended/v2/addresses/${match}/transactions?limit=1&offset=1`);
    const res = await fetch(`https://api.hiro.so/extended/v2/addresses/${match}/transactions?limit=3&offset=0`);
    const json = await res.json();
    const [latestTx] = json.results;
    const latestTxs = json.results;

    const output = `Latest Transaction:
    Type: ${latestTx.tx.tx_type}
    Receiver: ${latestTx.tx.token_transfer.recipient_address}
    Sender: ${latestTx.tx.sender_address}
    Stx Sent: ${(latestTx.stx_sent / 1000000).toFixed(6)}
    Stx Received: ${(latestTx.stx_received / 1000000).toFixed(2)}`;
    let output = '';
    for (let i = 0; i < latestTxs.length; i++) {
    const tx = latestTxs[i];
    output += `Transaction ${i + 1}:
    Type: ${tx.tx.tx_type}
    Receiver: ${tx.tx.token_transfer.recipient_address}
    Sender: ${tx.tx.sender_address}
    Stx Sent: ${(tx.stx_sent / 1000000).toFixed(6)}
    Stx Received: ${(tx.stx_received / 1000000).toFixed(2)}\n`;
    }

    await message.send(output);
    await message.send(output.trim());
    } catch (error) {
    return await message.send(error.message, { quoted: message.data });
    }
  5. iatomic1 revised this gist Dec 5, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion transactions.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ bot(
    return await message.send('Please provide a valid STX address.');
    }

    const res = await fetch(`https://api.hiro.so/extended/v2/addresses/${match}/transactions?limit=1&offset=0`);
    const res = await fetch(`https://api.hiro.so/extended/v2/addresses/${match}/transactions?limit=1&offset=1`);
    const json = await res.json();
    const [latestTx] = json.results;

  6. iatomic1 revised this gist Dec 5, 2024. 1 changed file with 15 additions and 46 deletions.
    61 changes: 15 additions & 46 deletions transactions.js
    Original file line number Diff line number Diff line change
    @@ -3,60 +3,29 @@ const { bot, forwardOrBroadCast } = require('../lib/');
    bot(
    {
    pattern: 'transactions ?(.*)',
    desc: 'Gets STX balance of the address the command is replied to',
    desc: 'Gets latest STX transaction for the provided address',
    type: 'all',
    },
    async (message, match) => {
    try {
    if (!match)
    return await message.send('it is necessary to send an address!')

    const res = await fetch(`https://api.hiro.so/extended/v2/addresses/${match}/transactions?limit=1&offset=1`)
    const json = await res.json()

    const transactionsRes = json.results
    function createTransactionTable(transactions) {
    const headers = ["Type", "Receiver Address", "Sender Address", "Stx Sent", "Stx Received"];
    const colWidths = headers.map((header, i) =>
    Math.max(
    header.length,
    ...transactions.map(tx => {
    const values = [
    tx.tx.tx_type,
    tx.tx.token_transfer.recipient_address,
    tx.tx.sender_address,
    tx.stx_sent,
    tx.stx_received
    ];
    return String(values[i]).length;
    })
    )
    );

    const formatRow = (row) =>
    row.map((cell, i) => String(cell).padEnd(colWidths[i])).join(' | ');

    const headerRow = formatRow(headers);
    const separator = colWidths.map(width => '-'.repeat(width)).join('-+-');

    const rows = transactions.map(tx =>
    formatRow([
    tx.tx.tx_type,
    tx.tx.token_transfer.recipient_address,
    tx.tx.sender_address,
    Number(tx.stx_sent) / 1000000,
    Number(tx.stx_received) / 1000000
    ])
    );

    return [headerRow, separator, ...rows].join('\n');
    if (!match) {
    return await message.send('Please provide a valid STX address.');
    }

    console.log(createTransactionTable(transactionsRes));
    const res = await fetch(`https://api.hiro.so/extended/v2/addresses/${match}/transactions?limit=1&offset=0`);
    const json = await res.json();
    const [latestTx] = json.results;

    const output = `Latest Transaction:
    Type: ${latestTx.tx.tx_type}
    Receiver: ${latestTx.tx.token_transfer.recipient_address}
    Sender: ${latestTx.tx.sender_address}
    Stx Sent: ${(latestTx.stx_sent / 1000000).toFixed(6)}
    Stx Received: ${(latestTx.stx_received / 1000000).toFixed(2)}`;

    await message.send(createTransactionTable(transactionsRes));
    await message.send(output);
    } catch (error) {
    return await message.send(error.message, { quoted: message.data });
    }
    }
    );
    );
  7. iatomic1 created this gist Dec 4, 2024.
    62 changes: 62 additions & 0 deletions transactions.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    const { bot, forwardOrBroadCast } = require('../lib/');

    bot(
    {
    pattern: 'transactions ?(.*)',
    desc: 'Gets STX balance of the address the command is replied to',
    type: 'all',
    },
    async (message, match) => {
    try {
    if (!match)
    return await message.send('it is necessary to send an address!')

    const res = await fetch(`https://api.hiro.so/extended/v2/addresses/${match}/transactions?limit=1&offset=1`)
    const json = await res.json()

    const transactionsRes = json.results
    function createTransactionTable(transactions) {
    const headers = ["Type", "Receiver Address", "Sender Address", "Stx Sent", "Stx Received"];
    const colWidths = headers.map((header, i) =>
    Math.max(
    header.length,
    ...transactions.map(tx => {
    const values = [
    tx.tx.tx_type,
    tx.tx.token_transfer.recipient_address,
    tx.tx.sender_address,
    tx.stx_sent,
    tx.stx_received
    ];
    return String(values[i]).length;
    })
    )
    );

    const formatRow = (row) =>
    row.map((cell, i) => String(cell).padEnd(colWidths[i])).join(' | ');

    const headerRow = formatRow(headers);
    const separator = colWidths.map(width => '-'.repeat(width)).join('-+-');

    const rows = transactions.map(tx =>
    formatRow([
    tx.tx.tx_type,
    tx.tx.token_transfer.recipient_address,
    tx.tx.sender_address,
    Number(tx.stx_sent) / 1000000,
    Number(tx.stx_received) / 1000000
    ])
    );

    return [headerRow, separator, ...rows].join('\n');
    }

    console.log(createTransactionTable(transactionsRes));

    await message.send(createTransactionTable(transactionsRes));
    } catch (error) {
    return await message.send(error.message, { quoted: message.data });
    }
    }
    );