diff --git a/dependencies/caminoethvm b/dependencies/caminoethvm index e4d544cd..a330dadb 160000 --- a/dependencies/caminoethvm +++ b/dependencies/caminoethvm @@ -1 +1 @@ -Subproject commit e4d544cdcd25de155bb16bc790ef9b247d2f7a54 +Subproject commit a330dadb77d9d030bff19de9ac59a8bce7603067 diff --git a/models/collections.go b/models/collections.go index 58fe1734..eb3cdf02 100644 --- a/models/collections.go +++ b/models/collections.go @@ -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 @@ -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 diff --git a/services/indexes/avax/reader.go b/services/indexes/avax/reader.go index d8d66ba1..216a144e 100644 --- a/services/indexes/avax/reader.go +++ b/services/indexes/avax/reader.go @@ -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" @@ -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 @@ -1096,29 +1115,37 @@ 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, @@ -1126,5 +1153,36 @@ func dacProposalFromDB(proposal *db.DACProposal) models.DACProposal { 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 }