feat: set up project
This commit is contained in:
72
internal/services/btc/get_block.go
Normal file
72
internal/services/btc/get_block.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package btc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func NewGetBlockParams(blockHash string) []interface{} {
|
||||
return []interface{}{blockHash}
|
||||
}
|
||||
|
||||
type GetBlockResponse struct {
|
||||
Result GetBlockResponseResult `json:"result"`
|
||||
RPCResponse
|
||||
}
|
||||
type GetBlockResponseResult struct {
|
||||
Hash string `json:"hash"`
|
||||
Confirmations int `json:"confirmations"`
|
||||
Height int `json:"height"`
|
||||
Version int `json:"version"`
|
||||
VersionHex string `json:"versionHex"`
|
||||
Merkleroot string `json:"merkleroot"`
|
||||
Time int `json:"time"`
|
||||
Mediantime int `json:"mediantime"`
|
||||
Nonce int `json:"nonce"`
|
||||
Bits string `json:"bits"`
|
||||
Difficulty float64 `json:"difficulty"`
|
||||
Chainwork string `json:"chainwork"`
|
||||
NTx int `json:"nTx"`
|
||||
Previousblockhash string `json:"previousblockhash"`
|
||||
Strippedsize int `json:"strippedsize"`
|
||||
Size int `json:"size"`
|
||||
Weight int `json:"weight"`
|
||||
Tx []string `json:"tx"`
|
||||
}
|
||||
|
||||
func NewGetBlockResponse(bts []byte) (*GetBlockResponse, error) {
|
||||
resp := new(GetBlockResponse)
|
||||
err := json.Unmarshal(bts, resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (b *BitcoinService) getBlock(blockHash string) (*GetBlockResponseResult, error) {
|
||||
req := b.NewRPCRequest().WithMethod(GET_BLOCK)
|
||||
if req == nil {
|
||||
return nil, fmt.Errorf("NewRPCRequest %s is nil", GET_BLOCK)
|
||||
}
|
||||
|
||||
resp, err := req.Call(NewGetBlockParams(blockHash)...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("req.Call: %s", err)
|
||||
}
|
||||
|
||||
strinResp := string(resp)
|
||||
if strinResp == "" {
|
||||
return nil, fmt.Errorf("strinResp: %s", err)
|
||||
}
|
||||
|
||||
block, err := NewGetBlockResponse(resp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NewGetBlockResponse: %s", err)
|
||||
}
|
||||
if block.Error != nil {
|
||||
return nil, fmt.Errorf("raw.Error: %+v", block.Error)
|
||||
}
|
||||
|
||||
return &block.Result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user