2021-09-16 19:23:17 +00:00
|
|
|
import Tables, options, json, strformat, strutils
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
from ../../eventemitter import Args
|
|
|
|
import ../types/[transaction]
|
|
|
|
|
2021-09-16 19:23:17 +00:00
|
|
|
type OpenseaCollectionTrait* = ref object
|
|
|
|
min*, max*: float
|
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
type OpenseaCollection* = ref object
|
|
|
|
name*, slug*, imageUrl*: string
|
|
|
|
ownedAssetCount*: int
|
2021-09-16 19:23:17 +00:00
|
|
|
trait*: Table[string, OpenseaCollectionTrait]
|
|
|
|
|
|
|
|
type OpenseaTrait* = ref object
|
|
|
|
traitType*, value*, displayType*, maxValue*: string
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
type OpenseaAsset* = ref object
|
|
|
|
id*: int
|
2021-09-16 19:23:17 +00:00
|
|
|
name*, description*, permalink*, imageThumbnailUrl*, imageUrl*, address*, backgroundColor*: string
|
|
|
|
properties*, rankings*, statistics*: seq[OpenseaTrait]
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
type CurrencyArgs* = ref object of Args
|
|
|
|
currency*: string
|
|
|
|
|
|
|
|
type Asset* = ref object
|
|
|
|
name*, symbol*, value*, fiatBalanceDisplay*, fiatBalance*, accountAddress*, address*: string
|
|
|
|
|
|
|
|
type WalletAccount* = ref object
|
|
|
|
name*, address*, iconColor*, path*, walletType*, publicKey*: string
|
|
|
|
balance*: Option[string]
|
|
|
|
realFiatBalance*: Option[float]
|
|
|
|
assetList*: seq[Asset]
|
|
|
|
wallet*, chat*: bool
|
|
|
|
transactions*: tuple[hasMore: bool, data: seq[Transaction]]
|
|
|
|
|
2021-09-16 19:23:17 +00:00
|
|
|
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
|
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
proc newWalletAccount*(name, address, iconColor, path, walletType, publicKey: string,
|
|
|
|
wallet, chat: bool, assets: seq[Asset]): WalletAccount =
|
|
|
|
result = new WalletAccount
|
|
|
|
result.name = name
|
|
|
|
result.address = address
|
|
|
|
result.iconColor = iconColor
|
|
|
|
result.path = path
|
|
|
|
result.walletType = walletType
|
|
|
|
result.publicKey = publicKey
|
|
|
|
result.wallet = wallet
|
|
|
|
result.chat = chat
|
|
|
|
result.assetList = assets
|
|
|
|
result.balance = none[string]()
|
|
|
|
result.realFiatBalance = none[float]()
|
|
|
|
|
|
|
|
type AccountArgs* = ref object of Args
|
|
|
|
account*: WalletAccount
|
|
|
|
|
|
|
|
proc `$`*(self: OpenseaCollection): string =
|
|
|
|
return fmt"OpenseaCollection(name:{self.name}, slug:{self.slug}, owned asset count:{self.ownedAssetCount})"
|
|
|
|
|
|
|
|
proc `$`*(self: OpenseaAsset): string =
|
2021-09-16 19:23:17 +00:00
|
|
|
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
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
proc toOpenseaCollection*(jsonCollection: JsonNode): OpenseaCollection =
|
|
|
|
return OpenseaCollection(
|
|
|
|
name: jsonCollection{"name"}.getStr,
|
|
|
|
slug: jsonCollection{"slug"}.getStr,
|
|
|
|
imageUrl: jsonCollection{"image_url"}.getStr,
|
2021-09-16 19:23:17 +00:00
|
|
|
ownedAssetCount: jsonCollection{"owned_asset_count"}.getInt,
|
|
|
|
trait: getOpenseaCollectionTraits(jsonCollection)
|
2021-09-08 18:05:39 +00:00
|
|
|
)
|
|
|
|
|
2021-09-16 19:23:17 +00:00
|
|
|
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
|
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
proc toOpenseaAsset*(jsonAsset: JsonNode): OpenseaAsset =
|
|
|
|
return OpenseaAsset(
|
|
|
|
id: jsonAsset{"id"}.getInt,
|
|
|
|
name: jsonAsset{"name"}.getStr,
|
|
|
|
description: jsonAsset{"description"}.getStr,
|
|
|
|
permalink: jsonAsset{"permalink"}.getStr,
|
|
|
|
imageThumbnailUrl: jsonAsset{"image_thumbnail_url"}.getStr,
|
|
|
|
imageUrl: jsonAsset{"image_url"}.getStr,
|
2021-09-16 19:23:17 +00:00
|
|
|
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)
|
|
|
|
)
|