Skip to content

Commit

Permalink
Implement new protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
gov-dmitry committed Oct 31, 2024
1 parent dfbe997 commit 3862fec
Show file tree
Hide file tree
Showing 20 changed files with 1,070 additions and 341 deletions.
43 changes: 40 additions & 3 deletions internal/delegate/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,53 @@ func (f DelegatorFilter) Apply(db *gorm.DB) *gorm.DB {
return db.Where("lower(address_from) = lower(?)", f.Address)
}

// DelegationFilter Whom delegate voting power
type DelegationFilter struct {
// DaoFilter Who delegate voting power
type DaoFilter struct {
ID string
}

func (f DaoFilter) Apply(db *gorm.DB) *gorm.DB {
var (
dummy = Summary{}
_ = dummy.DaoID
)

return db.Where("dao_id = ?", f.ID)
}

// DelegateFilter Whom delegate voting power
type DelegateFilter struct {
Address string
}

func (f DelegationFilter) Apply(db *gorm.DB) *gorm.DB {
func (f DelegateFilter) Apply(db *gorm.DB) *gorm.DB {
var (
dummy = Summary{}
_ = dummy.AddressTo
)

return db.Where("lower(address_to) = lower(?)", f.Address)
}

type PageFilter struct {
Offset int
Limit int
}

func (f PageFilter) Apply(db *gorm.DB) *gorm.DB {
return db.Offset(f.Offset).Limit(f.Limit)
}

type OrderByAddressToFilter struct {
}

func (f OrderByAddressToFilter) Apply(db *gorm.DB) *gorm.DB {
return db.Order("address_to")
}

type OrderByAddressFromFilter struct {
}

func (f OrderByAddressFromFilter) Apply(db *gorm.DB) *gorm.DB {
return db.Order("address_from")
}
3 changes: 3 additions & 0 deletions internal/delegate/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ type Summary struct {
LastBlockTimestamp int
ExpiresAt int64
CreatedAt time.Time

// virtual property
MaxCnt int `gorm:"-"`
}

func (Summary) TableName() string {
Expand Down
102 changes: 100 additions & 2 deletions internal/delegate/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,105 @@ func (r *Repo) FindDelegatorsByVotes(votes []Vote) ([]summaryByVote, error) {
return result, nil
}

func (r *Repo) GetByFilters(filters []Filter) ([]Summary, error) {
// fixme: check it
func (r *Repo) GetTopDelegatorsByAddress(address string, limit int) ([]Summary, error) {
rows, err := r.db.
Raw(`
SELECT
dao_id,
address_from,
weight,
expires_at,
max_cnt
FROM (SELECT
dao_id,
address_from,
weight,
expires_at,
ROW_NUMBER() OVER (PARTITION BY dao_id) row_number,
count(*) over (partition by dao_id) max_cnt
FROM delegates_summary
WHERE lower(address_to) = lower(?) ) dataset
WHERE dataset.row_number <= ?
`,
address,
limit,
).
Rows()
if err != nil {
return nil, fmt.Errorf("raw exec: %w", err)
}

result := make([]Summary, 0, limit*10)
defer rows.Close()
for rows.Next() {
si := Summary{AddressTo: address}
if err = rows.Scan(
&si.DaoID,
&si.AddressFrom,
&si.Weight,
&si.ExpiresAt,
&si.MaxCnt,
); err != nil {
return nil, fmt.Errorf("scan: %w", err)
}

result = append(result, si)
}

return result, nil
}

// fixme: check it
func (r *Repo) GetTopDelegatesByAddress(address string, limit int) ([]Summary, error) {
rows, err := r.db.
Raw(`
SELECT
dao_id,
address_to,
weight,
expires_at,
max_cnt
FROM (SELECT
dao_id,
address_to,
weight,
expires_at,
ROW_NUMBER() OVER (PARTITION BY dao_id) row_number,
count(*) over (partition by dao_id) max_cnt
FROM delegates_summary
WHERE lower(address_from) = lower(?) ) dataset
WHERE dataset.row_number <= ?
`,
address,
limit,
).
Rows()
if err != nil {
return nil, fmt.Errorf("raw exec: %w", err)
}

result := make([]Summary, 0, limit*10)
defer rows.Close()
for rows.Next() {
si := Summary{AddressFrom: address}
if err = rows.Scan(
&si.DaoID,
&si.AddressTo,
&si.Weight,
&si.ExpiresAt,
&si.MaxCnt,
); err != nil {
return nil, fmt.Errorf("scan: %w", err)
}

result = append(result, si)
}

return result, nil
}

func (r *Repo) GetByFilters(filters ...Filter) ([]Summary, error) {
db := r.db.Model(&Summary{})
for _, f := range filters {
db = f.Apply(db)
Expand All @@ -182,7 +280,7 @@ func (r *Repo) GetByFilters(filters []Filter) ([]Summary, error) {
return list, nil
}

func (r *Repo) GetCnt(filters []Filter) (int64, error) {
func (r *Repo) GetCnt(filters ...Filter) (int64, error) {
db := r.db.Model(&Summary{})
for _, f := range filters {
db = f.Apply(db)
Expand Down
Loading

0 comments on commit 3862fec

Please sign in to comment.