85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
|
package btc
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
func getTransactionParams(trxid string) []interface{} {
|
||
|
return []interface{}{trxid}
|
||
|
}
|
||
|
|
||
|
type GetTransactionResponseResult struct {
|
||
|
Amount float64 `json:"amount"`
|
||
|
Fee float64 `json:"fee"`
|
||
|
Confirmations int `json:"confirmations"`
|
||
|
Blockhash string `json:"blockhash"`
|
||
|
Blockheight int `json:"blockheight"`
|
||
|
Blockindex int `json:"blockindex"`
|
||
|
Blocktime int `json:"blocktime"`
|
||
|
Txid string `json:"txid"`
|
||
|
Walletconflicts []interface{} `json:"walletconflicts"`
|
||
|
Time int `json:"time"`
|
||
|
Timereceived int `json:"timereceived"`
|
||
|
Bip125Replaceable string `json:"bip125-replaceable"`
|
||
|
Details []struct {
|
||
|
Address string `json:"address,omitempty"`
|
||
|
Category string `json:"category"`
|
||
|
Amount float64 `json:"amount"`
|
||
|
Vout int `json:"vout"`
|
||
|
Fee float64 `json:"fee"`
|
||
|
Abandoned bool `json:"abandoned"`
|
||
|
} `json:"details"`
|
||
|
Hex string `json:"hex"`
|
||
|
}
|
||
|
|
||
|
type GetTransactionResponse struct {
|
||
|
Result GetTransactionResponseResult `json:"result"`
|
||
|
RPCResponse
|
||
|
}
|
||
|
|
||
|
func NewGetTransactionResponse(bts []byte) (*GetTransactionResponse, error) {
|
||
|
resp := new(GetTransactionResponse)
|
||
|
err := json.Unmarshal(bts, resp)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
|
func (b *BitcoinService) getTransaction(trxid string) (*GetTransactionResponseResult, error) {
|
||
|
req := b.NewRPCRequest().WithMethod(GET_TRANSACTION)
|
||
|
if req == nil {
|
||
|
return nil, fmt.Errorf("NewRPCRequest %s is nil", GET_TRANSACTION)
|
||
|
}
|
||
|
|
||
|
bts := getTransactionParams(trxid)
|
||
|
if bts == nil {
|
||
|
return nil, fmt.Errorf("NewGetTransactionParams is nil")
|
||
|
}
|
||
|
|
||
|
resp, err := req.Call(bts...)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("req.Call: %s", err)
|
||
|
}
|
||
|
|
||
|
strinResp := string(resp)
|
||
|
if strinResp == "" {
|
||
|
return nil, fmt.Errorf("strinResp: %s", err)
|
||
|
}
|
||
|
|
||
|
trx, err := NewGetTransactionResponse(resp)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("NewGetTransactionResponse: %s", err)
|
||
|
}
|
||
|
if trx.Error != nil {
|
||
|
return nil, fmt.Errorf("raw.Error: %+v", trx.Error)
|
||
|
}
|
||
|
if trx.Result.Txid != trxid {
|
||
|
return nil, fmt.Errorf("trx.Result.Txid: %s != trxid %s", trx.Result.Txid, trxid)
|
||
|
}
|
||
|
|
||
|
return &trx.Result, nil
|
||
|
}
|