This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
196 lines (166 loc) · 5.03 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package api
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/bytom/account"
"github.com/bytom/blockchain/query"
"github.com/bytom/consensus"
chainjson "github.com/bytom/encoding/json"
"github.com/bytom/protocol/bc"
)
// POST /list-accounts
func (a *API) listAccounts(ctx context.Context, filter struct {
ID string `json:"id"`
}) Response {
accounts, err := a.wallet.AccountMgr.ListAccounts(filter.ID)
if err != nil {
log.Errorf("listAccounts: %v", err)
return NewErrorResponse(err)
}
annotatedAccounts := []query.AnnotatedAccount{}
for _, acc := range accounts {
annotatedAccounts = append(annotatedAccounts, *account.Annotated(acc))
}
return NewSuccessResponse(annotatedAccounts)
}
// POST /get-asset
func (a *API) getAsset(ctx context.Context, filter struct {
ID string `json:"id"`
}) Response {
asset, err := a.wallet.AssetReg.GetAsset(filter.ID)
if err != nil {
log.Errorf("getAsset: %v", err)
return NewErrorResponse(err)
}
return NewSuccessResponse(asset)
}
// POST /list-assets
func (a *API) listAssets(ctx context.Context, filter struct {
ID string `json:"id"`
}) Response {
assets, err := a.wallet.AssetReg.ListAssets(filter.ID)
if err != nil {
log.Errorf("listAssets: %v", err)
return NewErrorResponse(err)
}
return NewSuccessResponse(assets)
}
// POST /list-balances
func (a *API) listBalances(ctx context.Context) Response {
balances, err := a.wallet.GetAccountBalances("")
if err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(balances)
}
// POST /get-transaction
func (a *API) getTransaction(ctx context.Context, txInfo struct {
TxID string `json:"tx_id"`
}) Response {
transaction, err := a.wallet.GetTransactionByTxID(txInfo.TxID)
if err != nil {
log.Errorf("getTransaction error: %v", err)
return NewErrorResponse(err)
}
return NewSuccessResponse(transaction)
}
// POST /list-transactions
func (a *API) listTransactions(ctx context.Context, filter struct {
ID string `json:"id"`
AccountID string `json:"account_id"`
Detail bool `json:"detail"`
}) Response {
transactions := []*query.AnnotatedTx{}
var err error
if filter.AccountID != "" {
transactions, err = a.wallet.GetTransactionsByAccountID(filter.AccountID)
} else {
transactions, err = a.wallet.GetTransactionsByTxID(filter.ID)
}
if err != nil {
log.Errorf("listTransactions: %v", err)
return NewErrorResponse(err)
}
if filter.Detail == false {
txSummary := a.wallet.GetTransactionsSummary(transactions)
return NewSuccessResponse(txSummary)
}
return NewSuccessResponse(transactions)
}
// POST /get-unconfirmed-transaction
func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
TxID chainjson.HexBytes `json:"tx_id"`
}) Response {
var tmpTxID [32]byte
copy(tmpTxID[:], filter.TxID[:])
txHash := bc.NewHash(tmpTxID)
txPool := a.chain.GetTxPool()
txDesc, err := txPool.GetTransaction(&txHash)
if err != nil {
return NewErrorResponse(err)
}
tx := &BlockTx{
ID: txDesc.Tx.ID,
Version: txDesc.Tx.Version,
Size: txDesc.Tx.SerializedSize,
TimeRange: txDesc.Tx.TimeRange,
Inputs: []*query.AnnotatedInput{},
Outputs: []*query.AnnotatedOutput{},
StatusFail: false,
}
for i := range txDesc.Tx.Inputs {
tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(txDesc.Tx, uint32(i)))
}
for i := range txDesc.Tx.Outputs {
tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(txDesc.Tx, i))
}
return NewSuccessResponse(tx)
}
type unconfirmedTxsResp struct {
Total uint64 `json:"total"`
TxIDs []bc.Hash `json:"tx_ids"`
}
// POST /list-unconfirmed-transactions
func (a *API) listUnconfirmedTxs(ctx context.Context) Response {
txIDs := []bc.Hash{}
txPool := a.chain.GetTxPool()
txs := txPool.GetTransactions()
for _, txDesc := range txs {
txIDs = append(txIDs, bc.Hash(txDesc.Tx.ID))
}
return NewSuccessResponse(&unconfirmedTxsResp{
Total: uint64(len(txIDs)),
TxIDs: txIDs,
})
}
// POST /list-unspent-outputs
func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
ID string `json:"id"`
}) Response {
accountUTXOs := a.wallet.GetAccountUTXOs(filter.ID)
UTXOs := []query.AnnotatedUTXO{}
for _, utxo := range accountUTXOs {
UTXOs = append([]query.AnnotatedUTXO{{
AccountID: utxo.AccountID,
OutputID: utxo.OutputID.String(),
SourceID: utxo.SourceID.String(),
AssetID: utxo.AssetID.String(),
Amount: utxo.Amount,
SourcePos: utxo.SourcePos,
Program: fmt.Sprintf("%x", utxo.ControlProgram),
ControlProgramIndex: utxo.ControlProgramIndex,
Address: utxo.Address,
ValidHeight: utxo.ValidHeight,
Alias: a.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
AssetAlias: a.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
Change: utxo.Change,
}}, UTXOs...)
}
return NewSuccessResponse(UTXOs)
}
// return gasRate
func (a *API) gasRate() Response {
gasrate := map[string]int64{"gas_rate": consensus.VMGasRate}
return NewSuccessResponse(gasrate)
}