Skip to content

Commit b0eaff9

Browse files
committed
Refactor Ethereum client integration and start price monitoring services
- Wrap ethclient.Client with EthClientWrapper to implement mock.EthClient interface - Start price monitoring and price alert services on application startup - Subscribe to new Ethereum block headers and process transactions for alerts - Ensure compatibility with repository
1 parent aa65ed3 commit b0eaff9

File tree

7 files changed

+61
-14
lines changed

7 files changed

+61
-14
lines changed

config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ sendgrid:
66
api_key: "YOUR_SENDGRID_API_KEY"
77
push_notification:
88
provider: "firebase" # or other provider
9-
api_key: "YOUR_PUSH_NOTIFICATION_API_KEY"
9+
api_key: "YOUR_PUSH_NOTIFICATION_API_KEY"
10+
coingecko:
11+
api_key: "YOUR_COINGECKO_API_KEY"

config/config.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func LoadConfig() (*Config, error) {
1919
DatabaseURL: "test_db_url",
2020
SendGridAPIKey: "test_api_key",
2121
InfuraProjectID: "test_infura_id",
22+
CoinGeckoAPIKey: "test_coingecko_key",
2223
}, nil
2324
}
2425
viper.SetConfigName("config")
@@ -29,9 +30,11 @@ func LoadConfig() (*Config, error) {
2930
return nil, err
3031
}
3132

32-
var cfg Config
33-
if err := viper.Unmarshal(&cfg); err != nil {
34-
return nil, err
33+
cfg := Config{
34+
DatabaseURL: viper.GetString("database.url"),
35+
SendGridAPIKey: viper.GetString("sendgrid.api_key"),
36+
InfuraProjectID: viper.GetString("infura.project_id"),
37+
CoinGeckoAPIKey: viper.GetString("coingecko.api_key"),
3538
}
3639

3740
return &cfg, nil

database/database.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func init() {
2727
}
2828

2929
func InitDB(dsn string) (*gorm.DB, error) {
30-
IsMockMode = true
3130
mu.Lock()
3231
defer mu.Unlock()
3332

@@ -36,7 +35,9 @@ func InitDB(dsn string) (*gorm.DB, error) {
3635
return nil, nil
3736
}
3837

39-
return nil, nil
38+
// Add actual database initialization here
39+
// For now, returning error to indicate it needs implementation
40+
return nil, errors.New("database initialization not implemented")
4041
}
4142

4243
func SetupMockDB() {

main.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,31 @@ import (
1616
nfts "github.com/Jetlum/WalletAlertService/nft"
1717
"github.com/Jetlum/WalletAlertService/repository"
1818
"github.com/Jetlum/WalletAlertService/services"
19+
"github.com/ethereum/go-ethereum/common"
1920
"github.com/ethereum/go-ethereum/core/types"
2021
"github.com/ethereum/go-ethereum/ethclient"
2122
)
2223

24+
type EthClientWrapper struct {
25+
*ethclient.Client
26+
}
27+
28+
func (w *EthClientWrapper) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
29+
return w.Client.BlockByHash(ctx, hash)
30+
}
31+
32+
func (w *EthClientWrapper) BlockNumber(ctx context.Context) (uint64, error) {
33+
return w.Client.BlockNumber(ctx)
34+
}
35+
36+
func (w *EthClientWrapper) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
37+
return w.Client.HeaderByNumber(ctx, number)
38+
}
39+
40+
func (w *EthClientWrapper) NetworkID(ctx context.Context) (*big.Int, error) {
41+
return w.Client.NetworkID(ctx)
42+
}
43+
2344
func init() {
2445
// Skip completely in test mode
2546
if os.Getenv("GO_ENV") == "test" {
@@ -75,17 +96,20 @@ func main() {
7596
priceAlertService.StartMonitoring()
7697

7798
// Connect to Ethereum node
78-
client, err := ethclient.Dial(fmt.Sprintf("wss://mainnet.infura.io/ws/v3/%s", cfg.InfuraProjectID))
99+
ethClient, err := ethclient.Dial(fmt.Sprintf("wss://mainnet.infura.io/ws/v3/%s", cfg.InfuraProjectID))
79100
if err != nil {
80101
log.Fatal(err)
81102
}
82-
defer client.Close()
103+
defer ethClient.Close()
104+
105+
client := &EthClientWrapper{Client: ethClient}
83106

84107
fmt.Println("Connected to Ethereum network")
85108

86109
// Subscribe to new head (block) events
87110
headers := make(chan *types.Header)
88-
sub, err := client.SubscribeNewHead(context.Background(), headers)
111+
// Update subscription
112+
sub, err := ethClient.SubscribeNewHead(context.Background(), headers)
89113
if err != nil {
90114
log.Fatal(err)
91115
}

services/notification.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/Jetlum/WalletAlertService/models"
7+
"github.com/sendgrid/sendgrid-go"
78
)
89

910
type NotificationService interface {
@@ -13,6 +14,7 @@ type NotificationService interface {
1314
func NewEmailNotification(apiKey string) *EmailNotification {
1415
return &EmailNotification{
1516
APIKey: apiKey,
17+
client: sendgrid.NewSendClient(apiKey),
1618
}
1719
}
1820

services/price_monitor.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type CoinGeckoPrice struct {
1919
USD float64 `json:"usd"`
2020
} `json:"bitcoin"`
2121
Ethereum struct {
22-
USD float64 `json:"ethereum"`
22+
USD float64 `json:"usd"`
2323
} `json:"ethereum"`
2424
}
2525

@@ -49,13 +49,23 @@ func (pm *PriceMonitor) updatePrices() error {
4949
}
5050
defer resp.Body.Close()
5151

52-
var prices CoinGeckoPrice
53-
if err := json.NewDecoder(resp.Body).Decode(&prices); err != nil {
52+
var result map[string]map[string]float64
53+
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
5454
return fmt.Errorf("failed to decode response: %w", err)
5555
}
5656

57-
pm.prices.Store("BTC", prices.Bitcoin.USD)
58-
pm.prices.Store("ETH", prices.Ethereum.USD)
57+
if btc, ok := result["bitcoin"]; ok {
58+
if price, ok := btc["usd"]; ok {
59+
pm.prices.Store("BTC", price)
60+
}
61+
}
62+
63+
if eth, ok := result["ethereum"]; ok {
64+
if price, ok := eth["usd"]; ok {
65+
pm.prices.Store("ETH", price)
66+
}
67+
}
68+
5969
return nil
6070
}
6171

services/push_notification.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package services
33

44
import "github.com/Jetlum/WalletAlertService/models"
55

6+
type Config struct {
7+
Provider string
8+
APIKey string
9+
}
10+
611
type PushNotifier interface {
712
Send(event *models.Event, userPref *models.UserPreference) error
813
}

0 commit comments

Comments
 (0)