From 6044a6206bca3a40f9e92a200df7564617958bfd Mon Sep 17 00:00:00 2001 From: zxl <327966982@qq.com> Date: Mon, 29 Apr 2019 15:37:19 +0800 Subject: [PATCH] add GetBalanceHttp --- AdaptorBTC.go | 3 +++ spend.go | 14 +++++++++----- unspend.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ unspend_test.go | 19 +++++++++++++++++++ 4 files changed, 78 insertions(+), 5 deletions(-) diff --git a/AdaptorBTC.go b/AdaptorBTC.go index 29b5e13..f0f4459 100644 --- a/AdaptorBTC.go +++ b/AdaptorBTC.go @@ -83,6 +83,9 @@ func (abtc AdaptorBTC) SignTxSend(params *adaptor.SignTxSendParams) (string, err func (abtc AdaptorBTC) GetBalance(params *adaptor.GetBalanceParams) (string, error) { return GetBalance(params, &abtc.RPCParams, abtc.NetID) } +func (abtc AdaptorBTC) GetBalanceHttp(params *adaptor.GetBalanceHttpParams) (string, error) { + return GetBalanceHttp(params, abtc.NetID) +} func (abtc AdaptorBTC) GetTransactions(params *adaptor.GetTransactionsParams) (string, error) { return GetTransactions(params, &abtc.RPCParams, abtc.NetID) } diff --git a/spend.go b/spend.go index 879af8d..787c052 100644 --- a/spend.go +++ b/spend.go @@ -319,8 +319,9 @@ func SignTransaction(signTransactionParams *adaptor.SignTransactionParams, netID var rawInputs []RawTxInput for { //decode Transaction hexString to bytes - rawTXBytes, err := hex.DecodeString(signTransactionParams.TransactionHex) - if err != nil { + rawTXBytes, err1 := hex.DecodeString(signTransactionParams.TransactionHex) + if err1 != nil { + err = err1 break } //deserialize to MsgTx @@ -413,8 +414,11 @@ func SignTransaction(signTransactionParams *adaptor.SignTransactionParams, netID } type SendTransactionHttppResponse struct { - Network string `json:"network"` - Txid string `json:"txid"` + //Status string `json:"status"` + Data struct { + Network string `json:"network"` + Txid string `json:"txid"` + } `json:"data"` } func SendTransactionHttp(sendTransactionParams *adaptor.SendTransactionHttpParams, netID int) (string, error) { @@ -450,7 +454,7 @@ func SendTransactionHttp(sendTransactionParams *adaptor.SendTransactionHttpParam //result for return var sendTransactionResult adaptor.SendTransactionHttpResult - sendTransactionResult.TransactionHah = txResult.Txid + sendTransactionResult.TransactionHah = txResult.Data.Txid jsonResult, err := json.Marshal(sendTransactionResult) if err != nil { diff --git a/unspend.go b/unspend.go index 80c1410..1373208 100644 --- a/unspend.go +++ b/unspend.go @@ -313,6 +313,53 @@ func GetBalance(getBalanceParams *adaptor.GetBalanceParams, rpcParams *RPCParams return string(jsonResult), nil } +type GetBalanceHttpResponse struct { + //Status string `json:"status"` + Data struct { + //Network string `json:"network"` + Address string `json:"address"` + ConfirmedBalance string `json:"confirmed_balance"` + UnconfirmedBalance string `json:"unconfirmed_balance"` + } `json:"data"` +} + +func GetBalanceHttp(params *adaptor.GetBalanceHttpParams, netID int) (string, error) { + if "" == params.Address { + return "", errors.New("Address is empty") + } + var request string + if netID == NETID_MAIN { + request = base + "get_address_balance/BTC/" + } else { + request = base + "get_address_balance/BTCTEST/" + } + request += params.Address + if params.Minconf != 0 { + request += "/" + strconv.Itoa(params.Minconf) + } + + strRespose, err, _ := httpGet(request) + if err != nil { + return "", err + } + + var balanceRes GetBalanceHttpResponse + err = json.Unmarshal([]byte(strRespose), &balanceRes) + if err != nil { + return "", err + } + //compute total Amount for balance + var result adaptor.GetBalanceHttpResult + balance, _ := strconv.ParseFloat(balanceRes.Data.ConfirmedBalance, 64) + result.Value = balance + jsonResult, err := json.Marshal(result) + if err != nil { + return "", err + } + + return string(jsonResult), nil +} + func getAddrValue(client *rpcclient.Client, chainParams *chaincfg.Params, txHash *chainhash.Hash, index int) (addr string, value float64) { //get raw transaction by txHash diff --git a/unspend_test.go b/unspend_test.go index 6131c57..e36ad0f 100644 --- a/unspend_test.go +++ b/unspend_test.go @@ -70,6 +70,25 @@ func TestGetBalance(t *testing.T) { } } +func TestGetBalanceHttp(t *testing.T) { + testResult := `{"value":16.0191}` + + //getBalanceParams := &adaptor.GetBalanceParams{"mp6DHyYNuD28aiE1MQdKuRAdH7ZNydqUBC", 0} + getBalanceParams := &adaptor.GetBalanceHttpParams{"mp6DHyYNuD28aiE1MQdKuRAdH7ZNydqUBC", 6} + //getBalanceParams := &adaptor.GetBalanceParams{"tb1q73un52phlrsug2r35fgnrlme987tr3cgm88k8j", 0}//? + //getBalanceParams := &adaptor.GetBalanceParams{"1DEP8i3QJCsomS4BSMY2RpU1upv62aGvhD", 0} + + result, err := GetBalanceHttp(getBalanceParams, NETID_TEST) + if err != nil { + fmt.Println(err.Error()) + } else { + fmt.Println(result) + if !strings.Contains(result, testResult) { + fmt.Printf("unexpected result - got: %v, "+"want: %v\n", result, testResult) + } + } +} + func TestGetTransactions(t *testing.T) { rpcParams := RPCParams{ Host: "localhost:18334",