From 84ea8bf104950c27d8a3793fa6ffb598250414c9 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 29 Oct 2021 14:30:02 -0400 Subject: [PATCH] refactor: add network file to handle adding and changing network --- status/ens.nim | 1 + status/statusgo_backend/network.nim | 49 +++++++++++++++++++++++++++-- status/stickers.nim | 1 + status/tokens.nim | 1 + status/types/network_type.nim | 10 ------ status/wallet.nim | 1 + status/wallet/balance_manager.nim | 1 + status/wallet/collectibles.nim | 1 + status/wallet2/balance_manager.nim | 1 + 9 files changed, 54 insertions(+), 12 deletions(-) diff --git a/status/ens.nim b/status/ens.nim index dcf98f3..85db851 100644 --- a/status/ens.nim +++ b/status/ens.nim @@ -13,6 +13,7 @@ import chronicles, libp2p/[multihash, multicodec, cid] import ./statusgo_backend/eth as eth import ./statusgo_backend/wallet +import ./statusgo_backend/network as status_network import ./statusgo_backend/accounts as status_accounts import ./statusgo_backend/settings as status_settings import ./types/[transaction, setting, rpc_response, network_type, network, profile] diff --git a/status/statusgo_backend/network.nim b/status/statusgo_backend/network.nim index fadf539..111fdaf 100644 --- a/status/statusgo_backend/network.nim +++ b/status/statusgo_backend/network.nim @@ -1,11 +1,15 @@ import - json, chronicles, atomics + json, chronicles, atomics, uuids import json_serialization from ./core import callPrivateRPC from ../types/rpc_response import RpcResponseTyped, RpcException from ../types/network import Network, toPayload +import ../types/network_type + +import ./settings +import ../types/setting logScope: topics = "wallet" @@ -40,4 +44,45 @@ proc upsertNetwork*(network: Network) = proc deleteNetwork*(network: Network) = let payload = %* [network.chainId] discard callPrivateRPC("wallet_deleteEthereumChain", payload) - dirty.store(true) \ No newline at end of file + dirty.store(true) + + +proc toNetwork*(self: NetworkType): Network = + for network in getNetworks(): + if self.toChainId() == network.chainId: + return network + + # Will be removed, this is used in case of legacy chain Id + return Network(chainId: self.toChainId()) + +proc addNetwork*(name: string, endpoint: string, networkId: int, networkType: string) = + var networks = settings.getSetting[JsonNode](Setting.Networks_Networks) + let id = genUUID() + networks.elems.add(%*{ + "id": $genUUID(), + "name": name, + "config": { + "NetworkId": networkId, + "DataDir": "/ethereum/" & networkType, + "UpstreamConfig": { + "Enabled": true, + "URL": endpoint + } + } + }) + discard saveSetting(Setting.Networks_Networks, networks) + +proc changeNetwork*(network: string) = + var statusGoResult = setNetwork(network) + if statusGoResult.error != "": + error "Error saving updated node config", msg=statusGoResult.error + + # remove all installed sticker packs (pack ids do not match across networks) + statusGoResult = saveSetting(Setting.Stickers_PacksInstalled, %* {}) + if statusGoResult.error != "": + error "Error removing all installed sticker packs", msg=statusGoResult.error + + # remove all recent stickers (pack ids do not match across networks) + statusGoResult = saveSetting(Setting.Stickers_Recent, %* {}) + if statusGoResult.error != "": + error "Error removing all recent stickers", msg=statusGoResult.error \ No newline at end of file diff --git a/status/stickers.nim b/status/stickers.nim index 6a8f183..54029cf 100644 --- a/status/stickers.nim +++ b/status/stickers.nim @@ -11,6 +11,7 @@ import # local deps eth/stickers as eth_stickers, transactions, statusgo_backend/wallet, ../eventemitter +import ./statusgo_backend/network as status_network import ./types/[sticker, transaction, rpc_response, network_type, network] logScope: diff --git a/status/tokens.nim b/status/tokens.nim index 7df531d..d0f9011 100644 --- a/status/tokens.nim +++ b/status/tokens.nim @@ -1,5 +1,6 @@ import statusgo_backend/settings as status_settings import statusgo_backend/tokens as statusgo_backend_tokens +import ./statusgo_backend/network import eth/tokens as status_tokens import eth/contracts import ./types/network_type diff --git a/status/types/network_type.nim b/status/types/network_type.nim index ef8cb6f..f43e455 100644 --- a/status/types/network_type.nim +++ b/status/types/network_type.nim @@ -4,8 +4,6 @@ include node_config include network_details include upstream_config -import ../statusgo_backend/network as status_network - import ./network type @@ -27,11 +25,3 @@ proc toChainId*(self: NetworkType): int = of NetworkType.XDai: result = XDai of NetworkType.Poa: result = 99 of NetworkType.Other: result = -1 - -proc toNetwork*(self: NetworkType): Network = - for network in status_network.getNetworks(): - if self.toChainId() == network.chainId: - return network - - # Will be removed, this is used in case of legacy chain Id - return Network(chainId: self.toChainId()) \ No newline at end of file diff --git a/status/wallet.nim b/status/wallet.nim index 44aa214..7fcee31 100644 --- a/status/wallet.nim +++ b/status/wallet.nim @@ -8,6 +8,7 @@ import statusgo_backend/settings as status_settings import statusgo_backend/wallet as status_wallet import statusgo_backend/accounts/constants as constants import statusgo_backend/eth as eth +import statusgo_backend/network as status_network import eth/contracts import eth/tokens as eth_tokens from utils as statusgo_backend_utils import eth2Wei, gwei2Wei, wei2Gwei, first, toUInt64, parseAddress diff --git a/status/wallet/balance_manager.nim b/status/wallet/balance_manager.nim index 712baa1..e9115a9 100644 --- a/status/wallet/balance_manager.nim +++ b/status/wallet/balance_manager.nim @@ -2,6 +2,7 @@ import strformat, strutils, stint, httpclient, json, chronicles, net import ../statusgo_backend/wallet as status_wallet import ../statusgo_backend/eth as eth import ../statusgo_backend/settings as status_settings +import ../statusgo_backend/network import ../eth/tokens as status_tokens import ../types/[rpc_response, network_type] import ../utils/cache diff --git a/status/wallet/collectibles.nim b/status/wallet/collectibles.nim index 451d458..6cbe5e7 100644 --- a/status/wallet/collectibles.nim +++ b/status/wallet/collectibles.nim @@ -13,6 +13,7 @@ import # status-desktop libs ../utils as status_utils, ../types/network_type, web3/[conversions, ethtypes], ../utils, account +import ../statusgo_backend/network const CRYPTOKITTY* = "cryptokitty" const KUDO* = "kudo" diff --git a/status/wallet2/balance_manager.nim b/status/wallet2/balance_manager.nim index eddceaf..ae8a28f 100644 --- a/status/wallet2/balance_manager.nim +++ b/status/wallet2/balance_manager.nim @@ -2,6 +2,7 @@ import strformat, strutils, stint, httpclient, json, chronicles, net import ../statusgo_backend/wallet as status_wallet import ../statusgo_backend/eth as eth import ../statusgo_backend/settings as status_settings +import ../statusgo_backend/network import ../eth/tokens as status_tokens import ../types/[rpc_response, network_type] import ../utils/cache