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) Response {
18 accounts, err := a.wallet.AccountMgr.ListAccounts()
20 log.Errorf("listAccounts: %v", err)
21 return NewErrorResponse(err)
24 annotatedAccounts := []query.AnnotatedAccount{}
25 for _, acc := range accounts {
26 annotatedAccounts = append(annotatedAccounts, *account.Annotated(acc))
29 return NewSuccessResponse(annotatedAccounts)
33 func (a *API) getAsset(ctx context.Context, filter struct {
36 asset, err := a.wallet.AssetReg.GetAsset(filter.ID)
38 log.Errorf("getAsset: %v", err)
39 return NewErrorResponse(err)
42 return NewSuccessResponse(asset)
46 func (a *API) listAssets(ctx context.Context) Response {
47 assets, err := a.wallet.AssetReg.ListAssets()
49 log.Errorf("listAssets: %v", err)
50 return NewErrorResponse(err)
53 return NewSuccessResponse(assets)
56 // POST /list-balances
57 func (a *API) listBalances(ctx context.Context) Response {
58 balances, err := a.wallet.GetAccountBalances("")
60 return NewErrorResponse(err)
62 return NewSuccessResponse(balances)
65 // POST /get-transaction
66 func (a *API) getTransaction(ctx context.Context, txInfo struct {
67 TxID string `json:"tx_id"`
69 transaction, err := a.wallet.GetTransactionByTxID(txInfo.TxID)
71 log.Errorf("getTransaction error: %v", err)
72 return NewErrorResponse(err)
75 return NewSuccessResponse(transaction)
78 // POST /list-transactions
79 func (a *API) listTransactions(ctx context.Context, filter struct {
81 AccountID string `json:"account_id"`
82 Detail bool `json:"detail"`
84 transactions := []*query.AnnotatedTx{}
87 if filter.AccountID != "" {
88 transactions, err = a.wallet.GetTransactionsByAccountID(filter.AccountID)
90 transactions, err = a.wallet.GetTransactionsByTxID(filter.ID)
94 log.Errorf("listTransactions: %v", err)
95 return NewErrorResponse(err)
98 if filter.Detail == false {
99 txSummary := a.wallet.GetTransactionsSummary(transactions)
100 return NewSuccessResponse(txSummary)
102 return NewSuccessResponse(transactions)
105 // POST /get-unconfirmed-transaction
106 func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
107 TxID chainjson.HexBytes `json:"tx_id"`
110 copy(tmpTxID[:], filter.TxID[:])
112 txHash := bc.NewHash(tmpTxID)
113 txPool := a.chain.GetTxPool()
114 txDesc, err := txPool.GetTransaction(&txHash)
116 return NewErrorResponse(err)
121 Version: txDesc.Tx.Version,
122 Size: txDesc.Tx.SerializedSize,
123 TimeRange: txDesc.Tx.TimeRange,
124 Inputs: []*query.AnnotatedInput{},
125 Outputs: []*query.AnnotatedOutput{},
129 for i := range txDesc.Tx.Inputs {
130 tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(txDesc.Tx, uint32(i)))
132 for i := range txDesc.Tx.Outputs {
133 tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(txDesc.Tx, i))
136 return NewSuccessResponse(tx)
139 type unconfirmedTxsResp struct {
140 Total uint64 `json:"total"`
141 TxIDs []bc.Hash `json:"tx_ids"`
144 // POST /list-unconfirmed-transactions
145 func (a *API) listUnconfirmedTxs(ctx context.Context) Response {
148 txPool := a.chain.GetTxPool()
149 txs := txPool.GetTransactions()
150 for _, txDesc := range txs {
151 txIDs = append(txIDs, bc.Hash(txDesc.Tx.ID))
154 return NewSuccessResponse(&unconfirmedTxsResp{
155 Total: uint64(len(txIDs)),
160 // POST /list-unspent-outputs
161 func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
162 ID string `json:"id"`
164 accountUTXOs := a.wallet.GetAccountUTXOs(filter.ID)
166 UTXOs := []query.AnnotatedUTXO{}
167 for _, utxo := range accountUTXOs {
168 UTXOs = append([]query.AnnotatedUTXO{{
169 AccountID: utxo.AccountID,
170 OutputID: utxo.OutputID.String(),
171 SourceID: utxo.SourceID.String(),
172 AssetID: utxo.AssetID.String(),
174 SourcePos: utxo.SourcePos,
175 Program: fmt.Sprintf("%x", utxo.ControlProgram),
176 ControlProgramIndex: utxo.ControlProgramIndex,
177 Address: utxo.Address,
178 ValidHeight: utxo.ValidHeight,
179 Alias: a.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
180 AssetAlias: a.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
185 return NewSuccessResponse(UTXOs)
189 func (a *API) gasRate() Response {
190 gasrate := map[string]int64{"gas_rate": consensus.VMGasRate}
191 return NewSuccessResponse(gasrate)