-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
220 lines (191 loc) · 5.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const CBBTCAgentPlugin = require('./src/CBBTCAgentPlugin');
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');
/**
* Configuration and Environment Management
*/
class ConfigManager {
/**
* Load environment variables with validation
* @throws {Error} If required environment variables are missing
*/
static loadConfig() {
// Determine the environment
const envFile = process.env.NODE_ENV
? `.env.${process.env.NODE_ENV}`
: '.env';
// Path to the environment file
const envPath = path.resolve(process.cwd(), envFile);
// Check if environment file exists
if (!fs.existsSync(envPath)) {
console.warn(`No environment file found at ${envPath}. Using default configuration.`);
}
// Load environment variables
const result = dotenv.config({
path: envPath,
debug: process.env.DEBUG === 'true'
});
// Handle any errors in loading environment variables
if (result.error) {
console.error('Error loading environment variables:', result.error);
throw result.error;
}
// Validate required environment variables
this.validateConfig();
}
/**
* Validate required configuration parameters
* @throws {Error} If any required configuration is missing
*/
static validateConfig() {
const requiredVars = ['PRIVATE_KEY', 'BASE_RPC_URL'];
const missingVars = requiredVars.filter(varName => !process.env[varName]);
if (missingVars.length > 0) {
throw new Error(`Missing required environment variables: ${missingVars.join(', ')}`);
}
}
}
/**
* Main application class
*/
class CBBTCAgentApp {
constructor() {
// Ensure configuration is loaded
ConfigManager.loadConfig();
// Initialize the plugin
this.plugin = new CBBTCAgentPlugin(
process.env.PRIVATE_KEY,
process.env.BASE_RPC_URL
);
}
/**
* Perform a balance check operation
* @param {string} address - Ethereum address to check
* @returns {Promise<string>} - Balance of the address
*/
async checkBalance(address) {
try {
return await this.plugin.checkBalance(address);
} catch (error) {
this.logError('Balance Check', error);
throw error;
}
}
/**
* Perform a token transfer
* @param {string} recipient - Recipient address
* @param {string} amount - Amount to transfer
* @returns {Promise<string>} - Transaction hash
*/
async transferTokens(recipient, amount) {
try {
return await this.plugin.transferCbBTC(recipient, amount);
} catch (error) {
this.logError('Token Transfer', error);
throw error;
}
}
/**
* Approve token spending
* @param {string} spender - Spender address
* @param {string} amount - Amount to approve
* @returns {Promise<string>} - Transaction hash
*/
async approveSpending(spender, amount) {
try {
return await this.plugin.approveSpender(spender, amount);
} catch (error) {
this.logError('Token Approval', error);
throw error;
}
}
/**
* Centralized error logging method
* @param {string} context - Context of the error
* @param {Error} error - Error object
* @private
*/
logError(context, error) {
const timestamp = new Date().toISOString();
const logEntry = `[${timestamp}] ${context} Error: ${error.message}\n`;
// Log to console
console.error(logEntry);
// Optionally log to file (uncomment if needed)
// this.writeErrorToFile(logEntry);
}
/**
* Write error to log file (optional)
* @param {string} logEntry - Log entry to write
* @private
*/
writeErrorToFile(logEntry) {
const logDir = path.resolve(process.cwd(), 'logs');
// Ensure logs directory exists
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const logFile = path.join(logDir, `error-${new Date().toISOString().split('T')[0]}.log`);
try {
fs.appendFileSync(logFile, logEntry);
} catch (writeError) {
console.error('Could not write to log file:', writeError);
}
}
}
/**
* Application entry point with error handling
*/
async function main() {
try {
// Create an instance of the application
const app = new CBBTCAgentApp();
// Example usage (remove or replace with actual logic)
if (process.argv.length > 2) {
switch (process.argv[2]) {
case 'balance':
if (!process.argv[3]) {
throw new Error('Please provide an address to check balance');
}
const balance = await app.checkBalance(process.argv[3]);
console.log(`Balance: ${balance} cbBTC`);
break;
case 'transfer':
if (process.argv.length < 5) {
throw new Error('Usage: node index.js transfer <recipient> <amount>');
}
const txHash = await app.transferTokens(process.argv[3], process.argv[4]);
console.log(`Transfer TX Hash: ${txHash}`);
break;
case 'approve':
if (process.argv.length < 5) {
throw new Error('Usage: node index.js approve <spender> <amount>');
}
const approvalHash = await app.approveSpending(process.argv[3], process.argv[4]);
console.log(`Approval TX Hash: ${approvalHash}`);
break;
default:
console.log('Invalid command. Use: balance, transfer, or approve');
}
}
} catch (error) {
// Global error handler
console.error('Application Error:', error.message);
process.exit(1);
}
}
// Handle any unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
// Handle any uncaught exceptions
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
process.exit(1);
});
// Run the main function
main();
module.exports = {
CBBTCAgentApp,
ConfigManager
};