npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@odfdata/blockchain-listener

v1.0.7

Published

The AWS Constructor to create a Blockchain Listener using AWS Fargate and Event Bridge

Downloads

19

Readme

Blockchain Listener on Amazon

This repository offers a CDK construct for deploying a Blockchain Listener using ECS Fargate. The ECS Task Definition is configured with a Docker image built locally, and thus requires the Docker daemon to be running. The Task Definition is equipped with the minimum available resources of .25 vCPU and .5 GB of RAM and is pre-configured with the necessary permissions to send events to the Custom Event Bus within Event Bridge. An ECS service is also established to keep a single task running at all times.

Installation

Note that this construct requires AWS CDK v2.

Typescript

yarn add @odfdata/blockchain-listener

Deployments

The Blockchain Listener automatically builds and deploys your custom Docker image and sets the environment variable named BLOCKCHAIN_LISTENER_EVENT_BRIDGE_BUS_ARN to the ARN of the custom event bridge bus created by the construct.

import path from 'path';
import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { BlockchainListener } from '@odfdata/blockchain-listener';

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);
    new BlockchainListener(
      this,
      'BlockchainListener',
      {
        cpuArchitecture: ecs.CpuArchitecture.ARM64 | ecs.CpuArchitecture.X86_64,
        cidrBlock: '10.0.0.0/24',
        containerImageDirectory: path.join(__dirname, '{directory_path_of_your_dockerfile}'),
      }
    );
  }
}

📝 When you instantiate a stack in your CDK Application, make sure to include the correct environment configuration for CDK to compile correctly. For example, if you want to specialize the stack for the AWS Account and Region that are implied by the current CLI configuration, use this line of code:
env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },

See the API Documentation for details on all available input and output parameters.

Examples

The following example explains how to set environment variables in your docker image.

blockchainListener.blockchainListenerContainer.addEnvironment('WEBSOCKET_URL', '{your_websocket_url}');
blockchainListener.blockchainListenerContainer.addEnvironment('NFT_CONTRACT_ADDRESS', '{your_nft_contract_address}');
blockchainListener.blockchainListenerContainer.addEnvironment('NFT_TRANSFER_TOPIC', '{your_nft_transfer_topic}');

The following example explains how to create your listener main using alchemy-sdk to subscribe to NFT transfer logs and publish them in the custom event bus created by the blockchain listener.

import { AlchemyWeb3, createAlchemyWeb3, Log } from '@alch/alchemy-web3';
import { EventBridgeClient, PutEventsCommand } from '@aws-sdk/client-eventbridge';

const websocketUrl: string = process.env.WEBSOCKET_URL as string;
const nftContractAddress: string = process.env.NFT_CONTRACT_ADDRESS as string;
const nftTransferTopic: string = process.env.NFT_TRANSFER_TOPIC;

const eventBridgeClient = new EventBridgeClient({});
const eventBridgeBusArn: string = process.env.BLOCKCHAIN_LISTENER_EVENT_BRIDGE_BUS_ARN as string;

const listener = async () => {
  const web3: AlchemyWeb3 = createAlchemyWeb3(websocketUrl);
  web3.eth.subscribe('logs', {
    address: nftContractAddress,
    // @ts-ignore
    topics: [nftTransferTopic]
  })
    // @ts-ignore
    .on('data', (log: Log) => {
      new Promise(async (resolve, reject) => {
        const eventBridgeEvent = new PutEventsCommand({
          Entries: [
            {
              Detail: JSON.stringify({
                eventName: 'NEW_MINT',
                log: log,
              }),
              EventBusName: eventBridgeBusArn,
              DetailType: 'newMint',
              Source: 'io.blockchain-listener',
            },
          ],
        });
        await eventBridgeClient.send(eventBridgeEvent).then();
      }).then(() => {});
    });
}

The following example explains how to subscribe your lambda function to listen to the events published in the custom event bus.


// create a new rule to subscribe newMintFunction to the blockchain listener event bus
new events.Rule(
  this,
  'EventRuleNewMint',
  {
    eventBus: blockchainListener.eventBus,
    enabled: true,
    description: 'The event rule responsible of filtering Blockchain Listener events published in the Event Bus ' +
      'and start the correct AWS Lambda Function',
    eventPattern: {
      source: ['io.blockchain-listener'],
      detail: {
        eventName: ['NEW_MINT'],
      },
    },
    targets: [
      new events_targets.LambdaFunction(newMintLamdaFunction),
    ],
  },
);

Authors

License

This project is licensed under the MIT-0 License. See the LICENSE file for details.