-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
178 lines (157 loc) · 4.95 KB
/
Copy pathmain.go
File metadata and controls
178 lines (157 loc) · 4.95 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
package main
import (
"encoding/csv"
"github.com/coti-io/coti-db-app/controllers"
dbprovider "github.com/coti-io/coti-db-app/db-provider"
"github.com/coti-io/coti-db-app/dto"
"github.com/coti-io/coti-db-app/entities"
service "github.com/coti-io/coti-db-app/services"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/shopspring/decimal"
"gorm.io/gorm"
"io"
"log"
"os"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
server := gin.Default()
// Init the db connection
dbprovider.Init()
// making sure all the app states exists and create them if not
verifyAppStates()
// making sure we have the native currency hash in the db
verifyNativeCurrencyHash()
// load cluster stamp if not loaded
loadClusterStamp()
// run the sync tasks
transactionService := service.NewTransactionService()
transactionService.RunSync()
// register routes
server.GET("/get-sync-state", controllers.GetSyncState)
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
// runs the server
serverRunError := server.Run(":" + port)
if serverRunError != nil {
log.Fatal("Server run error")
return
}
}
func verifyAppStates() {
for _, appStateName := range entities.AppStatesNames {
appState := entities.AppState{Name: appStateName}
appStateRes := dbprovider.DB.Where("name = ?", appStateName).FirstOrCreate(&appState)
if appStateRes.Error != nil {
panic("ERROR while checking app state: " + string(appStateName) + ",error: " + appStateRes.Error.Error())
}
}
}
func verifyNativeCurrencyHash() {
nativeSymbol := os.Getenv("NATIVE_SYMBOL")
if nativeSymbol == "" {
panic("NATIVE_SYMBOL is mandatory env variable")
}
var currencyService = service.NewCurrencyService()
// if not throw error
nativeCurrencyHash := currencyService.GetNativeCurrencyHash()
nativeCurrency := entities.Currency{Hash: nativeCurrencyHash}
nativeCurrencyError := dbprovider.DB.Where("hash = ?", nativeCurrencyHash).FirstOrCreate(&nativeCurrency).Error
if nativeCurrencyError != nil {
panic(nativeCurrencyError)
}
}
func loadClusterStamp() {
// check the app state if cluster stamp initialized for this db
appStateIsClusterStampInitialized := entities.AppState{}
err := dbprovider.DB.Where("name = ?", entities.IsClusterStampInitialized).First(&appStateIsClusterStampInitialized).Error
if err != nil {
panic(err)
}
if appStateIsClusterStampInitialized.Value == "true" {
return
}
currencyService := service.NewCurrencyService()
nativeCurrencyHash := currencyService.GetNativeCurrencyHash()
nativeCurrency := entities.Currency{}
nativeCurrencyError := dbprovider.DB.Where("hash = ?", nativeCurrencyHash).First(&nativeCurrency).Error
if nativeCurrencyError != nil {
panic(nativeCurrencyError)
}
clusterStampFileName := os.Getenv("CLUSTER_STAMP_FILE_NAME")
csvFile, err := os.Open(clusterStampFileName)
if err != nil {
panic(err)
}
log.Println("Successfully Opened CSV file")
defer fileClose(csvFile)
reader := csv.NewReader(csvFile)
err = dbprovider.DB.Transaction(initializeClusterStamp(reader, nativeCurrency, appStateIsClusterStampInitialized))
if err != nil {
panic(err)
}
}
func initializeClusterStamp(reader *csv.Reader, nativeCurrency entities.Currency, appStateIsClusterStampInitialized entities.AppState) func(dbTransaction *gorm.DB) error {
return func(dbTransaction *gorm.DB) error {
var addressBalances []entities.AddressBalance
recordsToSaveCounter := 0
for {
line, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Println("ERROR at initializeClusterStamp: " + err.Error())
break
}
clusterStampData := initClusterStampData(line, nativeCurrency)
addressBalance := entities.NewAddressBalanceFromClusterStamp(&clusterStampData)
addressBalances = append(addressBalances, *addressBalance)
recordsToSaveCounter += 1
if recordsToSaveCounter < 1000 {
continue
}
err = dbTransaction.Save(addressBalances).Error
if err != nil {
return err
}
addressBalances = []entities.AddressBalance{}
recordsToSaveCounter = 0
}
saveAddressBalances(dbTransaction, addressBalances)
appStateIsClusterStampInitialized.Value = "true"
return dbTransaction.Save(appStateIsClusterStampInitialized).Error
}
}
func initClusterStampData(line []string, nativeCurrency entities.Currency) dto.ClusterStampDataRow {
amount, err := decimal.NewFromString(line[1])
if err != nil {
log.Panic("Error while reading cluster stamp amount decimal value for " + line[0])
}
clusterStampData := dto.ClusterStampDataRow{
Address: line[0],
Amount: amount,
CurrencyId: nativeCurrency.ID,
}
return clusterStampData
}
func saveAddressBalances(dbTransaction *gorm.DB, addressBalances []entities.AddressBalance) {
if len(addressBalances) > 0 {
err := dbTransaction.Save(addressBalances).Error
if err != nil {
log.Panic("Error while saving address balances")
}
}
}
func fileClose(csvFile *os.File) {
err := csvFile.Close()
if err != nil {
panic(err)
}
}