You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
5.6 KiB

  1. package api
  2. import (
  3. "database/sql"
  4. "errors"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. "github.com/hermeznetwork/hermez-node/db/historydb"
  8. )
  9. const (
  10. // maxLimit is the max permited items to be returned in paginated responses
  11. maxLimit uint = 2049
  12. // dfltOrder indicates how paginated endpoints are ordered if not specified
  13. dfltOrder = historydb.OrderAsc
  14. // dfltLimit indicates the limit of returned items in paginated responses if the query param limit is not provided
  15. dfltLimit uint = 20
  16. // 2^32 -1
  17. maxUint32 = 4294967295
  18. )
  19. var (
  20. // ErrNillBidderAddr is used when a nil bidderAddr is received in the getCoordinator method
  21. ErrNillBidderAddr = errors.New("biderAddr can not be nil")
  22. )
  23. func postAccountCreationAuth(c *gin.Context) {
  24. // Parse body
  25. var apiAuth accountCreationAuthAPI
  26. if err := c.ShouldBindJSON(&apiAuth); err != nil {
  27. retBadReq(err, c)
  28. return
  29. }
  30. // API to common + verify signature
  31. dbAuth, err := accountCreationAuthAPIToCommon(&apiAuth)
  32. if err != nil {
  33. retBadReq(err, c)
  34. return
  35. }
  36. // Insert to DB
  37. if err := l2.AddAccountCreationAuth(dbAuth); err != nil {
  38. retSQLErr(err, c)
  39. return
  40. }
  41. // Return OK
  42. c.Status(http.StatusOK)
  43. }
  44. func getAccountCreationAuth(c *gin.Context) {
  45. // Get hezEthereumAddress
  46. addr, err := parseParamHezEthAddr(c)
  47. if err != nil {
  48. retBadReq(err, c)
  49. return
  50. }
  51. // Fetch auth from l2DB
  52. dbAuth, err := l2.GetAccountCreationAuth(*addr)
  53. if err != nil {
  54. retSQLErr(err, c)
  55. return
  56. }
  57. apiAuth := accountCreationAuthToAPI(dbAuth)
  58. // Build succesfull response
  59. c.JSON(http.StatusOK, apiAuth)
  60. }
  61. func postPoolTx(c *gin.Context) {
  62. // Parse body
  63. var receivedTx receivedPoolTx
  64. if err := c.ShouldBindJSON(&receivedTx); err != nil {
  65. retBadReq(err, c)
  66. return
  67. }
  68. // Transform from received to insert format and validate
  69. writeTx, err := receivedTx.toDBWritePoolL2Tx()
  70. if err != nil {
  71. retBadReq(err, c)
  72. return
  73. }
  74. // Insert to DB
  75. if err := l2.AddTx(writeTx); err != nil {
  76. retSQLErr(err, c)
  77. return
  78. }
  79. // Return TxID
  80. c.JSON(http.StatusOK, writeTx.TxID.String())
  81. }
  82. func getPoolTx(c *gin.Context) {
  83. // Get TxID
  84. txID, err := parseParamTxID(c)
  85. if err != nil {
  86. retBadReq(err, c)
  87. return
  88. }
  89. // Fetch tx from l2DB
  90. dbTx, err := l2.GetTx(txID)
  91. if err != nil {
  92. retSQLErr(err, c)
  93. return
  94. }
  95. apiTx := poolL2TxReadToSend(dbTx)
  96. // Build succesfull response
  97. c.JSON(http.StatusOK, apiTx)
  98. }
  99. func getAccounts(c *gin.Context) {
  100. }
  101. func getAccount(c *gin.Context) {
  102. }
  103. func getExits(c *gin.Context) {
  104. // Get query parameters
  105. // Account filters
  106. tokenID, addr, bjj, idx, err := parseAccountFilters(c)
  107. if err != nil {
  108. retBadReq(err, c)
  109. return
  110. }
  111. // BatchNum
  112. batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
  113. if err != nil {
  114. retBadReq(err, c)
  115. return
  116. }
  117. // Pagination
  118. fromItem, order, limit, err := parsePagination(c)
  119. if err != nil {
  120. retBadReq(err, c)
  121. return
  122. }
  123. // Fetch exits from historyDB
  124. exits, pagination, err := h.GetExits(
  125. addr, bjj, tokenID, idx, batchNum, fromItem, limit, order,
  126. )
  127. if err != nil {
  128. retSQLErr(err, c)
  129. return
  130. }
  131. // Build succesfull response
  132. apiExits := historyExitsToAPI(exits)
  133. c.JSON(http.StatusOK, &exitsAPI{
  134. Exits: apiExits,
  135. Pagination: pagination,
  136. })
  137. }
  138. func getExit(c *gin.Context) {
  139. // Get batchNum and accountIndex
  140. batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
  141. if err != nil {
  142. retBadReq(err, c)
  143. return
  144. }
  145. idx, err := parseParamIdx(c)
  146. if err != nil {
  147. retBadReq(err, c)
  148. return
  149. }
  150. // Fetch tx from historyDB
  151. exit, err := h.GetExit(batchNum, idx)
  152. if err != nil {
  153. retSQLErr(err, c)
  154. return
  155. }
  156. apiExits := historyExitsToAPI([]historydb.HistoryExit{*exit})
  157. // Build succesfull response
  158. c.JSON(http.StatusOK, apiExits[0])
  159. }
  160. func getHistoryTx(c *gin.Context) {
  161. // Get TxID
  162. txID, err := parseParamTxID(c)
  163. if err != nil {
  164. retBadReq(err, c)
  165. return
  166. }
  167. // Fetch tx from historyDB
  168. tx, err := h.GetHistoryTx(txID)
  169. if err != nil {
  170. retSQLErr(err, c)
  171. return
  172. }
  173. apiTxs := historyTxsToAPI([]historydb.HistoryTx{*tx})
  174. // Build succesfull response
  175. c.JSON(http.StatusOK, apiTxs[0])
  176. }
  177. func getSlots(c *gin.Context) {
  178. }
  179. func getBids(c *gin.Context) {
  180. }
  181. func getNextForgers(c *gin.Context) {
  182. }
  183. func getState(c *gin.Context) {
  184. }
  185. func getConfig(c *gin.Context) {
  186. c.JSON(http.StatusOK, cg)
  187. }
  188. func getRecommendedFee(c *gin.Context) {
  189. }
  190. func getCoordinators(c *gin.Context) {
  191. // Pagination
  192. fromItem, order, limit, err := parsePagination(c)
  193. if err != nil {
  194. retBadReq(err, c)
  195. return
  196. }
  197. // Fetch coordinators from historyDB
  198. coordinators, pagination, err := h.GetCoordinators(fromItem, limit, order)
  199. if err != nil {
  200. retSQLErr(err, c)
  201. return
  202. }
  203. // Build succesfull response
  204. apiCoordinators := coordinatorsToAPI(coordinators)
  205. c.JSON(http.StatusOK, &coordinatorsAPI{
  206. Coordinators: apiCoordinators,
  207. Pagination: pagination,
  208. })
  209. }
  210. func getCoordinator(c *gin.Context) {
  211. // Get bidderAddr
  212. const name = "bidderAddr"
  213. bidderAddr, err := parseParamEthAddr(name, c)
  214. if err != nil {
  215. retBadReq(err, c)
  216. return
  217. } else if bidderAddr == nil {
  218. retBadReq(ErrNillBidderAddr, c)
  219. return
  220. }
  221. coordinator, err := h.GetCoordinator(*bidderAddr)
  222. if err != nil {
  223. retSQLErr(err, c)
  224. return
  225. }
  226. apiCoordinator := coordinatorsToAPI([]historydb.HistoryCoordinator{*coordinator})
  227. c.JSON(http.StatusOK, apiCoordinator[0])
  228. }
  229. func retSQLErr(err error, c *gin.Context) {
  230. if err == sql.ErrNoRows {
  231. c.JSON(http.StatusNotFound, errorMsg{
  232. Message: err.Error(),
  233. })
  234. } else {
  235. c.JSON(http.StatusInternalServerError, errorMsg{
  236. Message: err.Error(),
  237. })
  238. }
  239. }
  240. func retBadReq(err error, c *gin.Context) {
  241. c.JSON(http.StatusBadRequest, errorMsg{
  242. Message: err.Error(),
  243. })
  244. }