refactor: add network file to handle adding and changing network
This commit is contained in:
parent
76e867f1d8
commit
84ea8bf104
|
@ -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]
|
||||
|
|
|
@ -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)
|
||||
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
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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())
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue