mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-29 17:32:31 +00:00
compatiblity hack for blockchain.info getbalance
This commit is contained in:
parent
160a843171
commit
331f25f506
@ -1047,6 +1047,10 @@ type ConnConfig struct {
|
|||||||
// however, not all servers support the websocket extensions, so this
|
// however, not all servers support the websocket extensions, so this
|
||||||
// flag can be set to true to use basic HTTP POST requests instead.
|
// flag can be set to true to use basic HTTP POST requests instead.
|
||||||
HttpPostMode bool
|
HttpPostMode bool
|
||||||
|
|
||||||
|
// EnableBCInfoHacks is an option provided to enable compatiblity hacks
|
||||||
|
// when connecting to blockchain.info RPC server
|
||||||
|
EnableBCInfoHacks bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// newHTTPClient returns a new http client that is configured according to the
|
// newHTTPClient returns a new http client that is configured according to the
|
||||||
|
38
wallet.go
38
wallet.go
@ -6,6 +6,7 @@ package btcrpcclient
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/conformal/btcjson"
|
"github.com/conformal/btcjson"
|
||||||
"github.com/conformal/btcnet"
|
"github.com/conformal/btcnet"
|
||||||
@ -1537,6 +1538,39 @@ func (r FutureGetBalanceResult) Receive() (btcutil.Amount, error) {
|
|||||||
return btcutil.Amount(satoshi), nil
|
return btcutil.Amount(satoshi), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FutureGetBalanceParseResult is same as FutureGetBalanceResult except
|
||||||
|
// that the result is expected to be a string which is then parsed into
|
||||||
|
// a float64 value
|
||||||
|
// This is required for compatiblity with servers like blockchain.info
|
||||||
|
type FutureGetBalanceParseResult chan *response
|
||||||
|
|
||||||
|
// Receive waits for the response promised by the future and returns the
|
||||||
|
// available balance from the server for the specified account.
|
||||||
|
func (r FutureGetBalanceParseResult) Receive() (btcutil.Amount, error) {
|
||||||
|
res, err := receiveFuture(r)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal result as a string
|
||||||
|
var balanceString string
|
||||||
|
err = json.Unmarshal(res, &balanceString)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
balance, err := strconv.ParseFloat(balanceString, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
satoshi, err := btcjson.JSONToAmount(balance)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return btcutil.Amount(satoshi), nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetBalanceAsync returns an instance of a type that can be used to get the
|
// GetBalanceAsync returns an instance of a type that can be used to get the
|
||||||
// result of the RPC at some future time by invoking the Receive function on the
|
// result of the RPC at some future time by invoking the Receive function on the
|
||||||
// returned instance.
|
// returned instance.
|
||||||
@ -1590,6 +1624,10 @@ func (c *Client) GetBalanceMinConfAsync(account string, minConfirms int) FutureG
|
|||||||
//
|
//
|
||||||
// See GetBalance to use the default minimum number of confirmations.
|
// See GetBalance to use the default minimum number of confirmations.
|
||||||
func (c *Client) GetBalanceMinConf(account string, minConfirms int) (btcutil.Amount, error) {
|
func (c *Client) GetBalanceMinConf(account string, minConfirms int) (btcutil.Amount, error) {
|
||||||
|
if c.config.EnableBCInfoHacks {
|
||||||
|
response := c.GetBalanceMinConfAsync(account, minConfirms)
|
||||||
|
return FutureGetBalanceParseResult(response).Receive()
|
||||||
|
}
|
||||||
return c.GetBalanceMinConfAsync(account, minConfirms).Receive()
|
return c.GetBalanceMinConfAsync(account, minConfirms).Receive()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user