Advertisement
jkbgm

Untitled

May 3rd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1.  
  2. func (restClient *RestClient) deleteWallet() gin.HandlerFunc {
  3. return func(c *gin.Context) {
  4. token, err := getToken(c)
  5. if err != nil {
  6. c.JSON(http.StatusBadRequest, gin.H{
  7. "code": http.StatusBadRequest,
  8. "message": msgErrHeaderError,
  9. })
  10. return
  11. }
  12.  
  13. walletIndex, err := strconv.Atoi(c.Param("walletindex"))
  14. restClient.log.Debugf("getWalletVerbose [%d] \t[walletindexr=%s]", walletIndex, c.Request.RemoteAddr)
  15. if err != nil {
  16. restClient.log.Errorf("getWalletVerbose: non int wallet index:[%d] %s \t[addr=%s]", walletIndex, err.Error(), c.Request.RemoteAddr)
  17. c.JSON(http.StatusBadRequest, gin.H{
  18. "code": http.StatusBadRequest,
  19. "message": msgErrDecodeWalletIndexErr,
  20. })
  21. return
  22. }
  23.  
  24. currencyId, err := strconv.Atoi(c.Param("currencyid"))
  25. restClient.log.Debugf("getWalletVerbose [%d] \t[currencyId=%s]", walletIndex, c.Request.RemoteAddr)
  26. if err != nil {
  27. restClient.log.Errorf("getWalletVerbose: non int currency id:[%d] %s \t[addr=%s]", currencyId, err.Error(), c.Request.RemoteAddr)
  28. c.JSON(http.StatusBadRequest, gin.H{
  29. "code": http.StatusBadRequest,
  30. "message": msgErrDecodeCurIndexErr,
  31. })
  32. return
  33. }
  34.  
  35. networkid, err := strconv.Atoi(c.Param("networkid"))
  36. restClient.log.Debugf("getWalletVerbose [%d] \t[networkid=%s]", walletIndex, c.Request.RemoteAddr)
  37. if err != nil {
  38. restClient.log.Errorf("getWalletVerbose: non int networkid index:[%d] %s \t[addr=%s]", networkid, err.Error(), c.Request.RemoteAddr)
  39. c.JSON(http.StatusBadRequest, gin.H{
  40. "code": http.StatusBadRequest,
  41. "message": msgErrDecodenetworkidErr,
  42. })
  43. return
  44. }
  45.  
  46. var (
  47. code int
  48. message string
  49. )
  50.  
  51. user := store.User{}
  52. query := bson.M{"devices.JWT": token}
  53. if err := restClient.userStore.FindUser(query, &user); err != nil {
  54. restClient.log.Errorf("deleteWallet: restClient.userStore.FindUser: %s\t[addr=%s]", err.Error(), c.Request.RemoteAddr)
  55. c.JSON(http.StatusBadRequest, gin.H{
  56. "code": http.StatusBadRequest,
  57. "message": msgErrUserNotFound,
  58. })
  59. return
  60. }
  61. code = http.StatusOK
  62. message = http.StatusText(http.StatusOK)
  63.  
  64. var totalBalance int64
  65.  
  66. switch currencyId {
  67. case currencies.Bitcoin:
  68.  
  69. if networkid == currencies.Main {
  70. for _, wallet := range user.Wallets {
  71. if wallet.WalletIndex == walletIndex {
  72. for _, address := range wallet.Adresses {
  73. totalBalance += checkBTCAddressbalance(address.Address, currencyId, networkid, restClient)
  74. }
  75. }
  76. }
  77. }
  78. if networkid == currencies.Test {
  79. for _, wallet := range user.Wallets {
  80. if wallet.WalletIndex == walletIndex {
  81. for _, address := range wallet.Adresses {
  82. totalBalance += checkBTCAddressbalance(address.Address, currencyId, networkid, restClient)
  83. }
  84. }
  85. }
  86. }
  87.  
  88. if totalBalance == 0 {
  89. err := restClient.userStore.DeleteWallet(user.UserID, walletIndex, currencyId, networkid)
  90. if err != nil {
  91. restClient.log.Errorf("deleteWallet: restClient.userStore.Update: %s\t[addr=%s]", err.Error(), c.Request.RemoteAddr)
  92. c.JSON(http.StatusBadRequest, gin.H{
  93. "code": http.StatusInternalServerError,
  94. "message": msgErrNoWallet,
  95. })
  96. return
  97. }
  98. }
  99.  
  100. if totalBalance != 0 {
  101. c.JSON(http.StatusBadRequest, gin.H{
  102. "code": http.StatusBadRequest,
  103. "message": msgErrWalletNonZeroBalance,
  104. })
  105. return
  106. }
  107.  
  108. code = http.StatusOK
  109. message = http.StatusText(http.StatusOK)
  110.  
  111. case currencies.Ether:
  112.  
  113. var address string
  114. for _, wallet := range user.Wallets {
  115. if wallet.WalletIndex == walletIndex {
  116. if len(wallet.Adresses) > 0 {
  117. address = wallet.Adresses[0].Address
  118. }
  119. }
  120. }
  121.  
  122. balance := &ethpb.Balance{}
  123. if networkid == currencies.ETHMain {
  124. balance, err = restClient.ETH.CliMain.EventGetAdressBalance(context.Background(), &ethpb.AddressToResync{
  125. Address: address,
  126. })
  127. }
  128. if networkid == currencies.ETHTest {
  129. restClient.ETH.CliTest.EventGetAdressBalance(context.Background(), &ethpb.AddressToResync{
  130. Address: address,
  131. })
  132. }
  133.  
  134. if balance.Balance == "0" {
  135. err := restClient.userStore.DeleteWallet(user.UserID, walletIndex, currencyId, networkid)
  136. if err != nil {
  137. restClient.log.Errorf("deleteWallet: restClient.userStore.Update: %s\t[addr=%s]", err.Error(), c.Request.RemoteAddr)
  138. c.JSON(http.StatusBadRequest, gin.H{
  139. "code": http.StatusInternalServerError,
  140. "message": msgErrNoWallet,
  141. })
  142. return
  143. }
  144. }
  145.  
  146. if balance.Balance != "0" {
  147. c.JSON(http.StatusBadRequest, gin.H{
  148. "code": http.StatusBadRequest,
  149. "message": msgErrWalletNonZeroBalance,
  150. })
  151. return
  152. }
  153.  
  154. code = http.StatusOK
  155. message = http.StatusText(http.StatusOK)
  156. default:
  157. c.JSON(http.StatusBadRequest, gin.H{
  158. "code": http.StatusBadRequest,
  159. "message": msgErrChainIsNotImplemented,
  160. })
  161. return
  162. }
  163.  
  164. if totalBalance != 0 {
  165. c.JSON(http.StatusBadRequest, gin.H{
  166. "code": http.StatusBadRequest,
  167. "message": msgErrWalletNonZeroBalance,
  168. })
  169. return
  170. }
  171. c.JSON(code, gin.H{
  172. "code": code,
  173. "message": message,
  174. })
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement