feat: saved addresses CRUD

This commit is contained in:
Eric Mastro 2021-09-09 18:08:55 +10:00 committed by Iuri Matias
parent 219238a952
commit bfe24d02ea
5 changed files with 139 additions and 5 deletions

View File

@ -4,7 +4,7 @@ import
import json_serialization
from ./core import callPrivateRPC
from ../types/rpc_response import RpcResponse, RpcException
from ../types/rpc_response import RpcResponseTyped, RpcException
from ../wallet2/network import Network, toPayload
logScope:
@ -24,11 +24,11 @@ proc getNetworks*(useCached: bool = true): seq[Network] =
else:
let payload = %* [false]
let responseStr = callPrivateRPC("wallet_getEthereumChains", payload)
let response = RpcResponse(result: $(responseStr.parseJSON()["result"]))
let response = Json.decode(responseStr, RpcResponseTyped[JsonNode])
if not response.error.isNil:
raise newException(RpcException, "Error getting networks: " & response.error.message)
result = if response.result == "null": @[] else: Json.decode(response.result, seq[Network])
result = if response.result.isNil or response.result.kind == JNull: @[]
else: Json.decode($response.result, seq[Network])
dirty.store(false)
networks = result
netowrksInited = true

View File

@ -2,7 +2,7 @@ import json, json, options, json_serialization, stint, chronicles
import core, conversions, ../types/[transaction, rpc_response], ../utils, strutils, strformat
from status_go import validateMnemonic#, startWallet
import ../wallet/account
import web3/ethtypes
import web3/conversions as web3_conversions, web3/ethtypes
proc getWalletAccounts*(): seq[WalletAccount] =
try:
@ -161,3 +161,21 @@ proc feeHistory*(n: int): string =
proc getGasPrice*(): string =
let payload = %* []
result = callPrivateRPC("eth_gasPrice", payload)
proc addSavedAddress*(name, address: string): string =
let
payload = %* [{"name": name, "address": address}]
jsonRaw = callPrivateRPC("wallet_addSavedAddress", payload)
jsonRaw
proc deleteSavedAddress*(address: string): string =
let
payload = %* [address]
jsonRaw = callPrivateRPC("wallet_deleteSavedAddress", payload)
jsonRaw
proc getSavedAddresses*(): string =
let
payload = %* []
jsonRaw = callPrivateRPC("wallet_getSavedAddresses", payload)
jsonRaw

9
status/types/address.nim Normal file
View File

@ -0,0 +1,9 @@
import
web3/[conversions, ethtypes]
export conversions, ethtypes
type SavedAddress* = ref object
name*: string
address*: Address
chainId*: int

View File

@ -0,0 +1,54 @@
import
json_serialization, json_serialization/lexer, stew/results, web3/conversions
export
results
proc writeValue*[V, E](writer: var JsonWriter, value: Result[V, E]) =
writer.beginRecord(type(value))
writer.writeField("o", value.isOk)
if value.isOk:
when V isnot void:
writer.writeField("v", value.get)
else:
writer.writeField("e", value.error)
writer.endRecord()
proc readValue*[V, E](reader: var JsonReader, value: var Result[V, E]) =
# {"o":true} - void value type
# {"o":true, "v":"some val"} - string value type
# {"o":false, "e":0} - if the error type is an enum, will be an int
reader.skipToken tkCurlyLe
let oName = reader.readValue(string)
if oName != "o":
reader.raiseUnexpectedValue("Missing field 'o'")
reader.skipToken tkColon
let o = reader.readValue(bool)
if o:
when V is void:
value = Result[V, E].ok()
when V isnot void:
reader.skipToken tkComma
let vName = reader.readValue(string)
if vName != "v":
reader.raiseUnexpectedValue("Missing field 'v'")
reader.skipToken tkColon
let v = reader.readValue(V)
value = Result[V, E].ok v
else:
reader.skipToken tkComma
let eName = reader.readValue(string)
if eName != "e":
reader.raiseUnexpectedValue("Missing field 'e'")
reader.skipToken tkColon
let e = reader.readValue(E)
value = Result[V, E].err e
reader.skipToken tkCurlyRi

View File

@ -0,0 +1,53 @@
import
std/json
import
chronicles, json_serialization, stew/results
import # modules
../types/[address, conversions, rpc_response], ../libstatus/wallet
export address, results
type
SavedAddressError* = enum
CreateSavedAddressError = "error creating saved address"
DeleteSavedAddressError = "error deleting saved address"
ParseAddressError = "error parsing address"
ReadSavedAddressesError = "error reading saved addresses"
UpdateSavedAddressError = "error updating saved address"
SavedAddressResult*[T] = Result[T, SavedAddressError]
proc addSavedAddress*(savedAddress: SavedAddress): SavedAddressResult[void] =
let
rpcResponseRaw = wallet.addSavedAddress(
savedAddress.name, $savedAddress.address)
rpcResponse = Json.decode(rpcResponseRaw, RpcResponseTyped[JsonNode])
if not rpcResponse.error.isNil:
error "error creating saved address", error = rpcResponse.error.message
return err SavedAddressError.CreateSavedAddressError
return ok()
proc deleteSavedAddress*(address: Address): SavedAddressResult[void] =
let
rpcResponseRaw = wallet.deleteSavedAddress($address)
rpcResponse = Json.decode(rpcResponseRaw, RpcResponseTyped[JsonNode])
if not rpcResponse.error.isNil:
error "error deleting saved address", error = rpcResponse.error.message
return err SavedAddressError.DeleteSavedAddressError
return ok()
proc editSavedAddress*(savedAddress: SavedAddress): SavedAddressResult[void] =
return savedAddress.addSavedAddress
proc getSavedAddresses*(): SavedAddressResult[seq[SavedAddress]] =
let
rpcResponseRaw = wallet.getSavedAddresses()
rpcResponse = Json.decode(rpcResponseRaw, RpcResponseTyped[JsonNode])
if not rpcResponse.error.isNil:
error "error getting saved addresses", error = rpcResponse.error.message
return err SavedAddressError.ReadSavedAddressesError
if rpcResponse.result.isNil or rpcResponse.result.kind == JNull:
return ok newSeq[SavedAddress]()
return ok Json.decode($rpcResponse.result, seq[SavedAddress])