Skip to content

Commit cc86d7c

Browse files
committed
refactor
1 parent a5cb015 commit cc86d7c

40 files changed

+1518
-508
lines changed

tooling/sparta/README.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ sparta/
4444
├── src/ # Source code
4545
│ ├── clients/ # External API clients (Discord, Ethereum, Google)
4646
│ ├── roles/ # Role-specific Discord commands
47-
│ │ ├── 00_guardians/ # Commands for Guardian role
47+
│ │ ├── nodeOperators/ # Commands for Node Operator role
4848
│ │ └── admins/ # Admin-only commands
4949
│ ├── services/ # Business logic services
5050
│ │ ├── chaininfo-service.ts # Chain information retrieval
@@ -156,7 +156,7 @@ terraform apply
156156
### Role Management
157157
- Monitors Google Sheets for user scores
158158
- Assigns Discord roles based on score thresholds:
159-
- Guardian (base role): Default role
159+
- Node Operator (base role): Default role
160160
- Defender (middle role): Score > 5
161161
- Sentinel (highest role): Score > 10
162162

@@ -173,7 +173,7 @@ terraform apply
173173

174174
## Available Commands
175175

176-
### Guardian Commands
176+
### Node Operator Commands
177177
- `/get-info`: Get chain information including pending block, proven block, current epoch, current slot, and proposer
178178
- `/validator check`: Check if an address is a validator
179179
- `/validator register`: Register a validator address
@@ -223,6 +223,44 @@ terraform apply
223223
- Error tracking and reporting
224224
- Performance monitoring
225225

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+
226264
## Contributing
227265

228266
1. Create a feature branch

tooling/sparta/src/.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ APPROVAL_AMOUNT=10000000000000000000000
1717
# Google Sheets Configuration
1818
GOOGLE_API_KEY=your_google_api_key_here
1919
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

tooling/sparta/src/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,4 @@ dist
173173

174174
# Finder (MacOS) folder config
175175
.DS_Store
176+
dist

tooling/sparta/src/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ The `services/` directory contains the core business logic:
3434

3535
The `roles/` directory contains command definitions for different user roles:
3636

37-
- **00_guardians/**: Commands available to users with the Guardian role
37+
- **nodeOperators/**: Commands available to users with the Node Operator role
3838
- **validator.ts**: Commands for validator registration and checking
3939
- **getChainInfo.ts**: Commands for retrieving chain information
4040
- **admins/**: Commands available only to administrators
@@ -45,18 +45,18 @@ The `roles/` directory contains command definitions for different user roles:
4545

4646
The bot automatically assigns hierarchical Discord roles based on user scores from Google Sheets:
4747

48-
1. **Guardian**: Base role (default)
49-
2. **Defender**: Middle role (score > 5)
50-
3. **Sentinel**: Highest role (score > 10)
48+
1. **Guardian**: Base role (default, requires minimum score of 0)
49+
2. **Defender**: Middle role (requires score > 5)
50+
3. **Sentinel**: Highest role (set manually, but can be removed by this service)
5151

5252
```typescript
5353
// Example from googlesheet-service.ts
5454
if (score > 10) {
55-
roleName = "Sentinel"; // Highest role
55+
roleName = NodeOperatorRoles.Sentinel; // Highest role
5656
} else if (score > 5) {
57-
roleName = "Defender"; // Middle role
57+
roleName = NodeOperatorRoles.Defender; // Middle role
5858
} else {
59-
roleName = "Guardian"; // Default/lowest role
59+
roleName = NodeOperatorRoles.Guardian; // Default/lowest role
6060
}
6161
```
6262

tooling/sparta/src/clients/aztec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @description Provides Aztec node interaction methods via JSON-RPC
44
* @module sparta/utils/aztec
55
*/
6+
import { logger } from "../utils/logger.js";
67

