|
@@ -13,11 +13,13 @@ import (
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- Config config
|
|
|
- CoinApi DcrCashApi
|
|
|
- WalleTxfee float64
|
|
|
- balanceparam map[string]float64
|
|
|
- mut sync.Mutex
|
|
|
+ Config config
|
|
|
+ CoinApi DcrCashApi
|
|
|
+ WalleTxfee float64
|
|
|
+ m_balanceparam map[string]float64
|
|
|
+ mut sync.Mutex
|
|
|
+ m_unspent []map[string]interface{}
|
|
|
+ m_AccValues map[string]float64
|
|
|
)
|
|
|
|
|
|
type DcrCash struct {
|
|
@@ -36,6 +38,10 @@ func (m DcrCash) InitDriver(param interface{}) bool {
|
|
|
return false
|
|
|
}
|
|
|
go m.getwalletinfo()
|
|
|
+ //列出当期钱包的所有账户
|
|
|
+ go listaccounts()
|
|
|
+ //列出所有账户的balance,和listunspent
|
|
|
+ go SaveGetbalance()
|
|
|
return true
|
|
|
}
|
|
|
|
|
@@ -93,9 +99,9 @@ func (m DcrCash) CoinBalance(parm interface{}) ([]byte, error) {
|
|
|
input, err := pjs.Get("Sync").Bool()
|
|
|
if !input {
|
|
|
fmt.Println("the balance is not sync!!")
|
|
|
- totalbalance = balanceparam["totalbalance"]
|
|
|
- maxConfirbalance = balanceparam["confirmedbalance"]
|
|
|
- maxUnconfirbalance = balanceparam["unconfirmed"]
|
|
|
+ totalbalance = m_balanceparam["totalbalance"]
|
|
|
+ maxConfirbalance = m_balanceparam["confirmedbalance"]
|
|
|
+ maxUnconfirbalance = m_balanceparam["unconfirmed"]
|
|
|
} else {
|
|
|
acountBalace, err := CoinApi.WalleGetBalance("*")
|
|
|
if err != nil {
|
|
@@ -107,7 +113,6 @@ func (m DcrCash) CoinBalance(parm interface{}) ([]byte, error) {
|
|
|
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
|
|
@@ -128,6 +133,7 @@ func (m DcrCash) CoinBalance(parm interface{}) ([]byte, error) {
|
|
|
resp["confirmedbalance"] = maxConfirbalance
|
|
|
resp["unconfirmed"] = maxUnconfirbalance
|
|
|
resp["totalbalance"] = totalbalance
|
|
|
+ resp["querybalancetime"] = m_balanceparam["ntime"]
|
|
|
resp["errcode"] = 0
|
|
|
resp["exact"] = 0
|
|
|
if jbuf, err = json.Marshal(resp); err != nil {
|
|
@@ -252,25 +258,13 @@ func (m DcrCash) TransferAccounts(parm interface{}) ([]byte, error) {
|
|
|
}
|
|
|
|
|
|
amount := float64(f)
|
|
|
- //list Accounts and account of balance
|
|
|
- accountsofValues, err := CoinApi.ListAccounts()
|
|
|
- if err != nil {
|
|
|
- LOG("ERROR", "List accounts error:%v", err.Error())
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- AccValues := map[string]float64{}
|
|
|
- if err = json.Unmarshal(accountsofValues, &AccValues); err != nil {
|
|
|
- LOG("ERROR", "Unmarshal error:%v", err.Error())
|
|
|
- return nil, err
|
|
|
- }
|
|
|
|
|
|
fromaccout := ""
|
|
|
- for account, value := range AccValues {
|
|
|
+ for account, value := range m_AccValues {
|
|
|
if value >= (amount + WalleTxfee) {
|
|
|
fromaccout = account
|
|
|
break
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
if fromaccout == "" {
|
|
|
fromaccout = DEFAULTACCOUNT
|
|
@@ -404,23 +398,31 @@ func (m DcrCash) TransInfo(parm interface{}) ([]byte, error) {
|
|
|
}
|
|
|
|
|
|
func (m DcrCash) ListUnspent(parm interface{}) ([]byte, error) {
|
|
|
+ i_listunspent := 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_listunspent = 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_listunspent = unspent
|
|
|
}
|
|
|
|
|
|
var jbuf []byte
|
|
|
resptmp := make(map[string]interface{})
|
|
|
resptmp["errcode"] = 0
|
|
|
resptmp["cointype"] = "DCR"
|
|
|
- resptmp["unspents"] = unspent
|
|
|
+ resptmp["unspents"] = i_listunspent
|
|
|
|
|
|
if jbuf, err = json.Marshal(resptmp); err != nil {
|
|
|
LOG("ERROR", " inner json error:%v", err.Error())
|
|
@@ -468,9 +470,11 @@ func (m DcrCash) ChainHeight(parm interface{}) ([]byte, error) {
|
|
|
}
|
|
|
|
|
|
func SaveGetbalance() {
|
|
|
- balanceparam = make(map[string]float64)
|
|
|
+ var address []string
|
|
|
+ m_unspent = make([]map[string]interface{}, 0)
|
|
|
+ m_balanceparam = make(map[string]float64)
|
|
|
for range time.Tick(2 * time.Minute) {
|
|
|
- //DcrCashApi.WalleGetBalance()
|
|
|
+ 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())
|
|
@@ -498,16 +502,41 @@ func SaveGetbalance() {
|
|
|
|
|
|
}
|
|
|
mut.Lock()
|
|
|
- balanceparam["totalbalance"] = totalbalance
|
|
|
- balanceparam["confirmedbalance"] = maxConfirbalance
|
|
|
- balanceparam["unconfirmed"] = maxUnconfirbalance
|
|
|
+ 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_unspent = unspent
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+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
|
|
|
+ }
|
|
|
mut.Unlock()
|
|
|
- fmt.Println("2 mine times", time.Now().Format("2006-01-02 15:04:05"))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func init() {
|
|
|
Register(NameCoin, &DcrCash{})
|
|
|
- go SaveGetbalance()
|
|
|
-
|
|
|
}
|