|
@@ -5,6 +5,7 @@ import (
|
|
|
//"errors"
|
|
|
"fmt"
|
|
|
"strconv"
|
|
|
+ "sync"
|
|
|
. "template"
|
|
|
"time"
|
|
|
|
|
@@ -13,8 +14,10 @@ import (
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- Config config
|
|
|
- CoinApi BuCashApi
|
|
|
+ Config config
|
|
|
+ CoinApi BuCashApi
|
|
|
+ m_unspent []map[string]interface{}
|
|
|
+ m_mutUnspent sync.Mutex
|
|
|
)
|
|
|
|
|
|
type Bucash struct {
|
|
@@ -30,6 +33,9 @@ func (m Bucash) InitDriver(parm interface{}) bool {
|
|
|
return false
|
|
|
}
|
|
|
go m.getwalletinfo()
|
|
|
+
|
|
|
+ //缓存当前未花费的
|
|
|
+ go AsyncRoutine()
|
|
|
return true
|
|
|
}
|
|
|
|
|
@@ -306,23 +312,31 @@ func (m Bucash) ChainHeight(parm interface{}) ([]byte, error) {
|
|
|
}
|
|
|
|
|
|
func (m Bucash) ListUnspent(parm interface{}) ([]byte, error) {
|
|
|
+ i_unspent := make([]map[string]interface{}, 0)
|
|
|
pjs := parm.(*simplejson.Json)
|
|
|
- address, err := pjs.Get("address").StringArray()
|
|
|
- if err != nil {
|
|
|
- LOG("ERROR", " input parm error:%v", err.Error())
|
|
|
- return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
|
|
|
- }
|
|
|
- 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))
|
|
|
+ Issync, err := pjs.Get("Sync").Bool()
|
|
|
+ if !Issync {
|
|
|
+ fmt.Println("The listunspent data Asynchrony!!!!")
|
|
|
+ i_unspent = m_unspent
|
|
|
+ } else {
|
|
|
+ address, err := pjs.Get("address").StringArray()
|
|
|
+ if err != nil {
|
|
|
+ LOG("ERROR", " input parm error:%v", err.Error())
|
|
|
+ return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
|
|
|
+ }
|
|
|
+ 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))
|
|
|
+ }
|
|
|
+ i_unspent = unspent
|
|
|
}
|
|
|
|
|
|
var jbuf []byte
|
|
|
resptmp := make(map[string]interface{})
|
|
|
resptmp["errcode"] = 0
|
|
|
resptmp["cointype"] = "BCC"
|
|
|
- resptmp["unspents"] = unspent
|
|
|
+ resptmp["unspents"] = i_unspent
|
|
|
|
|
|
if jbuf, err = json.Marshal(resptmp); err != nil {
|
|
|
LOG("ERROR", " inner json error:%v", err.Error())
|
|
@@ -363,6 +377,57 @@ func (m Bucash) SendRawTransaction(parm interface{}) ([]byte, error) {
|
|
|
func (m Bucash) SumcoinBalance(parm interface{}) ([]byte, error) {
|
|
|
return nil, nil
|
|
|
}
|
|
|
+func (m Bucash) AccountsBalance(parm interface{}) (jbuf []byte, err error) {
|
|
|
+ var balancestotal, offlinebalancetotal float64
|
|
|
+ ilistunspent := m_unspent
|
|
|
+ accountbalance := make([]map[string]interface{}, 0)
|
|
|
+ offlinebalance := make([]map[string]interface{}, 0)
|
|
|
+ for _, value := range ilistunspent {
|
|
|
+ if value["spendable"] == true {
|
|
|
+ balancestotal += value["Amount"].(float64)
|
|
|
+ accountbalance = append(accountbalance, value)
|
|
|
+ } else {
|
|
|
+ offlinebalancetotal += value["Amount"].(float64)
|
|
|
+ offlinebalance = append(offlinebalance, value)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ resptmp := make(map[string]interface{})
|
|
|
+ resptmp["errcode"] = 0
|
|
|
+ resptmp["cointype"] = "BCC"
|
|
|
+ resptmp["accountbalance"] = accountbalance
|
|
|
+ resptmp["balancestotal"] = balancestotal
|
|
|
+ resptmp["offlinebalance"] = offlinebalance
|
|
|
+ resptmp["offlinebalancetotal"] = offlinebalancetotal
|
|
|
+ if jbuf, err = json.Marshal(resptmp); err != nil {
|
|
|
+ LOG("ERROR", " inner json error:%v", err.Error())
|
|
|
+ return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
|
|
|
+ }
|
|
|
+ return jbuf, nil
|
|
|
+}
|
|
|
+
|
|
|
+func AsyncRoutine() {
|
|
|
+ CachelistunspentTicker := time.NewTicker(120 * time.Second)
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case _ = <-CachelistunspentTicker.C:
|
|
|
+ Cachelistunspent()
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+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_mutUnspent.Lock()
|
|
|
+ m_unspent = unspent
|
|
|
+ m_mutUnspent.Unlock()
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
func init() {
|
|
|
Register(NameCoin, &Bucash{})
|