Defender Autotask Utils
The Defender Autotasks service allows you to run small code snippets on a regular basis or via webhooks 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 defender-autotask-utils
yarn add 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 'defender-autotask-utils';
export async function handler(event: AutotaskEvent) {
const payload = event.request.body as SentinelTriggerEvent;
if (payload.type == SubscriberType.BLOCK) {
const { transaction, matchReasons } = payload;
} else if (payload.type == SubscriberType.FORTA) {
const { alert, matchReasons } = payload;
}
const contractPayload = event.request.body as BlockTriggerEvent;
const { transaction, matchReasons } = contractPayload;
const fortaPayload = event.request.body as FortaTriggerEvent;
const { alert, matchReasons } = fortaPayload;
if (isTxAlert(alert)) {
} else if (isBlockAlert(alert)) {
}
}
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 '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) {
if (!shouldMatch(match.transaction)) continue;
} else if (match.type == SubscriberType.FORTA) {
if (isTxAlert(match.alert)) {
} else if (isBlockAlert(match.alert)) {
}
if (!shouldMatch(match.alert)) continue;
}
matches.push({ hash: match.hash, metadata: { id: 'myCustomId' } });
}
return { matches };
}