OSDN Git Service

make some fields of BlockchainReactor public to facilitate separate API (#477)
[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 )
12
13 // POST /list-accounts
14 func (a *API) listAccounts(ctx context.Context, filter struct {
15         ID string `json:"id"`
16 }) Response {
17         accounts, err := a.wallet.AccountMgr.ListAccounts(filter.ID)
18         if err != nil {
19                 log.Errorf("listAccounts: %v", err)
20                 return NewErrorResponse(err)
21         }
22
23         annotatedAccounts := make([]query.AnnotatedAccount, 0, len(accounts))
24         for _, acc := range accounts {
25                 annotated, err := account.Annotated(acc)
26                 if err != nil {
27                         return NewErrorResponse(err)
28                 }
29
30                 annotatedAccounts = append(annotatedAccounts, *annotated)
31         }
32
33         return NewSuccessResponse(annotatedAccounts)
34 }
35
36 // POST /list-assets
37 func (a *API) listAssets(ctx context.Context, filter struct {
38         ID string `json:"id"`
39 }) Response {
40         assets, err := a.wallet.AssetReg.ListAssets(filter.ID)
41         if err != nil {
42                 log.Errorf("listAssets: %v", err)
43                 return NewErrorResponse(err)
44         }
45
46         return NewSuccessResponse(assets)
47 }
48
49 // POST /listBalances
50 func (a *API) listBalances(ctx context.Context) Response {
51         if balances, err := a.wallet.GetAccountBalances(""); err != nil {
52                 log.Errorf("GetAccountUTXOs: %v", err)
53                 return NewErrorResponse(err)
54         } else {
55                 return NewSuccessResponse(balances)
56         }
57 }
58
59 // POST /get-transaction
60 func (a *API) getTransaction(ctx context.Context, txInfo struct {
61         TxID string `json:"tx_id"`
62 }) Response {
63         transaction, err := a.wallet.GetTransactionByTxID(txInfo.TxID)
64         if err != nil {
65                 log.Errorf("getTransaction error: %v", err)
66                 return NewErrorResponse(err)
67         }
68
69         return NewSuccessResponse(transaction)
70 }
71
72 // POST /list-transactions
73 func (a *API) listTransactions(ctx context.Context, filter struct {
74         ID        string `json:"id"`
75         AccountID string `json:"account_id"`
76         Detail    bool   `json:"detail"`
77 }) Response {
78         var transactions []*query.AnnotatedTx
79         var err error
80
81         if filter.AccountID != "" {
82                 transactions, err = a.wallet.GetTransactionsByAccountID(filter.AccountID)
83         } else {
84                 transactions, err = a.wallet.GetTransactionsByTxID(filter.ID)
85         }
86
87         if err != nil {
88                 log.Errorf("listTransactions: %v", err)
89                 return NewErrorResponse(err)
90         }
91
92         if filter.Detail == false {
93                 txSummary := a.wallet.GetTransactionsSummary(transactions)
94                 return NewSuccessResponse(txSummary)
95         }
96         return NewSuccessResponse(transactions)
97 }
98
99 // POST /list-unspent-outputs
100 func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
101         ID string `json:"id"`
102 }) Response {
103         accountUTXOs, err := a.wallet.GetAccountUTXOs(filter.ID)
104         if err != nil {
105                 log.Errorf("list Unspent Outputs: %v", err)
106                 return NewErrorResponse(err)
107         }
108
109         var UTXOs []query.AnnotatedUTXO
110         for _, utxo := range accountUTXOs {
111                 UTXOs = append(UTXOs, query.AnnotatedUTXO{
112                         AccountID:           utxo.AccountID,
113                         OutputID:            utxo.OutputID.String(),
114                         SourceID:            utxo.SourceID.String(),
115                         AssetID:             utxo.AssetID.String(),
116                         Amount:              utxo.Amount,
117                         SourcePos:           utxo.SourcePos,
118                         Program:             fmt.Sprintf("%x", utxo.ControlProgram),
119                         ControlProgramIndex: utxo.ControlProgramIndex,
120                         Address:             utxo.Address,
121                         ValidHeight:         utxo.ValidHeight,
122                         Alias:               a.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
123                         AssetAlias:          a.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
124                 })
125         }
126
127         return NewSuccessResponse(UTXOs)
128 }