-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
291 lines (255 loc) · 8.88 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package price
import (
"github.com/dezswap/cosmwasm-etl/configs"
"github.com/dezswap/cosmwasm-etl/pkg/db"
"github.com/dezswap/cosmwasm-etl/pkg/db/schemas"
"github.com/lib/pq"
"github.com/pkg/errors"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"time"
)
type SrcRepo interface {
FirstHeight(priceToken string) (int64, error)
CurrHeight() (int64, error)
NextHeight(minHeight uint64) (int64, error)
Txs(height uint64) ([]schemas.ParsedTx, error)
Decimals(asset string) (int64, error)
LatestRouteUpdateTimestamp() (float64, error)
Route(endToken string) (map[string][][]string, error)
Liquidity(height uint64, token string, priceToken string) (string, string, error)
UpdateDirectPrice(height uint64, token string, price string, priceToken string, isReverse bool) error
UpdateRoutePrice(height uint64, token string, price string, priceToken string, route []string) error
}
var _ SrcRepo = &srcRepoImpl{}
type srcRepoImpl struct {
db *gorm.DB
chainId string
}
func NewRepo(chainId string, dbConfig configs.RdbConfig) SrcRepo {
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Error, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: false, // Disable color
},
)
pq := db.PostgresDb{}
err := pq.Init(dbConfig)
if err != nil {
panic(err)
}
gormDB, err := gorm.Open(postgres.New(postgres.Config{
Conn: pq.Db,
}), &gorm.Config{
Logger: newLogger,
})
if err != nil {
panic(err)
}
return &srcRepoImpl{
db: gormDB,
chainId: chainId,
}
}
func (r *srcRepoImpl) FirstHeight(priceToken string) (int64, error) {
height := NA_VALUE
tx := r.db.Model(schemas.ParsedTx{}).Where(
"chain_id = ? and (asset0 = ? or asset1 = ?)", r.chainId, priceToken, priceToken).Select(
"coalesce(min(height), ?)", NA_VALUE).Find(&height)
if tx.Error != nil {
return 0, errors.Wrap(tx.Error, "repo.FirstHeight")
}
return height, nil
}
func (r *srcRepoImpl) CurrHeight() (int64, error) {
query := `
select coalesce(max(height), 0) from price where chain_id = ?
`
height := NA_VALUE
tx := r.db.Raw(query, r.chainId).Find(&height)
if tx.Error != nil {
return 0, errors.Wrap(tx.Error, "repo.NextHeight")
}
return height, nil
}
func (r *srcRepoImpl) NextHeight(minHeight uint64) (int64, error) {
query := `
select coalesce(min(pt.height), ?)
from parsed_tx pt
left join ( -- include first provision
select contract, min(height) height
from parsed_tx
where type = 'provide'
group by contract) t on pt.contract = t.contract and pt.height = t.height
where pt.chain_id = ?
and (pt.type = 'swap' or t.height is not null)
and pt.height > (select coalesce(max(height), 0) from price where chain_id = ?)
and pt.height > ?
`
height := NA_VALUE
tx := r.db.Raw(query, NA_VALUE, r.chainId, r.chainId, minHeight).Find(&height)
if tx.Error != nil {
return 0, errors.Wrap(tx.Error, "repo.NextHeight")
}
return height, nil
}
func (r *srcRepoImpl) Txs(height uint64) ([]schemas.ParsedTx, error) {
var res []schemas.ParsedTx
tx := r.db.Model(
schemas.ParsedTx{}).Joins(
"left join (select contract, min(height) height from parsed_tx where type = 'provide' group by contract) t "+ // include first provision
"on parsed_tx.contract = t.contract and parsed_tx.height = t.height and parsed_tx.type = 'provide'").Select(
"parsed_tx.height, parsed_tx.asset0, parsed_tx.asset0_amount, parsed_tx.asset1, parsed_tx.asset1_amount").Where(
"parsed_tx.chain_id = ? and parsed_tx.height = ? and (type = 'swap' or t.height is not null)",
r.chainId, height).Find(&res)
if tx.Error != nil {
return nil, errors.Wrap(tx.Error, "repo.Txs")
}
return res, nil
}
func (r *srcRepoImpl) Decimals(asset string) (int64, error) {
var res int64
tx := r.db.Table("tokens").Select(
"decimals").Where(
"chain_id = ? and address = ?", r.chainId, asset).Find(&res)
if tx.Error != nil {
return 0, errors.Wrap(tx.Error, "repo.Decimals")
}
return res, nil
}
func (r *srcRepoImpl) LatestRouteUpdateTimestamp() (float64, error) {
var ts float64
if tx := r.db.Model(schemas.Route{}).Where(
"chain_id = ?", r.chainId).Select(
"coalesce(min(created_at), 0)").Find(&ts); tx.Error != nil {
return 0, errors.Wrap(tx.Error, "repo.LatestRouteUpdateTimestamp")
}
return ts, nil
}
func (r *srcRepoImpl) Route(endToken string) (map[string][][]string, error) {
type result struct {
Asset0 string
Route pq.StringArray `gorm:"column:route;type:text[]"`
}
var res []result
tx := r.db.Model(schemas.Route{}).Select(
"asset0, route").Where(
"chain_id = ? and asset1 = ?", r.chainId, endToken).Order(
"hop_count asc").Find( // hop_count ordering is essential for routes comparison, refer to priceImpl.selectRoute(...)
&res)
if tx.Error != nil {
return nil, errors.Wrap(tx.Error, "repo.Route")
}
convertedRes := make(map[string][][]string)
for _, r := range res {
if convertedRes[r.Asset0] == nil {
convertedRes[r.Asset0] = make([][]string, 0)
}
convertedRes[r.Asset0] = append(convertedRes[r.Asset0], r.Route)
}
return convertedRes, nil
}
func (r *srcRepoImpl) Liquidity(height uint64, token string, priceToken string) (string, string, error) {
type result struct {
Asset0 string
Liquidity0 string
Asset1 string
Liquidity1 string
}
var res result
tx := r.db.Model(schemas.LpHistory{}).Joins(
"join (select p.id pair_id, p.asset0, p.asset1, max(lh.height) height from lp_history lh join pair p on p.id = lh.pair_id "+
"where lh.chain_id = ? and lh.height <= ? and ((p.asset0 = ? and p.asset1 = ?) or (p.asset0 = ? and p.asset1 = ?)) group by p.id) t "+
"on lp_history.height = t.height and lp_history.pair_id = t.pair_id",
r.chainId, height, token, priceToken, priceToken, token).Select("asset0, liquidity0, asset1, liquidity1").Find(&res)
if tx.Error != nil {
return "", "", errors.Wrap(tx.Error, "repo.Liquidity")
}
if res.Asset0 == token {
return res.Liquidity0, res.Liquidity1, nil
} else if res.Asset1 == token {
return res.Liquidity1, res.Liquidity0, nil
}
return "0", "0", nil
}
func (r *srcRepoImpl) UpdateDirectPrice(height uint64, token string, price string, priceToken string, isReverse bool) error {
type result struct {
TokenId uint64
PriceTokenId uint64
RouteId uint64
}
var res result
var tx *gorm.DB
if isReverse {
tx = r.db.Table(
"tokens").Select(
"tokens.id token_id, tr.token_id price_token_id, tr.route_id").Joins(
"join (select t.id token_id, t.chain_id, r.asset1, r.id route_id "+
"from tokens t join route r on t.chain_id = r.chain_id and t.address = r.asset0 "+
"where t.chain_id = ? and t.address = ? and r.hop_count = 1) tr "+
"on tr.chain_id = tokens.chain_id and tr.asset1 = tokens.address", r.chainId, priceToken).Where(
"tokens.address = ?", token).Find(&res)
} else {
tx = r.db.Table(
"tokens").Select(
"tr.token_id, tokens.id price_token_id, tr.route_id").Joins(
"join (select t.id token_id, t.chain_id, r.asset1, r.id route_id "+
"from tokens t join route r on t.chain_id = r.chain_id and t.address = r.asset0 "+
"where t.chain_id = ? and t.address = ? and r.hop_count = 1) tr "+
"on tr.chain_id = tokens.chain_id and tr.asset1 = tokens.address", r.chainId, token).Where(
"tokens.address = ?", priceToken).Find(&res)
}
if tx.Error != nil {
return errors.Wrap(tx.Error, "repo.UpdateDirectPrice")
}
tx = r.db.Model(schemas.Price{}).Create(
&schemas.Price{
Height: height,
ChainId: r.chainId,
TokenId: res.TokenId,
Price: price,
PriceTokenId: res.PriceTokenId,
RouteId: res.RouteId})
if tx.Error != nil {
return errors.Wrap(tx.Error, "repo.UpdateDirectPrice")
}
return nil
}
func (r *srcRepoImpl) UpdateRoutePrice(height uint64, token string, price string, priceToken string, route []string) error {
type result struct {
TokenId uint64
PriceTokenId uint64
RouteId uint64
}
var res result
var tx *gorm.DB
tx = r.db.Table(
"tokens").Select(
"tr.token_id, tokens.id price_token_id, tr.route_id").Joins(
"join (select t.id token_id, t.chain_id, r.asset1, r.id route_id "+
"from tokens t join route r on t.chain_id = r.chain_id and t.address = r.asset0 "+
"where t.chain_id = ? and t.address = ? and r.route = ?) tr "+
"on tr.chain_id = tokens.chain_id and tr.asset1 = tokens.address", r.chainId, token, pq.StringArray(route)).Where(
"tokens.address = ?", priceToken).Find(&res)
if tx.Error != nil {
return errors.Wrap(tx.Error, "repo.UpdateRoutePrice")
}
tx = r.db.Model(schemas.Price{}).Create(
&schemas.Price{
Height: height,
ChainId: r.chainId,
TokenId: res.TokenId,
Price: price,
PriceTokenId: res.PriceTokenId,
RouteId: res.RouteId})
if tx.Error != nil {
return errors.Wrap(tx.Error, "repo.UpdateRoutePrice")
}
return nil
}