feat: set up project
This commit is contained in:
114
internal/services/btc/get_raw_transaction.go
Normal file
114
internal/services/btc/get_raw_transaction.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package btc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func getRawTransactionParams(trxid, blockchash string) []interface{} {
|
||||
return []interface{}{trxid, true, blockchash}
|
||||
}
|
||||
|
||||
type GetRawTransactionResponseResult struct {
|
||||
InActiveChain bool `json:"in_active_chain"`
|
||||
Txid string `json:"txid"`
|
||||
Hash string `json:"hash"`
|
||||
Version int `json:"version"`
|
||||
Size int `json:"size"`
|
||||
Vsize int `json:"vsize"`
|
||||
Weight int `json:"weight"`
|
||||
Locktime int `json:"locktime"`
|
||||
Vin []struct {
|
||||
Txid string `json:"txid"`
|
||||
Vout int `json:"vout"`
|
||||
ScriptSig struct {
|
||||
Asm string `json:"asm"`
|
||||
Hex string `json:"hex"`
|
||||
} `json:"scriptSig"`
|
||||
Txinwitness []string `json:"txinwitness"`
|
||||
Sequence int64 `json:"sequence"`
|
||||
} `json:"vin"`
|
||||
Vout []GetRawTransactionResponseResultVout `json:"vout"`
|
||||
Hex string `json:"hex"`
|
||||
Blockhash string `json:"blockhash"`
|
||||
Confirmations int `json:"confirmations"`
|
||||
Time int `json:"time"`
|
||||
Blocktime int `json:"blocktime"`
|
||||
}
|
||||
|
||||
type GetRawTransactionResponseResultVout struct {
|
||||
Value float64 `json:"value"`
|
||||
N int `json:"n"`
|
||||
ScriptPubKey struct {
|
||||
Asm string `json:"asm"`
|
||||
Hex string `json:"hex"`
|
||||
Address string `json:"address,omitempty"`
|
||||
Type string `json:"type"`
|
||||
} `json:"scriptPubKey,omitempty"`
|
||||
}
|
||||
|
||||
func (g GetRawTransactionResponseResult) getOpReturn() string {
|
||||
opReturn := ""
|
||||
for _, v := range g.Vout {
|
||||
if strings.HasPrefix(v.ScriptPubKey.Asm, "OP_RETURN") {
|
||||
opReturn = v.ScriptPubKey.Asm
|
||||
break
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(opReturn, "OP_RETURN") {
|
||||
return ""
|
||||
}
|
||||
hexMessage := strings.Split(opReturn, " ")[len(strings.Split(opReturn, " "))-1]
|
||||
return hexMessage
|
||||
}
|
||||
|
||||
type GetRawTransactionResponse struct {
|
||||
Result GetRawTransactionResponseResult `json:"result"`
|
||||
RPCResponse
|
||||
}
|
||||
|
||||
func NewGetRawTransactionResponse(bts []byte) (*GetRawTransactionResponse, error) {
|
||||
resp := new(GetRawTransactionResponse)
|
||||
err := json.Unmarshal(bts, resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (b *BitcoinService) getRawTransaction(trxid, blockhash string) (*GetRawTransactionResponseResult, error) {
|
||||
req := b.NewRPCRequest().WithMethod(GET_RAW_TRANSACTION)
|
||||
if req == nil {
|
||||
return nil, fmt.Errorf("NewRPCRequest %s is nil", GET_RAW_TRANSACTION)
|
||||
}
|
||||
|
||||
bts := getRawTransactionParams(trxid, blockhash)
|
||||
if bts == nil {
|
||||
return nil, fmt.Errorf("getRawTransactionParams 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 := NewGetRawTransactionResponse(resp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NewGetRawTransactionResponse: %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
|
||||
}
|
||||
Reference in New Issue
Block a user