fix(@desktop/wallet): Iterate over collectible with balance

This commit is contained in:
Anthony Laibe 2021-07-09 12:06:48 +01:00 committed by Iuri Matias
parent 0eb2a23600
commit 314d1833a2
2 changed files with 21 additions and 3 deletions

View File

@ -21,6 +21,7 @@ proc callPrivateRPC*(methodName: string, payload = %* []): string =
let response = status_go.callPrivateRPC($inputJSON)
result = $response
if parseJSON(result).hasKey("error"):
writeStackTrace()
error "rpc response error", result, payload, methodName
except Exception as e:
error "error doing rpc request", methodName = methodName, exception=e.msg

View File

@ -56,17 +56,34 @@ proc tokenOfOwnerByIndex(contract: Erc721Contract, address: Address, index: Stui
return -1
result = fromHex[int](res)
proc balanceOf(contract: Erc721Contract, address: Address): int =
let
balanceOf = BalanceOf(address: address)
payload = %* [{
"to": $contract.address,
"data": contract.methods["balanceOf"].encodeAbi(balanceOf)
}, "latest"]
response = callPrivateRPC("eth_call", payload)
jsonResponse = parseJson($response)
if (not jsonResponse.hasKey("result")):
return 0
let res = jsonResponse["result"].getStr
if (res == "0x"):
return 0
result = fromHex[int](res)
proc tokensOfOwnerByIndex(contract: Erc721Contract, address: Address): seq[int] =
var index = 0
var token: int
var maxIndex: int = balanceOf(contract, address)
result = @[]
while (true):
while index < maxIndex and result.len <= MAX_TOKENS:
token = tokenOfOwnerByIndex(contract, address, index.u256)
if (token == -1 or token == 0 or result.len > MAX_TOKENS):
return result
result.add(token)
index = index + 1
return result
proc getCryptoKittiesBatch*(address: Address, offset: int = 0): seq[Collectible] =
var cryptokitties: seq[Collectible]
cryptokitties = @[]