btc-pay-checker/internal/services/btc/get_balance.go

53 lines
1.0 KiB
Go
Raw Normal View History

2023-07-19 11:47:46 +02:00
package btc
import (
"encoding/json"
"fmt"
)
func NewGetBalanceParams() []interface{} {
return []interface{}{}
}
type GetBalanceResponse struct {
Result float64 `json:"result"`
RPCResponse
}
func NewGetBalanceResponse(bts []byte) (*GetBalanceResponse, error) {
resp := new(GetBalanceResponse)
err := json.Unmarshal(bts, resp)
if err != nil {
return nil, err
}
return resp, nil
}
func (b *BitcoinService) GetBalance() (float64, error) {
req := b.NewRPCRequest().WithMethod(GET_BALANCE)
if req == nil {
return 0.0, fmt.Errorf("NewRPCRequest %s is nil", GET_BALANCE)
}
resp, err := req.Call(NewGetBalanceParams()...)
if err != nil {
return 0.0, fmt.Errorf("req.Call: %s", err)
}
strinResp := string(resp)
if strinResp == "" {
return 0.0, fmt.Errorf("strinResp: %s", err)
}
trx, err := NewGetBalanceResponse(resp)
if err != nil {
return 0.0, fmt.Errorf("NewGetBalanceResponse: %s", err)
}
if trx.Error != nil {
return 0.0, fmt.Errorf("raw.Error: %+v", trx.Error)
}
return trx.Result, nil
}