Skip to content

Commit 1685f37

Browse files
authored
Merge pull request #523 from AztecProtocol/zkpedro/sparta_bot
this... is...
2 parents 15375d6 + 7ae12e8 commit 1685f37

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4391
-1521
lines changed

terraform.tfstate

-9
This file was deleted.

tooling/sparta/Dockerfile

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1+
# Sparta Discord Bot Dockerfile
2+
# This Dockerfile builds the Sparta Discord bot for deployment
3+
4+
# Start with the official Bun image
15
FROM oven/bun:latest
26

7+
# Add Foundry to PATH for Ethereum development tools
38
ENV PATH="/root/.foundry/bin:${PATH}"
49

10+
# Install required dependencies
11+
# - curl: For downloading tools
12+
# - apt-utils: For better apt functionality
513
RUN apt update && apt install -y curl apt-utils
14+
15+
# Install Docker within the container for potential nested container operations
616
RUN curl -fsSL https://get.docker.com | bash
7-
RUN curl -L https://foundry.paradigm.xyz | bash
817

18+
# Install Foundry toolkit for Ethereum development (cast, anvil, forge)
19+
RUN curl -L https://foundry.paradigm.xyz | bash
920
RUN foundryup
21+
22+
# Verify Foundry installation by checking cast version
1023
RUN cast --version
1124

25+
# Set the working directory
1226
WORKDIR /app
13-
COPY src ./
1427

28+
# Copy package files first to leverage Docker layer caching
29+
# This way, dependencies are only re-installed when package files change
30+
COPY src/package.json src/bun.lockb ./
1531
RUN bun install
32+
33+
# Then copy the rest of the source code
34+
# This step is separate to avoid reinstalling dependencies when only code changes
35+
COPY src ./
36+
37+
# Start the bot
38+
# Uses the production start command from package.json
1639
CMD ["bun", "run", "start"]

tooling/sparta/README.md

+136-61
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
# Sparta Discord Bot
22

3-
A Discord bot for managing Aztec validators, built with Node.js and deployed on AWS Elastic Beanstalk.
3+
A Discord bot for managing Aztec validators and community roles, built with Bun/TypeScript and deployed on AWS Elastic Beanstalk.
44

55
## Overview
66

7-
Sparta is a Discord bot designed to manage and monitor Aztec validators. It provides commands for:
8-
- Validator management (add, remove, list)
9-
- Chain information retrieval
10-
- Committee management
11-
- Stake management
7+
Sparta is a Discord bot designed to manage and monitor Aztec validators and community roles within the Discord server. It provides:
8+
9+
- **Role Management**: Automatically assigns roles based on user scores from Google Sheets
10+
- **Validator Management**: Commands to add, remove, and check validators
11+
- **Chain Information**: Retrieves blockchain data like pending blocks, proven blocks, epochs, slots
12+
- **Discord Integration**: Full integration with Discord slash commands
1213

1314
## Prerequisites
1415

