-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
261 lines (207 loc) · 4.86 KB
/
main.go
File metadata and controls
261 lines (207 loc) · 4.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
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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var newUser = User{Name: "Bryan"}
var bitcoin_asset = Asset{
Symbol: "BTC",
Name: "bitcoin",
}
var sol_asset = Asset{
Symbol: "SOL",
Name: "solana",
}
var eth_asset = Asset{
Symbol: "ETH",
Name: "ethereum",
}
type CoinGeckoResponse struct {
Bitcoin struct {
Usd int `json:"usd"`
} `json:"bitcoin"`
Solana struct {
Usd float64 `json:"usd"`
} `json:"solana"`
Ethereum struct {
Usd float64 `json:"usd"`
} `json:"ethereum"`
}
type User struct {
ID int `gorm:"primary_key`
Name string
}
type Asset struct {
ID int `gorm:"primary_key`
Symbol string `gorm:"unique;not null"`
Name string
}
type Transaction struct {
ID int `gorm:"primaryKey;"`
TS time.Time `gorm:"primaryKey;"`
UserId int
User User `gorm:"foreignKey:UserId"`
AssetId int
Asset Asset `gorm:"foreignKey:AssetId"`
Amount uint
PriceUsd float32
}
func seed_reference_data(db *gorm.DB) {
err := db.Transaction(func(tx *gorm.DB) error {
db_err := tx.Save(&newUser).Error
if db_err != nil {
fmt.Print(db_err.Error())
}
db_err = tx.Save(&newUser).Error
if db_err != nil {
fmt.Print(db_err.Error())
}
db_err = tx.Save(&bitcoin_asset).Error
if db_err != nil {
fmt.Print(db_err.Error())
}
db_err = tx.Save(&sol_asset).Error
if db_err != nil {
fmt.Print(db_err.Error())
}
db_err = tx.Create(ð_asset).Error
if db_err != nil {
fmt.Print(db_err.Error())
}
return nil
})
if err != nil {
fmt.Print(err.Error())
}
}
func get_asset_id_map(db *gorm.DB) (*[3]Asset, error) {
assets_from_tiger := [3]Asset{bitcoin_asset, sol_asset, eth_asset}
err := db.Find(&assets_from_tiger).Error
if err != nil {
fmt.Print(err.Error())
return nil, err
}
return &assets_from_tiger, nil
}
func get_user(db *gorm.DB) (*User, error) {
var user = User{}
err := db.First(&user).Error
if err != nil {
fmt.Print(err.Error())
return nil, err
}
fmt.Printf("Found User %v\n", user.ID)
return &user, nil
}
func fecth_prices() (*CoinGeckoResponse, error) {
fmt.Println("Calling Coin Gecko API...")
// COIN_GECKO_API_KEY
coin_gecko_api_key := os.Getenv("COIN_GECKO_API_KEY")
client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.coingecko.com/api/v3/simple/price?vs_currencies=usd&ids=bitcoin,solana,ethereum", nil)
if err != nil {
fmt.Print(err.Error())
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("x-cg-demo-api-key", coin_gecko_api_key)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Print(err.Error())
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Print(err.Error())
return nil, err
}
var responseObject CoinGeckoResponse
json.Unmarshal(bodyBytes, &responseObject)
fmt.Printf("API Response as struct %+v\n", responseObject)
return &responseObject, nil
}
func ingest_once(db *gorm.DB) {
var data, err = fecth_prices()
if err != nil {
panic(err.Error())
}
var user, err1 = get_user(db)
if err1 != nil {
panic(err1.Error())
}
ts := time.Now().UTC()
var asset_id_by_symbol, err2 = get_asset_id_map(db)
if err2 != nil {
panic(err2.Error())
}
err3 := db.Transaction(func(tx *gorm.DB) error {
btc_transaction := Transaction{
TS: ts,
User: *user,
Asset: asset_id_by_symbol[0],
UserId: user.ID,
AssetId: asset_id_by_symbol[0].ID,
PriceUsd: float32(data.Bitcoin.Usd),
Amount: 1,
ID: 1,
}
tx.Create(&btc_transaction)
sol_transaction := Transaction{
TS: ts,
User: *user,
Asset: asset_id_by_symbol[1],
UserId: user.ID,
AssetId: asset_id_by_symbol[1].ID,
PriceUsd: float32(data.Solana.Usd),
Amount: 1,
}
tx.Create(&sol_transaction)
eth_transaction := Transaction{
TS: ts,
User: *user,
Asset: asset_id_by_symbol[2],
UserId: user.ID,
AssetId: asset_id_by_symbol[2].ID,
PriceUsd: float32(data.Ethereum.Usd),
Amount: 1,
}
tx.Create(ð_transaction)
return nil
})
if err3 != nil {
panic(err3.Error())
}
}
func main() {
err := godotenv.Load()
dsn := os.Getenv("TIMESCALE_SERVICE_URL")
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
err = db.AutoMigrate(&User{})
err = db.AutoMigrate(&Asset{})
err = db.AutoMigrate(&Transaction{})
if err != nil {
panic("failed to migrate database")
}
// create the hypertable
tx := db.Exec("SELECT create_hypertable('transactions', 'ts')")
if tx.Error != nil {
fmt.Fprintf(os.Stderr, "Hypertable exists %v\n", tx.Error.Error())
}
seed_reference_data(db)
for {
ingest_once(db)
time.Sleep(30 * time.Second)
}
}