This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
thirdparty_bakers.go
62 lines (53 loc) · 1.9 KB
/
thirdparty_bakers.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
package thirdparty_bakers
import (
"github.com/everstake/teztracker/models"
"github.com/jinzhu/gorm"
gormbulk "github.com/t-tiger/gorm-bulk-insert"
)
type (
// Repository is the third party bakers repo implementation.
Repository struct {
db *gorm.DB
}
Repo interface {
DeleteAll() error
Create(bakers []models.ThirdPartyBaker) error
GetAggregatedBakers() (bakers []models.ThirdPartyBakerAgg, err error)
}
)
const thirdPartyBakersTable = "tezos.third_party_bakers"
// New creates an instance of repository using the provided db.
func New(db *gorm.DB) *Repository {
return &Repository{
db: db,
}
}
func (r *Repository) getDb() *gorm.DB {
db := r.db.
Model(&models.ThirdPartyBaker{})
return db
}
// Get all aggregated third party bakers
func (r *Repository) GetAggregatedBakers() (bakers []models.ThirdPartyBakerAgg, err error) {
err = r.db.Select("third_party_bakers.address,max(alias) as alias,max(staking_balance) as staking_balance,json_agg(json_build_object('provider',provider,'number',number,'name',name,'address',third_party_bakers.address,'yield',yield,'staking_balance',staking_balance,'fee',fee,'available_capacity',available_capacity,'efficiency',efficiency,'payout_accuracy',payout_accuracy)) as providers").
Model(models.ThirdPartyBakerAgg{}).
Table(thirdPartyBakersTable).
Joins("left join tezos.known_addresses ON third_party_bakers.address = known_addresses.address").
Order("staking_balance DESC").
Group("third_party_bakers.address").
Find(&bakers).
Error
return bakers, err
}
// Delete all third party bakers
func (r *Repository) DeleteAll() error {
return r.getDb().Delete(&models.ThirdPartyBaker{}).Error
}
// Create third party bakers
func (r *Repository) Create(bakers []models.ThirdPartyBaker) error {
insertRecords := make([]interface{}, len(bakers))
for i := range bakers {
insertRecords[i] = bakers[i]
}
return gormbulk.BulkInsert(r.db, insertRecords, 2000)
}