feat: enable loading all the cryptokitties assets with recursion
This commit is contained in:
parent
9acb6609ab
commit
51e1ba6f38
|
@ -59,42 +59,52 @@ proc tokensOfOwnerByIndex(contract: Contract, address: Address): seq[int] =
|
||||||
result.add(token)
|
result.add(token)
|
||||||
index = index + 1
|
index = index + 1
|
||||||
|
|
||||||
proc getCryptoKitties*(address: Address): string =
|
proc getCryptoKittiesBatch*(address: Address, offset: int = 0): seq[Collectible] =
|
||||||
var cryptokitties: seq[Collectible]
|
var cryptokitties: seq[Collectible]
|
||||||
cryptokitties = @[]
|
cryptokitties = @[]
|
||||||
try:
|
# TODO handle testnet -- does this API exist in testnet??
|
||||||
# TODO handle testnet -- does this API exist in testnet??
|
let url: string = fmt"https://api.cryptokitties.co/kitties?limit=20&offset={$offset}&owner_wallet_address={$address}&parents=false"
|
||||||
# TODO handle offset (recursive method?)
|
let client = newHttpClient()
|
||||||
# Crypto kitties has a limit of 20
|
client.headers = newHttpHeaders({ "Content-Type": "application/json" })
|
||||||
let url: string = fmt"https://api.cryptokitties.co/kitties?limit=20&offset=0&owner_wallet_address={$address}&parents=false"
|
|
||||||
let client = newHttpClient()
|
|
||||||
client.headers = newHttpHeaders({ "Content-Type": "application/json" })
|
|
||||||
|
|
||||||
let response = client.request(url)
|
let response = client.request(url)
|
||||||
let kitties = parseJson(response.body)["kitties"]
|
let responseBody = parseJson(response.body)
|
||||||
for kitty in kitties:
|
let kitties = responseBody["kitties"]
|
||||||
try:
|
for kitty in kitties:
|
||||||
var id = kitty["id"]
|
try:
|
||||||
var name = kitty["name"]
|
var id = kitty["id"]
|
||||||
var finalId = ""
|
var name = kitty["name"]
|
||||||
var finalName = ""
|
var finalId = ""
|
||||||
if (not (id.kind == JNull)):
|
var finalName = ""
|
||||||
finalId = $id
|
if (not (id.kind == JNull)):
|
||||||
if (not (name.kind == JNull)):
|
finalId = $id
|
||||||
finalName = $name
|
if (not (name.kind == JNull)):
|
||||||
cryptokitties.add(Collectible(id: finalId,
|
finalName = $name
|
||||||
name: finalName,
|
cryptokitties.add(Collectible(id: finalId,
|
||||||
image: kitty["image_url_png"].str,
|
name: finalName,
|
||||||
collectibleType: CRYPTOKITTY,
|
image: kitty["image_url_png"].str,
|
||||||
description: "",
|
collectibleType: CRYPTOKITTY,
|
||||||
externalUrl: ""))
|
description: "",
|
||||||
except Exception as e2:
|
externalUrl: ""))
|
||||||
error "Error with this individual cat", msg = e2.msg, cat = kitty
|
except Exception as e2:
|
||||||
|
error "Error with this individual cat", msg = e2.msg, cat = kitty
|
||||||
|
|
||||||
|
let limit = responseBody["limit"].getInt
|
||||||
|
let total = responseBody["total"].getInt
|
||||||
|
if (limit * (offset + 1) < total):
|
||||||
|
# Call the API again with oofset + 1
|
||||||
|
let nextBatch = getCryptoKittiesBatch(address, offset + 1)
|
||||||
|
return concat(cryptokitties, nextBatch)
|
||||||
|
return cryptokitties
|
||||||
|
|
||||||
|
proc getCryptoKitties*(address: Address): string =
|
||||||
|
try:
|
||||||
|
let cryptokitties = getCryptoKittiesBatch(address, 0)
|
||||||
|
|
||||||
|
return $(%*cryptokitties)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error "Error getting Cryptokitties", msg = e.msg
|
error "Error getting Cryptokitties", msg = e.msg
|
||||||
return e.msg
|
return e.msg
|
||||||
|
|
||||||
return $(%*cryptokitties)
|
|
||||||
|
|
||||||
proc getCryptoKitties*(address: string): string =
|
proc getCryptoKitties*(address: string): string =
|
||||||
let eth_address = parseAddress(address)
|
let eth_address = parseAddress(address)
|
||||||
|
|
Loading…
Reference in New Issue