7 log "github.com/sirupsen/logrus"
9 "github.com/bytom/account"
10 "github.com/bytom/blockchain/query"
11 "github.com/bytom/consensus"
12 chainjson "github.com/bytom/encoding/json"
13 "github.com/bytom/protocol/bc"
14 "github.com/bytom/protocol/bc/types"
17 // POST /list-accounts
18 func (a *API) listAccounts(ctx context.Context, filter struct {
21 accounts, err := a.wallet.AccountMgr.ListAccounts(filter.ID)
23 log.Errorf("listAccounts: %v", err)
24 return NewErrorResponse(err)
27 annotatedAccounts := []query.AnnotatedAccount{}
28 for _, acc := range accounts {
29 annotatedAccounts = append(annotatedAccounts, *account.Annotated(acc))
32 return NewSuccessResponse(annotatedAccounts)
36 func (a *API) getAsset(ctx context.Context, filter struct {
39 asset, err := a.wallet.AssetReg.GetAsset(filter.ID)
41 log.Errorf("getAsset: %v", err)
42 return NewErrorResponse(err)
45 return NewSuccessResponse(asset)
49 func (a *API) listAssets(ctx context.Context, filter struct {
52 assets, err := a.wallet.AssetReg.ListAssets(filter.ID)
54 log.Errorf("listAssets: %v", err)
55 return NewErrorResponse(err)
58 return NewSuccessResponse(assets)
61 // POST /list-balances
62 func (a *API) listBalances(ctx context.Context) Response {
63 balances, err := a.wallet.GetAccountBalances("")
65 return NewErrorResponse(err)
67 return NewSuccessResponse(balances)
70 // POST /get-transaction
71 func (a *API) getTransaction(ctx context.Context, txInfo struct {
72 TxID string `json:"tx_id"`
74 var annotatedTx *query.AnnotatedTx
77 annotatedTx, err = a.wallet.GetTransactionByTxID(txInfo.TxID)
79 // transaction not found in blockchain db, search it from unconfirmed db
80 annotatedTx, err = a.wallet.GetUnconfirmedTxByTxID(txInfo.TxID)
82 return NewErrorResponse(err)
86 return NewSuccessResponse(annotatedTx)
89 // POST /list-transactions
90 func (a *API) listTransactions(ctx context.Context, filter struct {
92 AccountID string `json:"account_id"`
93 Detail bool `json:"detail"`
94 Unconfirmed bool `json:"unconfirmed"`
96 transactions := []*query.AnnotatedTx{}
98 var transaction *query.AnnotatedTx
101 if filter.Unconfirmed {
102 transaction, err = a.wallet.GetUnconfirmedTxByTxID(filter.ID)
104 transaction, err = a.wallet.GetTransactionByTxID(filter.ID)
106 transactions = []*query.AnnotatedTx{transaction}
108 if filter.Unconfirmed {
109 transactions, err = a.wallet.GetUnconfirmedTxs(filter.AccountID)
111 transactions, err = a.wallet.GetTransactions(filter.AccountID)
116 log.Errorf("listTransactions: %v", err)
117 return NewErrorResponse(err)
120 if filter.Detail == false {
121 txSummary := a.wallet.GetTransactionsSummary(transactions)
122 return NewSuccessResponse(txSummary)
124 return NewSuccessResponse(transactions)
127 // POST /get-unconfirmed-transaction
128 func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
129 TxID chainjson.HexBytes `json:"tx_id"`
132 copy(tmpTxID[:], filter.TxID[:])
134 txHash := bc.NewHash(tmpTxID)
135 txPool := a.chain.GetTxPool()
136 txDesc, err := txPool.GetTransaction(&txHash)
138 return NewErrorResponse(err)
143 Version: txDesc.Tx.Version,
144 Size: txDesc.Tx.SerializedSize,
145 TimeRange: txDesc.Tx.TimeRange,
146 Inputs: []*query.AnnotatedInput{},
147 Outputs: []*query.AnnotatedOutput{},
151 for i := range txDesc.Tx.Inputs {
152 tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(txDesc.Tx, uint32(i)))
154 for i := range txDesc.Tx.Outputs {
155 tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(txDesc.Tx, i))
158 return NewSuccessResponse(tx)
161 type unconfirmedTxsResp struct {
162 Total uint64 `json:"total"`
163 TxIDs []bc.Hash `json:"tx_ids"`
166 // POST /list-unconfirmed-transactions
167 func (a *API) listUnconfirmedTxs(ctx context.Context) Response {
170 txPool := a.chain.GetTxPool()
171 txs := txPool.GetTransactions()
172 for _, txDesc := range txs {
173 txIDs = append(txIDs, bc.Hash(txDesc.Tx.ID))
176 return NewSuccessResponse(&unconfirmedTxsResp{
177 Total: uint64(len(txIDs)),
182 // RawTx is the tx struct for getRawTransaction
184 Version uint64 `json:"version"`
185 Size uint64 `json:"size"`
186 TimeRange uint64 `json:"time_range"`
187 Inputs []*query.AnnotatedInput `json:"inputs"`
188 Outputs []*query.AnnotatedOutput `json:"outputs"`
189 Fee int64 `json:"fee"`
192 // POST /decode-raw-transaction
193 func (a *API) decodeRawTransaction(ctx context.Context, ins struct {
194 Tx types.Tx `json:"raw_transaction"`
197 Version: ins.Tx.Version,
198 Size: ins.Tx.SerializedSize,
199 TimeRange: ins.Tx.TimeRange,
200 Inputs: []*query.AnnotatedInput{},
201 Outputs: []*query.AnnotatedOutput{},
204 for i := range ins.Tx.Inputs {
205 tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(&ins.Tx, uint32(i)))
207 for i := range ins.Tx.Outputs {
208 tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(&ins.Tx, i))
211 totalInputBtm := uint64(0)
212 totalOutputBtm := uint64(0)
213 for _, input := range tx.Inputs {
214 if input.AssetID.String() == consensus.BTMAssetID.String() {
215 totalInputBtm += input.Amount
219 for _, output := range tx.Outputs {
220 if output.AssetID.String() == consensus.BTMAssetID.String() {
221 totalOutputBtm += output.Amount
225 tx.Fee = int64(totalInputBtm) - int64(totalOutputBtm)
226 return NewSuccessResponse(tx)
229 // POST /list-unspent-outputs
230 func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
231 ID string `json:"id"`
233 accountUTXOs := a.wallet.GetAccountUTXOs(filter.ID)
235 UTXOs := []query.AnnotatedUTXO{}
236 for _, utxo := range accountUTXOs {
237 UTXOs = append([]query.AnnotatedUTXO{{
238 AccountID: utxo.AccountID,
239 OutputID: utxo.OutputID.String(),
240 SourceID: utxo.SourceID.String(),
241 AssetID: utxo.AssetID.String(),
243 SourcePos: utxo.SourcePos,
244 Program: fmt.Sprintf("%x", utxo.ControlProgram),
245 ControlProgramIndex: utxo.ControlProgramIndex,
246 Address: utxo.Address,
247 ValidHeight: utxo.ValidHeight,
248 Alias: a.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
249 AssetAlias: a.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
254 return NewSuccessResponse(UTXOs)
258 func (a *API) gasRate() Response {
259 gasrate := map[string]int64{"gas_rate": consensus.VMGasRate}
260 return NewSuccessResponse(gasrate)