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"
16 // POST /list-accounts
17 func (a *API) listAccounts(ctx context.Context, filter struct {
20 accounts, err := a.wallet.AccountMgr.ListAccounts(filter.ID)
22 log.Errorf("listAccounts: %v", err)
23 return NewErrorResponse(err)
26 annotatedAccounts := []query.AnnotatedAccount{}
27 for _, acc := range accounts {
28 annotatedAccounts = append(annotatedAccounts, *account.Annotated(acc))
31 return NewSuccessResponse(annotatedAccounts)
35 func (a *API) listAssets(ctx context.Context, filter struct {
38 assets, err := a.wallet.AssetReg.ListAssets(filter.ID)
40 log.Errorf("listAssets: %v", err)
41 return NewErrorResponse(err)
44 return NewSuccessResponse(assets)
47 // POST /list-balances
48 func (a *API) listBalances(ctx context.Context) Response {
49 return NewSuccessResponse(a.wallet.GetAccountBalances(""))
52 // POST /get-transaction
53 func (a *API) getTransaction(ctx context.Context, txInfo struct {
54 TxID string `json:"tx_id"`
56 transaction, err := a.wallet.GetTransactionByTxID(txInfo.TxID)
58 log.Errorf("getTransaction error: %v", err)
59 return NewErrorResponse(err)
62 return NewSuccessResponse(transaction)
65 // POST /list-transactions
66 func (a *API) listTransactions(ctx context.Context, filter struct {
68 AccountID string `json:"account_id"`
69 Detail bool `json:"detail"`
71 transactions := []*query.AnnotatedTx{}
74 if filter.AccountID != "" {
75 transactions, err = a.wallet.GetTransactionsByAccountID(filter.AccountID)
77 transactions, err = a.wallet.GetTransactionsByTxID(filter.ID)
81 log.Errorf("listTransactions: %v", err)
82 return NewErrorResponse(err)
85 if filter.Detail == false {
86 txSummary := a.wallet.GetTransactionsSummary(transactions)
87 return NewSuccessResponse(txSummary)
89 return NewSuccessResponse(transactions)
92 // POST /get-unconfirmed-transaction
93 func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
94 TxID chainjson.HexBytes `json:"tx_id"`
97 copy(tmpTxID[:], filter.TxID[:])
99 txHash := bc.NewHash(tmpTxID)
100 txPool := a.chain.GetTxPool()
101 txDesc, err := txPool.GetTransaction(&txHash)
103 return NewErrorResponse(err)
108 Version: txDesc.Tx.Version,
109 Size: txDesc.Tx.SerializedSize,
110 TimeRange: txDesc.Tx.TimeRange,
111 Inputs: []*query.AnnotatedInput{},
112 Outputs: []*query.AnnotatedOutput{},
116 for i := range txDesc.Tx.Inputs {
117 tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(txDesc.Tx, uint32(i)))
119 for i := range txDesc.Tx.Outputs {
120 tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(txDesc.Tx, i))
123 return NewSuccessResponse(tx)
126 // POST /list-unconfirmed-transactions
127 func (a *API) listUnconfirmedTxs(ctx context.Context) Response {
130 txPool := a.chain.GetTxPool()
131 txs := txPool.GetTransactions()
132 for _, txDesc := range txs {
133 txIDs = append(txIDs, bc.Hash(txDesc.Tx.ID))
136 return NewSuccessResponse(txIDs)
139 // POST /list-unspent-outputs
140 func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
141 ID string `json:"id"`
143 accountUTXOs := a.wallet.GetAccountUTXOs(filter.ID)
145 UTXOs := []query.AnnotatedUTXO{}
146 for _, utxo := range accountUTXOs {
147 UTXOs = append([]query.AnnotatedUTXO{{
148 AccountID: utxo.AccountID,
149 OutputID: utxo.OutputID.String(),
150 SourceID: utxo.SourceID.String(),
151 AssetID: utxo.AssetID.String(),
153 SourcePos: utxo.SourcePos,
154 Program: fmt.Sprintf("%x", utxo.ControlProgram),
155 ControlProgramIndex: utxo.ControlProgramIndex,
156 Address: utxo.Address,
157 ValidHeight: utxo.ValidHeight,
158 Alias: a.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
159 AssetAlias: a.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
164 return NewSuccessResponse(UTXOs)
168 func (a *API) gasRate() Response {
169 gasrate := map[string]int64{"gasRate": consensus.VMGasRate}
170 return NewSuccessResponse(gasrate)