Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i2038 - Added token tracking functionality to indexer #2044

Merged
merged 13 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions src/main/resources/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6036,6 +6036,130 @@ paths:
schema:
$ref: '#/components/schemas/ApiError'

/blockchain/box/byTokenId/{tokenId}:
get:
summary: Retrieve boxes by an associated token id
operationId: getBoxesByTokenId
tags:
- blockchain
parameters:
- in: path
name: tokenId
required: true
description: id of the token
schema:
$ref: '#/components/schemas/ModifierId'
- in: query
name: offset
required: false
description: amount of elements to skip from the start
schema:
type: integer
format: int32
default: 0
- in: query
name: limit
required: false
description: amount of elements to retrieve
schema:
type: integer
format: int32
default: 5
responses:
'200':
description: boxes associated with wanted token
content:
application/json:
schema:
type: object
properties:
items:
type: array
description: Array of boxes
items:
$ref: '#/components/schemas/IndexedErgoBox'
total:
type: integer
description: Total number of retreived boxes
'404':
description: No boxes found for wanted token
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
default:
description: Error
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'

/blockchain/box/unspent/byTokenId/{tokenId}:
get:
summary: Retrieve unspent boxes by an associated token id
operationId: getBoxesByTokenIdUnspent
tags:
- blockchain
parameters:
- in: path
name: tokenId
required: true
description: id of the token
schema:
$ref: '#/components/schemas/ModifierId'
- in: query
name: offset
required: false
description: amount of elements to skip from the start
schema:
type: integer
format: int32
default: 0
- in: query
name: limit
required: false
description: amount of elements to retrieve
schema:
type: integer
format: int32
default: 5
- in: query
name: sortDirection
required: false
description: desc = new boxes first ; asc = old boxes first
schema:
type: string
default: desc
- in: query
name: includeUnconfirmed
required: false
description: if true include unconfirmed transactions from mempool
schema:
type: boolean
default: false
responses:
'200':
description: unspent boxes associated with wanted token
content:
application/json:
schema:
type: array
description: Array of boxes
items:
$ref: '#/components/schemas/IndexedErgoBox'
'404':
description: No unspent boxes found for wanted token
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'
default:
description: Error
content:
application/json:
schema:
$ref: '#/components/schemas/ApiError'

/blockchain/box/byAddress:
post:
summary: Retrieve boxes by their associated address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.ergoplatform.nodeView.history.ErgoHistoryReader
import org.ergoplatform.nodeView.history.extra.ExtraIndexer.ReceivableMessages.GetSegmentTreshold
import org.ergoplatform.nodeView.history.extra.ExtraIndexer.{GlobalBoxIndexKey, GlobalTxIndexKey, getIndex}
import org.ergoplatform.nodeView.history.extra.IndexedErgoAddressSerializer.hashErgoTree
import org.ergoplatform.nodeView.history.extra.IndexedTokenSerializer.uniqueId
import org.ergoplatform.nodeView.history.extra._
import org.ergoplatform.nodeView.mempool.ErgoMemPoolReader
import org.ergoplatform.settings.ErgoSettings
Expand Down Expand Up @@ -74,6 +75,8 @@ case class BlockchainApiRoute(readersHolder: ActorRef, ergoSettings: ErgoSetting
getTxRangeR ~
getBoxByIdR ~
getBoxByIndexR ~
getBoxesByTokenIdR ~
getBoxesByTokenIdUnspentR ~
getBoxesByAddressR ~
getBoxesByAddressGetRoute ~
getBoxesByAddressUnspentR ~
Expand Down Expand Up @@ -327,14 +330,44 @@ case class BlockchainApiRoute(readersHolder: ActorRef, ergoSettings: ErgoSetting

private def getTokenInfoById(id: ModifierId): Future[Option[IndexedToken]] = {
getHistory.map { history =>
history.typedExtraIndexById[IndexedToken](IndexedTokenSerializer.uniqueId(id))
history.typedExtraIndexById[IndexedToken](uniqueId(id))
}
}

private def getTokenInfoByIdR: Route = (get & pathPrefix("token" / "byId") & modifierId) { id =>
ApiResponse(getTokenInfoById(id))
}

private def getBoxesByTokenId(id: ModifierId, offset: Int, limit: Int): Future[(Seq[IndexedErgoBox],Long)] =
getHistory.map { history =>
history.typedExtraIndexById[IndexedToken](uniqueId(id)) match {
case Some(token) => (token.retrieveBoxes(history, offset, limit), token.boxCount)
case None => (Seq.empty[IndexedErgoBox], 0L)
}
}

private def getBoxesByTokenIdR: Route = (get & pathPrefix("box" / "byTokenId") & modifierId & paging) { (id, offset, limit) =>
ApiResponse(getBoxesByTokenId(id, offset, limit))
}

private def getBoxesByTokenIdUnspent(id: ModifierId, offset: Int, limit: Int, sortDir: Direction, unconfirmed: Boolean): Future[Seq[IndexedErgoBox]] =
getHistoryWithMempool.map { case (history, mempool) =>
history.typedExtraIndexById[IndexedToken](uniqueId(id)) match {
case Some(token) => token.retrieveUtxos(history, mempool, offset, limit, sortDir, unconfirmed)
case None => Seq.empty[IndexedErgoBox]
}
}

private def getBoxesByTokenIdUnspentR: Route = (get & pathPrefix("box" / "unspent" / "byTokenId") & modifierId & paging & sortDir & unconfirmed) { (id, offset, limit, dir, unconfirmed) =>
if (limit > MaxItems) {
BadRequest(s"No more than $MaxItems boxes can be requested")
} else if (dir == SortDirection.INVALID) {
BadRequest("Invalid parameter for sort direction, valid values are 'ASC' and 'DESC'")
} else {
ApiResponse(getBoxesByTokenIdUnspent(id, offset, limit, dir, unconfirmed))
}
}

private def getUnconfirmedForAddress(address: ErgoAddress)(mempool: ErgoMemPoolReader): BalanceInfo = {
val bal: BalanceInfo = BalanceInfo()
mempool.getAll.map(_.transaction).foreach(tx => {
Expand Down
Loading
Loading