Skip to content

Commit

Permalink
feat: /governance/votes api
Browse files Browse the repository at this point in the history
  • Loading branch information
romever committed Jul 29, 2024
1 parent 6f0923f commit d160c8c
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 22 deletions.
78 changes: 77 additions & 1 deletion api/internal/logic/governance/governancevoteslogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package governance

import (
"context"
"errors"
"fmt"
"github.com/zeromicro/go-zero/core/logc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"math/big"
"oasisscan-backend/api/internal/errort"
"oasisscan-backend/common"

"oasisscan-backend/api/internal/svc"
"oasisscan-backend/api/internal/types"
Expand All @@ -24,6 +31,75 @@ func NewGovernanceVotesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *G
}

func (l *GovernanceVotesLogic) GovernanceVotes(req *types.GovernanceVotesRequest) (resp *types.GovernanceVotesResponse, err error) {
// todo: add your logic here and delete this line
pageable := common.Pageable{
Limit: req.Size,
Offset: (req.Page - 1) * req.Size,
}
votes, err := l.svcCtx.VoteModel.FindByValidator(l.ctx, req.ValidatorAddress, req.ProposalId, pageable)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
logc.Errorf(l.ctx, "FindAllByValidator error, %v", err)
return nil, errort.NewDefaultError()
}
list := make([]*types.ProposalVote, 0)
totalSize, err := l.svcCtx.VoteModel.CountByValidator(l.ctx, req.ValidatorAddress, req.ProposalId)
if err != nil {
logc.Errorf(l.ctx, "CountByValidator error, %v", err)
return nil, errort.NewDefaultError()
}
validatorName, validatorIcon := "", ""
if req.ValidatorAddress != "" {
validatorInfo, err := l.svcCtx.ValidatorModel.FindOneByEntityAddress(l.ctx, req.ValidatorAddress)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
logc.Errorf(l.ctx, "validator FindOneByEntityAddress error, %v", err)
return nil, errort.NewDefaultError()
}
if validatorInfo != nil {
validatorName = validatorInfo.Name
validatorIcon = validatorInfo.Icon
}
}

for _, vote := range votes {
title := ""
proposal, err := l.svcCtx.ProposalModel.FindOneByProposalId(l.ctx, vote.ProposalId)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
logc.Errorf(l.ctx, "proposal FindOneByProposalId error, %v", err)
return nil, errort.NewDefaultError()
}
if proposal != nil {
title = proposal.Title
}
if req.ValidatorAddress == "" {
validatorInfo, err := l.svcCtx.ValidatorModel.FindOneByEntityAddress(l.ctx, vote.VoteAddress)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
logc.Errorf(l.ctx, "validator FindOneByEntityAddress error, %v", err)
return nil, errort.NewDefaultError()
}
if validatorInfo != nil {
validatorName = validatorInfo.Name
validatorIcon = validatorInfo.Icon
}
}
list = append(list, &types.ProposalVote{
ProposalId: vote.ProposalId,
Title: title,
Name: validatorName,
Icon: validatorIcon,
Address: vote.VoteAddress,
Vote: vote.Option,
Amount: fmt.Sprintf("%.9f", common.ValueToFloatByDecimals(big.NewInt(vote.Amount), common.Decimals)),
Percent: vote.Percent,
})
}
page := types.Page{
Page: req.Page,
Size: req.Size,
MaxPage: common.MaxPage(req.Size, totalSize),
TotalSize: totalSize,
}
resp = &types.GovernanceVotesResponse{
List: list,
Page: page,
}
return
}
22 changes: 12 additions & 10 deletions api/internal/types/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions api/oasisscan.api
Original file line number Diff line number Diff line change
Expand Up @@ -510,18 +510,20 @@ type (
Percent float64 `json:"percent"`
}
ProposalVote {
Name string `json:"name"`
Icon string `json:"icon"`
Address string `json:"address"`
Vote string `json:"vote"`
Amount string `json:"amount"`
Percent float64 `json:"percent"`
ProposalId int64 `json:"proposalId"`
Title string `json:"title"`
Name string `json:"name"`
Icon string `json:"icon"`
Address string `json:"address"`
Vote string `json:"vote"`
Amount string `json:"amount"`
Percent float64 `json:"percent"`
}
GovernanceVotesRequest {
PoposalId int64 `form:"proposal_id,optional"`
ValidatorAddress int64 `form:"validator_address,optional"`
Page int64 `form:"page,default=1"`
Size int64 `form:"size,default=5"`
ProposalId int64 `form:"proposalId,optional"`
ValidatorAddress string `form:"validator,optional"`
Page int64 `form:"page,default=1"`
Size int64 `form:"size,default=5"`
}
GovernanceVotesResponse {
List []*ProposalVote `json:"list"`
Expand Down
75 changes: 74 additions & 1 deletion job/model/votemodel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package model

import "github.com/zeromicro/go-zero/core/stores/sqlx"
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"oasisscan-backend/common"
"strings"
)

var _ VoteModel = (*customVoteModel)(nil)

Expand All @@ -10,6 +16,8 @@ type (
VoteModel interface {
voteModel
withSession(session sqlx.Session) VoteModel
FindByValidator(ctx context.Context, validator string, proposalId int64, pageable common.Pageable) ([]*Vote, error)
CountByValidator(ctx context.Context, validator string, proposalId int64) (int64, error)
}

customVoteModel struct {
Expand All @@ -27,3 +35,68 @@ func NewVoteModel(conn sqlx.SqlConn) VoteModel {
func (m *customVoteModel) withSession(session sqlx.Session) VoteModel {
return NewVoteModel(sqlx.NewSqlConnFromSession(session))
}

func (m *customVoteModel) FindByValidator(ctx context.Context, validator string, proposalId int64, pageable common.Pageable) ([]*Vote, error) {
var resp []*Vote
query := fmt.Sprintf("select %s from %s where ", voteRows, m.table)
var conditions []string
conditions = append(conditions, "1=1")
var args []interface{}
paramIndex := 1
if validator != "" {
conditions = append(conditions, fmt.Sprintf("vote_address = $%d", paramIndex))
args = append(args, validator)
paramIndex++
}
if proposalId != 0 {
conditions = append(conditions, fmt.Sprintf("proposal_id = $%d", paramIndex))
args = append(args, proposalId)
paramIndex++
}

if len(conditions) > 0 {
query += strings.Join(conditions, " AND ")
}
query += fmt.Sprintf(" order by proposal_id desc,percent desc limit %d offset %d", pageable.Limit, pageable.Offset)

err := m.conn.QueryRowsCtx(ctx, &resp, query, args...)
switch err {
case nil:
return resp, nil
case sqlx.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}

func (m *customVoteModel) CountByValidator(ctx context.Context, validator string, proposalId int64) (int64, error) {
var resp int64
query := fmt.Sprintf("select count(*) from %s where ", m.table)
var conditions []string
conditions = append(conditions, "1=1")
var args []interface{}
paramIndex := 1
if validator != "" {
conditions = append(conditions, fmt.Sprintf("vote_address = $%d", paramIndex))
args = append(args, validator)
paramIndex++
}
if proposalId != 0 {
conditions = append(conditions, fmt.Sprintf("proposal_id = $%d", paramIndex))
args = append(args, proposalId)
paramIndex++
}

if len(conditions) > 0 {
query += strings.Join(conditions, " AND ")
}

err := m.conn.QueryRowCtx(ctx, &resp, query, args...)
switch err {
case nil:
return resp, nil
default:
return 0, err
}
}

0 comments on commit d160c8c

Please sign in to comment.