OSDN Git Service

Merge pull request #635 from Bytom/unit_test
[bytom/bytom.git] / api / query.go
1 package api
2
3 import (
4         "context"
5         "fmt"
6
7         log "github.com/sirupsen/logrus"
8
9         "github.com/bytom/account"
10         "github.com/bytom/blockchain/query"
11         "github.com/bytom/consensus"
12 )
13
14 // POST /list-accounts
15 func (a *API) listAccounts(ctx context.Context, filter struct {
16         ID string `json:"id"`
17 }) Response {
18         accounts, err := a.wallet.AccountMgr.ListAccounts(filter.ID)
19         if err != nil {
20                 log.Errorf("listAccounts: %v", err)
21                 return NewErrorResponse(err)
22         }
23
24         var annotatedAccounts []query.AnnotatedAccount
25         for _, acc := range accounts {
26                 annotatedAccounts = append(annotatedAccounts, *account.Annotated(acc))
27         }
28
29         return NewSuccessResponse(annotatedAccounts)
30 }
31
32 // POST /list-assets
33 func (a *API) listAssets(ctx context.Context, filter struct {
34         ID string `json:"id"`
35 }) Response {
36         assets, err := a.wallet.AssetReg.ListAssets(filter.ID)
37         if err != nil {
38                 log.Errorf("listAssets: %v", err)
39                 return NewErrorResponse(err)
40         }
41
42         return NewSuccessResponse(assets)
43 }
44
45 // POST /listBalances
46 func (a *API) listBalances(ctx context.Context) Response {
47         return NewSuccessResponse(a.wallet.GetAccountBalances(""))
48 }
49
50 // POST /get-transaction
51 func (a *API) getTransaction(ctx context.Context, txInfo struct {
52         TxID string `json:"tx_id"`
53 }) Response {
54         transaction, err := a.wallet.GetTransactionByTxID(txInfo.TxID)
55         if err != nil {
56                 log.Errorf("getTransaction error: %v", err)
57                 return NewErrorResponse(err)
58         }
59
60         return NewSuccessResponse(transaction)
61 }
62
63 // POST /list-transactions
64 func (a *API) listTransactions(ctx context.Context, filter struct {
65         ID        string `json:"id"`
66         AccountID string `json:"account_id"`
67         Detail    bool   `json:"detail"`
68 }) Response {
69         var transactions []*query.AnnotatedTx
70         var err error
71
72         if filter.AccountID != "" {
73                 transactions, err = a.wallet.GetTransactionsByAccountID(filter.AccountID)
74         } else {
75                 transactions, err = a.wallet.GetTransactionsByTxID(filter.ID)
76         }
77
78         if err != nil {
79                 log.Errorf("listTransactions: %v", err)
80                 return NewErrorResponse(err)
81         }
82
83         if filter.Detail == false {
84                 txSummary := a.wallet.GetTransactionsSummary(transactions)
85                 return NewSuccessResponse(txSummary)
86         }
87         return NewSuccessResponse(transactions)
88 }
89
90 // POST /list-unspent-outputs
91 func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
92         ID string `json:"id"`
93 }) Response {
94         accountUTXOs := a.wallet.GetAccountUTXOs(filter.ID)
95
96         var UTXOs []query.AnnotatedUTXO
97         for _, utxo := range accountUTXOs {
98                 UTXOs = append([]query.AnnotatedUTXO{{
99                         AccountID:           utxo.AccountID,
100                         OutputID:            utxo.OutputID.String(),
101                         SourceID:            utxo.SourceID.String(),
102                         AssetID:             utxo.AssetID.String(),
103                         Amount:              utxo.Amount,
104                         SourcePos:           utxo.SourcePos,
105                         Program:             fmt.Sprintf("%x", utxo.ControlProgram),
106                         ControlProgramIndex: utxo.ControlProgramIndex,
107                         Address:             utxo.Address,
108                         ValidHeight:         utxo.ValidHeight,
109                         Alias:               a.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
110                         AssetAlias:          a.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
111                         Change:              utxo.Change,
112                 }}, UTXOs...)
113         }
114
115         return NewSuccessResponse(UTXOs)
116 }
117
118 // return gasRate
119 func (a *API) gasRate() Response {
120         gasrate := map[string]int64{"gasRate": consensus.VMGasRate}
121         return NewSuccessResponse(gasrate)
122 }