Skip to content

Commit

Permalink
Merge pull request #2074 from anon-yum/master
Browse files Browse the repository at this point in the history
#2051 Add limit and offset parameter to /wallet/boxes/unspent
  • Loading branch information
kushti committed Dec 13, 2023
2 parents cfec5e6 + 60ccfc4 commit 6cecf7e
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 17 deletions.
78 changes: 77 additions & 1 deletion src/main/resources/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4479,6 +4479,25 @@ paths:
type: integer
format: int32
default: -1
- in: query
name: limit
required: false
description: amount of elements to retrieve
schema:
type: integer
format: int32
minimum: 1
default: 500
maximum: 2500
- in: query
name: offset
required: false
description: The number of items in list to skip
schema:
type: integer
format: int32
minimum: 0
default: 0
get:
security:
- ApiKeyAuth: [api_key]
Expand Down Expand Up @@ -4570,6 +4589,25 @@ paths:
type: integer
format: int32
default: -1
- in: query
name: limit
required: false
description: amount of elements to retrieve
schema:
type: integer
format: int32
minimum: 1
default: 500
maximum: 2500
- in: query
name: offset
required: false
description: The number of items in list to skip
schema:
type: integer
format: int32
minimum: 0
default: 0
get:
security:
- ApiKeyAuth: [api_key]
Expand Down Expand Up @@ -5529,6 +5567,25 @@ paths:
type: integer
format: int32
default: -1
- in: query
name: limit
required: false
description: amount of elements to retrieve
schema:
type: integer
format: int32
minimum: 1
default: 500
maximum: 2500
- in: query
name: offset
required: false
description: The number of items in list to skip
schema:
type: integer
format: int32
minimum: 0
default: 0
get:
security:
- ApiKeyAuth: [api_key]
Expand Down Expand Up @@ -5596,6 +5653,25 @@ paths:
type: integer
format: int32
default: -1
- in: query
name: limit
required: false
description: amount of elements to retrieve
schema:
type: integer
format: int32
minimum: 1
default: 500
maximum: 2500
- in: query
name: offset
required: false
description: The number of items in list to skip
schema:
type: integer
format: int32
minimum: 0
default: 0
get:
security:
- ApiKeyAuth: [api_key]
Expand Down Expand Up @@ -6592,4 +6668,4 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
$ref: '#/components/schemas/ApiError'
6 changes: 4 additions & 2 deletions src/main/scala/org/ergoplatform/http/api/ScanApiRoute.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ case class ScanApiRoute(readersHolder: ActorRef, ergoSettings: ErgoSettings)
}

def unspentR: Route = (path("unspentBoxes" / IntNumber) & get & boxParams) {
(scanIdInt, minConfNum, maxConfNum, minHeight, maxHeight) =>
(scanIdInt, minConfNum, maxConfNum, minHeight, maxHeight, limit, offset) =>
val scanId = ScanId @@ scanIdInt.toShort
val considerUnconfirmed = minConfNum == -1
withWallet(_.scanUnspentBoxes(scanId, considerUnconfirmed, minHeight, maxHeight).map {
_.filter(boxConfirmationFilter(_, minConfNum, maxConfNum))
.slice(offset, offset + limit)
})
}

def spentR: Route = (path("spentBoxes" / IntNumber) & get & boxParams) {
(scanIdInt, minConfNum, maxConfNum, minHeight, maxHeight) =>
(scanIdInt, minConfNum, maxConfNum, minHeight, maxHeight, limit, offset) =>
val scanId = ScanId @@ scanIdInt.toShort
withWallet(_.scanSpentBoxes(scanId).map {
_.filter(boxConfirmationHeightFilter(_, minConfNum, maxConfNum, minHeight, maxHeight))
.slice(offset, offset + limit)
})
}

Expand Down
31 changes: 23 additions & 8 deletions src/main/scala/org/ergoplatform/http/api/WalletApiOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,33 @@ trait WalletApiOperations extends ErgoBaseApiRoute {

val readersHolder: ActorRef

private val isLegalBoxParamCombination: ((Int, Int, Int, Int)) => Boolean = {
case (minConfNum, _, _, maxHeight) =>

val MaxLimit = 2500

private def isLegalOffset(offset: Int): Boolean = offset >= 0

private def isLegalLimit(limit: Int): Boolean = limit >= 1 && limit <= MaxLimit

private val isLegalBoxParamCombination: ((Int, Int, Int, Int, Int, Int)) => Boolean = {
case (minConfNum, _, _, maxHeight, limit, offset) =>
isLegalOffset(offset) &&
isLegalLimit(limit) &&
// maxInclusionHeight cannot be specified when we consider unconfirmed
!(minConfNum == -1 && maxHeight != -1)
}

val boxParams: Directive[(Int, Int, Int, Int)] =
parameters("minConfirmations".as[Int] ? 0, "maxConfirmations".as[Int] ? -1, "minInclusionHeight".as[Int] ? 0, "maxInclusionHeight".as[Int] ? -1)
.tfilter(
isLegalBoxParamCombination,
ValidationRejection("maxInclusionHeight cannot be specified when we consider unconfirmed")
)
val boxParams: Directive[(Int, Int, Int, Int, Int, Int)] =
parameters(
"minConfirmations".as[Int] ? 0,
"maxConfirmations".as[Int] ? -1,
"minInclusionHeight".as[Int] ? 0,
"maxInclusionHeight".as[Int] ? -1,
"limit".as[Int] ? 500,
"offset".as[Int] ? 0
).tfilter(
isLegalBoxParamCombination,
ValidationRejection("maxInclusionHeight cannot be specified when we consider unconfirmed")
)

/**
* Filter function that filters boxes by height or number of confirmations.
Expand Down
15 changes: 9 additions & 6 deletions src/main/scala/org/ergoplatform/http/api/WalletApiRoute.scala
Original file line number Diff line number Diff line change
Expand Up @@ -305,23 +305,26 @@ case class WalletApiRoute(readersHolder: ActorRef,
}

def unspentBoxesR: Route = (path("boxes" / "unspent") & get & boxParams) {
(minConfNum, maxConfNum, minHeight, maxHeight) =>
(minConfNum, maxConfNum, minHeight, maxHeight, limit, offset) =>
val considerUnconfirmed = minConfNum == -1
withWallet {
_.walletBoxes(unspentOnly = true, considerUnconfirmed)
.map {
_.filter(boxConfirmationHeightFilter(_, minConfNum, maxConfNum, minHeight, maxHeight))
withWallet { wallet =>
wallet.walletBoxes(unspentOnly = true, considerUnconfirmed)
.map { boxes =>
boxes
.filter(boxConfirmationHeightFilter(_, minConfNum, maxConfNum, minHeight, maxHeight))
.slice(offset, offset + limit)
}
}
}

def boxesR: Route = (path("boxes") & get & boxParams) {
(minConfNum, maxConfNum, minHeight, maxHeight) =>
(minConfNum, maxConfNum, minHeight, maxHeight, limit, offset) =>
val considerUnconfirmed = minConfNum == -1
withWallet {
_.walletBoxes(unspentOnly = false, considerUnconfirmed = considerUnconfirmed)
.map {
_.filter(boxConfirmationHeightFilter(_, minConfNum, maxConfNum, minHeight, maxHeight))
.slice(offset, offset + limit)
}
}
}
Expand Down

0 comments on commit 6cecf7e

Please sign in to comment.