Overview
This tutorial shows you how to build a faucet agent using the Brewit Agent Template. The agent uses Brewit’s delegation system to distribute tokens from delegated accounts, providing a secure and efficient way to run a token faucet service.What You’ll Learn
- How to set up and run the faucet agent
- How Brewit delegation works for token distribution
- How to customize the agent for your needs
- How to interact with the agent’s API from any client
Setting up the agent
Clone the Agent Template
Start by cloning the Brewit Agent Template repository to your local machine.
Install Dependencies
Install all required dependencies using your preferred package manager.
Configure Environment
Set up your environment variables by copying the example file and adding your configuration.
Edit the .env
file with your configuration:
Configure Agent Settings
Set up your agent configuration by editing the config file with your private key and salt.
Start the Agent
Launch the development server to start your faucet agent.
The agent will start on http://localhost:8002
Using the Agent API
Once your agent is running and you have set up delegation, here’s the complete flow to claim tokens:Get Agent Information
First, get the agent’s validator information by querying the info endpoint.
This returns the agent’s validator details needed for delegation setup.
Set Up Delegation
Create a delegated account using the validator info from the previous step. Follow the delegation guide to set up the delegated account for the agent.
The delegated account will have permission to spend tokens on behalf of the main account, enabling the faucet to distribute tokens securely.
Claim Tokens
Once delegation is set up, you can claim tokens from the faucet using the claim endpoint.
How Delegation Works
The faucet agent uses Brewit’s delegation system to distribute tokens securely:1. Account Setup
- The agent creates a main account using the private key
- It gets delegated access to tokens from other accounts
- This allows the agent to spend tokens without needing the original account’s keys
2. Token Distribution Flow
3. Security Benefits
- No private key exposure: The agent doesn’t need the original account’s private keys
- Controlled access: Delegation can be revoked at any time
- Account abstraction: Uses smart contract wallets for enhanced security
Other Endpoints
Health Check
Customizing the Agent
1. Modify Token Amounts
Editsrc/executer/index.ts
to change the token distribution amounts:
2. Add Rate Limiting
Add rate limiting to prevent abuse insrc/routes/faucet.ts
:
3. Auto Claim
Add auto claim flow using a scheduler to drip tokens every hour or day4. Auto swap
Add auto swap flow to convert existing token if the requested token doesn’t existProject Structure
Next Steps
- Deploy to Production: Set up proper environment variables and deploy to your preferred hosting platform
- Add Monitoring: Implement logging and monitoring for better observability
- Enhance Security: Add additional validation and rate limiting
- Scale Up: Consider using a database to track claims and prevent abuse