From 314d1833a2cc72d836833e321378561b73fd682c Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Fri, 9 Jul 2021 12:06:48 +0100 Subject: [PATCH] fix(@desktop/wallet): Iterate over collectible with balance --- src/status/libstatus/core.nim | 1 + src/status/wallet/collectibles.nim | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/status/libstatus/core.nim b/src/status/libstatus/core.nim index 557fdf85c2..25d1554076 100644 --- a/src/status/libstatus/core.nim +++ b/src/status/libstatus/core.nim @@ -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 diff --git a/src/status/wallet/collectibles.nim b/src/status/wallet/collectibles.nim index 9887667b49..af9ee24d05 100644 --- a/src/status/wallet/collectibles.nim +++ b/src/status/wallet/collectibles.nim @@ -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 = @[]