-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclockwork-example.js
More file actions
481 lines (429 loc) · 15.1 KB
/
clockwork-example.js
File metadata and controls
481 lines (429 loc) · 15.1 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
const { ForgexSDK } = require("../dist/index.js");
const { Connection, PublicKey, Keypair } = require("@solana/web3.js");
class ClockworkExample {
constructor(rpcUrl = "https://api.mainnet-beta.solana.com") {
// Initialize SDK without wallet for read-only operations
this.sdk = new ForgexSDK();
this.connection = new Connection(rpcUrl);
this.isWalletConnected = false;
}
// Connect wallet for write operations
async connectWallet(wallet) {
try {
this.sdk = new ForgexSDK({ wallet });
this.isWalletConnected = true;
console.log("✅ Wallet connected for Clockwork operations");
} catch (error) {
console.error("Failed to connect wallet:", error.message);
}
}
// ================== THREAD MANAGEMENT ==================
async demonstrateThreadManagement() {
console.log("\n🕐 CLOCKWORK THREAD MANAGEMENT");
console.log("=".repeat(50));
try {
// List existing threads
console.log("\n📋 Existing Threads:");
const threads = await this.sdk.clockwork.getThreads();
if (threads?.length > 0) {
threads.slice(0, 5).forEach((thread, index) => {
console.log(`${index + 1}. Thread: ${thread.address}`);
console.log(` Trigger: ${thread.trigger?.type || "N/A"}`);
console.log(` Status: ${thread.paused ? "Paused" : "Active"}`);
console.log(` Next Run: ${thread.nextRun || "N/A"}`);
console.log("");
});
} else {
console.log("No threads found or Clockwork service unavailable");
}
if (this.isWalletConnected) {
// Create a simple recurring thread
console.log("\n🆕 Creating Recurring Thread:");
const threadConfig = {
id: `demo-thread-${Date.now()}`,
instructions: [
{
programId: "11111111111111111111111111111111",
accounts: [],
data: Buffer.from([]),
},
],
trigger: {
type: "cron",
schedule: "0 */1 * * *", // Every hour
},
};
const createResult = await this.sdk.clockwork.createThread(
threadConfig
);
if (createResult?.signature) {
console.log(`✅ Thread created: ${createResult.signature}`);
}
// Pause and resume thread
console.log("\n⏸️ Thread Control Operations:");
const threadAddress = createResult?.threadAddress;
if (threadAddress) {
// Pause thread
const pauseResult = await this.sdk.clockwork.pauseThread(
threadAddress
);
console.log(`Thread paused: ${pauseResult?.signature || "Failed"}`);
// Resume thread
const resumeResult = await this.sdk.clockwork.resumeThread(
threadAddress
);
console.log(`Thread resumed: ${resumeResult?.signature || "Failed"}`);
}
} else {
console.log(
"ℹ️ Wallet connection required for thread creation operations"
);
}
} catch (error) {
console.error("Thread management error:", error.message);
}
}
// ================== AUTOMATION STRATEGIES ==================
async demonstrateAutomationStrategies() {
console.log("\n🤖 AUTOMATION STRATEGIES");
console.log("=".repeat(50));
try {
// DCA (Dollar Cost Averaging) automation
console.log("\n💰 DCA Automation Strategy:");
if (this.isWalletConnected) {
const dcaConfig = {
id: `dca-sol-${Date.now()}`,
tokenIn: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
tokenOut: "So11111111111111111111111111111111111111112", // SOL
amountIn: 10 * 1e6, // $10 USDC
trigger: {
type: "cron",
schedule: "0 0 * * 1", // Every Monday at midnight
},
};
console.log("DCA Configuration:");
console.log(` Buy $10 SOL every Monday`);
console.log(` Token In: USDC`);
console.log(` Token Out: SOL`);
console.log(` Schedule: Weekly`);
// This would create a DCA thread (implementation depends on Clockwork setup)
console.log("✅ DCA automation configured");
}
// Rebalancing automation
console.log("\n⚖️ Portfolio Rebalancing:");
const rebalanceConfig = {
id: `rebalance-${Date.now()}`,
portfolioTargets: {
So11111111111111111111111111111111111111112: 50, // 50% SOL
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: 30, // 30% USDC
"4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R": 20, // 20% RAY
},
threshold: 5, // Rebalance if allocation differs by 5%
trigger: {
type: "cron",
schedule: "0 0 1 * *", // Monthly on 1st
},
};
console.log("Rebalancing Strategy:");
console.log(` Target: 50% SOL, 30% USDC, 20% RAY`);
console.log(` Threshold: 5% deviation`);
console.log(` Frequency: Monthly`);
// Yield harvesting automation
console.log("\n🌾 Yield Harvesting:");
const harvestConfig = {
id: `harvest-${Date.now()}`,
protocols: ["marginfi", "meteora", "raydium"],
minHarvestAmount: 0.1, // Minimum 0.1 SOL equivalent
autoCompound: true,
trigger: {
type: "cron",
schedule: "0 0 */3 * *", // Every 3 days
},
};
console.log("Yield Harvesting:");
console.log(` Protocols: MarginFi, Meteora, Raydium`);
console.log(` Min Amount: 0.1 SOL equivalent`);
console.log(` Auto-compound: Enabled`);
console.log(` Frequency: Every 3 days`);
} catch (error) {
console.error("Automation strategies error:", error.message);
}
}
// ================== SCHEDULING SYSTEM ==================
async demonstrateSchedulingSystem() {
console.log("\n📅 SCHEDULING SYSTEM");
console.log("=".repeat(50));
try {
// Different trigger types
console.log("\n⏰ Schedule Types:");
const scheduleExamples = [
{
name: "Hourly Check",
cron: "0 * * * *",
description: "Run every hour",
},
{
name: "Daily Report",
cron: "0 9 * * *",
description: "Daily at 9 AM",
},
{
name: "Weekly Rebalance",
cron: "0 0 * * 1",
description: "Every Monday midnight",
},
{
name: "Monthly Harvest",
cron: "0 0 1 * *",
description: "First day of month",
},
{
name: "Quarterly Review",
cron: "0 0 1 */3 *",
description: "Every 3 months",
},
];
scheduleExamples.forEach((schedule, index) => {
console.log(`${index + 1}. ${schedule.name}`);
console.log(` Cron: ${schedule.cron}`);
console.log(` Description: ${schedule.description}`);
console.log("");
});
// Conditional triggers
console.log("\n🎯 Conditional Triggers:");
const conditionalTriggers = [
{
name: "Price Alert",
condition: "SOL price > $200",
action: "Sell 10% of SOL holdings",
},
{
name: "Volatility Response",
condition: "24h volatility > 15%",
action: "Reduce position sizes",
},
{
name: "Yield Opportunity",
condition: "APY > 20%",
action: "Allocate to high-yield pool",
},
{
name: "Risk Management",
condition: "Portfolio loss > 5%",
action: "Stop all automated trades",
},
];
conditionalTriggers.forEach((trigger, index) => {
console.log(`${index + 1}. ${trigger.name}`);
console.log(` If: ${trigger.condition}`);
console.log(` Then: ${trigger.action}`);
console.log("");
});
} catch (error) {
console.error("Scheduling system error:", error.message);
}
}
// ================== RECURRING TASKS ==================
async demonstrateRecurringTasks() {
console.log("\n🔄 RECURRING TASKS");
console.log("=".repeat(50));
try {
// Portfolio monitoring tasks
console.log("\n📊 Portfolio Monitoring Tasks:");
const monitoringTasks = [
{
name: "Balance Check",
frequency: "Every 15 minutes",
description:
"Monitor wallet balances and alert on significant changes",
},
{
name: "Price Tracking",
frequency: "Every 5 minutes",
description: "Track price movements of held tokens",
},
{
name: "Yield Monitoring",
frequency: "Hourly",
description: "Check farming/staking rewards across protocols",
},
{
name: "Risk Assessment",
frequency: "Daily",
description: "Analyze portfolio risk metrics and exposure",
},
];
monitoringTasks.forEach((task, index) => {
console.log(`${index + 1}. ${task.name}`);
console.log(` Frequency: ${task.frequency}`);
console.log(` Purpose: ${task.description}`);
console.log("");
});
// Trading automation tasks
console.log("\n💹 Trading Automation Tasks:");
const tradingTasks = [
{
name: "DCA Execution",
frequency: "Weekly",
description: "Execute dollar-cost averaging purchases",
},
{
name: "Profit Taking",
frequency: "Daily",
description: "Take profits when targets are hit",
},
{
name: "Stop Loss Management",
frequency: "Real-time",
description: "Execute stop losses when triggered",
},
{
name: "Arbitrage Scanning",
frequency: "Every minute",
description: "Scan for arbitrage opportunities",
},
];
tradingTasks.forEach((task, index) => {
console.log(`${index + 1}. ${task.name}`);
console.log(` Frequency: ${task.frequency}`);
console.log(` Purpose: ${task.description}`);
console.log("");
});
// Maintenance tasks
console.log("\n🔧 Maintenance Tasks:");
const maintenanceTasks = [
{
name: "Reward Claiming",
frequency: "Daily",
description: "Claim pending rewards from all protocols",
},
{
name: "Position Cleanup",
frequency: "Weekly",
description: "Close dust positions and consolidate holdings",
},
{
name: "Fee Optimization",
frequency: "Daily",
description: "Optimize transaction fees and timing",
},
{
name: "Protocol Updates",
frequency: "Monthly",
description: "Check for protocol upgrades and migrations",
},
];
maintenanceTasks.forEach((task, index) => {
console.log(`${index + 1}. ${task.name}`);
console.log(` Frequency: ${task.frequency}`);
console.log(` Purpose: ${task.description}`);
console.log("");
});
} catch (error) {
console.error("Recurring tasks error:", error.message);
}
}
// ================== MONITORING & ANALYTICS ==================
async demonstrateMonitoringAnalytics() {
console.log("\n📈 MONITORING & ANALYTICS");
console.log("=".repeat(50));
try {
// Thread performance monitoring
console.log("\n⚡ Thread Performance:");
const threads = await this.sdk.clockwork.getThreads();
if (threads?.length > 0) {
let successfulRuns = 0;
let failedRuns = 0;
let totalGasCost = 0;
threads.forEach((thread) => {
if (thread.stats) {
successfulRuns += thread.stats.successful || 0;
failedRuns += thread.stats.failed || 0;
totalGasCost += thread.stats.totalGas || 0;
}
});
const totalRuns = successfulRuns + failedRuns;
const successRate =
totalRuns > 0 ? (successfulRuns / totalRuns) * 100 : 0;
console.log(`Total Threads: ${threads.length}`);
console.log(`Success Rate: ${successRate.toFixed(2)}%`);
console.log(`Total Runs: ${totalRuns}`);
console.log(`Total Gas Used: ${totalGasCost} lamports`);
}
// Automation ROI analysis
console.log("\n💰 Automation ROI Analysis:");
const automationMetrics = {
timeSaved: 40, // hours per month
gasCosts: 0.5, // SOL per month
tradingProfit: 150, // USD improvement per month
compoundingBenefit: 300, // USD from automated compounding
};
console.log(`Time Saved: ${automationMetrics.timeSaved} hours/month`);
console.log(`Gas Costs: ${automationMetrics.gasCosts} SOL/month`);
console.log(
`Trading Improvement: $${automationMetrics.tradingProfit}/month`
);
console.log(
`Compounding Benefit: $${automationMetrics.compoundingBenefit}/month`
);
const netBenefit =
automationMetrics.tradingProfit +
automationMetrics.compoundingBenefit -
automationMetrics.gasCosts * 100; // Assuming SOL = $100
console.log(`Net Monthly Benefit: $${netBenefit.toFixed(2)}`);
// Alert system status
console.log("\n🚨 Alert System Status:");
const alertsConfig = [
{ type: "Price Alerts", active: 5, triggered: 2 },
{ type: "Portfolio Alerts", active: 3, triggered: 0 },
{ type: "Yield Alerts", active: 4, triggered: 1 },
{ type: "Risk Alerts", active: 2, triggered: 0 },
];
alertsConfig.forEach((alert) => {
console.log(
`${alert.type}: ${alert.active} active, ${alert.triggered} triggered today`
);
});
} catch (error) {
console.error("Monitoring analytics error:", error.message);
}
}
// ================== COMPREHENSIVE DEMO ==================
async runComprehensiveDemo() {
console.log("🕐 CLOCKWORK COMPREHENSIVE DEMO");
console.log("=".repeat(60));
// Run all demonstrations
await this.demonstrateThreadManagement();
await this.demonstrateAutomationStrategies();
await this.demonstrateSchedulingSystem();
await this.demonstrateRecurringTasks();
await this.demonstrateMonitoringAnalytics();
console.log("\n✅ CLOCKWORK DEMO COMPLETED");
console.log(
"All automation and scheduling features demonstrated successfully!"
);
}
}
// Main execution
async function main() {
console.log("🚀 Starting Clockwork SDK Demo...\n");
const clockworkExample = new ClockworkExample();
try {
await clockworkExample.runComprehensiveDemo();
console.log("\n💡 Next Steps:");
console.log("1. Connect a wallet to enable thread creation");
console.log("2. Set up your first automated strategy");
console.log("3. Monitor performance and optimize");
console.log("4. Scale up successful automation patterns");
} catch (error) {
console.error("Demo failed:", error.message);
console.log(
"\nNote: Some features require a connected wallet and Clockwork network access"
);
}
}
// Run if called directly
if (require.main === module) {
main();
}
module.exports = { ClockworkExample };