diff --git a/src/app/chat/core.nim b/src/app/chat/core.nim index cc2e46345c..b64f85b9bb 100644 --- a/src/app/chat/core.nim +++ b/src/app/chat/core.nim @@ -9,10 +9,12 @@ type ChatController* = ref object of SignalSubscriber view*: ChatsView model*: ChatModel variant*: QVariant + appEvents*: EventEmitter -proc newController*(events: EventEmitter): ChatController = +proc newController*(appEvents: EventEmitter): ChatController = result = ChatController() - result.model = newChatModel(events) + result.appEvents = appEvents + result.model = newChatModel() result.view = newChatsView(result.model) result.variant = newQVariant(result.view) diff --git a/src/app/node/core.nim b/src/app/node/core.nim index 6f9f9fa4e0..023b0d7afd 100644 --- a/src/app/node/core.nim +++ b/src/app/node/core.nim @@ -1,23 +1,25 @@ import NimQml import chronicles +import eventemitter import "../../status/core" as status import ../../signals/types +import ../../models/node import view logScope: topics = "node" type NodeController* = ref object of SignalSubscriber + model*: NodeModel view*: NodeView variant*: QVariant + appEvents*: EventEmitter -var sendRPCMessage = proc (msg: string): string = - echo "sending RPC message" - status.callPrivateRPC(msg) - -proc newController*(): NodeController = +proc newController*(appEvents: EventEmitter): NodeController = result = NodeController() - result.view = newNodeView(sendRPCMessage) + result.appEvents = appEvents + result.model = newNodeModel() + result.view = newNodeView(result.model) result.variant = newQVariant(result.view) proc delete*(self: NodeController) = diff --git a/src/app/node/view.nim b/src/app/node/view.nim index c7e6a3e7e1..93b155aa8c 100644 --- a/src/app/node/view.nim +++ b/src/app/node/view.nim @@ -1,17 +1,18 @@ import NimQml +import ../../models/node QtObject: type NodeView* = ref object of QObject + model: NodeModel callResult: string - sendRPCMessage: proc (msg: string): string lastMessage*: string proc setup(self: NodeView) = self.QObject.setup - proc newNodeView*(sendRPCMessage: proc): NodeView = + proc newNodeView*(model: NodeModel): NodeView = new(result) - result.sendRPCMessage = sendRPCMessage + result.model = model result.callResult = "Use this tool to call JSONRPC methods" result.lastMessage = "" result.setup @@ -37,7 +38,7 @@ QtObject: notify = callResultChanged proc onSend*(self: NodeView, inputJSON: string) {.slot.} = - self.setCallResult(self.sendRPCMessage(inputJSON)) + self.setCallResult(self.model.sendRPCMessageRaw(inputJSON)) echo "Done!: ", self.callResult proc onMessage*(self: NodeView, message: string) {.slot.} = diff --git a/src/app/onboarding/core.nim b/src/app/onboarding/core.nim index 90c733fa47..9233730c7c 100644 --- a/src/app/onboarding/core.nim +++ b/src/app/onboarding/core.nim @@ -1,5 +1,5 @@ import NimQml -import ../../models/accounts as Models +import ../../models/accounts import ../../signals/types import eventemitter import view @@ -8,12 +8,16 @@ type OnboardingController* = ref object of SignalSubscriber view*: OnboardingView variant*: QVariant model*: AccountModel + appEvents*: EventEmitter -proc newController*(model: AccountModel): OnboardingController = +proc newController*(appEvents: EventEmitter): OnboardingController = result = OnboardingController() - result.model = model + result.appEvents = appEvents + result.model = newAccountModel() result.view = newOnboardingView(result.model) result.variant = newQVariant(result.view) + result.model.events.on("accountsReady") do(a: Args): + appEvents.emit("accountsReady", a) proc delete*(self: OnboardingController) = delete self.view diff --git a/src/app/profile/core.nim b/src/app/profile/core.nim index 3a47e7360c..9e34e6a7a4 100644 --- a/src/app/profile/core.nim +++ b/src/app/profile/core.nim @@ -1,4 +1,5 @@ import NimQml +import eventemitter import strformat import json import "../../status/core" as status @@ -11,9 +12,11 @@ import ../../models/profile type ProfileController* = ref object of SignalSubscriber view*: ProfileView variant*: QVariant + appEvents*: EventEmitter -proc newController*(): ProfileController = +proc newController*(appEvents: EventEmitter): ProfileController = result = ProfileController() + result.appEvents = appEvents result.view = newProfileView() result.variant = newQVariant(result.view) diff --git a/src/app/wallet/core.nim b/src/app/wallet/core.nim index f00d5aac6b..6a5ce909b2 100644 --- a/src/app/wallet/core.nim +++ b/src/app/wallet/core.nim @@ -12,10 +12,12 @@ type WalletController* = ref object of SignalSubscriber model: WalletModel view*: WalletView variant*: QVariant + appEvents*: EventEmitter -proc newController*(events: EventEmitter): WalletController = +proc newController*(appEvents: EventEmitter): WalletController = result = WalletController() - result.model = newWalletModel(events) + result.appEvents = appEvents + result.model = newWalletModel() result.view = newWalletView(result.model) result.variant = newQVariant(result.view) diff --git a/src/models/chat.nim b/src/models/chat.nim index 22c5857ef5..a506f13117 100644 --- a/src/models/chat.nim +++ b/src/models/chat.nim @@ -18,10 +18,10 @@ type events*: EventEmitter channels*: HashSet[string] -proc newChatModel*(events: EventEmitter): ChatModel = +proc newChatModel*(): ChatModel = result = ChatModel() + result.events = createEventEmitter() result.channels = initHashSet[string]() - result.events = events proc delete*(self: ChatModel) = discard diff --git a/src/models/node.nim b/src/models/node.nim new file mode 100644 index 0000000000..a50cc3bd7e --- /dev/null +++ b/src/models/node.nim @@ -0,0 +1,19 @@ +import eventemitter +# import json +# import strformat +# import strutils +import "../status/core" as status + +type NodeModel* = ref object + events*: EventEmitter + +proc newNodeModel*(): NodeModel = + result = NodeModel() + result.events = createEventEmitter() + +proc delete*(self: NodeModel) = + discard + +proc sendRPCMessageRaw*(self: NodeModel, msg: string): string = + echo "sending RPC message" + status.callPrivateRPCRaw(msg) diff --git a/src/models/wallet.nim b/src/models/wallet.nim index e8ab317895..0abdba3ca5 100644 --- a/src/models/wallet.nim +++ b/src/models/wallet.nim @@ -10,9 +10,9 @@ type Asset* = ref object type WalletModel* = ref object events*: EventEmitter -proc newWalletModel*(events: EventEmitter): WalletModel = +proc newWalletModel*(): WalletModel = result = WalletModel() - result.events = events + result.events = createEventEmitter() proc delete*(self: WalletModel) = discard diff --git a/src/nim_status_client.nim b/src/nim_status_client.nim index 9ba5fd23b3..94ca9b6cd0 100644 --- a/src/nim_status_client.nim +++ b/src/nim_status_client.nim @@ -9,6 +9,7 @@ import app/onboarding/core as onboarding import state import json import status/accounts as status_accounts +import status/core as status_core import status/chat as status_chat import status/types as types import status/libstatus @@ -27,7 +28,7 @@ proc mainProc() = let app = newQApplication() let engine = newQQmlApplicationEngine() let signalController = signals.newController(app) - let events = createEventEmitter() + let appEvents = createEventEmitter() defer: # Defer will run this just before mainProc() function ends app.delete() @@ -41,28 +42,29 @@ proc mainProc() = var appState = state.newAppState() debug "Application State", title=appState.title - var wallet = wallet.newController(events) + var wallet = wallet.newController(appEvents) engine.setRootContextProperty("assetsModel", wallet.variant) - var chat = chat.newController(events) + var chat = chat.newController(appEvents) chat.init() engine.setRootContextProperty("chatsModel", chat.variant) - var node = node.newController() + var node = node.newController(appEvents) node.init() engine.setRootContextProperty("nodeModel", node.variant) - var profile = profile.newController() + var profile = profile.newController(appEvents) engine.setRootContextProperty("profileModel", profile.variant) - var accountsModel = newAccountModel() - accountsModel.events.on("accountsReady") do(a: Args): + # var accountsModel = newAccountModel() + appEvents.on("accountsReady") do(a: Args): var args = AccountArgs(a) - status_chat.startMessenger() + status_core.startMessenger() wallet.init() profile.init(args.account) # TODO: use correct account - var onboarding = onboarding.newController(accountsModel) + # var onboarding = onboarding.newController(accountsModel) + var onboarding = onboarding.newController(appEvents) onboarding.init() engine.setRootContextProperty("onboardingModel", onboarding.variant) @@ -78,7 +80,8 @@ proc mainProc() = chat.load(channel.name) ) - accountsModel.events.on("accountsReady") do(a: Args): + # accountsModel.appEvents.on("accountsReady") do(a: Args): + appEvents.on("accountsReady") do(a: Args): appState.addChannel("test") appState.addChannel("test2") appState.addChannel("status") diff --git a/src/status/accounts.nim b/src/status/accounts.nim index 1c22774343..f457edd0e7 100644 --- a/src/status/accounts.nim +++ b/src/status/accounts.nim @@ -8,15 +8,7 @@ import os import uuids proc queryAccounts*(): string = - var payload = %* { - "jsonrpc": "2.0", - "method": "eth_accounts", - "params": [ - [] - ] - } - var response = callPrivateRPC($payload) - echo response + var response = callPrivateRPC("eth_accounts") result = parseJson(response)["result"][0].getStr() proc generateAddresses*(): string = diff --git a/src/status/chat.nim b/src/status/chat.nim index 50054dc876..0c509df617 100644 --- a/src/status/chat.nim +++ b/src/status/chat.nim @@ -1,79 +1,41 @@ -import libstatus +import core import json import utils -proc startMessenger*() = - let payload = %* { - "jsonrpc": "2.0", - "id": 3, #TODO: - "method": "startMessenger".prefix, - "params": [] - } - discard $libstatus.callPrivateRPC($payload) - # TODO: create template for error handling - proc loadFilters*(chatId: string, oneToOne = false) = - let payload = %* { - "jsonrpc": "2.0", - "id": 3, #TODO: - "method": "loadFilters".prefix, - "params": [ - [{ - "ChatID": chatId, - "OneToOne": oneToOne - }] - ] - } - discard $libstatus.callPrivateRPC($payload) + discard callPrivateRPC("loadFilters".prefix, %* [ + [{ + "ChatID": chatId, + "OneToOne": oneToOne + }] + ]) proc saveChat*(chatId: string, oneToOne = false) = - let payload = %* { - "jsonrpc": "2.0", - "id": 4, - "method": "saveChat".prefix, - "params": [ #TODO: determine where do these values come from - { - "lastClockValue": 0, - "color": "#51d0f0", - "name": chatId, - "lastMessage": nil, - "active": true, - "id": chatId, - "unviewedMessagesCount": 0, - "chatType": if oneToOne: 1 else: 2, - "timestamp": 1588940692659 - } - ] - } - discard $libstatus.callPrivateRPC($payload) + discard callPrivateRPC("saveChat".prefix, %* [ + { + "lastClockValue": 0, + "color": "#51d0f0", + "name": chatId, + "lastMessage": nil, + "active": true, + "id": chatId, + "unviewedMessagesCount": 0, + "chatType": if oneToOne: 1 else: 2, + "timestamp": 1588940692659 + } + ]) proc chatMessages*(chatId: string) = - let payload = %* { - "jsonrpc": "2.0", - "id": 3, #TODO: - "method": "chatMessages".prefix, - "params": [ - chatId, nil, 20 - ] - } - discard $libstatus.callPrivateRPC($payload) - # TODO: create template for error handling - + discard callPrivateRPC("chatMessages".prefix, %* [chatId, nil, 20]) proc sendChatMessage*(chatId: string, msg: string): string = - let payload = %* { - "jsonrpc": "2.0", - "id": 40, - "method": "sendChatMessage".prefix, - "params": [ - { - "chatId": chatId, - "text": msg, - "responseTo": nil, - "ensName": nil, - "sticker": nil, - "contentType": 1 - } - ] - } - $libstatus.callPrivateRPC($payload) + callPrivateRPC("sendChatMessage".prefix, %* [ + { + "chatId": chatId, + "text": msg, + "responseTo": nil, + "ensName": nil, + "sticker": nil, + "contentType": 1 + } + ]) diff --git a/src/status/core.nim b/src/status/core.nim index 3e2a66ffbf..d8f7717d33 100644 --- a/src/status/core.nim +++ b/src/status/core.nim @@ -1,16 +1,30 @@ +import json import libstatus -import chat import nimcrypto - -proc startMessenger*() = - chat.startMessenger() +import utils proc callRPC*(inputJSON: string): string = return $libstatus.callRPC(inputJSON) -proc callPrivateRPC*(inputJSON: string): string = +proc callPrivateRPCRaw*(inputJSON: string): string = return $libstatus.callPrivateRPC(inputJSON) +proc callPrivateRPC*(methodName: string, payload = %* []): string = + try: + let inputJSON = %* { + "jsonrpc": "2.0", + "method": methodName, + "params": %payload + } + echo inputJSON + result = $libstatus.callPrivateRPC($inputJSON) + except: + echo "error doing rpc request" + echo methodName + proc sendTransaction*(inputJSON: string, password: string): string = var hashed_password = "0x" & $keccak_256.digest(password) return $libstatus.sendTransaction(inputJSON, hashed_password) + +proc startMessenger*() = + discard callPrivateRPC("startMessenger".prefix) diff --git a/src/status/wallet.nim b/src/status/wallet.nim index db69da3792..665aa7a803 100644 --- a/src/status/wallet.nim +++ b/src/status/wallet.nim @@ -7,14 +7,7 @@ import stint import strutils proc getAccounts*(): seq[string] = - var payload = %* { - "jsonrpc": "2.0", - "method": "eth_accounts", - "params": [ - [] - ] - } - var response = status.callPrivateRPC($payload) + var response = callPrivateRPC("eth_accounts") result = parseJson(response)["result"].to(seq[string]) proc getAccount*(): string = @@ -31,24 +24,16 @@ proc sendTransaction*(from_address: string, to: string, value: string, password: result = response proc getPrice*(crypto: string, fiat: string): string = - var url: string = fmt"https://min-api.cryptocompare.com/data/price?fsym={crypto}&tsyms={fiat}" - let client = newHttpClient() - client.headers = newHttpHeaders({ "Content-Type": "application/json" }) + var url: string = fmt"https://min-api.cryptocompare.com/data/price?fsym={crypto}&tsyms={fiat}" + let client = newHttpClient() + client.headers = newHttpHeaders({ "Content-Type": "application/json" }) - let response = client.request(url) - $parseJson(response.body)["USD"] + let response = client.request(url) + $parseJson(response.body)["USD"] proc getBalance*(address: string): string = - let payload = %* { - "jsonrpc": "2.0", - "id": 50, - "method": "eth_getBalance", - "params": [ - address, - "latest" - ] - } - parseJson(status.callPrivateRPC($payload))["result"].str + let payload = %* [address, "latest"] + parseJson(status.callPrivateRPC("eth_getBalance", payload))["result"].str proc hex2Eth*(input: string): string = var value = fromHex(Stuint[256], input)