Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Sep 7, 2023
1 parent 78e901a commit e8387fa
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 27 deletions.
2 changes: 1 addition & 1 deletion dependencies/caminoethvm
Submodule caminoethvm updated 1 files
+1 −1 caminogo
12 changes: 6 additions & 6 deletions models/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ import (
"github.com/chain4travel/magellan/modelsc"
)

type DACProposalWithVotes struct {
DACProposal DACProposal `json:"dacProposal"`
DACVotes []DACVote `json:"dacVotes"`
}

type DACProposalsList struct {
DACProposals []DACProposal `json:"dacProposals"`
DACProposals []DACProposalWithVotes `json:"dacProposals"`
}

type ProposalStatus int
Expand Down Expand Up @@ -53,11 +58,6 @@ type DACProposal struct {
BlockHeight uint64 `json:"blockHeight"` // height of proposal block
}

type DACProposalWithVotes struct {
DACProposal DACProposal `json:"dacProposal"`
DACVotes []DACVote `json:"dacVotes"`
}

type DACVote struct {
VoteTxID string `json:"voteTxID"` // addVoteTx id
VoterAddr string `json:"voterAddr"` // address which authorized this vote
Expand Down
98 changes: 78 additions & 20 deletions services/indexes/avax/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"time"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/chain4travel/magellan/caching"
"github.com/chain4travel/magellan/cfg"
"github.com/chain4travel/magellan/db"
Expand Down Expand Up @@ -1052,16 +1054,33 @@ func (r *Reader) ListDACProposals(ctx context.Context, p *params.ListDACProposal
return nil, err
}

proposalsList := make([]models.DACProposal, 0, len(proposals))
for _, dbProposal := range proposals {
height, err := r.sc.Persist.GetTxHeight(ctx, dbRunner, dbProposal.ID)
proposalsList := make([]models.DACProposalWithVotes, len(proposals))
for i := range proposals {
height, err := r.sc.Persist.GetTxHeight(ctx, dbRunner, proposals[i].ID)
if err != nil {
return nil, err
}

proposal := dacProposalFromDB(&dbProposal) //nolint:gosec,scopelint
proposal.BlockHeight = height
proposalsList = append(proposalsList, proposal)
votes, err := r.sc.Persist.QueryDACProposalVotes(ctx, dbRunner, proposals[i].ID)
if err != nil {
return nil, err
}

proposalModel, err := r.dacProposalFromDB(&proposals[i])
if err != nil {
return nil, err
}
proposalModel.BlockHeight = height

votesModel, err := r.dacVotesFromDB(votes)
if err != nil {
return nil, err
}

proposalsList[i] = models.DACProposalWithVotes{
DACProposal: proposalModel,
DACVotes: votesModel,
}
}

return &models.DACProposalsList{DACProposals: proposalsList}, nil
Expand Down Expand Up @@ -1096,35 +1115,74 @@ func (r *Reader) GetDACProposalWithVotes(ctx context.Context, proposalID string)
return nil, err
}

dacVotes := make([]models.DACVote, 0, len(votes))
for _, vote := range votes {
dacVotes = append(dacVotes, models.DACVote{
VoteTxID: vote.VoteTxID,
VoterAddr: vote.VoterAddr,
VotedAt: vote.VotedAt,
VotedOptions: vote.VotedOptions,
})
proposalModel, err := r.dacProposalFromDB(&proposals[0])
if err != nil {
return nil, err
}
proposalModel.BlockHeight = height

proposal := dacProposalFromDB(&proposals[0])
proposal.BlockHeight = height
votesModel, err := r.dacVotesFromDB(votes)
if err != nil {
return nil, err
}

return &models.DACProposalWithVotes{
DACVotes: dacVotes,
DACProposal: proposal,
DACVotes: votesModel,
DACProposal: proposalModel,
}, nil
}

func dacProposalFromDB(proposal *db.DACProposal) models.DACProposal {
func (r *Reader) dacProposalFromDB(proposal *db.DACProposal) (models.DACProposal, error) {
id, err := ids.ShortFromString(proposal.ProposerAddr)
if err != nil {
return models.DACProposal{}, err
}

proposerAddr, err := address.Format("P", constants.GetHRP(r.networkID), id[:])
if err != nil {
return models.DACProposal{}, err
}

return models.DACProposal{
ID: proposal.ID,
ProposerAddr: proposal.ProposerAddr,
ProposerAddr: proposerAddr,
StartTime: proposal.StartTime,
EndTime: proposal.EndTime,
Type: proposal.Type,
Options: proposal.Options,
Memo: proposal.Memo,
Outcome: proposal.Outcome,
Status: proposal.Status,
}, nil
}

func (r *Reader) dacVotesFromDB(votes []db.DACVote) ([]models.DACVote, error) {
dacVotes := make([]models.DACVote, 0, len(votes))
for i := range votes {
dacVote, err := r.dacVoteFromDB(&votes[i])
if err != nil {
return nil, err
}
dacVotes[i] = dacVote
}
return dacVotes, nil
}

func (r *Reader) dacVoteFromDB(vote *db.DACVote) (models.DACVote, error) {
id, err := ids.ShortFromString(vote.VoterAddr)
if err != nil {
return models.DACVote{}, err
}

voterAddr, err := address.Format("P", constants.GetHRP(r.networkID), id[:])
if err != nil {
return models.DACVote{}, err
}

return models.DACVote{
VoteTxID: vote.VoteTxID,
VoterAddr: voterAddr,
VotedAt: vote.VotedAt,
VotedOptions: vote.VotedOptions,
}, nil
}

0 comments on commit e8387fa

Please sign in to comment.