15-
- Node.js v18 or higher
16+
- [Bun](https://bun.sh) v1.0 or higher (used as runtime and package manager)
17+
- Node.js v18 or higher (for development tools)
1618
- AWS CLI configured with appropriate credentials
1719
- Terraform v1.0 or higher
1820
- Discord Bot Token and Application ID from [Discord Developer Portal](https://discord.com/developers/applications)
1921
- Ethereum node access (local or remote)
22+
- Google Sheets API access (for role management)
2023

2124
## Security Notice
2225

2326
⚠️ **Important**: This project uses sensitive credentials that should never be committed to version control:
2427
- Discord bot tokens
2528
- Ethereum private keys
2629
- AWS credentials
30+
- Google Sheets API credentials
2731
- Environment variables
2832

2933
Always use:
@@ -37,14 +41,20 @@ Always use:
3741

3842
```
3943
sparta/
40-
├── src/ # Source code
41-
│ ├── commands/ # Discord bot commands
42-
│ ├── discord/ # Discord bot setup
43-
│ ├── services/ # Business logic services
44-
│ ├── utils/ # Utility functions
45-
│ └── admins/ # Admin-only commands
46-
├── terraform/ # Infrastructure as Code
47-
└── docker/ # Docker configuration
44+
├── src/ # Source code
45+
│ ├── clients/ # External API clients (Discord, Ethereum, Google)
46+
│ ├── roles/ # Role-specific Discord commands
47+
│ │ ├── nodeOperators/ # Commands for Node Operator role
48+
│ │ └── admins/ # Admin-only commands
49+
│ ├── services/ # Business logic services
50+
│ │ ├── chaininfo-service.ts # Chain information retrieval
51+
│ │ ├── discord-service.ts # Discord role management
52+
│ │ ├── googlesheet-service.ts # Google Sheets integration
53+
│ │ ├── validator-service.ts # Validator management
54+
│ │ └── index.ts # Service exports
55+
│ └── utils/ # Utility functions
56+
├── terraform/ # Infrastructure as Code
57+
└── Dockerfile # Docker configuration
4858
```
4959

5060
## Local Development
@@ -55,18 +65,18 @@ git clone <repository-url>
5565
cd sparta
5666
```
5767

58-
2. Install dependencies:
68+
2. Install dependencies using Bun:
5969
```bash
6070
cd src
61-
npm install
71+
bun install
6272
```
6373

6474
3. Create a `.env` file in the `src` directory using `.env.example` as a template:
6575
```bash
6676
cp .env.example .env
6777
```
6878

69-
4. Fill in the required environment variables in `.env`:
79+
4. Fill in the required environment variables in `.env`. Required variables include:
7080
```
7181
# Discord Bot Configuration
7282
BOT_TOKEN=your_bot_token
@@ -75,20 +85,47 @@ GUILD_ID=your_guild_id
7585
7686
# Ethereum Configuration
7787
ETHEREUM_HOST=http://localhost:8545
78-
ETHEREUM_ROLLUP_ADDRESS=your_rollup_address
79-
ETHEREUM_CHAIN_ID=1337
8088
MINTER_PRIVATE_KEY=your_private_key
89+
ETHEREUM_REGISTRY_ADDRESS=your_registry_address
8190
WITHDRAWER_ADDRESS=address_to_withdraw_funds_to
91+
ETHEREUM_CHAIN_ID=1337
8292
ETHEREUM_VALUE=20ether
83-
APPROVAL_AMOUNT=some_amount
93+
MINIMUM_STAKE=100000000000000000000
94+
APPROVAL_AMOUNT=10000000000000000000000
95+
96+
# Google Sheets Configuration
97+
GOOGLE_API_KEY=your_api_key
98+
SPREADSHEET_ID=your_spreadsheet_id
8499
```
85100

86-
5. Start the bot in development mode:
101+
5. Start the bot in development mode with hot reloading:
87102
```bash
88-
npm run watch
103+
bun run dev
89104
```
90105

91-
## Deployment
106+
6. For building a production version:
107+
```bash
108+
bun run build
109+
```
110+
111+
7. To start the production version:
112+
```bash
113+
bun run start
114+
```
115+
116+
## Building with Docker
117+
118+
1. Build the Docker image:
119+
```bash
120+
docker build -t sparta-bot .
121+
```
122+
123+
2. Run the container:
124+
```bash
125+
docker run -d --name sparta-bot --env-file ./src/.env sparta-bot
126+
```
127+
128+
## Deployment with Terraform
92129

93130
The bot is deployed using Terraform to AWS Elastic Container Service (ECS). Follow these steps:
94131

@@ -102,16 +139,7 @@ cd terraform
102139
cp terraform.tfvars.example terraform.tfvars
103140
```
104141

105-
3. Fill in the required variables in `terraform.tfvars`:
106-
```hcl
107-
environment = "production"
108-
aws_region = "us-west-2"
109-
bot_token = "your_bot_token"
110-
bot_client_id = "your_client_id"
111-
guild_id = "your_guild_id"
112-
ethereum_host = "your_ethereum_host"
113-
# ... other variables
114-
```
142+
3. Fill in the required variables in `terraform.tfvars`.
115143

116144
4. Initialize Terraform:
117145
```bash
@@ -123,40 +151,49 @@ terraform init
123151
terraform apply
124152
```
125153

126-
## Architecture
154+
## Bot Functionality
155+
156+
### Role Management
157+
- Monitors Google Sheets for user scores
158+
- Assigns Discord roles based on score thresholds:
159+
- Node Operator (base role): Default role
160+
- Defender (middle role): Score > 5
161+
- Sentinel (highest role): Score > 10
162+
163+
### Validator Management
164+
- Add validators to the blockchain
165+
- Remove validators from the blockchain
166+
- Check validator status and information
167+
168+
### Chain Information
169+
- Get pending block number
170+
- Get proven block number
171+
- Check current epoch and slot
172+
- View committee members
127173

128-
- **Discord.js**: Handles bot interactions and commands
129-
- **AWS ECS**: Runs the bot in containers for high availability
130-
- **AWS Secrets Manager**: Securely stores sensitive configuration
131-
- **TypeScript**: Provides type safety and better development experience
132-
- **Terraform**: Manages infrastructure as code
133-
- **Docker**: Containerizes the application
174+
## Available Commands
175+
176+
### Node Operator Commands
177+
- `/get-info`: Get chain information including pending block, proven block, current epoch, current slot, and proposer
178+
- `/validator check`: Check if an address is a validator
179+
- `/validator register`: Register a validator address
180+
- `/validator help`: Get help for validator commands
181+
182+
### Admin Commands
183+
(More details in the admin command section)
134184

135185
## Environment Variables
136186

137187
### Development
138188
- Uses `.env` file for local configuration
139-
- Supports hot reloading through `npm run watch`
189+
- Supports hot reloading through `bun run dev`
140190
- Environment-specific configurations (.env.local, .env.staging)
141191

142192
### Production
143193
- Uses AWS Secrets Manager for secure configuration
144194
- Automatically loads secrets in production environment
145195
- Supports staging and production environments
146196

147-
## Available Commands
148-
149-
### User Commands
150-
- `/get-info`: Get chain information
151-
- `/validator info`: Get validator information
152-
153-
### Admin Commands
154-
- `/admin validators get`: List validators
155-
- `/admin validators add`: Add a validator
156-
- `/admin validators remove`: Remove a validator
157-
- `/admin committee get`: Get committee information
158-
- `/admin stake manage`: Manage validator stakes
159-
160197
## Security Best Practices
161198

162199
1. **Environment Variables**
@@ -179,19 +216,57 @@ terraform apply
179216
- Use secure RPC endpoints
180217
- Implement transaction signing safeguards
181218

182-
## Contributing
183-
184-
1. Create a feature branch
185-
2. Make your changes
186-
3. Submit a pull request
187-
188219
## Monitoring and Logging
189220

190221
- AWS CloudWatch for container logs
191222
- Discord command execution logging
192223
- Error tracking and reporting
193224
- Performance monitoring
194225

226+
## Logging
227+
228+
The application uses Pino for structured logging with the following features:
229+
230+
- **Multiple log levels**: trace, debug, info, warn, error, fatal
231+
- **Colorful output**: Different colors for different log levels when pretty printing is enabled
232+
- **Timestamps**: Each log includes an ISO timestamp
233+
- **Request logging**: HTTP requests can be logged at the debug level
234+
- **Structured logging**: Logs are output in JSON format for easy parsing
235+
236+
### Configuration
237+
238+
Logging can be configured through environment variables:
239+
240+
- `LOG_LEVEL`: Set the minimum log level (trace, debug, info, warn, error, fatal)
241+
- `LOG_PRETTY_PRINT`: Enable/disable colorful, human-readable logs (true/false)
242+
243+
#### Example
244+
245+
```sh
246+
# Set log level to debug and enable pretty printing
247+
export LOG_LEVEL=debug
248+
export LOG_PRETTY_PRINT=true
249+
npm run dev
250+
```
251+
252+
### Terraform Configuration
253+
254+
Logging can also be configured through Terraform variables:
255+
256+
```hcl
257+
module "sparta" {
258+
# ...
259+
log_level = "debug"
260+
log_pretty_print = true
261+
}
262+
```
263+
264+
## Contributing
265+
266+
1. Create a feature branch
267+
2. Make your changes
268+
3. Submit a pull request
269+
195270
## Support
196271

197272
For support, please open an issue in the repository or contact the maintainers.

tooling/sparta/src/.env.example

+10
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ ETHEREUM_VALUE=20ether
1313

1414
MINIMUM_STAKE=100000000000000000000
1515
APPROVAL_AMOUNT=10000000000000000000000
16+
17+
# Google Sheets Configuration
18+
GOOGLE_API_KEY=your_google_api_key_here
19+
SPREADSHEET_ID=your_spreadsheet_id_here
20+
21+
# Logging Configuration
22+
# Available levels: trace, debug, info, warn, error, fatal
23+
LOG_LEVEL=info
24+
# Enable/disable pretty printing with colors (true/false)
25+
LOG_PRETTY_PRINT=true

0 commit comments

Comments
 (0)