"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-votes", jsonHandler(a.listVotes))
m.Handle("/decode-program", jsonHandler(a.decodeProgram))
return NewSuccessResponse(balances)
}
+// POST /list-balances
+func (a *API) listVotes(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 {
// 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
}
+
+// AccountVote account vote
+type AccountVote struct {
+ AccountID string `json:"account_id"`
+ Alias string `json:"account_alias"`
+ AssetAlias string `json:"asset_alias"`
+ AssetID string `json:"asset_id"`
+ Amount uint64 `json:"amount"`
+ AssetDefinition map[string]interface{} `json:"asset_definition"`
+}
+
+// GetAccountVotes return all account votes
+func (w *Wallet) GetAccountVotes(accountID string, id string) ([]AccountVote, error) {
+ return w.indexVotes(w.GetAccountUtxos(accountID, "", false, false, true))
+}
+
+func (w *Wallet) indexVotes(accountUTXOs []*account.UTXO) ([]AccountVote, error) {
+ accBalance := make(map[string]map[string]uint64)
+ votes := []AccountVote{}
+
+ for _, accountUTXO := range accountUTXOs {
+ assetID := accountUTXO.AssetID.String()
+ if _, ok := accBalance[accountUTXO.AccountID]; ok {
+ if _, ok := accBalance[accountUTXO.AccountID][assetID]; ok {
+ accBalance[accountUTXO.AccountID][assetID] += accountUTXO.Amount
+ } else {
+ accBalance[accountUTXO.AccountID][assetID] = accountUTXO.Amount
+ }
+ } else {
+ accBalance[accountUTXO.AccountID] = map[string]uint64{assetID: accountUTXO.Amount}
+ }
+ }
+
+ var sortedAccount []string
+ for k := range accBalance {
+ sortedAccount = append(sortedAccount, k)
+ }
+ sort.Strings(sortedAccount)
+
+ for _, id := range sortedAccount {
+ var sortedAsset []string
+ for k := range accBalance[id] {
+ sortedAsset = append(sortedAsset, k)
+ }
+ sort.Strings(sortedAsset)
+
+ for _, assetID := range sortedAsset {
+ alias := w.AccountMgr.GetAliasByID(id)
+ targetAsset, err := w.AssetReg.GetAsset(assetID)
+ if err != nil {
+ return nil, err
+ }
+
+ assetAlias := *targetAsset.Alias
+ votes = append(votes, AccountVote{
+ Alias: alias,
+ AccountID: id,
+ AssetID: assetID,
+ AssetAlias: assetAlias,
+ Amount: accBalance[id][assetID],
+ AssetDefinition: targetAsset.DefinitionMap,
+ })
+ }
+ }
+
+ 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)
}
w.AccountMgr = account.NewManager(testDB, nil)
w.AccountMgr.AddUnconfirmedUtxo(c.unconfirmedUtxos)
- gotUtxos := w.GetAccountUtxos("", c.id, c.unconfirmed, c.isSmartContract)
+ gotUtxos := w.GetAccountUtxos("", c.id, c.unconfirmed, c.isSmartContract, false)
if !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos)
}