From 12a8b59780fa586663ec3414d904f5f822b63d86 Mon Sep 17 00:00:00 2001 From: Khushboo-dev-cpp <60327365+Khushboo-dev-cpp@users.noreply.github.com> Date: Thu, 16 Sep 2021 21:23:17 +0200 Subject: [PATCH] feat: Added apis to read collection and asset traits from opensea (#30) --- status/wallet2/account.nim | 60 ++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/status/wallet2/account.nim b/status/wallet2/account.nim index deeb86b..2ab58de 100644 --- a/status/wallet2/account.nim +++ b/status/wallet2/account.nim @@ -1,4 +1,4 @@ -import options, json, strformat +import Tables, options, json, strformat, strutils from ../../eventemitter import Args import ../types/[transaction] @@ -10,13 +10,21 @@ type CollectibleList* = ref object type Collectible* = ref object name*, image*, id*, collectibleType*, description*, externalUrl*: string +type OpenseaCollectionTrait* = ref object + min*, max*: float + type OpenseaCollection* = ref object name*, slug*, imageUrl*: string ownedAssetCount*: int + trait*: Table[string, OpenseaCollectionTrait] + +type OpenseaTrait* = ref object + traitType*, value*, displayType*, maxValue*: string type OpenseaAsset* = ref object id*: int - name*, description*, permalink*, imageThumbnailUrl*, imageUrl*, address*: string + name*, description*, permalink*, imageThumbnailUrl*, imageUrl*, address*, backgroundColor*: string + properties*, rankings*, statistics*: seq[OpenseaTrait] type CurrencyArgs* = ref object of Args currency*: string @@ -33,6 +41,18 @@ type WalletAccount* = ref object collectiblesLists*: seq[CollectibleList] transactions*: tuple[hasMore: bool, data: seq[Transaction]] +type TraitType* {.pure.} = enum + Properties = 0, + Rankings = 1, + Statistics = 2 + +proc isNumeric(s: string): bool = + try: + discard s.parseFloat() + result = true + except ValueError: + result = false + proc newWalletAccount*(name, address, iconColor, path, walletType, publicKey: string, wallet, chat: bool, assets: seq[Asset]): WalletAccount = result = new WalletAccount @@ -55,16 +75,40 @@ proc `$`*(self: OpenseaCollection): string = return fmt"OpenseaCollection(name:{self.name}, slug:{self.slug}, owned asset count:{self.ownedAssetCount})" proc `$`*(self: OpenseaAsset): string = - return fmt"OpenseaAsset(id:{self.id}, name:{self.name}, address:{self.address}, imageUrl: {self.imageUrl}, imageThumbnailUrl: {self.imageThumbnailUrl})" + return fmt"OpenseaAsset(id:{self.id}, name:{self.name}, address:{self.address}, imageUrl: {self.imageUrl}, imageThumbnailUrl: {self.imageThumbnailUrl}, backgroundColor: {self.backgroundColor})" + +proc getOpenseaCollectionTraits*(jsonCollection: JsonNode): Table[string, OpenseaCollectionTrait] = + var traitList: Table[string, OpenseaCollectionTrait] = initTable[string, OpenseaCollectionTrait]() + for key, value in jsonCollection{"traits"}: + traitList[key] = OpenseaCollectionTrait(min: value{"min"}.getFloat, max: value{"max"}.getFloat) + return traitList proc toOpenseaCollection*(jsonCollection: JsonNode): OpenseaCollection = return OpenseaCollection( name: jsonCollection{"name"}.getStr, slug: jsonCollection{"slug"}.getStr, imageUrl: jsonCollection{"image_url"}.getStr, - ownedAssetCount: jsonCollection{"owned_asset_count"}.getInt + ownedAssetCount: jsonCollection{"owned_asset_count"}.getInt, + trait: getOpenseaCollectionTraits(jsonCollection) ) +proc getOpenseaTraits*(jsonAsset: JsonNode, traitType: TraitType): seq[OpenseaTrait] = + var traitList: seq[OpenseaTrait] = @[] + case traitType: + of TraitType.Properties: + for index in jsonAsset{"traits"}.items: + if((index{"display_type"}.getStr != "number") and (index{"display_type"}.getStr != "boost_percentage") and (index{"display_type"}.getStr != "boost_number") and not isNumeric(index{"value"}.getStr)): + traitList.add(OpenseaTrait(traitType: index{"trait_type"}.getStr, value: index{"value"}.getStr, displayType: index{"display_type"}.getStr, maxValue: index{"max_value"}.getStr)) + of TraitType.Rankings: + for index in jsonAsset{"traits"}.items: + if(index{"display_type"}.getStr != "number" and (index{"display_type"}.getStr != "boost_percentage") and (index{"display_type"}.getStr != "boost_number") and isNumeric(index{"value"}.getStr)): + traitList.add(OpenseaTrait(traitType: index{"trait_type"}.getStr, value: index{"value"}.getStr, displayType: index{"display_type"}.getStr, maxValue: index{"max_value"}.getStr)) + of TraitType.Statistics: + for index in jsonAsset{"traits"}.items: + if(index{"display_type"}.getStr == "number" and (index{"display_type"}.getStr != "boost_percentage") and (index{"display_type"}.getStr != "boost_number") and isNumeric(index{"value"}.getStr)): + traitList.add(OpenseaTrait(traitType: index{"trait_type"}.getStr, value: index{"value"}.getStr, displayType: index{"display_type"}.getStr, maxValue: index{"max_value"}.getStr)) + return traitList + proc toOpenseaAsset*(jsonAsset: JsonNode): OpenseaAsset = return OpenseaAsset( id: jsonAsset{"id"}.getInt, @@ -73,5 +117,9 @@ proc toOpenseaAsset*(jsonAsset: JsonNode): OpenseaAsset = permalink: jsonAsset{"permalink"}.getStr, imageThumbnailUrl: jsonAsset{"image_thumbnail_url"}.getStr, imageUrl: jsonAsset{"image_url"}.getStr, - address: jsonAsset{"asset_contract"}{"address"}.getStr - ) \ No newline at end of file + address: jsonAsset{"asset_contract"}{"address"}.getStr, + backgroundColor: jsonAsset{"background_color"}.getStr, + properties: getOpenseaTraits(jsonAsset, TraitType.Properties), + rankings: getOpenseaTraits(jsonAsset, TraitType.Rankings), + statistics: getOpenseaTraits(jsonAsset, TraitType.Statistics) + )