+
+// GetAccountVotes return all account votes
+func (w *Wallet) GetAccountVotes(accountID string, id string) ([]AccountVotes, error) {
+ return w.indexVotes(w.GetAccountUtxos(accountID, "", false, false, true))
+}
+
+type voteDetail struct {
+ Vote string `json:"vote"`
+ VoteNumber uint64 `json:"vote_number"`
+}
+
+// AccountVotes account vote
+type AccountVotes struct {
+ AccountID string `json:"account_id"`
+ Alias string `json:"account_alias"`
+ TotalVoteNumber uint64 `json:"total_vote_number"`
+ VoteDetails []voteDetail `json:"vote_details"`
+}
+
+func (w *Wallet) indexVotes(accountUTXOs []*account.UTXO) ([]AccountVotes, error) {
+ accVote := make(map[string]map[string]uint64)
+ votes := []AccountVotes{}
+
+ for _, accountUTXO := range accountUTXOs {
+ if accountUTXO.AssetID != *consensus.BTMAssetID || accountUTXO.Vote == nil {
+ continue
+ }
+ xpub := hex.EncodeToString(accountUTXO.Vote)
+ if _, ok := accVote[accountUTXO.AccountID]; ok {
+ accVote[accountUTXO.AccountID][xpub] += accountUTXO.Amount
+ } else {
+ accVote[accountUTXO.AccountID] = map[string]uint64{xpub: accountUTXO.Amount}
+
+ }
+ }
+
+ var sortedAccount []string
+ for k := range accVote {
+ sortedAccount = append(sortedAccount, k)
+ }
+ sort.Strings(sortedAccount)
+
+ for _, id := range sortedAccount {
+ var sortedXpub []string
+ for k := range accVote[id] {
+ sortedXpub = append(sortedXpub, k)
+ }
+ sort.Strings(sortedXpub)
+
+ voteDetails := []voteDetail{}
+ voteTotal := uint64(0)
+ for _, xpub := range sortedXpub {
+ voteDetails = append(voteDetails, voteDetail{
+ Vote: xpub,
+ VoteNumber: accVote[id][xpub],
+ })
+ voteTotal += accVote[id][xpub]
+ }
+ alias := w.AccountMgr.GetAliasByID(id)
+ votes = append(votes, AccountVotes{
+ Alias: alias,
+ AccountID: id,
+ VoteDetails: voteDetails,
+ TotalVoteNumber: voteTotal,
+ })
+ }
+
+ return votes, nil
+}