arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
Merge pull request #252 from hermeznetwork/feature/api-exits-update

API add pending withdraws filter on GET /exits
a_bennassar arnaubennassar5@gmail.com
Tue, 03 Nov 2020 16:19:31 +0100
commit

91ef32693c01a9bb06e2350c9caa2f9468b1094b

parent

954b24020c8f3cb1defa232bbfefaa03dc635b86

4 files changed, 43 insertions(+), 3 deletions(-)

jump to
M api/exits.go → api/exits.go

@@ -22,6 +22,12 @@ if err != nil {

retBadReq(err, c) return } + // OnlyPendingWithdraws + onlyPendingWithdraws, err := parseQueryBool("onlyPendingWithdraws", nil, c) + if err != nil { + retBadReq(err, c) + return + } // Pagination fromItem, order, limit, err := parsePagination(c) if err != nil {

@@ -31,7 +37,7 @@ }

// Fetch exits from historyDB exits, pagination, err := h.GetExitsAPI( - addr, bjj, tokenID, idx, batchNum, fromItem, limit, order, + addr, bjj, tokenID, idx, batchNum, onlyPendingWithdraws, fromItem, limit, order, ) if err != nil { retSQLErr(err, c)
M api/exits_test.go → api/exits_test.go

@@ -190,6 +190,22 @@ batchNumExits = append(batchNumExits, tc.exits[i])

} } assertExitAPIs(t, batchNumExits, fetchedExits) + // OnlyPendingWithdraws + fetchedExits = []testExit{} + limit = 7 + path = fmt.Sprintf( + "%s?&onlyPendingWithdraws=%t&limit=%d&fromItem=", + endpoint, true, limit, + ) + err = doGoodReqPaginated(path, historydb.OrderAsc, &testExitsResponse{}, appendIter) + assert.NoError(t, err) + pendingExits := []testExit{} + for i := 0; i < len(tc.exits); i++ { + if tc.exits[i].InstantWithdrawn == nil && tc.exits[i].DelayedWithdrawn == nil { + pendingExits = append(pendingExits, tc.exits[i]) + } + } + assertExitAPIs(t, pendingExits, fetchedExits) // Multiple filters fetchedExits = []testExit{} limit = 1
M api/swagger.yml → api/swagger.yml

@@ -286,6 +286,12 @@ description: Get exits from the exit tree of a specific batch.

required: false schema: $ref: '#/components/schemas/BatchNum' + - name: onlyPendingWithdraws + in: query + description: Get exits with pending withdrawals. + required: false + schema: + type: boolean - name: fromItem in: query required: false
M db/historydb/historydb.go → db/historydb/historydb.go

@@ -911,8 +911,8 @@ }

// GetExitsAPI returns a list of exits from the DB and pagination info func (hdb *HistoryDB) GetExitsAPI( - ethAddr *ethCommon.Address, bjj *babyjub.PublicKey, - tokenID *common.TokenID, idx *common.Idx, batchNum *uint, + ethAddr *ethCommon.Address, bjj *babyjub.PublicKey, tokenID *common.TokenID, + idx *common.Idx, batchNum *uint, onlyPendingWithdraws *bool, fromItem, limit *uint, order string, ) ([]ExitAPI, *db.Pagination, error) { if ethAddr != nil && bjj != nil {

@@ -974,6 +974,18 @@ }

queryStr += "exit_tree.batch_num = ? " args = append(args, batchNum) nextIsAnd = true + } + // onlyPendingWithdraws + if onlyPendingWithdraws != nil { + if *onlyPendingWithdraws { + if nextIsAnd { + queryStr += "AND " + } else { + queryStr += "WHERE " + } + queryStr += "(exit_tree.instant_withdrawn IS NULL AND exit_tree.delayed_withdrawn IS NULL) " + nextIsAnd = true + } } if fromItem != nil { if nextIsAnd {