mirror of
https://github.com/status-im/status-lib.git
synced 2025-01-11 13:04:26 +00:00
feat: Added apis to read collection and asset traits from opensea (#30)
This commit is contained in:
parent
99b85d2063
commit
12a8b59780
@ -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
|
||||
)
|
||||
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)
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user