package dcrcash import ( "encoding/json" "fmt" "strconv" "sync" . "template" "time" simplejson "github.com/go-simplejson" tml "github.com/toml-master" ) var ( 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 { walletstatus bool } const ( NameCoin = "DCR" SendToError = "-32603" DefaultMaxsumamout = 20.0 DEFAULTACCOUNT = "default" ) func (m DcrCash) InitDriver(param interface{}) bool { if m.initconfig() == false { return false } go m.getwalletinfo() //缓存数据 go AsyncRoutine() return true } func (m DcrCash) initconfig() bool { if _, err := tml.DecodeFile("./src/dcrcash/dcrcash.toml", &Config); err != nil { fmt.Println(err) return false } fmt.Printf("%+v\n", Config) User := Config.Rpc.Rpcuser Password := Config.Rpc.Rpcpassword Host := Config.Rpc.Rpchost Port := Config.Rpc.Rpcport //Walletpass = Config.WalletInfo.Pripassphrase CoinApi.InitRpcConf(User, Password, Host, Port) initDcrError() f, err := strconv.ParseFloat(Config.Limit.Fee, 64) if err != nil { LOG("ERROR", "init json error:%v", err.Error()) return false } fee := float64(f) CoinApi.WalletSetTxFee(fee) // return true } func (m DcrCash) getwalletinfo() bool { fmt.Println("Walletinfo:") m.walletstatus = false for { if m.walletstatus == false { resp, err := CoinApi.WalletInfo() if err != nil { LOG("ERROR", "WalletInfo error:%v", err.Error()) time.Sleep(time.Second * 1) continue } LOG("DEBUG", "walletInfo resp:%v", string(resp)) m.walletstatus = true } time.Sleep(time.Second) } } func (m DcrCash) CoinBalance(parm interface{}) ([]byte, error) { var totalbalance, maxConfirbalance, maxUnconfirbalance, querytime float64 pjs := parm.(*simplejson.Json) input, err := pjs.Get("Sync").Bool() if !input { fmt.Println("the balance is not sync!!") totalbalance = m_balanceparam["totalbalance"] maxConfirbalance = m_balanceparam["confirmedbalance"] maxUnconfirbalance = m_balanceparam["unconfirmed"] querytime = m_balanceparam["ntime"] } 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)) } 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 resp["querybalancetime"] = querytime resp["errcode"] = 0 resp["exact"] = 0 if jbuf, err = json.Marshal(resp); err != nil { LOG("ERROR", "lite inner json error:%v", err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil)) } return jbuf, nil } func (m DcrCash) CreateAddress(parm interface{}) ([]byte, error) { addr, err := CoinApi.WalletNewAddr() if err != nil { LOG("ERROR", " get new address error:%v", err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETNEWADDRESS_ERR, getErrMsg(GETNEWADDRESS_ERR, nil)) } if len(addr) != 0 { //get key by the address key, err := CoinApi.WalletDumpPrivKey(addr) if err != nil { LOG("ERROR", " dump private key error:%v", err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, DUMPPRIVKEY_ERR, getErrMsg(DUMPPRIVKEY_ERR, nil)) } if len(key) != 0 { var jbuf []byte resp := make(map[string]interface{}) resp["address"] = addr resp["privkey"] = key resp["errcode"] = 0 if jbuf, err = json.Marshal(resp); 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 } LOG("ERROR", " privkey is null error:%v") return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil)) } LOG("ERROR", " address is null error:%v") return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil)) } func (m *DcrCash) SumcoinBalance(parm interface{}) ([]byte, error) { pjs := parm.(*simplejson.Json) // Sumamount := pjs.Get("sumamount").MustFloat64() Minsumamount := pjs.Get("minamount").MustFloat64() if Sumamount <= 0 { LOG("ERROR", " Summary amount is nil!") Sumamount = DefaultMaxsumamout } if Minsumamount < WalleTxfee { Minsumamount = WalleTxfee } //list Accounts and account of balance accountsofValues, err := CoinApi.ListAccounts() if err != nil { LOG("ERROR", "List accounts error:%v", err.Error()) return nil, err } //var AccValues []AccountsOfvalue AccValues := map[string]float64{} if err = json.Unmarshal(accountsofValues, &AccValues); err != nil { LOG("ERROR", "Unmarshal error:%v", err.Error()) return nil, err } var Totalbalance float64 fmt.Println("SumcoinBalance >>>>:", len(AccValues), "Sumamount:", Sumamount, "Minsumamount", Minsumamount) transactionsids := make([]string, 0) for account, value := range AccValues { if (value > Minsumamount) && account != DEFAULTACCOUNT { fmt.Println("SumcoinBalance account:", account, "value:", value) amount := value - WalleTxfee transactionid, err := CoinApi.WalletSendFrom(account, DEFAULTACCOUNT, amount, 1, "", "") if err != nil { LOG("ERROR", " Send coin error:%v", err.Error()) //return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, SENDCOINS_ERR, getErrMsg(SENDCOINS_ERR, nil)) continue } transactionsids = append(transactionsids, transactionid) Totalbalance += amount if Totalbalance >= Sumamount { break } } } if len(transactionsids) != 0 { var jbuf []byte resp := make(map[string]interface{}) resp["transcationids"] = transactionsids resp["errcode"] = 0 resp["msg"] = "ok" if jbuf, err = json.Marshal(resp); 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 } return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, SUMCOIN_ERR, getErrMsg(SUMCOIN_ERR, nil)) //fromaccout = "default" } func (m DcrCash) TransferAccounts(parm interface{}) ([]byte, error) { pjs := parm.(*simplejson.Json) amountTmp := pjs.Get("amount").MustString() destination := pjs.Get("address").MustString() if len(amountTmp) == 0 { LOG("ERROR", " amount illeagal") return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, AMOUNT_ERR, getErrMsg(AMOUNT_ERR, nil)) } f, err := strconv.ParseFloat(amountTmp, 64) if err != nil { LOG("ERROR", " inner json error:%v", err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil)) } amount := float64(f) fromaccout := "" for account, value := range m_AccValues { if value >= (amount + WalleTxfee) { fromaccout = account break } } if fromaccout == "" { fromaccout = DEFAULTACCOUNT } transactionid, err := CoinApi.WalletSendFrom(fromaccout, destination, amount, 1, "", "") if err != nil { LOG("ERROR", " Send coin error:%v", err.Error()) fmt.Println(transactionid) if transactionid == SendToError { return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INSUFFICIENT_FUNDS_TRANSACTION_ERR, getErrMsg(INSUFFICIENT_FUNDS_TRANSACTION_ERR, nil)) } return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, SENDCOINS_ERR, getErrMsg(SENDCOINS_ERR, nil)) } go func() { Cachelistaccount() }() if len(transactionid) != 0 { var jbuf []byte resp := make(map[string]interface{}) resp["transcationids"] = []string{transactionid} resp["errcode"] = 0 resp["msg"] = "ok" if jbuf, err = json.Marshal(resp); 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 } LOG("ERROR", " transactionid is null error:%v", err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil)) } func (m DcrCash) TransInfo(parm interface{}) ([]byte, error) { pjs := parm.(*simplejson.Json) startH := pjs.Get("starth").MustUint64() endH := pjs.Get("endh").MustUint64() if startH < 1 { LOG("DEBUG", " input para err:%v", startH) startH = 1 } //get longest block count blockcount, err := CoinApi.WalleGetBlockCount() if err != nil { LOG("ERROR", " WalleGetBlockCount err:%v", err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETCLOCKCOUNT_ERR, getErrMsg(GETCLOCKCOUNT_ERR, nil)) } //get startblock hash by height if startH > blockcount { LOG("DEBUG", " startheight:%v > blockcount:%v", startH, blockcount) startH = blockcount } startblockhash, err := CoinApi.WalletGetBlockHash(uint64(startH - 1)) if err != nil { LOG("ERROR", " WalletGetBlockHash startH:%v err:%v", startH, err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil)) } //get endblock time by height if endH > blockcount { LOG("DEBUG", " endheight:%v > blockcount:%v", endH, blockcount) endH = blockcount } endblockhash, err := CoinApi.WalletGetBlockHash(uint64(endH)) if err != nil { LOG("ERROR", " WalletGetBlockHash startH:%v err:%v", startH, err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil)) } endblocktime, err := CoinApi.WalletGetBlockTime(endblockhash) if err != nil { LOG("ERROR", " WalletGetBlockTime endH:%v err:%v", endH, err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil)) } //Get all transactions in blocks since block [blockhash] resp, err := CoinApi.WalleListSinceBlock(startblockhash) if err != nil { LOG("ERROR", " WalleListSinceBlock err:%v", err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, TRANSINFO_ERR, getErrMsg(TRANSINFO_ERR, nil)) } var litelisttransactionsinfo []Listtransactions if err = json.Unmarshal([]byte(resp), &litelisttransactionsinfo); err != nil { LOG("ERROR", " inner json error:%v", err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil)) } outputsArr := make([]map[string]interface{}, 0) fmt.Println("WalleListSinceBlock_num:", len(litelisttransactionsinfo)) for index, listtransaction := range litelisttransactionsinfo { // get all transactions from block startHright to endHeight fmt.Println("BLockTime,Endblocktime", listtransaction.Blocktime, endblocktime, index) if listtransaction.Blocktime <= endblocktime { requestData := make(map[string]interface{}) confirmationheight, err := CoinApi.WalletGetBlockHeight(listtransaction.Blockhash) if err != nil { LOG("ERROR", "WalletGetBlockHeight error:%v", err.Error()) continue } requestData["confirmationheight"] = confirmationheight requestData["address"] = listtransaction.Address requestData["confirmationtimestamp"] = listtransaction.Timereceived requestData["value"] = listtransaction.Amount requestData["transactionid"] = listtransaction.Txid requestData["confirmations"] = listtransaction.Confirmations requestData["walletaddress"] = true requestData["vout"] = listtransaction.Vout outputsArr = append(outputsArr, requestData) } } var jbuf []byte resptmp := make(map[string]interface{}) resptmp["errcode"] = 0 resptmp["cointype"] = "DCR" resptmp["outputs"] = outputsArr 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 (m DcrCash) ListUnspent(parm interface{}) ([]byte, error) { i_listunspent := make([]map[string]interface{}, 0) pjs := parm.(*simplejson.Json) 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"] = i_listunspent 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 (m DcrCash) SendRawTransaction(parm interface{}) ([]byte, error) { pjs := parm.(*simplejson.Json) transdata := pjs.Get("data").MustString() if len(transdata) == 0 { return nil, fmt.Errorf(`{"cointype":"DCR","errcode":%v,"msg":"%v"}`, PARM_ERR, getErrMsg(PARM_ERR, []byte("hexstring is null"))) } transactionhex, err := CoinApi.SendRawTransaction(transdata) if err != nil { LOG("ERROR", " SendRawTransaction err:%s", err.Error()) return nil, fmt.Errorf(`{"cointype":"DCR","errcode":%v,"msg":"%s"}`, SEND_RAW_TRANSACTION_ERR, getErrMsg(SEND_RAW_TRANSACTION_ERR, nil)) } var jbuf []byte resp := make(map[string]interface{}) resp["cointype"] = "DCR" resp["transactionids"] = transactionhex resp["errcode"] = 0 resp["msg"] = "OK" if jbuf, err = json.Marshal(resp); err != nil { LOG("ERROR", "inner json error:%v", err.Error()) return nil, fmt.Errorf(`{"cointype":"DCR","errcode":%v,"msg":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil)) } return jbuf, nil } func (m DcrCash) ChainHeight(parm interface{}) ([]byte, error) { blockcount, err := CoinApi.WalleGetBlockCount() if err != nil { LOG("ERROR", " get current block count error:%v", err.Error()) return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETCLOCKCOUNT_ERR, getErrMsg(GETCLOCKCOUNT_ERR, nil)) } sendstr := fmt.Sprintf(`{"errcode":%v,"height":%v}`, OK, blockcount) return []byte(sendstr), nil } func (m DcrCash) 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"] = "DCR" 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 CacheGetbalance() { fmt.Println("Cachegetbalance time:", time.Now().Format("2006-01-02 15:04:05")) m_balanceparam = make(map[string]float64) 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 } } 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() } 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 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() } } } func init() { Register(NameCoin, &DcrCash{}) }