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

@openzeppelin/defender-autotask-utils

v1.54.1

Published

Utils library for writing Defender Autotasks

Downloads

144

Readme

Defender Autotask Utils

The Defender Autotasks service allows you to run small code snippets on a regular basis, via webhooks or from Sentinels that can make calls to the Ethereum network or to external APIs. Thanks to tight integration to Defender Relayers, you can use Autotasks to automate regular actions on your contracts.

This library provides typings for simplifying the writing of Autotask code when using Typescript.

Note: For programmatically interacting with your Autotasks, such as updating their code from your local workstation, check out the defender-autotask-client package.

Install

npm install @openzeppelin/defender-autotask-utils
yarn add @openzeppelin/defender-autotask-utils

Typings

This library includes typings for 1) the event payload injected by Defender when invoking an Autotask, and 2) the result expected by Defender from an Autotask when used as a Sentinel condition.

AutotaskEvent

Event data injected by Defender when invoking an Autotask. Includes credentials for communicating with whitelisted internal services, as well as the main request object of type AutotaskRequestData. This data contains a main payload that varies depending on the Autotask trigger:

  • A generic object representing the HTTP payload when invoked via webhook
  • A list of matches to evaluate as a SentinelConditionRequest when invoked as a Sentinel condition.
  • A list of transactions matched by a Sentinel as a SentinelTriggerEvent when invoked as Sentinel notification

Example usage for a Sentinel trigger event:

import {
  AutotaskEvent,
  SentinelTriggerEvent,
  SubscriberType,
  BlockTriggerEvent,
  FortaTriggerEvent,
  isTxAlert,
  isBlockAlert,
} from '@openzeppelin/defender-autotask-utils';

export async function handler(event: AutotaskEvent) {
  const payload = event.request.body as SentinelTriggerEvent;

  // You can either check the payload type to destructure the correct properties
  if (payload.type == SubscriberType.BLOCK) {
    const { transaction, matchReasons } = payload;
  } else if (payload.type == SubscriberType.FORTA) {
    const { alert, matchReasons } = payload;
  }

  // Or if you know what type of sentinel you'll be using

  // Contract Sentinel
  const contractPayload = event.request.body as BlockTriggerEvent;
  const { transaction, matchReasons } = contractPayload;
  // Forta Sentinel
  const fortaPayload = event.request.body as FortaTriggerEvent;
  const { alert, matchReasons } = fortaPayload;

  // For forta Alerts you can check whether the alert is related to a transaction or a block
  if (isTxAlert(alert)) {
    // Do something here
  } else if (isBlockAlert(alert)) {
    // Do something here
  }

  // Rest of logic...
}

SentinelConditionResponse

When invoked as a Sentinel condition, the Autotask is expected to return the list of matches, refined from the set of potential transactions initially matched by the Sentinel.

import {
  AutotaskEvent,
  SentinelConditionRequest,
  SentinelConditionResponse,
  SentinelConditionMatch,
  SubscriberType,
  isTxAlert,
  isBlockAlert,
} from '@openzeppelin/defender-autotask-utils';

export async function handler(event: AutotaskEvent): Promise<SentinelConditionResponse> {
  const { events } = event.request.body as SentinelConditionRequest;
  const matches: SentinelConditionMatch[] = [];

  for (const match of events) {
    if (match.type == SubscriberType.BLOCK) {
      // Custom logic to decide whether this tx should be matched by the Sentinel
      if (!shouldMatch(match.transaction)) continue;
    } else if (match.type == SubscriberType.FORTA) {
      // For forta alerts you can check whether the alert is related to a transaction or a block
      if (isTxAlert(match.alert)) {
        // Do something here
      } else if (isBlockAlert(match.alert)) {
        // Do something here
      }
      if (!shouldMatch(match.alert)) continue;
    }
    // Metadata can be any JSON-marshalable object (or undefined)
    matches.push({ hash: match.hash, metadata: { id: 'myCustomId' } });
  }

  return { matches };
}