From 04d969c274e142e14c9d38885327792be962e766 Mon Sep 17 00:00:00 2001 From: laisolizq Date: Tue, 3 Nov 2020 13:44:30 +0100 Subject: [PATCH] API add pending withdraws filter on GET /exits --- api/exits.go | 8 +++++++- api/exits_test.go | 16 ++++++++++++++++ api/swagger.yml | 6 ++++++ db/historydb/historydb.go | 16 ++++++++++++++-- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/api/exits.go b/api/exits.go index 76f64d6..3e5a258 100644 --- a/api/exits.go +++ b/api/exits.go @@ -22,6 +22,12 @@ func getExits(c *gin.Context) { 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 @@ func getExits(c *gin.Context) { // 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) diff --git a/api/exits_test.go b/api/exits_test.go index 5cbf82a..0675301 100644 --- a/api/exits_test.go +++ b/api/exits_test.go @@ -190,6 +190,22 @@ func TestGetExits(t *testing.T) { } } 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 diff --git a/api/swagger.yml b/api/swagger.yml index dbc5a6f..599d918 100644 --- a/api/swagger.yml +++ b/api/swagger.yml @@ -286,6 +286,12 @@ paths: 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 diff --git a/db/historydb/historydb.go b/db/historydb/historydb.go index 0c5d1c8..9eabfed 100644 --- a/db/historydb/historydb.go +++ b/db/historydb/historydb.go @@ -911,8 +911,8 @@ func (hdb *HistoryDB) GetExitAPI(batchNum *uint, idx *common.Idx) (*ExitAPI, err // 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 { @@ -975,6 +975,18 @@ func (hdb *HistoryDB) GetExitsAPI( 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 { queryStr += "AND "