substreams-sink-socials
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Substreams Sink Socials CLI Node.js

substreams-sink-socials is the code template to build Substreams sinks in NodeJS that send blockchain data to different messaging platform.

📖 Documentation

Further resources

🚀 Quick start

Installation

npm install substreams-sink-socials

Features

  • [x] includes Commander.js helper CLI for social config file option: -c --config <string>
  • [x] parses YAML and JSON social config file
  • [x] handles rate limiting
  • [x] format messages

Example

Config file example:

[
    {
        "entity": "Transfer",
        "chat_ids": [
            "some_chat_id"
        ],
        "message": "This *{user_id}* made a transaction with id _{trx_id}_"
    },
    {
        "entity": "Grants",
        "chat_ids": [
            "some_chat_id"
        ],
        "message": "This {grant}"
    }
]

Code example for Telegram:

import { setup, logger, commander } from "substreams-sink";
import { Social, EntityChanges } from "substreams-sink-socials";
import { z } from "zod";

import pkg from "./package.json" assert { type: "json" };
import { Telegram } from "./src/telegram";

// default telegram options
export const DEFAULT_TELEGRAM_API_TOKEN_ENV = 'TELEGRAM_API_TOKEN';

const TelegramConfigSchema = z.object({
    parse_mode: z.enum(["MarkdownV2", "HTML"]).default("MarkdownV2"),
});

// Custom user options interface
interface ActionOptions extends commander.RunOptions {
    config: string,
    telegramApiTokenEnvvar: string,
    telegramApiToken: string,
}

export async function action(manifest: string, moduleName: string, options: ActionOptions) {
    const { emitter } = await setup(options, pkg);

    // Get command options
    const { config, telegramApiTokenEnvvar, telegramApiToken } = options;

    // Social setup
    let social: Social = new Social(config, TelegramConfigSchema, 1500);

    // Telegram options
    const telegram_api_token = telegramApiToken ?? process.env[telegramApiTokenEnvvar];

    if (!telegram_api_token) {
        logger.error('[telegram_api_token] is required');
        process.exit(1);
    }

    // Initialize Telegram bot
    const telegramBot = new Telegram(telegram_api_token);

    // Run substreams
    emitter.on("anyMessage", async (messages: EntityChanges) => {
        await social.distributeMessages(messages, (chatId, message, config) => {
            telegramBot.sendMessage(chatId, message, config);
        });
    });

    await emitter.start();
}

/substreams-sink-socials/

    Package Sidebar

    Install

    npm i substreams-sink-socials

    Weekly Downloads

    4

    Version

    0.1.0

    License

    MIT OR Apache-2.0

    Unpacked Size

    40.7 kB

    Total Files

    18

    Last publish

    Collaborators

    • chamorin