decharge-scout 1.3.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +39 -12
- package/package.json +1 -1
- package/src/energy-data.js +40 -5
package/index.js
CHANGED
|
@@ -46,7 +46,8 @@ const __dirname = dirname(__filename);
|
|
|
46
46
|
// Configuration
|
|
47
47
|
const STAKE_AMOUNT = parseFloat(process.env.STAKE_AMOUNT || '0.01');
|
|
48
48
|
const CYCLE_INTERVAL_MS = 15 * 60 * 1000; // 15 minutes
|
|
49
|
-
|
|
49
|
+
// Use current working directory for wallet by default (not package installation dir)
|
|
50
|
+
const DEFAULT_WALLET_PATH = path.join(process.cwd(), 'wallet.json');
|
|
50
51
|
|
|
51
52
|
// Global state
|
|
52
53
|
let isRunning = true;
|
|
@@ -74,16 +75,35 @@ function generateAgentName() {
|
|
|
74
75
|
*/
|
|
75
76
|
function findExistingWallets() {
|
|
76
77
|
const wallets = [];
|
|
78
|
+
const homeDir = os.homedir();
|
|
79
|
+
const cwd = process.cwd();
|
|
80
|
+
|
|
77
81
|
const searchPaths = [
|
|
78
|
-
// Current directory
|
|
79
|
-
path.join(
|
|
80
|
-
path.join(
|
|
82
|
+
// Current directory - most common
|
|
83
|
+
path.join(cwd, 'wallet.json'),
|
|
84
|
+
path.join(cwd, 'id.json'),
|
|
85
|
+
path.join(cwd, 'keypair.json'),
|
|
86
|
+
path.join(cwd, 'solana-wallet.json'),
|
|
87
|
+
path.join(cwd, 'my-wallet.json'),
|
|
88
|
+
|
|
89
|
+
// Package directory (for global installations)
|
|
90
|
+
path.join(__dirname, 'wallet.json'),
|
|
91
|
+
path.join(__dirname, 'id.json'),
|
|
92
|
+
|
|
93
|
+
// Solana CLI default locations
|
|
94
|
+
path.join(homeDir, '.config', 'solana', 'id.json'),
|
|
95
|
+
path.join(homeDir, '.solana', 'id.json'),
|
|
96
|
+
path.join(homeDir, '.solana', 'devnet.json'),
|
|
97
|
+
path.join(homeDir, '.solana', 'testnet.json'),
|
|
98
|
+
|
|
81
99
|
// Home directory
|
|
82
|
-
path.join(
|
|
83
|
-
path.join(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
path.join(
|
|
100
|
+
path.join(homeDir, 'wallet.json'),
|
|
101
|
+
path.join(homeDir, 'solana-wallet.json'),
|
|
102
|
+
|
|
103
|
+
// Downloads (users often save here)
|
|
104
|
+
path.join(homeDir, 'Downloads', 'wallet.json'),
|
|
105
|
+
path.join(homeDir, 'Downloads', 'solana-wallet.json'),
|
|
106
|
+
path.join(homeDir, 'Downloads', 'keypair.json'),
|
|
87
107
|
];
|
|
88
108
|
|
|
89
109
|
for (const walletPath of searchPaths) {
|
|
@@ -119,10 +139,11 @@ async function ensureWallet(walletPath) {
|
|
|
119
139
|
console.log(chalk.yellow(`\n⚠️ No wallet found at ${walletPath}`));
|
|
120
140
|
|
|
121
141
|
// Search for existing wallets
|
|
142
|
+
console.log(chalk.blue('🔍 Searching for existing Solana wallets...'));
|
|
122
143
|
const existingWallets = findExistingWallets();
|
|
123
144
|
|
|
124
145
|
if (existingWallets.length > 0) {
|
|
125
|
-
console.log(chalk.green(`\n
|
|
146
|
+
console.log(chalk.green(`\n✓ Found ${existingWallets.length} existing wallet(s):\n`));
|
|
126
147
|
|
|
127
148
|
existingWallets.forEach((wallet, index) => {
|
|
128
149
|
console.log(chalk.cyan(` ${index + 1}. ${wallet.name}`));
|
|
@@ -142,6 +163,9 @@ async function ensureWallet(walletPath) {
|
|
|
142
163
|
console.log(chalk.yellow('Invalid selection, creating new wallet...'));
|
|
143
164
|
}
|
|
144
165
|
}
|
|
166
|
+
} else {
|
|
167
|
+
console.log(chalk.yellow('⚠️ No existing wallets found in common locations.'));
|
|
168
|
+
console.log(chalk.gray(' Searched: ~/.solana/id.json, ./wallet.json, ./id.json, etc.\n'));
|
|
145
169
|
}
|
|
146
170
|
|
|
147
171
|
const answer = await question('Create a new wallet? (Y/n): ');
|
|
@@ -177,6 +201,7 @@ async function ensureWallet(walletPath) {
|
|
|
177
201
|
writeFileSync(walletPath, JSON.stringify(privateKey));
|
|
178
202
|
|
|
179
203
|
console.log(chalk.green(`✓ Wallet imported: ${keypair.publicKey.toBase58()}`));
|
|
204
|
+
console.log(chalk.blue(`📁 Wallet saved to: ${walletPath}`));
|
|
180
205
|
return walletPath;
|
|
181
206
|
} catch (error) {
|
|
182
207
|
console.log(chalk.red(`\n❌ Invalid private key: ${error.message}`));
|
|
@@ -193,6 +218,7 @@ async function ensureWallet(walletPath) {
|
|
|
193
218
|
writeFileSync(walletPath, JSON.stringify(secretKey));
|
|
194
219
|
|
|
195
220
|
console.log(chalk.green(`✓ Wallet created: ${keypair.publicKey.toBase58()}`));
|
|
221
|
+
console.log(chalk.blue(`📁 Wallet saved to: ${walletPath}`));
|
|
196
222
|
console.log(chalk.yellow('⚠️ IMPORTANT: Backup this wallet file!'));
|
|
197
223
|
console.log(chalk.blue(`\nYou need devnet SOL. Get it from:`));
|
|
198
224
|
console.log(chalk.gray(' solana airdrop 1 ' + keypair.publicKey.toBase58() + ' --url devnet'));
|
|
@@ -373,8 +399,9 @@ async function runQueryCycle(wallet, agentName, location, options) {
|
|
|
373
399
|
let energyData;
|
|
374
400
|
|
|
375
401
|
try {
|
|
376
|
-
energyData = await fetchEnergyData();
|
|
377
|
-
|
|
402
|
+
energyData = await fetchEnergyData(location);
|
|
403
|
+
const gridRegion = energyData[0]?.source?.split('-')[1] || 'Unknown';
|
|
404
|
+
dataSpinner.succeed(chalk.green(`Fetched ${energyData.length} data points from ${energyData[0]?.source || 'EIA'}`));
|
|
378
405
|
} catch (error) {
|
|
379
406
|
dataSpinner.warn(chalk.yellow(`EIA API failed, trying Electricity Maps...`));
|
|
380
407
|
|
package/package.json
CHANGED
package/src/energy-data.js
CHANGED
|
@@ -13,15 +13,48 @@ const EIA_API_KEY = process.env.EIA_API_KEY;
|
|
|
13
13
|
const EIA_BASE_URL = 'https://api.eia.gov/v2';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
16
|
+
* Map location to EIA grid region codes
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
function getGridRegionForLocation(location) {
|
|
19
|
+
const locationLower = location.toLowerCase();
|
|
20
|
+
|
|
21
|
+
// Map states/regions to grid operators
|
|
22
|
+
if (locationLower.includes('texas') || locationLower.includes('tx') || locationLower.includes('dallas') || locationLower.includes('houston') || locationLower.includes('austin')) {
|
|
23
|
+
return 'ERCT'; // ERCOT (Texas)
|
|
24
|
+
} else if (locationLower.includes('california') || locationLower.includes('ca')) {
|
|
25
|
+
return 'CISO'; // California ISO
|
|
26
|
+
} else if (locationLower.includes('new york') || locationLower.includes('ny')) {
|
|
27
|
+
return 'NYIS'; // New York ISO
|
|
28
|
+
} else if (locationLower.includes('new england') || locationLower.includes('massachusetts') || locationLower.includes('ma')) {
|
|
29
|
+
return 'ISNE'; // ISO New England
|
|
30
|
+
} else if (locationLower.includes('pjm') || locationLower.includes('pennsylvania') || locationLower.includes('pa')) {
|
|
31
|
+
return 'PJM'; // PJM Interconnection
|
|
32
|
+
} else if (locationLower.includes('miso') || locationLower.includes('midwest')) {
|
|
33
|
+
return 'MISO'; // Midcontinent ISO
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Default to ERCOT if unknown
|
|
37
|
+
return 'ERCT';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Fetch energy data from EIA API
|
|
42
|
+
*/
|
|
43
|
+
export async function fetchEnergyData(location = 'Texas, USA') {
|
|
19
44
|
if (!EIA_API_KEY || EIA_API_KEY === 'your_eia_api_key_here' || EIA_API_KEY.length < 20) {
|
|
20
45
|
console.warn('⚠️ EIA_API_KEY not configured or invalid');
|
|
21
46
|
console.warn(' Get a free key: https://www.eia.gov/opendata/register.php');
|
|
22
47
|
throw new Error('EIA_API_KEY not configured');
|
|
23
48
|
}
|
|
24
49
|
|
|
50
|
+
// Debug logging (show first 6 and last 4 chars of API key)
|
|
51
|
+
const keyPreview = `${EIA_API_KEY.substring(0, 6)}...${EIA_API_KEY.substring(EIA_API_KEY.length - 4)}`;
|
|
52
|
+
console.log(`📡 Using EIA API key: ${keyPreview}`);
|
|
53
|
+
|
|
54
|
+
// Determine grid region from location
|
|
55
|
+
const gridRegion = getGridRegionForLocation(location);
|
|
56
|
+
console.log(`🗺️ Location: ${location} → Grid Region: ${gridRegion}`);
|
|
57
|
+
|
|
25
58
|
try {
|
|
26
59
|
// Calculate time range (last 24 hours to next 24 hours for forecast)
|
|
27
60
|
const now = new Date();
|
|
@@ -29,12 +62,12 @@ export async function fetchEnergyData() {
|
|
|
29
62
|
const startDate = yesterday.toISOString().split('T')[0] + 'T00';
|
|
30
63
|
const endDate = now.toISOString().split('T')[0] + 'T23';
|
|
31
64
|
|
|
32
|
-
// EIA API endpoint for
|
|
65
|
+
// EIA API endpoint for regional real-time data
|
|
33
66
|
const url = `${EIA_BASE_URL}/electricity/rto/region-data/data/?` +
|
|
34
67
|
`api_key=${EIA_API_KEY}` +
|
|
35
68
|
`&frequency=hourly` +
|
|
36
69
|
`&data[0]=value` +
|
|
37
|
-
`&facets[respondent][]
|
|
70
|
+
`&facets[respondent][]=${gridRegion}` +
|
|
38
71
|
`&facets[type][]=D` + // Demand
|
|
39
72
|
`&start=${startDate}` +
|
|
40
73
|
`&end=${endDate}` +
|
|
@@ -42,6 +75,8 @@ export async function fetchEnergyData() {
|
|
|
42
75
|
`&sort[0][direction]=desc` +
|
|
43
76
|
`&length=48`;
|
|
44
77
|
|
|
78
|
+
console.log(`🔗 EIA API URL: ${url.replace(EIA_API_KEY, keyPreview)}`);
|
|
79
|
+
|
|
45
80
|
const response = await fetch(url, {
|
|
46
81
|
headers: {
|
|
47
82
|
'Accept': 'application/json'
|
|
@@ -74,7 +109,7 @@ export async function fetchEnergyData() {
|
|
|
74
109
|
hour: new Date(item.period).getHours(),
|
|
75
110
|
demand: demand,
|
|
76
111
|
price: Math.max(0.03, Math.min(0.15, price)), // Clamp between 3-15 cents
|
|
77
|
-
source:
|
|
112
|
+
source: `EIA-${gridRegion}`
|
|
78
113
|
};
|
|
79
114
|
});
|
|
80
115
|
|