Build a Farcaster Bot for human-readable alerts
In this guide, you will learn how to create a Farcaster bot that sends human-readable alerts about transactions happening on-chain. You can customize this bot for any EVM-compatible blockchain, and you don’t need any specific knowledge about EVM transaction decoding and interpretation.
Guide
Step 0: Prerequisites
- An installed Bun (see installation guide here)
- An Alchemy account (sign up here)
- An Etherscan(Basescan) account (sign up here)
- A Farcaster account (can be yours or a separate one for your bot)
Step 1: Clone the Repository
Clone the Bot repository and install project dependencies:
Step 2: Add Etherescan and Alchemy API Keys
Copy and rename the .env.example
file to .env
, then paste the Alchemy and Etherescan API keys into the ALCHEMY_API_KEY
and ETHERSCAN_API_KEY
variables.
To reproduce the Friend.tech alerts bot, you need to create an Alchemy App for the Base Mainnet and get an API key from the Basescan. We use the Alchemy API key to monitor new transactions, and the Etherscan API key (from the free plan) to fetch contract ABIs and avoid hitting rate limits. The Etherscan API could be optional if the transactions you are interested in do not interact with many contracts.
Step 3: Create a Farcaster Account Key (Signer)
A Farcaster signer is a separate Ed25519 public and private key pair connected to your Farcaster account that you need for posting messages on your behalf. To connect the key pair, you have to send a transaction from your Farcaster wallet to the Key Registry Farcaster smart contract. At the moment of writing this guide, there was no simple way to create and connect the signer without using 3rd party APIs. So we made a script to generate the required transaction, and to run it you need to do the following:
- Fund your Farcaster custody wallet on Optimism:: You need some ETH on the Optimism chain to pay for the gas. A few dollars would be enough. Click on the 3 dots near your profile, press “About,” and there you will find your custody address.
- Get your Farcaster recovery phrase: On your phone, go to settings -> advanced -> recovery phrase, and write this recovery phrase into the
MNEMONIC
variable in thescripts/create-signer.ts
file. - Run the script: Run the following command
bun run scripts/create-signer.ts
. The result of this script will be an Optimism transaction like this, and a public and private key printed in the console. Do not share the private key. - Add env variables: Add the private key generated from the script and the bot’s account FID into the
SIGNER_PRIVATE_KEY
andACCOUNT_FID
variables.
Step 4: Setup the Decoder
Now, let’s go through the code. To begin using the Loop Decoder, we need to set up the following components:
RPC Provider
We’ll create a function that returns an object with aPublicClient
based on the chain ID. In this example, we are going to support the Base Mainnet.
The constants.ts
file contains the RPC URL and configuration. The Base RPCs do not support a trace API, so we need to specify it in the configuration:
The getPublicClient
returns an RPC client based on the chaind ID and the config from constants.ts
:
ABI Loader
To avoid making unecessary calls to third-party APIs, Loop Decoder uses an API that allows cache. For this example, we will keep it simple and use an in-memory cache. We will also use some strategies to download contract ABIs from Etherscan and 4byte.directory. You can find more information about the strategies in the Strategies reference.
Create a cache for contract ABI:
Contract Metadata Loader
Create an in-memory cache for contract meta-information. Using ERC20RPCStrategyResolver
we will automatically retrieve token meta information from the contract such as token name, decimals, symbol, etc.
Loop Decoder instance
Finally, we’ll create a new instance of the LoopDecoder class.
Step 5: Decode and Interpret a Transaction
After creating the decoder
object in the decoder.ts
file, we can use its decodeTransaction
method to decode transactions by hash:
The decodeTransaction
method returns a DecodedTransaction
object, which you can inspect using our playground. Here is an example of the Friend.tech transaction.
To interpret a decoded transaction, the interpreter.ts
file contains a transformEvent
function that transforms the DecodedTransaction
and adds an action description. You can test the transformEvent
function by putting it into the Interpretation field on the playground with the Friend.tech example, and pressing “Interpret”.
Step 6: Create a Contract Subscription
The bot is set to monitor Friend.tech transactions on the Base Mainnet. The configuration variables are located in the constants.ts
file:
In the program’s entry point, we start a WebSocket subscription for new transactions using Alchemy’s WebSocket RPC.
Step 7: Handle a new Transaction
The handleTransaction
function is responsible for decoding incoming alerts and publishing them as Farcaster casts.
Publishing to the Farcaster
We use the @standard-crypto/farcaster-js-hub-rest
package to publish new casts to Farcaster. The submitCast
function allows us to attach embeds like URLs or frames to the casts. We publish the casts in the bot’s profile by including the bot’s FID as the second argument.
Step 8: Start the Bot
Use the following command to start bot locally:
Your Farcaster bot is now set up and will monitor blockchain transactions and send casts to the bot’s account.
Conculsion
In this guide, you’ve learned how to create a Farcaster bot that monitors blockchain transactions and sends human-readable alerts. By using the Loop Decoder library, you can easily set up a bot to track specific contract addresses and chains without needing in-depth knowledge of EVM transaction decoding.
Let us know on X/Twitter (@3loop_io) if you encounter any problems or have any questions, we’d love to help you!
Happy coding!