|
@@ -4,6 +4,7 @@ import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"strconv"
|
|
|
+ "sync"
|
|
|
. "template"
|
|
|
"time"
|
|
|
|
|
@@ -12,9 +13,11 @@ import (
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- Config config
|
|
|
- CoinApi DcrCashApi
|
|
|
- WalleTxfee float64
|
|
|
+ Config config
|
|
|
+ CoinApi DcrCashApi
|
|
|
+ WalleTxfee float64
|
|
|
+ balanceparam map[string]float64
|
|
|
+ mut sync.Mutex
|
|
|
)
|
|
|
|
|
|
type DcrCash struct {
|
|
@@ -58,7 +61,6 @@ func (m DcrCash) initconfig() bool {
|
|
|
LOG("ERROR", "init json error:%v", err.Error())
|
|
|
return false
|
|
|
}
|
|
|
-
|
|
|
fee := float64(f)
|
|
|
CoinApi.WalletSetTxFee(fee) //
|
|
|
return true
|
|
@@ -86,40 +88,43 @@ func (m DcrCash) getwalletinfo() bool {
|
|
|
}
|
|
|
|
|
|
func (m DcrCash) CoinBalance(parm interface{}) ([]byte, error) {
|
|
|
- confirmedBalance, err := CoinApi.WalleGetBalance("*")
|
|
|
- if err != nil {
|
|
|
- LOG("ERROR", " GetBalance err:%v", err.Error())
|
|
|
- return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBALANCE_ERR, getErrMsg(GETBALANCE_ERR, nil))
|
|
|
- }
|
|
|
- var balance []account
|
|
|
- if err = json.Unmarshal(confirmedBalance, &balance); err != nil {
|
|
|
- LOG("ERROR", " inner json error:%v", err.Error())
|
|
|
- return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
|
|
|
- }
|
|
|
- var totalbalance float64
|
|
|
- //var totalUnconfirbalance float64
|
|
|
- var maxConfirbalance float64
|
|
|
- var maxUnconfirbalance float64
|
|
|
- for _, vbalance := range balance {
|
|
|
- /*if vbalance.Spendable != 0 {
|
|
|
- totalConfirbalance += vbalance.Spendable
|
|
|
- }
|
|
|
- if vbalance.Unconfirmed != 0 {
|
|
|
- totalUnconfirbalance += vbalance.Unconfirmed
|
|
|
- }*/
|
|
|
- if vbalance.Total != 0 {
|
|
|
- totalbalance += vbalance.Total
|
|
|
- }
|
|
|
- if vbalance.Spendable > maxConfirbalance {
|
|
|
- maxConfirbalance = vbalance.Spendable
|
|
|
+ var totalbalance, maxConfirbalance, maxUnconfirbalance float64
|
|
|
+ pjs := parm.(*simplejson.Json)
|
|
|
+ input, err := pjs.Get("Sync").Bool()
|
|
|
+ if !input {
|
|
|
+ fmt.Println("the balance is not sync!!")
|
|
|
+ totalbalance = balanceparam["totalbalance"]
|
|
|
+ maxConfirbalance = balanceparam["confirmedbalance"]
|
|
|
+ maxUnconfirbalance = balanceparam["unconfirmed"]
|
|
|
+ } else {
|
|
|
+ acountBalace, err := CoinApi.WalleGetBalance("*")
|
|
|
+ if err != nil {
|
|
|
+ LOG("ERROR", " GetBalance err:%v", err.Error())
|
|
|
+ return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBALANCE_ERR, getErrMsg(GETBALANCE_ERR, nil))
|
|
|
}
|
|
|
- if vbalance.Unconfirmed > maxUnconfirbalance {
|
|
|
- maxUnconfirbalance = vbalance.Unconfirmed
|
|
|
+ var accounts []account
|
|
|
+ if err = json.Unmarshal(acountBalace, &accounts); err != nil {
|
|
|
+ LOG("ERROR", " inner json error:%v", err.Error())
|
|
|
+ return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
|
|
|
}
|
|
|
|
|
|
+ for _, vbalance := range accounts {
|
|
|
+ if vbalance.Total != 0 {
|
|
|
+ totalbalance += vbalance.Total
|
|
|
+ }
|
|
|
+ if vbalance.Spendable > maxConfirbalance {
|
|
|
+ maxConfirbalance = vbalance.Spendable
|
|
|
+ }
|
|
|
+ if vbalance.Unconfirmed > maxUnconfirbalance {
|
|
|
+ maxUnconfirbalance = vbalance.Unconfirmed
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
var jbuf []byte
|
|
|
resp := make(map[string]interface{})
|
|
|
+
|
|
|
resp["confirmedbalance"] = maxConfirbalance
|
|
|
resp["unconfirmed"] = maxUnconfirbalance
|
|
|
resp["totalbalance"] = totalbalance
|
|
@@ -462,7 +467,47 @@ func (m DcrCash) ChainHeight(parm interface{}) ([]byte, error) {
|
|
|
return []byte(sendstr), nil
|
|
|
}
|
|
|
|
|
|
-func init() {
|
|
|
+func SaveGetbalance() {
|
|
|
+ balanceparam = make(map[string]float64)
|
|
|
+ for range time.Tick(2 * time.Minute) {
|
|
|
+ //DcrCashApi.WalleGetBalance()
|
|
|
+ acountBalace, err := CoinApi.WalleGetBalance("*")
|
|
|
+ if err != nil {
|
|
|
+ LOG("ERROR", " GetBalance err:%v", err.Error())
|
|
|
+ //return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBALANCE_ERR, getErrMsg(GETBALANCE_ERR, nil))
|
|
|
+ }
|
|
|
+ var accounts []account
|
|
|
+ if err = json.Unmarshal(acountBalace, &accounts); err != nil {
|
|
|
+ LOG("ERROR", " inner json error:%v", err.Error())
|
|
|
+ //return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
|
|
|
+ }
|
|
|
+ var totalbalance float64
|
|
|
+ //var totalUnconfirbalance float64
|
|
|
+ var maxConfirbalance float64
|
|
|
+ var maxUnconfirbalance float64
|
|
|
+ for _, vbalance := range accounts {
|
|
|
+ if vbalance.Total != 0 {
|
|
|
+ totalbalance += vbalance.Total
|
|
|
+ }
|
|
|
+ if vbalance.Spendable > maxConfirbalance {
|
|
|
+ maxConfirbalance = vbalance.Spendable
|
|
|
+ }
|
|
|
+ if vbalance.Unconfirmed > maxUnconfirbalance {
|
|
|
+ maxUnconfirbalance = vbalance.Unconfirmed
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ mut.Lock()
|
|
|
+ balanceparam["totalbalance"] = totalbalance
|
|
|
+ balanceparam["confirmedbalance"] = maxConfirbalance
|
|
|
+ balanceparam["unconfirmed"] = maxUnconfirbalance
|
|
|
+ mut.Unlock()
|
|
|
+ fmt.Println("2 mine times", time.Now().Format("2006-01-02 15:04:05"))
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
+func init() {
|
|
|
Register(NameCoin, &DcrCash{})
|
|
|
+ go SaveGetbalance()
|
|
|
+
|
|
|
}
|