From ab029aa56f2caa6eb3af6efade7888531cbde1a0 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 18 Nov 2021 10:45:17 -0500 Subject: [PATCH] refactor: add new module for stickers (#107) --- status/statusgo_backend_new/core.nim | 12 +++++- status/statusgo_backend_new/ens.nim | 4 +- status/statusgo_backend_new/eth.nim | 8 +++- status/statusgo_backend_new/stickers.nim | 43 ++++++++++++++++++++ status/statusgo_backend_new/transactions.nim | 5 ++- 5 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 status/statusgo_backend_new/stickers.nim diff --git a/status/statusgo_backend_new/core.nim b/status/statusgo_backend_new/core.nim index b9f143a..954dfec 100644 --- a/status/statusgo_backend_new/core.nim +++ b/status/statusgo_backend_new/core.nim @@ -1,4 +1,4 @@ -import json, json_serialization, strformat, chronicles +import json, json_serialization, strformat, chronicles, nimcrypto import status_go import response_type @@ -43,3 +43,13 @@ proc signMessage*(rpcParams: string): string = proc signTypedData*(data: string, address: string, password: string): string = return $status_go.signTypedData(data, address, password) + +proc sendTransaction*(inputJSON: string, password: string): RpcResponse[JsonNode] + {.raises: [RpcException, ValueError, Defect, SerializationError].} = + try: + var hashed_password = "0x" & $keccak_256.digest(password) + let rpcResponseRaw = status_go.sendTransaction(inputJSON, hashed_password) + result = Json.decode(rpcResponseRaw, RpcResponse[JsonNode]) + except RpcException as e: + error "error sending tx", inputJSON, exception=e.msg + raise newException(RpcException, e.msg) \ No newline at end of file diff --git a/status/statusgo_backend_new/ens.nim b/status/statusgo_backend_new/ens.nim index 9055095..f384f73 100644 --- a/status/statusgo_backend_new/ens.nim +++ b/status/statusgo_backend_new/ens.nim @@ -36,7 +36,7 @@ proc resolver*(usernameHash: string): string = "data": fmt"{resolver_signature}{userNameHash}" }, "latest"] - var resolverAddr = eth.call(payload).result.getStr() + var resolverAddr = eth.doEthCall(payload).result.getStr() resolverAddr.removePrefix("0x000000000000000000000000") result = "0x" & resolverAddr @@ -51,7 +51,7 @@ proc contenthash*(ensAddr: string): string = "data": fmt"{contenthash_signature}{ensHash}" }, "latest"] - let bytesResponse = eth.call(payload).result.getStr() + let bytesResponse = eth.doEthCall(payload).result.getStr() if bytesResponse == "0x": return "" diff --git a/status/statusgo_backend_new/eth.nim b/status/statusgo_backend_new/eth.nim index 1db66cf..c7af8d2 100644 --- a/status/statusgo_backend_new/eth.nim +++ b/status/statusgo_backend_new/eth.nim @@ -22,5 +22,9 @@ proc getTokenBalance*(tokenAddress: string, accountAddress: string): RpcResponse }, "latest"] return core.callPrivateRPC("eth_call", payload) -proc call*(payload = %* []): RpcResponse[JsonNode] {.raises: [Exception].} = - return core.callPrivateRPC("eth_call", payload) +proc sendTransaction*(transactionData: string, password: string): RpcResponse[JsonNode] {.raises: [Exception].} = + core.sendTransaction(transactionData, password) + +# This is the replacement of the `call` function +proc doEthCall*(payload = %* []): RpcResponse[JsonNode] {.raises: [Exception].} = + core.callPrivateRPC("eth_call", payload) \ No newline at end of file diff --git a/status/statusgo_backend_new/stickers.nim b/status/statusgo_backend_new/stickers.nim new file mode 100644 index 0000000..1e3a28a --- /dev/null +++ b/status/statusgo_backend_new/stickers.nim @@ -0,0 +1,43 @@ +import json +import ../statusgo_backend_new/eth as eth + + +# Retrieves number of sticker packs owned by user +# See https://notes.status.im/Q-sQmQbpTOOWCQcYiXtf5g#Read-Sticker-Packs-owned-by-a-user +# for more details +proc getBalance*(address: string, data: string): RpcResponse[JsonNode] {.raises: [Exception].} = + let payload = %* [{ + "to": address, + "data": data + }, "latest"] + + let response = eth.doEthCall(payload) + + if not response.error.isNil: + raise newException(RpcException, "Error getting stickers balance: " & response.error.message) + + return response + +proc tokenOfOwnerByIndex*(address: string, data: string): RpcResponse[JsonNode] {.raises: [Exception].} = + let payload = %* [{ + "to": address, + "data": data + }, "latest"] + + let response = eth.doEthCall(payload) + if not response.error.isNil: + raise newException(RpcException, "Error getting owned tokens: " & response.error.message) + + return response + +proc getPackIdFromTokenId*(address: string, data: string): RpcResponse[JsonNode] {.raises: [Exception].} = + let payload = %* [{ + "to": address, + "data": data + }, "latest"] + + let response = eth.doEthCall(payload) + if not response.error.isNil: + raise newException(RpcException, "Error getting pack id from token id: " & response.error.message) + + return response diff --git a/status/statusgo_backend_new/transactions.nim b/status/statusgo_backend_new/transactions.nim index db3f241..e91ed62 100644 --- a/status/statusgo_backend_new/transactions.nim +++ b/status/statusgo_backend_new/transactions.nim @@ -14,4 +14,7 @@ proc getTransfersByAddress*(address: string, toBlock: Uint256, limit: int, loadM limitParsed = "0x" & limit.toHex.stripLeadingZeros callPrivateRPC("wallet_getTransfersByAddress", %* [address, toBlockParsed, limitParsed, loadMore]) - \ No newline at end of file + +proc trackPendingTransaction*(hash: string, fromAddress: string, toAddress: string, trxType: PendingTransactionType, data: string): RpcResponse[JsonNode] {.raises: [Exception].} = + let payload = %* [{"hash": hash, "from": fromAddress, "to": toAddress, "type": $trxType, "additionalData": data, "data": "", "value": 0, "timestamp": 0, "gasPrice": 0, "gasLimit": 0}] + callPrivateRPC("wallet_storePendingTransaction", payload) \ No newline at end of file