From 07593887107520c770da70fc42a597cf275ea68e Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 20 Oct 2021 15:25:52 +0200 Subject: [PATCH] Refactor/wallet 1 (#89) * refactor: add custom token new backend * refactor: add transactions new backend * refactor: add collectible new backend * refactor: add accounts backend * refactor: add settings backend * refactor: Add eth call to fetch balance * refactor: add call to get eth block --- status/statusgo_backend_new/accounts.nim | 9 ++++++--- status/statusgo_backend_new/collectibles.nim | 14 +++++++++++++ status/statusgo_backend_new/custom_tokens.nim | 15 ++++++++++++++ status/statusgo_backend_new/eth.nim | 20 +++++++++++++++++++ status/statusgo_backend_new/settings.nim | 7 +++++++ status/statusgo_backend_new/transactions.nim | 7 +++++++ 6 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 status/statusgo_backend_new/collectibles.nim create mode 100644 status/statusgo_backend_new/custom_tokens.nim create mode 100644 status/statusgo_backend_new/eth.nim create mode 100644 status/statusgo_backend_new/settings.nim create mode 100644 status/statusgo_backend_new/transactions.nim diff --git a/status/statusgo_backend_new/accounts.nim b/status/statusgo_backend_new/accounts.nim index befe13e..82e4f72 100644 --- a/status/statusgo_backend_new/accounts.nim +++ b/status/statusgo_backend_new/accounts.nim @@ -1,6 +1,6 @@ import json, json_serialization, chronicles -import core, utils -import response_type +import ./core, ./utils +import ./response_type import status_go @@ -12,6 +12,9 @@ logScope: const NUMBER_OF_ADDRESSES_TO_GENERATE = 5 const MNEMONIC_PHRASE_LENGTH = 12 +proc getAccounts*(): RpcResponse[JsonNode] {.raises: [Exception].} = + return core.callPrivateRPC("accounts_getAccounts") + proc generateAddresses*(paths: seq[string]): RpcResponse[JsonNode] {.raises: [Exception].} = let payload = %* { "n": NUMBER_OF_ADDRESSES_TO_GENERATE, @@ -138,4 +141,4 @@ proc login*(name, keyUid, hashedPassword, identicon, thumbnail, large: string): except RpcException as e: error "error doing rpc request", methodName = "login", exception=e.msg - raise newException(RpcException, e.msg) \ No newline at end of file + raise newException(RpcException, e.msg) diff --git a/status/statusgo_backend_new/collectibles.nim b/status/statusgo_backend_new/collectibles.nim new file mode 100644 index 0000000..f9cc796 --- /dev/null +++ b/status/statusgo_backend_new/collectibles.nim @@ -0,0 +1,14 @@ +import json +import ./core, ./response_type + +export response_type + +proc getOpenseaCollections*(chainId: int, address: string): RpcResponse[JsonNode] = + let payload = %* [chainId, address] + return callPrivateRPC("wallet_getOpenseaCollectionsByOwner", payload) + +proc getOpenseaAssets*( + chainId: int, address: string, collectionSlug: string, limit: int +): RpcResponse[JsonNode] = + let payload = %* [chainId, address, collectionSlug, limit] + return callPrivateRPC("wallet_getOpenseaAssetsByOwnerAndCollection", payload) \ No newline at end of file diff --git a/status/statusgo_backend_new/custom_tokens.nim b/status/statusgo_backend_new/custom_tokens.nim new file mode 100644 index 0000000..c494b15 --- /dev/null +++ b/status/statusgo_backend_new/custom_tokens.nim @@ -0,0 +1,15 @@ +import json +import ./core, ./response_type + +export response_type + +proc getCustomTokens*(): RpcResponse[JsonNode] {.raises: [Exception].} = + return callPrivateRPC("wallet_getCustomTokens", %* []) + +proc addCustomToken*(address: string, name: string, symbol: string, decimals: int, color: string) {.raises: [Exception].} = + discard callPrivateRPC("wallet_addCustomToken", %* [ + {"address": address, "name": name, "symbol": symbol, "decimals": decimals, "color": color} + ]) + +proc removeCustomToken*(address: string) {.raises: [Exception].} = + discard callPrivateRPC("wallet_deleteCustomToken", %* [address]) diff --git a/status/statusgo_backend_new/eth.nim b/status/statusgo_backend_new/eth.nim new file mode 100644 index 0000000..e72264b --- /dev/null +++ b/status/statusgo_backend_new/eth.nim @@ -0,0 +1,20 @@ +import json, strutils, strformat +import ./core, ./response_type + +export response_type + +proc getBlockByNumber*(blockNumber: string): RpcResponse[JsonNode] {.raises: [Exception].} = + let payload = %* [blockNumber, false] + return core.callPrivateRPC("eth_getBlockByNumber", payload) + +proc getEthBalance*(address: string): RpcResponse[JsonNode] {.raises: [Exception].} = + let payload = %* [address, "latest"] + return core.callPrivateRPC("eth_getBalance", payload) + +proc getTokenBalance*(tokenAddress: string, accountAddress: string): RpcResponse[JsonNode] {.raises: [Exception].} = + var postfixedAccount: string = accountAddress + postfixedAccount.removePrefix("0x") + let payload = %* [{ + "to": tokenAddress, "from": accountAddress, "data": fmt"0x70a08231000000000000000000000000{postfixedAccount}" + }, "latest"] + return core.callPrivateRPC("eth_call", payload) \ No newline at end of file diff --git a/status/statusgo_backend_new/settings.nim b/status/statusgo_backend_new/settings.nim new file mode 100644 index 0000000..b9d1a2d --- /dev/null +++ b/status/statusgo_backend_new/settings.nim @@ -0,0 +1,7 @@ +import json +import ./core, ./response_type + +export response_type + +proc getSettings*(): RpcResponse[JsonNode] {.raises: [Exception].} = + return core.callPrivateRPC("settings_getSettings") \ No newline at end of file diff --git a/status/statusgo_backend_new/transactions.nim b/status/statusgo_backend_new/transactions.nim new file mode 100644 index 0000000..0c3bb12 --- /dev/null +++ b/status/statusgo_backend_new/transactions.nim @@ -0,0 +1,7 @@ +import json + +import ./core + +proc checkRecentHistory*(addresses: seq[string]) {.raises: [Exception].} = + let payload = %* [addresses] + discard callPrivateRPC("wallet_checkRecentHistory", payload) \ No newline at end of file