Skip to content

Commit

Permalink
using a single query to get a page of tokens no matters if the user p…
Browse files Browse the repository at this point in the history
…rovides a cursor or not
  • Loading branch information
lucasmenendez committed Oct 10, 2023
1 parent 7e17a8f commit 1110a07
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions api/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ func (capi *census3API) getTokens(msg *api.APIdata, ctx *httprouter.HTTPContext)
}()
qtx := capi.db.QueriesRO.WithTx(tx)
// get tokens from the database, if there is a cursor use it to paginate
// the results, if not get the first page
// the results, if not get the first page using empty cursor
var rows []queries.Token
strCursor := ctx.Request.URL.Query().Get("cursor")
if strCursor != "" {
rows, err = qtx.ListTokensPaginated(internalCtx, queries.ListTokensPaginatedParams{
PageCursor: common.HexToAddress(strCursor).Bytes(),
Limit: pageSize,
})
} else {
rows, err = qtx.ListTokens(internalCtx, pageSize)
bCursor := []byte{}
if strCursor := ctx.Request.URL.Query().Get("cursor"); strCursor != "" {
bCursor = common.HexToAddress(strCursor).Bytes()
}
rows, err = qtx.ListTokensPaginated(internalCtx, queries.ListTokensPaginatedParams{
PageCursor: bCursor,
Limit: pageSize,
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNoTokens.WithErr(err)
Expand All @@ -89,8 +88,8 @@ func (capi *census3API) getTokens(msg *api.APIdata, ctx *httprouter.HTTPContext)
}
// parse current cursor
var currentCursor common.Address
if strCursor != "" {
currentCursor = common.HexToAddress(strCursor)
if len(bCursor) > 0 {
currentCursor = common.BytesToAddress(bCursor)
} else {
currentCursor = common.BytesToAddress(rows[0].ID)
}
Expand All @@ -104,6 +103,7 @@ func (capi *census3API) getTokens(msg *api.APIdata, ctx *httprouter.HTTPContext)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return ErrCantGetTokens.WithErr(err)
}
// there is a next cursor, so add it to the pagination of the response
if nextCursor != nil {
tokensResponse.Pagination.NextCursor = common.BytesToAddress(nextCursor).String()
}
Expand All @@ -115,8 +115,11 @@ func (capi *census3API) getTokens(msg *api.APIdata, ctx *httprouter.HTTPContext)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return ErrCantGetTokens.WithErr(err)
}
if cursor := common.BytesToAddress(prevCursor); cursor.Big().Cmp(big.NewInt(0)) != 0 && cursor != currentCursor {
tokensResponse.Pagination.PrevCursor = cursor.String()
// if the previous cursor is different from the current cursor, the current
// page is not the first one, so add the previous cursor to the pagination
// of the response
if !bytes.Equal(prevCursor, currentCursor.Bytes()) {
tokensResponse.Pagination.PrevCursor = common.BytesToAddress(prevCursor).String()
}
// parse results from database to the response format
for _, tokenData := range rows {
Expand Down

0 comments on commit 1110a07

Please sign in to comment.