"github.com/vapor/net/websocket"
"github.com/vapor/netsync/peers"
"github.com/vapor/p2p"
+ "github.com/vapor/proposal/blockproposer"
"github.com/vapor/protocol"
"github.com/vapor/wallet"
- "github.com/vapor/proposal/blockproposer"
)
var (
m.Handle("/list-balances", jsonHandler(a.listBalances))
m.Handle("/list-unspent-outputs", jsonHandler(a.listUnspentOutputs))
+ m.Handle("/list-account-votes", jsonHandler(a.listAccountVotes))
m.Handle("/decode-program", jsonHandler(a.decodeProgram))
return NewSuccessResponse(balances)
}
+func (a *API) listAccountVotes(ctx context.Context, filter struct {
+ AccountID string `json:"account_id"`
+ AccountAlias string `json:"account_alias"`
+}) Response {
+ accountID := filter.AccountID
+ if filter.AccountAlias != "" {
+ acc, err := a.wallet.AccountMgr.FindByAlias(filter.AccountAlias)
+ if err != nil {
+ return NewErrorResponse(err)
+ }
+ accountID = acc.ID
+ }
+
+ votes, err := a.wallet.GetAccountVotes(accountID, "")
+ if err != nil {
+ return NewErrorResponse(err)
+ }
+ return NewSuccessResponse(votes)
+}
+
// POST /get-transaction
func (a *API) getTransaction(ctx context.Context, txInfo struct {
TxID string `json:"tx_id"`
}
accountID = acc.ID
}
- accountUTXOs := a.wallet.GetAccountUtxos(accountID, filter.ID, filter.Unconfirmed, filter.SmartContract)
+ accountUTXOs := a.wallet.GetAccountUtxos(accountID, filter.ID, filter.Unconfirmed, filter.SmartContract, false)
UTXOs := []query.AnnotatedUTXO{}
for _, utxo := range accountUTXOs {
import (
"encoding/binary"
+ "encoding/hex"
"encoding/json"
"fmt"
"sort"
"github.com/vapor/account"
"github.com/vapor/asset"
"github.com/vapor/blockchain/query"
+ "github.com/vapor/consensus"
"github.com/vapor/crypto/sha3pool"
dbm "github.com/vapor/database/leveldb"
chainjson "github.com/vapor/encoding/json"
// GetAccountBalances return all account balances
func (w *Wallet) GetAccountBalances(accountID string, id string) ([]AccountBalance, error) {
- return w.indexBalances(w.GetAccountUtxos(accountID, "", false, false))
+ return w.indexBalances(w.GetAccountUtxos(accountID, "", false, false, false))
}
// AccountBalance account balance
return balances, nil
}
+
+// 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"`
+ VoteAmount uint64 `json:"vote_amount"`
+}
+
+// AccountVotes account vote
+type AccountVotes struct {
+ AccountID string `json:"account_id"`
+ Alias string `json:"account_alias"`
+ TotalVoteAmount uint64 `json:"total_vote_amount"`
+ 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,
+ VoteAmount: accVote[id][xpub],
+ })
+ voteTotal += accVote[id][xpub]
+ }
+ alias := w.AccountMgr.GetAliasByID(id)
+ votes = append(votes, AccountVotes{
+ Alias: alias,
+ AccountID: id,
+ VoteDetails: voteDetails,
+ TotalVoteAmount: voteTotal,
+ })
+ }
+
+ return votes, nil
+}
)
// GetAccountUtxos return all account unspent outputs
-func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool) []*account.UTXO {
+func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool, vote bool) []*account.UTXO {
prefix := account.UTXOPreFix
if isSmartContract {
prefix = account.SUTXOPrefix
continue
}
+ if vote && accountUtxo.Vote == nil {
+ continue
+ }
+
if accountID == accountUtxo.AccountID || accountID == "" {
accountUtxos = append(accountUtxos, accountUtxo)
}