78
export class Aztec {
89
private readonly rpcUrl: string;
@@ -13,7 +14,7 @@ export class Aztec {
1314

1415
static new = async () => {
1516
try {
16-
console.log("Initializing Aztec client");
17+
logger.info("Initializing Aztec client");
1718
const rpcUrl = process.env.AZTEC_NODE_URL;
1819

1920
if (!rpcUrl) {
@@ -22,7 +23,7 @@ export class Aztec {
2223

2324
return new Aztec(rpcUrl);
2425
} catch (error) {
25-
console.error("Error initializing Aztec client:", error);
26+
logger.error({ error }, "Error initializing Aztec client");
2627
throw error;
2728
}
2829
};

tooling/sparta/src/clients/discord.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import {
1414
REST,
1515
Routes,
1616
} from "discord.js";
17-
import guardianCommands from "../roles/00_guardians/index.js";
17+
import nodeOperatorCommands from "../roles/nodeOperators/index.js";
1818
import adminsCommands from "../roles/admins/index.js";
19+
import { logger } from "../utils/logger.js";
1920

2021
// Extended Discord client interface with commands collection
2122
export interface ExtendedClient extends Client {
@@ -34,7 +35,7 @@ export class Discord {
3435
*/
3536
static new = async () => {
3637
try {
37-
console.log("Initializing Discord client");
38+
logger.info("Initializing Discord client");
3839

3940
// Initialize Discord client with required intents
4041
const client = new Client({
@@ -55,7 +56,7 @@ export class Discord {
5556

5657
return new Discord(client);
5758
} catch (error) {
58-
console.error("Error initializing Discord client:", error);
59+
logger.error({ error }, "Error initializing Discord client");
5960
throw error;
6061
}
6162
};
@@ -69,15 +70,18 @@ export class Discord {
6970
* Error event handler
7071
*/
7172
client.once("error", (error) => {
72-
console.error("Error:", error);
73+
logger.error({ error }, "Discord client error");
7374
});
7475

7576
/**
7677
* Ready event handler - called when bot is initialized
7778
*/
7879
client.once("ready", async () => {
79-
console.log("Sparta bot is ready!");
80-
console.log("Bot Client ID: ", process.env.BOT_CLIENT_ID);
80+
logger.info("Sparta bot is ready!");
81+
logger.info(
82+
{ clientId: process.env.BOT_CLIENT_ID },
83+
"Bot connected with Client ID"
84+
);
8185
Discord.deployCommands(client);
8286
});
8387

@@ -95,15 +99,21 @@ export class Discord {
9599
const channel = interaction.channel as TextChannel;
96100

97101
const reply = await command.execute(interaction);
98-
console.log("Command:", {
99-
name: interaction.commandName,
100-
channel: channel.name,
101-
user: interaction.user.username,
102-
date: interaction.createdAt,
103-
result: reply,
104-
});
102+
logger.info(
103+
{
104+
name: interaction.commandName,
105+
channel: channel.name,
106+
user: interaction.user.username,
107+
date: interaction.createdAt,
108+
result: reply,
109+
},
110+
"Command executed"
111+
);
105112
} catch (error) {
106-
console.error(error);
113+
logger.error(
114+
{ error, command: interaction.commandName },
115+
"Error executing command"
116+
);
107117
await interaction.reply({
108118
content: "There was an error executing this command!",
109119
flags: MessageFlags.Ephemeral,
@@ -122,10 +132,10 @@ export class Discord {
122132
);
123133

124134
try {
125-
console.log("Started refreshing application (/) commands.");
135+
logger.info("Started refreshing application (/) commands");
126136

127137
const commandsData = Object.values({
128-
...guardianCommands,
138+
...nodeOperatorCommands,
129139
...adminsCommands,
130140
}).map((command) => command.data.toJSON());
131141

@@ -140,16 +150,16 @@ export class Discord {
140150
);
141151

142152
for (const command of Object.values({
143-
...guardianCommands,
153+
...nodeOperatorCommands,
144154
...adminsCommands,
145155
})) {
146156
client.commands.set(command.data.name, command);
147-
console.log(`Registered command: ${command.data.name}`);
157+
logger.debug(`Registered command: ${command.data.name}`);
148158
}
149159

150-
console.log("Successfully reloaded application (/) commands.");
160+
logger.info("Successfully reloaded application (/) commands");
151161
} catch (error) {
152-
console.error(error);
162+
logger.error({ error }, "Error deploying commands");
153163
}
154164
}
155165

tooling/sparta/src/clients/ethereum.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { RegistryAbi } from "../utils/abis/registryAbi.js";
2323
import { privateKeyToAccount } from "viem/accounts";
2424
import type { Hex } from "viem";
2525
import { ForwarderBytecode, ForwarderAbi } from "../utils/abis/forwarder.js";
26+
import { logger } from "../utils/logger.js";
27+
2628
export const DEPLOYER_ADDRESS: Hex =
2729
"0x4e59b44847b379578588920cA78FbF26c0B4956C";
2830

@@ -67,6 +69,7 @@ export function getExpectedAddress(args: [`0x${string}`], salt: Hex) {
6769
calldata,
6870
};
6971
}
72+
7073
export class Ethereum {
7174
constructor(
7275
private publicClient: ReturnType<typeof createPublicClient>,
@@ -77,7 +80,7 @@ export class Ethereum {
7780

7881
static new = async () => {
7982
try {
80-
console.log("Initializing Ethereum client");
83+
logger.info("Initializing Ethereum client");
8184
const rpcUrl = process.env.ETHEREUM_HOST as string;
8285
const privateKey = process.env.MINTER_PRIVATE_KEY as `0x${string}`;
8386

@@ -119,7 +122,7 @@ export class Ethereum {
119122
stakingAsset
120123
);
121124
} catch (error) {
122-
console.error("Error initializing Ethereum client:", error);
125+
logger.error({ error }, "Error initializing Ethereum client");
123126
throw error;
124127
}
125128
};

0 commit comments

Comments
 (0)