feat: Added apis to read collection and asset traits from opensea (#30)

This commit is contained in:
Khushboo-dev-cpp 2021-09-16 21:23:17 +02:00 committed by GitHub
parent 99b85d2063
commit 12a8b59780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 6 deletions

View File

@ -1,4 +1,4 @@
import options, json, strformat import Tables, options, json, strformat, strutils
from ../../eventemitter import Args from ../../eventemitter import Args
import ../types/[transaction] import ../types/[transaction]
@ -10,13 +10,21 @@ type CollectibleList* = ref object
type Collectible* = ref object type Collectible* = ref object
name*, image*, id*, collectibleType*, description*, externalUrl*: string name*, image*, id*, collectibleType*, description*, externalUrl*: string
type OpenseaCollectionTrait* = ref object
min*, max*: float
type OpenseaCollection* = ref object type OpenseaCollection* = ref object
name*, slug*, imageUrl*: string name*, slug*, imageUrl*: string
ownedAssetCount*: int ownedAssetCount*: int
trait*: Table[string, OpenseaCollectionTrait]
type OpenseaTrait* = ref object
traitType*, value*, displayType*, maxValue*: string
type OpenseaAsset* = ref object type OpenseaAsset* = ref object
id*: int 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 type CurrencyArgs* = ref object of Args
currency*: string currency*: string
@ -33,6 +41,18 @@ type WalletAccount* = ref object
collectiblesLists*: seq[CollectibleList] collectiblesLists*: seq[CollectibleList]
transactions*: tuple[hasMore: bool, data: seq[Transaction]] 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, proc newWalletAccount*(name, address, iconColor, path, walletType, publicKey: string,
wallet, chat: bool, assets: seq[Asset]): WalletAccount = wallet, chat: bool, assets: seq[Asset]): WalletAccount =
result = new 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})" return fmt"OpenseaCollection(name:{self.name}, slug:{self.slug}, owned asset count:{self.ownedAssetCount})"
proc `$`*(self: OpenseaAsset): string = 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 = proc toOpenseaCollection*(jsonCollection: JsonNode): OpenseaCollection =
return OpenseaCollection( return OpenseaCollection(
name: jsonCollection{"name"}.getStr, name: jsonCollection{"name"}.getStr,
slug: jsonCollection{"slug"}.getStr, slug: jsonCollection{"slug"}.getStr,
imageUrl: jsonCollection{"image_url"}.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 = proc toOpenseaAsset*(jsonAsset: JsonNode): OpenseaAsset =
return OpenseaAsset( return OpenseaAsset(
id: jsonAsset{"id"}.getInt, id: jsonAsset{"id"}.getInt,
@ -73,5 +117,9 @@ proc toOpenseaAsset*(jsonAsset: JsonNode): OpenseaAsset =
permalink: jsonAsset{"permalink"}.getStr, permalink: jsonAsset{"permalink"}.getStr,
imageThumbnailUrl: jsonAsset{"image_thumbnail_url"}.getStr, imageThumbnailUrl: jsonAsset{"image_thumbnail_url"}.getStr,
imageUrl: jsonAsset{"image_url"}.getStr, imageUrl: jsonAsset{"image_url"}.getStr,
address: jsonAsset{"asset_contract"}{"address"}.getStr 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)
)