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 transaction, err := a.wallet.GetTransactionByTxID(txInfo.TxID)
76 log.Errorf("getTransaction error: %v", err)
77 return NewErrorResponse(err)
80 return NewSuccessResponse(transaction)
83 // POST /list-transactions
84 func (a *API) listTransactions(ctx context.Context, filter struct {
86 AccountID string `json:"account_id"`
87 Detail bool `json:"detail"`
89 transactions := []*query.AnnotatedTx{}
92 if filter.AccountID != "" {
93 transactions, err = a.wallet.GetTransactionsByAccountID(filter.AccountID)
95 transactions, err = a.wallet.GetTransactionsByTxID(filter.ID)
99 log.Errorf("listTransactions: %v", err)
100 return NewErrorResponse(err)
103 if filter.Detail == false {
104 txSummary := a.wallet.GetTransactionsSummary(transactions)
105 return NewSuccessResponse(txSummary)
107 return NewSuccessResponse(transactions)
110 // POST /get-unconfirmed-transaction
111 func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
112 TxID chainjson.HexBytes `json:"tx_id"`
115 copy(tmpTxID[:], filter.TxID[:])
117 txHash := bc.NewHash(tmpTxID)
118 txPool := a.chain.GetTxPool()
119 txDesc, err := txPool.GetTransaction(&txHash)
121 return NewErrorResponse(err)
126 Version: txDesc.Tx.Version,
127 Size: txDesc.Tx.SerializedSize,
128 TimeRange: txDesc.Tx.TimeRange,
129 Inputs: []*query.AnnotatedInput{},
130 Outputs: []*query.AnnotatedOutput{},
134 for i := range txDesc.Tx.Inputs {
135 tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(txDesc.Tx, uint32(i)))
137 for i := range txDesc.Tx.Outputs {
138 tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(txDesc.Tx, i))
141 return NewSuccessResponse(tx)
144 type unconfirmedTxsResp struct {
145 Total uint64 `json:"total"`
146 TxIDs []bc.Hash `json:"tx_ids"`
149 // POST /list-unconfirmed-transactions
150 func (a *API) listUnconfirmedTxs(ctx context.Context) Response {
153 txPool := a.chain.GetTxPool()
154 txs := txPool.GetTransactions()
155 for _, txDesc := range txs {
156 txIDs = append(txIDs, bc.Hash(txDesc.Tx.ID))
159 return NewSuccessResponse(&unconfirmedTxsResp{
160 Total: uint64(len(txIDs)),
165 // RawTx is the tx struct for getRawTransaction
167 Version uint64 `json:"version"`
168 Size uint64 `json:"size"`
169 TimeRange uint64 `json:"time_range"`
170 Inputs []*query.AnnotatedInput `json:"inputs"`
171 Outputs []*query.AnnotatedOutput `json:"outputs"`
172 Fee int64 `json:"fee"`
175 // POST /decode-raw-transaction
176 func (a *API) decodeRawTransaction(ctx context.Context, ins struct {
177 Tx types.Tx `json:"raw_transaction"`
180 Version: ins.Tx.Version,
181 Size: ins.Tx.SerializedSize,
182 TimeRange: ins.Tx.TimeRange,
183 Inputs: []*query.AnnotatedInput{},
184 Outputs: []*query.AnnotatedOutput{},
187 for i := range ins.Tx.Inputs {
188 tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(&ins.Tx, uint32(i)))
190 for i := range ins.Tx.Outputs {
191 tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(&ins.Tx, i))
194 totalInputBtm := uint64(0)
195 totalOutputBtm := uint64(0)
196 for _, input := range tx.Inputs {
197 if input.AssetID.String() == consensus.BTMAssetID.String() {
198 totalInputBtm += input.Amount
202 for _, output := range tx.Outputs {
203 if output.AssetID.String() == consensus.BTMAssetID.String() {
204 totalOutputBtm += output.Amount
208 tx.Fee = int64(totalInputBtm) - int64(totalOutputBtm)
209 return NewSuccessResponse(tx)
212 // POST /list-unspent-outputs
213 func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
214 ID string `json:"id"`
216 accountUTXOs := a.wallet.GetAccountUTXOs(filter.ID)
218 UTXOs := []query.AnnotatedUTXO{}
219 for _, utxo := range accountUTXOs {
220 UTXOs = append([]query.AnnotatedUTXO{{
221 AccountID: utxo.AccountID,
222 OutputID: utxo.OutputID.String(),
223 SourceID: utxo.SourceID.String(),
224 AssetID: utxo.AssetID.String(),
226 SourcePos: utxo.SourcePos,
227 Program: fmt.Sprintf("%x", utxo.ControlProgram),
228 ControlProgramIndex: utxo.ControlProgramIndex,
229 Address: utxo.Address,
230 ValidHeight: utxo.ValidHeight,
231 Alias: a.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
232 AssetAlias: a.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
237 return NewSuccessResponse(UTXOs)
241 func (a *API) gasRate() Response {
242 gasrate := map[string]int64{"gas_rate": consensus.VMGasRate}
243 return NewSuccessResponse(gasrate)