Skip to content

Commit

Permalink
Fix parsing of Token Entities
Browse files Browse the repository at this point in the history
  • Loading branch information
waltkb committed Sep 11, 2023
1 parent 6ecee76 commit c36027f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,5 @@ import kotlinx.serialization.json.JsonPrimitive
@Serializable
data class TokenEntity(
val image: JSONObject? = null,
val attributes: Map<String, TokenEntityAttributes>? = null,
) {
@Serializable
data class TokenEntityAttributes(
val name: String,
val value: JsonPrimitive
)
}
val attributes: JSONObject? = null
)
3 changes: 3 additions & 0 deletions src/main/kotlin/id/walt/nftkit/rest/NftKitApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,17 @@ object NftKitApi {
}

}.exception(InvalidFormatException::class.java) { e, ctx ->
e.printStackTrace()
log.error(e.stackTraceToString())
ctx.json(ErrorResponse(e.message ?: " Unknown application error", 400))
ctx.status(400)
}.exception(IllegalArgumentException::class.java) { e, ctx ->
e.printStackTrace()
log.error(e.stackTraceToString())
ctx.json(ErrorResponse(e.message ?: " Unknown application error", 400))
ctx.status(400)
}.exception(Exception::class.java) { e, ctx ->
e.printStackTrace()
log.error(e.stackTraceToString())
ctx.json(ErrorResponse(e.message ?: " Unknown server error", 500))
ctx.status(500)
Expand Down
44 changes: 27 additions & 17 deletions src/main/kotlin/id/walt/nftkit/services/PolkadotNftService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import io.ktor.serialization.kotlinx.json.*
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.*

enum class PolkadotParachain {
ASTAR,
Expand Down Expand Up @@ -77,13 +74,13 @@ data class SubscanEvmErc721CollectiblesResult(
@Serializable
data class UniqueNftMetadata(
val fullUrl: String,
val ipfsCid: String,
val ipfsCid: String? = null,
val attributes: List<Attribute>? = null
) {
@Serializable
data class Attribute(
val name: String,
var value: String,
var value: JsonElement,
)
}

Expand Down Expand Up @@ -157,15 +154,17 @@ object PolkadotNftService {

fun fetchUniqueNFTs(network: UniqueNetwork, account: String): TokenOwnersDataResponse = runBlocking {
println("--- FETCH UNIQUE NFTs")
val uniqueGraphqlClient = GraphQLWebClient(url = getUniqueNetworkIndexerUrl(network), serializer = PolkadotGraphQLClientKotlinxSerializer())
val uniqueGraphqlClient =
GraphQLWebClient(url = getUniqueNetworkIndexerUrl(network), serializer = PolkadotGraphQLClientKotlinxSerializer())
val tokenOwnersQuery = TokenOwnersQuery()
tokenOwnersQuery.query = tokenOwnersQuery.query.replace("address", account)
uniqueGraphqlClient.execute(tokenOwnersQuery).data!!.token_owners
}

fun fetchUniqueNFTMetadata(network: UniqueNetwork, collectionId: String, tokenId: String): TokenDataResponse? = runBlocking {
println("--- FETCH UNIQUE NFT METADATA")
val uniqueGraphqlClient = GraphQLWebClient(url = getUniqueNetworkIndexerUrl(network), serializer = PolkadotGraphQLClientKotlinxSerializer())
val uniqueGraphqlClient =
GraphQLWebClient(url = getUniqueNetworkIndexerUrl(network), serializer = PolkadotGraphQLClientKotlinxSerializer())
val tokensQuery = TokensQuery()
tokensQuery.query = tokensQuery.query.replace("tokenId", tokenId)
tokensQuery.query = tokensQuery.query.replace("collectionId", collectionId)
Expand All @@ -191,22 +190,33 @@ object PolkadotNftService {
println("Tokens: ${Json.encodeToString(tokens)}")
println("Metadata: ${Json.encodeToString(metadata)}")

/*
val attributes = metadata.attributes?.values?.map {
Json.decodeFromJsonElement<UniqueNftMetadata.Attribute>(it)
}
*/

val attributes = metadata.attributes?.values?.map {
//Json.decodeFromJsonElement<UniqueNftMetadata.Attribute>()
UniqueNftMetadata.Attribute(it.name, it.value.content)
val attributes = metadata.attributes?.values?.map { attributeElement ->
val attribute = attributeElement.jsonObject
println("ATTRIBUTE: $attribute")

val name = attribute["name"]!!.jsonObject["_"]!!.jsonPrimitive.content

val isArray = attribute["isArray"]?.jsonPrimitive?.booleanOrNull ?: false


val value: JsonElement? =
when {
isArray -> JsonArray(attribute["value"]?.jsonArray?.map { it.jsonObject["_"]!!.jsonPrimitive } ?: throw IllegalArgumentException("Invalid value array for attribute: $name"))
else -> attribute["value"]?.jsonObject?.get("_")?.jsonPrimitive
}

value ?: throw IllegalArgumentException("No value was provided for attribute: $name")


UniqueNftMetadata.Attribute(name, value)
}

val tokenImage = metadata.image!!

return UniqueNftMetadata(
fullUrl = tokenImage["fullUrl"]!!.jsonPrimitive.content,
ipfsCid = tokenImage["ipfsCid"]!!.jsonPrimitive.content,
ipfsCid = tokenImage["ipfsCid"]?.jsonPrimitive?.content,
attributes
)
}
Expand Down

0 comments on commit c36027f

Please sign in to comment.