-
Notifications
You must be signed in to change notification settings - Fork 4
/
repository.go
222 lines (177 loc) · 5.07 KB
/
repository.go
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
package coins
import (
"fmt"
"github.com/MinterTeam/minter-explorer-api/v2/blocks"
"github.com/MinterTeam/minter-explorer-api/v2/helpers"
"github.com/MinterTeam/minter-explorer-extender/v2/models"
"github.com/go-pg/pg/v10"
"strconv"
"sync"
)
type Repository struct {
db *pg.DB
baseModel *models.Coin
coins *sync.Map
blockListCoinIds []uint64
}
var GlobalRepository *Repository
func NewRepository(db *pg.DB) *Repository {
GlobalRepository = &Repository{
db: db,
coins: new(sync.Map),
}
return GlobalRepository
}
func (r Repository) GetAll() []models.Coin {
var coins []models.Coin
err := r.db.Model(&coins).Relation("OwnerAddress").Where("deleted_at is null").Select()
helpers.CheckErr(err)
return coins
}
// Get list of coins
func (r *Repository) GetCoins() []models.Coin {
var coins []models.Coin
allCoins := r.db.Model(new(models.Coin)).
ColumnExpr("id").
ColumnExpr("reserve").
Where("deleted_at is null").
UnionAll(r.db.Model(new(models.LiquidityPool)).
ColumnExpr("first_coin_id as id").
ColumnExpr("liquidity_bip as reserve")).
UnionAll(r.db.Model(new(models.LiquidityPool)).
ColumnExpr("second_coin_id as id").
ColumnExpr("liquidity_bip as reserve"))
err := r.db.Model().
With("all_coins", allCoins).
Table("all_coins").
ColumnExpr("coins.*").
ColumnExpr(`addresses.address as owner_address__address`).
Join("JOIN coins ON coins.id = all_coins.id").
Join("left outer JOIN addresses on addresses.id = coins.owner_address_id").
Where("coins.id not in (?)", pg.In(r.blockListCoinIds)).
GroupExpr("coins.id, addresses.address").
OrderExpr(`case when coins.id = 0 then 0 else 1 end`).
OrderExpr("max(all_coins.reserve) desc").
Select(&coins)
helpers.CheckErr(err)
return coins
}
// Get coin detail like symbol
func (r *Repository) GetLikeSymbolAndVersion(symbol string, version *uint64) []models.Coin {
var coins []models.Coin
query := r.db.Model(&coins).
Relation("OwnerAddress").
Where("symbol LIKE ?", fmt.Sprintf("%%%s%%", symbol)).
Where("deleted_at IS NULL").
Where(`"coin"."id" not in (?)`, pg.In(r.blockListCoinIds)).
OrderExpr(`case when "coin"."id" = 0 then 0 else 1 end`).
Order("reserve DESC")
if version != nil {
query.Where("version = ?", version)
}
err := query.Select()
helpers.CheckErr(err)
return coins
}
// Get coin detail by symbol
func (r *Repository) GetBySymbolAndVersion(symbol string, version *uint64) []models.Coin {
var coins []models.Coin
query := r.db.Model(&coins).
Relation("OwnerAddress").
Where("symbol = ?", symbol).
Where("deleted_at IS NULL").
OrderExpr(`case when "coin"."id" = 0 then 0 else 1 end`).
Order("reserve DESC")
if version != nil {
query.Where("version = ?", version)
}
err := query.Select()
helpers.CheckErr(err)
return coins
}
type CustomCoinsStatusData struct {
ReserveSum string
Count uint
}
// Get custom coins data for status page
func (r *Repository) GetCustomCoinsStatusData() (CustomCoinsStatusData, error) {
var data CustomCoinsStatusData
err := r.db.
Model(&models.Coin{}).
ColumnExpr("SUM(reserve) as reserve_sum, COUNT(*) as count").
Where("id != ?", 0).
Select(&data)
return data, err
}
func (r *Repository) FindByID(id uint) (models.Coin, error) {
var coin models.Coin
//if id == 0 && r.baseModel != nil {
// return *r.baseModel, nil
//}
if c, ok := r.coins.Load(id); ok {
return c.(models.Coin), nil
}
err := r.db.Model(&coin).
Where("id = ?", id).
Where("deleted_at IS NULL").
Select()
//if id == 0 && r.baseModel == nil {
// r.baseModel = &coin
//}
if err == nil {
r.coins.Store(id, coin)
}
return coin, err
}
func (r Repository) FindByIdWithOwner(id uint) (models.Coin, error) {
var coin models.Coin
if id == 0 && r.baseModel != nil {
return *r.baseModel, nil
}
err := r.db.Model(&coin).
Relation("OwnerAddress").
Where(`"coin"."id" = ?`, id).
Where("deleted_at IS NULL").
Select()
if id == 0 && r.baseModel == nil {
r.baseModel = &coin
}
return coin, err
}
func (r *Repository) FindIdBySymbol(symbol string) (uint64, error) {
if id, err := strconv.ParseUint(symbol, 10, 64); err != nil {
symbol, version := helpers.GetSymbolAndDefaultVersionFromStr(symbol)
coins := r.GetBySymbolAndVersion(symbol, &version)
if len(coins) == 0 {
return 0, pg.ErrNoRows
}
return uint64(coins[0].ID), nil
} else {
return id, nil
}
}
func (r *Repository) GetBySymbols(symbols []string) (coins []models.Coin, err error) {
err = r.db.Model(&coins).Where("symbol in (?)", pg.In(symbols)).Select()
return
}
func (r *Repository) OnNewBlock(block blocks.Resource) {
r.fillCoinsMap()
}
func (r *Repository) fillCoinsMap() {
wg := &sync.WaitGroup{}
for _, coin := range r.GetAll() {
wg.Add(1)
go func(wg *sync.WaitGroup, coin models.Coin) {
defer wg.Done()
r.coins.Store(uint64(coin.ID), coin)
}(wg, coin)
}
wg.Wait()
}
func (r *Repository) SetBlocklistCoinIds(ids []uint64) {
r.blockListCoinIds = ids
}
func (r *Repository) GetVerifiedCoins() (coins []models.Coin, err error) {
err = r.db.Model(&coins).Where("id in (select coin_id from token_contracts)").Select()
return
}