This module contains some common approaches for building Forta Agents. You will also find some tools for writing tests for these agents. These approaches can be composed for creating more complex agents or used only for checking without returning their findings.
- Using npm:
npm i forta-agent-tools
or
- Clone the repo
- npm install
- npm run test
There are multiple types used across all the modules.
metadataVault
This type works as a store for every data that is passed to the
FindingGenerator
. It is adict
withstring
as keys andany
type in its values.FindingGenerator
All the approaches receive a function with this type. This function will be in charge of creating the Findings when the agent's conditions are met. This function can receive a
metadataVault
as a parameter where finding relevant information will be passed, this information can be used for creating more informative findings. This type is an alias for(metadata?: metadataVault) => Finding
. The information set in themetadataVault
for every approach will be described in the approach documentation.CallParams
This is an object containing two properties, an
inputs
array and anoutputs
array.
This approach detects method calls on Smart Contracts. You need to provide the signature of the method you want to detect. You can also provide options for specifying extra filters, such as "what account made the call" or "what contract was called".
import { provideFunctionCallsDetectorHandler } from "forta-agent-tools";
const handler = provideFunctionCallsDetectorHandler (findingGenerator, functionSignature, handlerOptions?);
findingGenerator
: The purpose of this argument was explained in the "General Types" section. The function provided as an argument will receive ametadataVault
with the keys:from
: The account calling the method.to
: The Smart Contract called.functionSelector
: The function selector of the transaction (The keccak256 hash of the signature of the function being called).arguments
: The arguments used for calling the function.
functionSignature
: The signature of the method you want to detect.handlerOptions
: This is an optional argument, it contains extra information for adding extra filtering to your detections. It is a JS object with the following optional properties:from
: If provided, the approach will only detect method calls from the specified account.to
: If provided, the approach will only detect method calls to the specified Smart Contract.filterOnArguments
: This is a predicate receiving an array with the arguments used for calling the function. If provided, the approach will only detect method calls with arguments fitting with the passed predicate.
This approach detects events emitted. You need to provide the signature of the event you want to detect. You can also provide other arguments for specifying extra filters as "who emitted the event" or manually adding a specific filtering function.
import { provideEventCheckerHandler } from "forta-agent-tools";
const handler = provideEventCheckerHandler(findingGenerator, eventSignature, address?, filter?);
findingGenerator
: The purpose of this argument was explained in the "General Types" section. The function provided as an argument will receive ametadataVault
with the keys:eventFragment
: An ethers.js EventFragment object related to the specific event.name
: The event name.signature
: The event signature.topic
: The topic hash.args
: The event input parameter values (both index-based and key-based access based on parameter order and names).address
: The log originating address.
eventSignature
: The event signature to be detected.address
: If provided, the approach will only detect events emitted from the specified account.filter
: If provided, the approach will only detect events that are not discarded by the filter. This function has the type(log: LogDescription, index?: number, array?: LogDescription[]) => boolean
, it will be used as an argument for the commonfilter
arrays function.
This approach detects ETH transfers. You can also provide more arguments for specifying extra filters, such as "who made the transfer", "who is the receiver", and a minimum amount for detecting transfers.
import { provideETHTransferHandler } from "forta-agent-tools";
const handler = provideETHTransferHandler(findingGenerator, agentOptions?);
findingGenerator
: The purpose of this argument was explained in the "General Types" section. The function provided as an argument will receive ametadataVault
with the keys:from
: The account making the transfer.to
: The account receiving the transfer.amount
: The amount ofeth
sent inwei
.
agentOptions
: This is an optional argument, it contains extra information for adding extra filtering to your detections. It is a JS object with the following optional properties:from
: If provided, the approach will only detect transfers from the specified account.to
: If provided, the approach will only detect transfers to the specified account.valueThreshold
: If provided, the approach will only detect transfers with a greater or equal amount ofeth
inwei
.
This approach detects ERC-20 transfers. You will need to provide the address of the ERC-20 contract you want to detect transfers of. You can also provide more arguments for specifying extra filters, such as "who made the transfer", "who is the receiver of the transfer", and a minimum amount for detecting transfers.
import { provideERC20TransferHandler } from "forta-agent-tools";
const handler = provideERC20TransferHandler(findingGenerator, tokenAddress, agentOptions?);
findingGenerator
: The purpose of this argument was explained in the "General Types" section. The function provided as an argument will receive ametadataVault
with the keys:from
: The account making the transfer.to
: The account receiving the transfer.amount
: The number of tokens sent.
tokenAddress
: The address of the ERC-20 contract you want to detect transfers of.agentOptions
: This is an optional argument, it contains extra information for adding extra filtering to your detections. It is a JS object with the following optional properties:from
: If provided, the approach will only detect transfers from the specified account.to
: If provided, the approach will only detect transfers to the specified account.valueThreshold
: If provided, the approach will only detect transfers with a greater or equal number of tokens.
This approach detects transactions involving at least one blacklisted address. You will need to provide a list with the addresses you want to blacklist.
import { provideBlacklistedAddressesHandler } from "forta-agent-tools";
const agent = provideBlacklistedAddressesHandler(findingGenerator, blacklistedAddressesList);
findingGenerator
: The purpose of this argument was explained in the "General Types" section. The function provided as an argument will receive ametadataVault
with the key:addresses
: The list of blacklisted addresses involved in the transaction.
blacklistedAddressesList
: The list of blacklisted addresses.
This approach detects transactions when an address funded from Tornado Cash deploys a contract that contains suspicious hex strings. You need to provide the signature of the event, a list of Tornado Cash addresses and a list of suspicious hex strings.
import { providePotentialExploiterHandler } from "forta-agent-tools";
const handler = providePotentialExploiterHandler (findingGenerator, tornadoAddressesList, eventSignature, suspiciousHexStrings);
findingGenerator
: The purpose of this argument was explained in the "General Types" section. The function provided as an argument will receive ametadataVault
with the keys:from
: The account calling the transfer.contractAddress
: The address of the contract deployed by the tornado funded address.
tornadoAddressesList
: The list of Tornado cash addresses involved in the transaction.eventSignature
: The event signature to be detected.suspiciousHexStrings
: The list of suspicious hex strings to be searched in deployed contract code.
This is a helper class for creating TransactionEvents
using the fluent interface pattern.
import { TestTransactionEvent } from "forta-agent-tools/lib/tests";
const txEvent: TransactionEvent = new TestTransactionEvent().setFrom(address1).setTo(address2);
There are multiple methods you can use for creating the exact TransactionEvent
you want:
setFrom(address)
This method sets thetransaction.from
field in the event.setTo(address)
This method sets thetransaction.to
field in the event.setGas(value)
This method sets thetransaction.gas
field in the event.setGasPrice(value)
This method sets thetransaction.gasPrice
field in the event.setValue(value)
This method sets thetransaction.value
field in the event.setData(data)
This method sets thetransaction.data
field in the event.setGasUsed(value)
This method sets thereceipt.gasUsed
field in the event.setStatus(status)
This method sets thereceipt.status
field in the event.setTimestamp(timestamp)
This method sets theblock.timestamp
field in the event.setBlock(block)
This method sets theblock.number
field in the event.addEventLog(eventSignature, address, data, topics)
This method adds a log to thereceipt.logs
field. The only mandatory argument is theeventSignature
,address
argument is the zero address by default,topics
is a spread list with the indexed event arguments, anddata
is the empty string by default.The
keccak256
hash of the signature is added at the beginning of thetopics
list automatically.addAnonymousEventLog(address, data, topics)
This method adds a log to thereceipt.logs
field.address
argument is the zero address by default,topics
is a spread list with the indexed event arguments, anddata
is the empty string by default.addInterfaceEventLog(event, address, inputs)
This method adds a log to thereceipt.logs
field.event
is anethers.utils.EventFragment
instance,address
argument is the zero address by default,inputs
argument is an array of event parameter values and is an empty array by default.addInvolvedAddresses(addresses)
This method adds a spread list of addresses toaddresses
field.addTrace(traceProps)
This method adds a list ofTrace
objects at the end oftraces
field in the event. The traces are created from thetraceProps
spread list.TraceProps
is a TS object with the following optional fields{ to, from, input, output }
.
This is a helper class for creating BlockEvents
using the fluent interface pattern.
import { TestBlockEvent } from "forta-agent-tools/lib/tests";
const blockEvent: BlockEvent = new TestBlockEvent().setHash(blockHash).setNumber(blockNumber);
There are multiple methods you can use for creating the exact BlockEvent
you want:
setHash(blockHash)
This method sets theblock.hash
field in the event.setNumber(blockNumber)
This method sets theblock.number
field in the event.addTransactions(txns)
This method adds the hashes of a spread list of transaction events at the end ofblock.transactions
field in the event.addTransactionsHashes(hashes)
This method adds a hashes spread list to the end ofblock.transactions
field in the event.
This is a helper function to simulate the execution of run block
cli command when the agent has implemented a handleTransaction
and a handleBlock
.
import { runBlock } from "forta-agent-tools/lib/tests";
async myFunction(params) => {
...
const findings: Findings[] = await runBlock(agent, block, tx1, tx2, tx3, ..., txN);
...
};
Parameters description:
agent
: It is a JS object with two properties,handleTransaction
andhandleBlock
.block
: It is theBlockEvent
that the agent will handle.tx#
: These are theTransactionEvent
objects asociated with theBlockEvent
that the agent will handle.
This is a helper class for mocking the ethers.providers.Provider
class.
Basic usage:
import {
MockEthersProvider,
encodeParameter,
createAddress,
} from "forta-agent-tools/lib/tests";
import { utils, Contract } from "ethers";
const iface: utils.Interface = new utils.Interface([
"function myAwersomeFunction(uint256 param1, string param2) extenal view returns (unit8 id, uint256 val)"
]);
const address: string = createAddress("0xf00");
const data: string = createAddress("0xda7a");
const mockProvider: MockEthersProvider = new MockEthersProvider()
.addCallTo(
address, 20, iface,
"myAwersomeFunction",
{ inputs:[10, "tests"], outputs:[1, 2000]},
)
.addStorage(address, 5, 15, encodeParameter("address", data));
This mock provides some methods to set up the values that the provider should return:
addCallTo(contract, block, iface, id, { inputs, outputs })
. This method prepares a call to thecontract
address at the specifiedblock
, whereiface
is theethers.utils.Interface
object relative to the contract,id
is the identifier of the function to call,inputs
are the parameters passed in the call andoutputs
are the values the call should return.addCallFrom(contract, from, block, iface, id, { inputs, outputs })
. Similar toaddCallTo
but only thefrom
will be able to call the function.addStorage(contract, slot, block, result)
. This method prepares the value stored in the specificslot
ofcontract
address in the givenblock
to beresult
.addBlock(blockNumber, block)
. This method prepares the block with numberblockNumber
to beblock
.setLatestBlock(block)
. This method allows you to set up what the number of the latest block in the provider is.addSigner(addr)
. This function prepares a valid signer for the given address that uses the provider being used.addFilteredLogs(filter, logs)
. This method allows you to set up thelogs
returned by the provider given afilter
.clear()
. This function clears all the mocked data.
All the data you set in the provider will be used until the clear
function is called.
This is a helper class for mocking the ethers.providers.JsonRpcSigner
class. This class extends MockEthersProvider
.
Basic usage:
import {
MockEthersProvider,
MockEthersSigner,
encodeParameter,
createAddress,
} from "forta-agent-tools/lib/tests";
import { utils, Contract } from "ethers";
const iface: utils.Interface = new utils.Interface([
"function myAwersomeFunction(uint256 param1, string param2)"
]);
const address: string = createAddress("0xf00");
const contract: string = createAddress("0xda0");
const mockProvider: MockEthersProvider = new MockEthersProvider();
const mockSigner: MockEthersSigner = new MockEthersSigner(mockProvider)
.setAddress(from)
.allowTransaction(
address, contract, iface,
"myAwersomeFunction", [20, "twenty"]
{ confirmations: 42 }, // receipt data
)
This mock provides some methods to set up the values that the signer should return:
setAddress(address)
. This method sets the address that the signer can sign.allowTransaction(from, to, iface, id, inputs)
. This method prepares a txn sent toto
and signed fromfrom
. The transaction is meant to call the methodid
taken from theiface
of theto
contract passing theinputs
as parameters.receipt
will be the receipt returned by the transaction.denyTransaction(from, to, iface, id, inputs, msg)
. Same conditions ofallowTransaction
but in this case the transaction will be reverted withmsg
message.
All the data you set in the signer will be used until the clear
function is called.