-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (83 loc) · 2.86 KB
/
main.go
File metadata and controls
100 lines (83 loc) · 2.86 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
// Main entry point for the swap script
package main
import (
"context"
"log"
"path/filepath"
"swap/internal/datatypes"
"swap/internal/utils"
"swap/pkg/logger"
"swap/service/jupiter"
"swap/service/swap"
"github.com/gagliardetto/solana-go/rpc"
jupClient "github.com/ilkamo/jupiter-go/jupiter"
"github.com/joho/godotenv"
solanaService "swap/service/solana"
"github.com/gagliardetto/solana-go"
)
func main() {
// Initialize the logger
activityLogPath := filepath.Join("logs", "activity.txt")
if err := logger.Init(activityLogPath); err != nil {
log.Fatalf("Failed to initialize logger: %v", err)
}
defer logger.Close()
// Initialize the swap logger
swapLogPath := filepath.Join("logs", "swap.txt")
if err := logger.InitSwapLogger(swapLogPath); err != nil {
logger.Error("Failed to initialize swap logger: %v", err)
}
defer logger.CloseSwapLogger()
logger.Info("Starting swap script")
// Load environment variables from .env file
if err := godotenv.Load(); err != nil {
logger.Warn("Error loading .env file: %v", err)
}
// Get private key and RPC endpoint from environment variables
privateKeyStr := utils.GetEnv("PRIVATE_KEY", "")
rpcEndpoint := utils.GetEnv("RPC_ENDPOINT", "https://api.mainnet-beta.solana.com")
// We need to create a config object manually since we don't have the config package yet
cfg := &datatypes.Config{
PrivateKey: privateKeyStr,
RPCEndpoint: rpcEndpoint,
StopLossPrice: 130.0,
MinimumSOL: 0.1,
CheckInterval: 2,
// Retry configuration
EnableRetry: true, // Enable retry by default
RetryAttempts: 3,
RetryDelay: 2,
// Dynamic stop loss configuration
DynamicStopLoss: true,
StopLossAdjustment: 5.0, // Keep stop loss $5 below the highest price
HighestPrice: 0.0, // Initialize highest price to 0
}
// Derive public key from private key
privateKey := solana.MustPrivateKeyFromBase58(cfg.PrivateKey)
publicKey := privateKey.PublicKey()
logger.Info("Public Key: %s", publicKey.String())
cfg.PublicKey = publicKey
// Initialize Solana client
client := rpc.New(cfg.RPCEndpoint)
jupClient, err := jupClient.NewClientWithResponses(jupClient.DefaultAPIURL)
if err != nil {
logger.Error("Failed to initialize Jupiter client: %v", err)
log.Fatalf("Failed to initialize Jupiter client: %v", err)
}
// Initialize services
solService := solanaService.NewService(client)
jupiterSvc := jupiter.NewService(jupClient, cfg)
// Create swap service
swapService, err := swap.NewService(cfg, client, solService, jupiterSvc)
if err != nil {
logger.Error("Failed to initialize swap service: %v", err)
log.Fatalf("Failed to initialize swap service: %v", err)
}
logger.Info("Starting swap monitoring service")
// Start the swap monitoring service
err = swapService.Start(context.Background())
if err != nil {
logger.Error("Swap service error: %v", err)
log.Fatalf("Swap service error: %v", err)
}
}