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.

311 lines
5.7 KiB

  1. package api
  2. import (
  3. "database/sql"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/hermeznetwork/hermez-node/common"
  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. func postAccountCreationAuth(c *gin.Context) {
  20. }
  21. func getAccountCreationAuth(c *gin.Context) {
  22. }
  23. func postPoolTx(c *gin.Context) {
  24. // Parse body
  25. var receivedTx receivedPoolTx
  26. if err := c.ShouldBindJSON(&receivedTx); err != nil {
  27. retBadReq(err, c)
  28. return
  29. }
  30. // Transform from received to insert format and validate
  31. writeTx, err := receivedTx.toDBWritePoolL2Tx()
  32. if err != nil {
  33. retBadReq(err, c)
  34. return
  35. }
  36. // Insert to DB
  37. if err := l2.AddTx(writeTx); err != nil {
  38. retSQLErr(err, c)
  39. return
  40. }
  41. // Return TxID
  42. c.JSON(http.StatusOK, writeTx.TxID.String())
  43. }
  44. func getPoolTx(c *gin.Context) {
  45. // Get TxID
  46. txID, err := parseParamTxID(c)
  47. if err != nil {
  48. retBadReq(err, c)
  49. return
  50. }
  51. // Fetch tx from l2DB
  52. dbTx, err := l2.GetTx(txID)
  53. if err != nil {
  54. retSQLErr(err, c)
  55. return
  56. }
  57. apiTx := poolL2TxReadToSend(dbTx)
  58. // Build succesfull response
  59. c.JSON(http.StatusOK, apiTx)
  60. }
  61. func getAccounts(c *gin.Context) {
  62. }
  63. func getAccount(c *gin.Context) {
  64. }
  65. func getExits(c *gin.Context) {
  66. // Get query parameters
  67. // Account filters
  68. tokenID, addr, bjj, idx, err := parseAccountFilters(c)
  69. if err != nil {
  70. retBadReq(err, c)
  71. return
  72. }
  73. // BatchNum
  74. batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
  75. if err != nil {
  76. retBadReq(err, c)
  77. return
  78. }
  79. // Pagination
  80. fromItem, order, limit, err := parsePagination(c)
  81. if err != nil {
  82. retBadReq(err, c)
  83. return
  84. }
  85. // Fetch exits from historyDB
  86. exits, pagination, err := h.GetExits(
  87. addr, bjj, tokenID, idx, batchNum, fromItem, limit, order,
  88. )
  89. if err != nil {
  90. retSQLErr(err, c)
  91. return
  92. }
  93. // Build succesfull response
  94. apiExits := historyExitsToAPI(exits)
  95. c.JSON(http.StatusOK, &exitsAPI{
  96. Exits: apiExits,
  97. Pagination: pagination,
  98. })
  99. }
  100. func getExit(c *gin.Context) {
  101. // Get batchNum and accountIndex
  102. batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
  103. if err != nil {
  104. retBadReq(err, c)
  105. return
  106. }
  107. idx, err := parseParamIdx(c)
  108. if err != nil {
  109. retBadReq(err, c)
  110. return
  111. }
  112. // Fetch tx from historyDB
  113. exit, err := h.GetExit(batchNum, idx)
  114. if err != nil {
  115. retSQLErr(err, c)
  116. return
  117. }
  118. apiExits := historyExitsToAPI([]historydb.HistoryExit{*exit})
  119. // Build succesfull response
  120. c.JSON(http.StatusOK, apiExits[0])
  121. }
  122. func getHistoryTxs(c *gin.Context) {
  123. // Get query parameters
  124. tokenID, addr, bjj, idx, err := parseAccountFilters(c)
  125. if err != nil {
  126. retBadReq(err, c)
  127. return
  128. }
  129. // BatchNum
  130. batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
  131. if err != nil {
  132. retBadReq(err, c)
  133. return
  134. }
  135. // TxType
  136. txType, err := parseQueryTxType(c)
  137. if err != nil {
  138. retBadReq(err, c)
  139. return
  140. }
  141. // Pagination
  142. fromItem, order, limit, err := parsePagination(c)
  143. if err != nil {
  144. retBadReq(err, c)
  145. return
  146. }
  147. // Fetch txs from historyDB
  148. txs, pagination, err := h.GetHistoryTxs(
  149. addr, bjj, tokenID, idx, batchNum, txType, fromItem, limit, order,
  150. )
  151. if err != nil {
  152. retSQLErr(err, c)
  153. return
  154. }
  155. // Build succesfull response
  156. apiTxs := historyTxsToAPI(txs)
  157. c.JSON(http.StatusOK, &historyTxsAPI{
  158. Txs: apiTxs,
  159. Pagination: pagination,
  160. })
  161. }
  162. func getHistoryTx(c *gin.Context) {
  163. // Get TxID
  164. txID, err := parseParamTxID(c)
  165. if err != nil {
  166. retBadReq(err, c)
  167. return
  168. }
  169. // Fetch tx from historyDB
  170. tx, err := h.GetHistoryTx(txID)
  171. if err != nil {
  172. retSQLErr(err, c)
  173. return
  174. }
  175. apiTxs := historyTxsToAPI([]historydb.HistoryTx{*tx})
  176. // Build succesfull response
  177. c.JSON(http.StatusOK, apiTxs[0])
  178. }
  179. func getBatches(c *gin.Context) {
  180. }
  181. func getBatch(c *gin.Context) {
  182. }
  183. func getFullBatch(c *gin.Context) {
  184. }
  185. func getSlots(c *gin.Context) {
  186. }
  187. func getBids(c *gin.Context) {
  188. }
  189. func getNextForgers(c *gin.Context) {
  190. }
  191. func getState(c *gin.Context) {
  192. }
  193. func getConfig(c *gin.Context) {
  194. c.JSON(http.StatusOK, cg)
  195. }
  196. func getTokens(c *gin.Context) {
  197. // Account filters
  198. tokenIDs, symbols, name, err := parseTokenFilters(c)
  199. if err != nil {
  200. retBadReq(err, c)
  201. return
  202. }
  203. // Pagination
  204. fromItem, order, limit, err := parsePagination(c)
  205. if err != nil {
  206. retBadReq(err, c)
  207. return
  208. }
  209. // Fetch exits from historyDB
  210. tokens, pagination, err := h.GetTokens(
  211. tokenIDs, symbols, name, fromItem, limit, order,
  212. )
  213. if err != nil {
  214. retSQLErr(err, c)
  215. return
  216. }
  217. // Build succesfull response
  218. apiTokens := tokensToAPI(tokens)
  219. c.JSON(http.StatusOK, &tokensAPI{
  220. Tokens: apiTokens,
  221. Pagination: pagination,
  222. })
  223. }
  224. func getToken(c *gin.Context) {
  225. // Get TokenID
  226. tokenIDUint, err := parseParamUint("id", nil, 0, maxUint32, c)
  227. if err != nil {
  228. retBadReq(err, c)
  229. return
  230. }
  231. tokenID := common.TokenID(*tokenIDUint)
  232. // Fetch token from historyDB
  233. token, err := h.GetToken(tokenID)
  234. if err != nil {
  235. retSQLErr(err, c)
  236. return
  237. }
  238. apiToken := tokensToAPI([]historydb.TokenRead{*token})
  239. c.JSON(http.StatusOK, apiToken[0])
  240. }
  241. func getRecommendedFee(c *gin.Context) {
  242. }
  243. func getCoordinators(c *gin.Context) {
  244. }
  245. func getCoordinator(c *gin.Context) {
  246. }
  247. func retSQLErr(err error, c *gin.Context) {
  248. if err == sql.ErrNoRows {
  249. c.JSON(http.StatusNotFound, errorMsg{
  250. Message: err.Error(),
  251. })
  252. } else {
  253. c.JSON(http.StatusInternalServerError, errorMsg{
  254. Message: err.Error(),
  255. })
  256. }
  257. }
  258. func retBadReq(err error, c *gin.Context) {
  259. c.JSON(http.StatusBadRequest, errorMsg{
  260. Message: err.Error(),
  261. })
  262. }