Skip to content

Commit

Permalink
add GetBalanceHttp
Browse files Browse the repository at this point in the history
  • Loading branch information
zxlwdgithub committed Apr 29, 2019
1 parent be475f0 commit 6044a62
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
3 changes: 3 additions & 0 deletions AdaptorBTC.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 9 additions & 5 deletions spend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
47 changes: 47 additions & 0 deletions unspend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions unspend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 6044a62

Please sign in to comment.