JSPM

substreams-sink-socials

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q71038F
  • License MIT OR Apache-2.0

Substreams Sink Socials

Package Exports

  • substreams-sink-socials
  • substreams-sink-socials/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (substreams-sink-socials) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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

  • includes Commander.js helper CLI for social config file option: -c --config <string>
  • parses YAML and JSON social config file
  • handles rate limiting
  • 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();
}