status-desktop/src/nim_status_client.nim

118 lines
3.6 KiB
Nim
Raw Normal View History

2020-05-06 17:40:00 +00:00
import NimQml
import applicationView
2020-05-15 12:27:29 +00:00
import chat/chatView
import wallet/assetsView
2020-05-13 19:14:35 +00:00
import json
import state
2020-05-13 19:14:35 +00:00
import status/utils
import strformat
import strutils
2020-05-13 15:37:12 +00:00
import status/core as status
import status/chat as status_chat
2020-05-13 15:37:12 +00:00
import status/test as status_test
2020-05-13 20:20:50 +00:00
import status/types as types
import status/wallet as status_wallet
2020-05-06 17:40:00 +00:00
proc mainProc() =
2020-05-10 23:24:06 +00:00
# From QT docs:
# For any GUI application using Qt, there is precisely one QApplication object,
# no matter whether the application has 0, 1, 2 or more windows at any given time.
# For non-QWidget based Qt applications, use QGuiApplication instead, as it does
2020-05-10 23:24:06 +00:00
# not depend on the QtWidgets library. Use QCoreApplication for non GUI apps
var app = newQApplication()
defer: app.delete() # Defer will run this just before mainProc() function ends
2020-05-06 17:40:00 +00:00
2020-05-13 20:20:50 +00:00
var appState = state.newAppState()
echo appState.title
var chatsModel = newChatsModel();
defer: chatsModel.delete
2020-05-14 20:04:38 +00:00
var assetsModel = newAssetsModel();
defer: assetsModel.delete
2020-05-06 17:40:00 +00:00
var engine = newQQmlApplicationEngine()
defer: engine.delete()
2020-05-13 20:20:50 +00:00
status.init(appState)
2020-05-13 15:37:12 +00:00
status_test.setupNewAccount()
discard status_test.addPeer("enode://2c8de3cbb27a3d30cbb5b3e003bc722b126f5aef82e2052aaef032ca94e0c7ad219e533ba88c70585ebd802de206693255335b100307645ab5170e88620d2a81@47.244.221.14:443")
echo status.callPrivateRPC("{\"jsonrpc\":\"2.0\", \"method\":\"wakuext_requestMessages\", \"params\":[{\"topics\": [\"0x7998f3c8\"]}], \"id\": 1}")
# 1. get balance of an address
var balance = status_wallet.getBalance("0x0000000000000000000000000000000000000000")
echo(fmt"balance in hex: {balance}")
# 2. convert balance to eth
var eth_value = status_wallet.hex2Eth(balance)
echo(fmt"balance in eth: {eth_value}")
# 3. get usd price of 1 eth
var usd_eth_price = status_wallet.getPrice("ETH", "USD")
echo(fmt"usd_price: {usd_eth_price}")
# 4. convert balance to usd
var usd_balance = parseFloat(eth_value) * parseFloat(usd_eth_price)
echo(fmt"balance in usd: {usd_balance}")
# result.accountResult = status.queryAccounts()
2020-05-13 19:14:35 +00:00
var sendMessage = proc (msg: string): string =
status_chat.sendPublicChatMessage("test", msg)
2020-05-13 19:14:35 +00:00
let logic = newApplicationView(app, sendMessage)
2020-05-10 23:24:06 +00:00
defer: logic.delete
2020-05-06 17:40:00 +00:00
let logicVariant = newQVariant(logic)
defer: logicVariant.delete
let chatsVariant = newQVariant(chatsModel)
defer: chatsVariant.delete
appState.subscribe(proc () =
chatsModel.names = @[]
for channel in appState.channels:
echo channel.name
chatsModel.addNameTolist(channel.name)
)
2020-05-14 20:04:38 +00:00
let assetsVariant = newQVariant(assetsModel)
defer: chatsVariant.delete
2020-05-13 17:46:12 +00:00
status.startMessenger()
2020-05-13 20:20:50 +00:00
appState.addChannel("test")
appState.addChannel("test2")
2020-05-06 17:40:00 +00:00
engine.setRootContextProperty("logic", logicVariant)
engine.setRootContextProperty("chatsModel", chatsVariant)
2020-05-14 20:04:38 +00:00
engine.setRootContextProperty("assetsModel", assetsVariant)
2020-05-14 20:11:27 +00:00
let symbol = "ETH"
assetsModel.addAssetToList("Ethereum", symbol, fmt"{eth_value:.6}", "$" & fmt"{usd_balance:.6}", fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg")
2020-05-13 17:46:12 +00:00
engine.load("../ui/main.qml")
2020-05-13 20:20:50 +00:00
# EXAMPLE: this will be triggered once a message is received
appState.onSignal(SignalType.Message, proc(myMessage: string): void =
echo "I received a message: ", myMessage
);
# Handle signals as part of the state
var signalWorker: Thread[AppState]
signalWorker.createThread(proc(s:AppState) = s.processSignals, appState)
defer: signalWorker.joinThread()
2020-05-10 23:24:06 +00:00
# Qt main event loop is entered here
# The termination of the loop will be performed when exit() or quit() is called
2020-05-06 17:40:00 +00:00
app.exec()
2020-05-13 20:20:50 +00:00
2020-05-06 17:40:00 +00:00
when isMainModule:
mainProc()
GC_fullcollect()