|
@@ -13,13 +13,15 @@ import (
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
- Config config
|
|
|
|
- CoinApi DcrCashApi
|
|
|
|
- WalleTxfee float64
|
|
|
|
- m_balanceparam map[string]float64
|
|
|
|
- mut sync.Mutex
|
|
|
|
- m_unspent []map[string]interface{}
|
|
|
|
- m_AccValues map[string]float64
|
|
|
|
|
|
+ Config config
|
|
|
|
+ CoinApi DcrCashApi
|
|
|
|
+ WalleTxfee float64
|
|
|
|
+ m_balanceparam map[string]float64
|
|
|
|
+ m_mutblance sync.Mutex
|
|
|
|
+ m_unspent []map[string]interface{}
|
|
|
|
+ m_mutspent sync.Mutex
|
|
|
|
+ m_AccValues map[string]float64
|
|
|
|
+ m_mutaccoutValue sync.Mutex
|
|
)
|
|
)
|
|
|
|
|
|
type DcrCash struct {
|
|
type DcrCash struct {
|
|
@@ -38,10 +40,8 @@ func (m DcrCash) InitDriver(param interface{}) bool {
|
|
return false
|
|
return false
|
|
}
|
|
}
|
|
go m.getwalletinfo()
|
|
go m.getwalletinfo()
|
|
- //列出当期钱包的所有账户
|
|
|
|
- go listaccounts()
|
|
|
|
- //列出所有账户的balance,和listunspent
|
|
|
|
- go SaveGetbalance()
|
|
|
|
|
|
+ //缓存数据
|
|
|
|
+ go AsyncRoutine()
|
|
return true
|
|
return true
|
|
}
|
|
}
|
|
|
|
|
|
@@ -279,7 +279,9 @@ func (m DcrCash) TransferAccounts(parm interface{}) ([]byte, error) {
|
|
}
|
|
}
|
|
return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, SENDCOINS_ERR, getErrMsg(SENDCOINS_ERR, nil))
|
|
return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, SENDCOINS_ERR, getErrMsg(SENDCOINS_ERR, nil))
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ go func() {
|
|
|
|
+ Cachelistaccount()
|
|
|
|
+ }()
|
|
if len(transactionid) != 0 {
|
|
if len(transactionid) != 0 {
|
|
var jbuf []byte
|
|
var jbuf []byte
|
|
resp := make(map[string]interface{})
|
|
resp := make(map[string]interface{})
|
|
@@ -469,71 +471,87 @@ func (m DcrCash) ChainHeight(parm interface{}) ([]byte, error) {
|
|
return []byte(sendstr), nil
|
|
return []byte(sendstr), nil
|
|
}
|
|
}
|
|
|
|
|
|
-func SaveGetbalance() {
|
|
|
|
- var address []string
|
|
|
|
- m_unspent = make([]map[string]interface{}, 0)
|
|
|
|
|
|
+func CacheGetbalance() {
|
|
|
|
+ fmt.Println("Cachegetbalance time:", time.Now().Format("2006-01-02 15:04:05"))
|
|
m_balanceparam = make(map[string]float64)
|
|
m_balanceparam = make(map[string]float64)
|
|
- for range time.Tick(2 * time.Minute) {
|
|
|
|
- fmt.Println("2 mine times", time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
- 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))
|
|
|
|
|
|
+ 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
|
|
}
|
|
}
|
|
- 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))
|
|
|
|
|
|
+ if vbalance.Spendable > maxConfirbalance {
|
|
|
|
+ maxConfirbalance = vbalance.Spendable
|
|
}
|
|
}
|
|
- 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
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ if vbalance.Unconfirmed > maxUnconfirbalance {
|
|
|
|
+ maxUnconfirbalance = vbalance.Unconfirmed
|
|
}
|
|
}
|
|
- mut.Lock()
|
|
|
|
- m_balanceparam["totalbalance"] = totalbalance
|
|
|
|
- m_balanceparam["confirmedbalance"] = maxConfirbalance
|
|
|
|
- m_balanceparam["unconfirmed"] = maxUnconfirbalance
|
|
|
|
- m_balanceparam["ntime"] = float64(time.Now().Unix())
|
|
|
|
- mut.Unlock()
|
|
|
|
|
|
|
|
- unspent, err := CoinApi.Listunspent(address)
|
|
|
|
- if err != nil {
|
|
|
|
- LOG("ERROR", " get listunspent:%v", err.Error())
|
|
|
|
- //return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
|
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
+ m_mutblance.Lock()
|
|
|
|
+ m_balanceparam["totalbalance"] = totalbalance
|
|
|
|
+ m_balanceparam["confirmedbalance"] = maxConfirbalance
|
|
|
|
+ m_balanceparam["unconfirmed"] = maxUnconfirbalance
|
|
|
|
+ m_balanceparam["ntime"] = float64(time.Now().Unix())
|
|
|
|
+ m_mutblance.Unlock()
|
|
|
|
+}
|
|
|
|
|
|
- m_unspent = unspent
|
|
|
|
|
|
+func Cachelistunspent() {
|
|
|
|
+ fmt.Println("cachelistunspent time:", time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
+ var address []string
|
|
|
|
+ m_unspent = make([]map[string]interface{}, 0)
|
|
|
|
+ unspent, err := CoinApi.Listunspent(address)
|
|
|
|
+ if err != nil {
|
|
|
|
+ LOG("ERROR", " get listunspent:%v", err.Error())
|
|
|
|
+ //return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
|
|
}
|
|
}
|
|
|
|
+ m_mutspent.Lock()
|
|
|
|
+ m_unspent = unspent
|
|
|
|
+ m_mutspent.Unlock()
|
|
}
|
|
}
|
|
|
|
|
|
-func listaccounts() {
|
|
|
|
- for range time.Tick(90 * time.Second) {
|
|
|
|
- fmt.Println("listaccounts after 90s.....", time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
- //list Accounts and account of balance
|
|
|
|
- accountsofValues, err := CoinApi.ListAccounts()
|
|
|
|
- if err != nil {
|
|
|
|
- LOG("ERROR", "List accounts error:%v", err.Error())
|
|
|
|
- //return nil, err
|
|
|
|
- }
|
|
|
|
- //
|
|
|
|
- mut.Lock()
|
|
|
|
- if err = json.Unmarshal(accountsofValues, &m_AccValues); err != nil {
|
|
|
|
- LOG("ERROR", "Unmarshal error:%v", err.Error())
|
|
|
|
- //return nil, err
|
|
|
|
|
|
+func Cachelistaccount() {
|
|
|
|
+ fmt.Println("Cachelistaccounts time", time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
+ //list Accounts and account of balance
|
|
|
|
+ accountsofValues, err := CoinApi.ListAccounts()
|
|
|
|
+ if err != nil {
|
|
|
|
+ LOG("ERROR", "List accounts error:%v", err.Error())
|
|
|
|
+ //return nil, err
|
|
|
|
+ }
|
|
|
|
+ //
|
|
|
|
+ m_mutaccoutValue.Lock()
|
|
|
|
+ if err = json.Unmarshal(accountsofValues, &m_AccValues); err != nil {
|
|
|
|
+ LOG("ERROR", "Unmarshal error:%v", err.Error())
|
|
|
|
+ //return nil, err
|
|
|
|
+ }
|
|
|
|
+ m_mutaccoutValue.Unlock()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func AsyncRoutine() {
|
|
|
|
+ CacheGetbalanceTicker := time.NewTicker(120 * time.Second)
|
|
|
|
+ CachelistaccountTicker := time.NewTicker(90 * time.Second)
|
|
|
|
+ CachelistunspentTicker := time.NewTicker(120 * time.Second)
|
|
|
|
+ for {
|
|
|
|
+ select {
|
|
|
|
+ case _ = <-CacheGetbalanceTicker.C:
|
|
|
|
+ CacheGetbalance()
|
|
|
|
+ case _ = <-CachelistaccountTicker.C:
|
|
|
|
+ Cachelistaccount()
|
|
|
|
+ case _ = <-CachelistunspentTicker.C:
|
|
|
|
+ Cachelistunspent()
|
|
}
|
|
}
|
|
- mut.Unlock()
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|