dcrcashcoin.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. package dcrcash
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. . "template"
  7. "time"
  8. simplejson "github.com/go-simplejson"
  9. tml "github.com/toml-master"
  10. )
  11. var (
  12. Config config
  13. CoinApi DcrCashApi
  14. WalleTxfee float64
  15. )
  16. type DcrCash struct {
  17. walletstatus bool
  18. }
  19. const (
  20. NameCoin = "DCR"
  21. SendToError = "-32603"
  22. DefaultMaxsumamout = 20.0
  23. DEFAULTACCOUNT = "default"
  24. )
  25. func (m DcrCash) InitDriver(param interface{}) bool {
  26. if m.initconfig() == false {
  27. return false
  28. }
  29. go m.getwalletinfo()
  30. return true
  31. }
  32. func (m DcrCash) initconfig() bool {
  33. if _, err := tml.DecodeFile("./src/dcrcash/dcrcash.toml", &Config); err != nil {
  34. fmt.Println(err)
  35. return false
  36. }
  37. fmt.Printf("%+v\n", Config)
  38. User := Config.Rpc.Rpcuser
  39. Password := Config.Rpc.Rpcpassword
  40. Host := Config.Rpc.Rpchost
  41. Port := Config.Rpc.Rpcport
  42. //Walletpass = Config.WalletInfo.Pripassphrase
  43. CoinApi.InitRpcConf(User, Password, Host, Port)
  44. initDcrError()
  45. f, err := strconv.ParseFloat(Config.Limit.Fee, 64)
  46. if err != nil {
  47. LOG("ERROR", "init json error:%v", err.Error())
  48. return false
  49. }
  50. fee := float64(f)
  51. CoinApi.WalletSetTxFee(fee) //
  52. return true
  53. }
  54. func (m DcrCash) getwalletinfo() bool {
  55. fmt.Println("Walletinfo:")
  56. m.walletstatus = false
  57. for {
  58. if m.walletstatus == false {
  59. resp, err := CoinApi.WalletInfo()
  60. if err != nil {
  61. LOG("ERROR", "WalletInfo error:%v", err.Error())
  62. time.Sleep(time.Second * 1)
  63. continue
  64. }
  65. LOG("DEBUG", "walletInfo resp:%v", string(resp))
  66. m.walletstatus = true
  67. }
  68. time.Sleep(time.Second)
  69. }
  70. }
  71. func (m DcrCash) CoinBalance(parm interface{}) ([]byte, error) {
  72. confirmedBalance, err := CoinApi.WalleGetBalance("*")
  73. if err != nil {
  74. LOG("ERROR", " GetBalance err:%v", err.Error())
  75. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBALANCE_ERR, getErrMsg(GETBALANCE_ERR, nil))
  76. }
  77. var balance []account
  78. if err = json.Unmarshal(confirmedBalance, &balance); err != nil {
  79. LOG("ERROR", " inner json error:%v", err.Error())
  80. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  81. }
  82. //var totalConfirbalance float64
  83. //var totalUnconfirbalance float64
  84. var maxConfirbalance float64
  85. var maxUnconfirbalance float64
  86. for _, vbalance := range balance {
  87. /*if vbalance.Spendable != 0 {
  88. totalConfirbalance += vbalance.Spendable
  89. }
  90. if vbalance.Unconfirmed != 0 {
  91. totalUnconfirbalance += vbalance.Unconfirmed
  92. }*/
  93. if vbalance.Spendable > maxConfirbalance {
  94. maxConfirbalance = vbalance.Spendable
  95. }
  96. if vbalance.Unconfirmed > maxUnconfirbalance {
  97. maxUnconfirbalance = vbalance.Unconfirmed
  98. }
  99. }
  100. var jbuf []byte
  101. resp := make(map[string]interface{})
  102. resp["confirmedbalance"] = maxConfirbalance
  103. resp["unconfirmed"] = maxUnconfirbalance
  104. resp["errcode"] = 0
  105. resp["exact"] = 0
  106. if jbuf, err = json.Marshal(resp); err != nil {
  107. LOG("ERROR", "lite inner json error:%v", err.Error())
  108. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  109. }
  110. return jbuf, nil
  111. }
  112. func (m DcrCash) CreateAddress(parm interface{}) ([]byte, error) {
  113. addr, err := CoinApi.WalletNewAddr()
  114. if err != nil {
  115. LOG("ERROR", " get new address error:%v", err.Error())
  116. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETNEWADDRESS_ERR, getErrMsg(GETNEWADDRESS_ERR, nil))
  117. }
  118. if len(addr) != 0 {
  119. //get key by the address
  120. key, err := CoinApi.WalletDumpPrivKey(addr)
  121. if err != nil {
  122. LOG("ERROR", " dump private key error:%v", err.Error())
  123. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, DUMPPRIVKEY_ERR, getErrMsg(DUMPPRIVKEY_ERR, nil))
  124. }
  125. if len(key) != 0 {
  126. var jbuf []byte
  127. resp := make(map[string]interface{})
  128. resp["address"] = addr
  129. resp["privkey"] = key
  130. resp["errcode"] = 0
  131. if jbuf, err = json.Marshal(resp); err != nil {
  132. LOG("ERROR", " inner json error:%v", err.Error())
  133. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  134. }
  135. return jbuf, nil
  136. }
  137. LOG("ERROR", " privkey is null error:%v")
  138. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  139. }
  140. LOG("ERROR", " address is null error:%v")
  141. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  142. }
  143. func (m DcrCash) SumcoinBalance(parm interface{}) ([]byte, error) {
  144. pjs := parm.(*simplejson.Json)
  145. //
  146. Sumamount := pjs.Get("sumamount").MustFloat64()
  147. Minsumamount := pjs.Get("minamount").MustFloat64()
  148. if Sumamount <= 0.0 {
  149. LOG("ERROR", " Summary amount is nil!")
  150. Sumamount = DefaultMaxsumamout
  151. }
  152. if Minsumamount < WalleTxfee {
  153. Minsumamount = WalleTxfee
  154. }
  155. //list Accounts and account of balance
  156. accountsofValues, err := CoinApi.ListAccounts()
  157. if err != nil {
  158. LOG("ERROR", "List accounts error:%v", err.Error())
  159. return nil, err
  160. }
  161. //var AccValues []AccountsOfvalue
  162. AccValues := map[string]float64{}
  163. if err = json.Unmarshal(accountsofValues, &AccValues); err != nil {
  164. LOG("ERROR", "Unmarshal error:%v", err.Error())
  165. return nil, err
  166. }
  167. var Totalbalance float64
  168. transactionsids := make([]string, 0)
  169. for account, value := range AccValues {
  170. if (value > Minsumamount) && account != DEFAULTACCOUNT {
  171. amount := value - WalleTxfee
  172. transactionid, err := CoinApi.WalletSendFrom(account, DEFAULTACCOUNT, amount, 1, "", "")
  173. if err != nil {
  174. LOG("ERROR", " Send coin error:%v", err.Error())
  175. //return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, SENDCOINS_ERR, getErrMsg(SENDCOINS_ERR, nil))
  176. continue
  177. }
  178. transactionsids = append(transactionsids, transactionid)
  179. Totalbalance += amount
  180. if Totalbalance >= Sumamount {
  181. break
  182. }
  183. }
  184. }
  185. if len(transactionsids) != 0 {
  186. var jbuf []byte
  187. resp := make(map[string]interface{})
  188. resp["transcationids"] = transactionsids
  189. resp["errcode"] = 0
  190. resp["msg"] = "ok"
  191. if jbuf, err = json.Marshal(resp); err != nil {
  192. LOG("ERROR", " inner json error:%v", err.Error())
  193. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  194. }
  195. return jbuf, nil
  196. }
  197. LOG("ERROR", " transactionids is null error:%v", err.Error())
  198. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  199. //fromaccout = "default"
  200. }
  201. func (m DcrCash) TransferAccounts(parm interface{}) ([]byte, error) {
  202. pjs := parm.(*simplejson.Json)
  203. amountTmp := pjs.Get("amount").MustString()
  204. destination := pjs.Get("address").MustString()
  205. if len(amountTmp) == 0 {
  206. LOG("ERROR", " amount illeagal")
  207. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, AMOUNT_ERR, getErrMsg(AMOUNT_ERR, nil))
  208. }
  209. f, err := strconv.ParseFloat(amountTmp, 64)
  210. if err != nil {
  211. LOG("ERROR", " inner json error:%v", err.Error())
  212. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  213. }
  214. amount := float64(f)
  215. //list Accounts and account of balance
  216. accountsofValues, err := CoinApi.ListAccounts()
  217. if err != nil {
  218. LOG("ERROR", "List accounts error:%v", err.Error())
  219. return nil, err
  220. }
  221. AccValues := map[string]float64{}
  222. if err = json.Unmarshal(accountsofValues, &AccValues); err != nil {
  223. LOG("ERROR", "Unmarshal error:%v", err.Error())
  224. return nil, err
  225. }
  226. fromaccout := ""
  227. for account, value := range AccValues {
  228. if value >= (amount + WalleTxfee) {
  229. fromaccout = account
  230. break
  231. }
  232. }
  233. transactionid, err := CoinApi.WalletSendFrom(fromaccout, destination, amount, 1, "", "")
  234. if err != nil {
  235. LOG("ERROR", " Send coin error:%v", err.Error())
  236. fmt.Println(transactionid)
  237. if transactionid == SendToError {
  238. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INSUFFICIENT_FUNDS_TRANSACTION_ERR, getErrMsg(INSUFFICIENT_FUNDS_TRANSACTION_ERR, nil))
  239. }
  240. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, SENDCOINS_ERR, getErrMsg(SENDCOINS_ERR, nil))
  241. }
  242. if len(transactionid) != 0 {
  243. var jbuf []byte
  244. resp := make(map[string]interface{})
  245. resp["transcationids"] = []string{transactionid}
  246. resp["errcode"] = 0
  247. resp["msg"] = "ok"
  248. if jbuf, err = json.Marshal(resp); err != nil {
  249. LOG("ERROR", " inner json error:%v", err.Error())
  250. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  251. }
  252. return jbuf, nil
  253. }
  254. LOG("ERROR", " transactionid is null error:%v", err.Error())
  255. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  256. }
  257. func (m DcrCash) TransInfo(parm interface{}) ([]byte, error) {
  258. pjs := parm.(*simplejson.Json)
  259. startH := pjs.Get("starth").MustUint64()
  260. endH := pjs.Get("endh").MustUint64()
  261. if startH < 1 {
  262. LOG("DEBUG", " input para err:%v", startH)
  263. startH = 1
  264. }
  265. //get longest block count
  266. blockcount, err := CoinApi.WalleGetBlockCount()
  267. if err != nil {
  268. LOG("ERROR", " WalleGetBlockCount err:%v", err.Error())
  269. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETCLOCKCOUNT_ERR, getErrMsg(GETCLOCKCOUNT_ERR, nil))
  270. }
  271. //get startblock hash by height
  272. if startH > blockcount {
  273. LOG("DEBUG", " startheight:%v > blockcount:%v", startH, blockcount)
  274. startH = blockcount
  275. }
  276. startblockhash, err := CoinApi.WalletGetBlockHash(uint64(startH - 1))
  277. if err != nil {
  278. LOG("ERROR", " WalletGetBlockHash startH:%v err:%v", startH, err.Error())
  279. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil))
  280. }
  281. //get endblock time by height
  282. if endH > blockcount {
  283. LOG("DEBUG", " endheight:%v > blockcount:%v", endH, blockcount)
  284. endH = blockcount
  285. }
  286. endblockhash, err := CoinApi.WalletGetBlockHash(uint64(endH))
  287. if err != nil {
  288. LOG("ERROR", " WalletGetBlockHash startH:%v err:%v", startH, err.Error())
  289. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil))
  290. }
  291. endblocktime, err := CoinApi.WalletGetBlockTime(endblockhash)
  292. if err != nil {
  293. LOG("ERROR", " WalletGetBlockTime endH:%v err:%v", endH, err.Error())
  294. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETBLOCKINFO_ERR, getErrMsg(GETBLOCKINFO_ERR, nil))
  295. }
  296. //Get all transactions in blocks since block [blockhash]
  297. resp, err := CoinApi.WalleListSinceBlock(startblockhash)
  298. if err != nil {
  299. LOG("ERROR", " WalleListSinceBlock err:%v", err.Error())
  300. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, TRANSINFO_ERR, getErrMsg(TRANSINFO_ERR, nil))
  301. }
  302. var litelisttransactionsinfo []Listtransactions
  303. if err = json.Unmarshal([]byte(resp), &litelisttransactionsinfo); err != nil {
  304. LOG("ERROR", " inner json error:%v", err.Error())
  305. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  306. }
  307. outputsArr := make([]map[string]interface{}, 0)
  308. fmt.Println("WalleListSinceBlock_num:", len(litelisttransactionsinfo))
  309. for index, listtransaction := range litelisttransactionsinfo {
  310. // get all transactions from block startHright to endHeight
  311. fmt.Println("BLockTime,Endblocktime", listtransaction.Blocktime, endblocktime, index)
  312. if listtransaction.Blocktime <= endblocktime {
  313. requestData := make(map[string]interface{})
  314. confirmationheight, err := CoinApi.WalletGetBlockHeight(listtransaction.Blockhash)
  315. if err != nil {
  316. LOG("ERROR", "WalletGetBlockHeight error:%v", err.Error())
  317. continue
  318. }
  319. requestData["confirmationheight"] = confirmationheight
  320. requestData["address"] = listtransaction.Address
  321. requestData["confirmationtimestamp"] = listtransaction.Timereceived
  322. requestData["value"] = listtransaction.Amount
  323. requestData["transactionid"] = listtransaction.Txid
  324. requestData["confirmations"] = listtransaction.Confirmations
  325. requestData["walletaddress"] = true
  326. requestData["vout"] = listtransaction.Vout
  327. outputsArr = append(outputsArr, requestData)
  328. }
  329. }
  330. var jbuf []byte
  331. resptmp := make(map[string]interface{})
  332. resptmp["errcode"] = 0
  333. resptmp["cointype"] = "DCR"
  334. resptmp["outputs"] = outputsArr
  335. if jbuf, err = json.Marshal(resptmp); err != nil {
  336. LOG("ERROR", " inner json error:%v", err.Error())
  337. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  338. }
  339. return jbuf, nil
  340. }
  341. func (m DcrCash) ListUnspent(parm interface{}) ([]byte, error) {
  342. pjs := parm.(*simplejson.Json)
  343. address, err := pjs.Get("address").StringArray()
  344. if err != nil {
  345. LOG("ERROR", " input parm error:%v", err.Error())
  346. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
  347. }
  348. unspent, err := CoinApi.Listunspent(address)
  349. if err != nil {
  350. LOG("ERROR", " get listunspent:%v", err.Error())
  351. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, LISTUNSPENT_ERR, getErrMsg(LISTUNSPENT_ERR, nil))
  352. }
  353. var jbuf []byte
  354. resptmp := make(map[string]interface{})
  355. resptmp["errcode"] = 0
  356. resptmp["cointype"] = "DCR"
  357. resptmp["unspents"] = unspent
  358. if jbuf, err = json.Marshal(resptmp); err != nil {
  359. LOG("ERROR", " inner json error:%v", err.Error())
  360. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  361. }
  362. return jbuf, nil
  363. }
  364. func (m DcrCash) SendRawTransaction(parm interface{}) ([]byte, error) {
  365. pjs := parm.(*simplejson.Json)
  366. transdata := pjs.Get("data").MustString()
  367. if len(transdata) == 0 {
  368. return nil, fmt.Errorf(`{"cointype":"DCR","errcode":%v,"msg":"%v"}`, PARM_ERR, getErrMsg(PARM_ERR, []byte("hexstring is null")))
  369. }
  370. transactionhex, err := CoinApi.SendRawTransaction(transdata)
  371. if err != nil {
  372. LOG("ERROR", " SendRawTransaction err:%s", err.Error())
  373. return nil, fmt.Errorf(`{"cointype":"DCR","errcode":%v,"msg":"%s"}`, SEND_RAW_TRANSACTION_ERR, getErrMsg(SEND_RAW_TRANSACTION_ERR, nil))
  374. }
  375. var jbuf []byte
  376. resp := make(map[string]interface{})
  377. resp["cointype"] = "DCR"
  378. resp["transactionids"] = transactionhex
  379. resp["errcode"] = 0
  380. resp["msg"] = "OK"
  381. if jbuf, err = json.Marshal(resp); err != nil {
  382. LOG("ERROR", "inner json error:%v", err.Error())
  383. return nil, fmt.Errorf(`{"cointype":"DCR","errcode":%v,"msg":"%s"}`, INNER_ERR, getErrMsg(INNER_ERR, nil))
  384. }
  385. return jbuf, nil
  386. }
  387. func (m DcrCash) ChainHeight(parm interface{}) ([]byte, error) {
  388. blockcount, err := CoinApi.WalleGetBlockCount()
  389. if err != nil {
  390. LOG("ERROR", " get current block count error:%v", err.Error())
  391. return nil, fmt.Errorf(`{"errcode":%v,"mesage":"%s"}`, GETCLOCKCOUNT_ERR, getErrMsg(GETCLOCKCOUNT_ERR, nil))
  392. }
  393. sendstr := fmt.Sprintf(`{"errcode":%v,"height":%v}`, OK, blockcount)
  394. return []byte(sendstr), nil
  395. }
  396. func init() {
  397. Register(NameCoin, &DcrCash{})
  398. }