From 19d42792372b5906c366c657c6a4c885d36f0219 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 20 Apr 2021 14:52:09 -0400 Subject: [PATCH] use new wallet api to receive wallet signals --- src/app/onboarding/view.nim | 15 ++++++++ src/app/wallet/core.nim | 14 ++++++-- src/app/wallet/view.nim | 44 +++++++++++++---------- src/nim_status_client.nim | 1 - src/status/libstatus/core.nim | 4 +-- src/status/libstatus/wallet.nim | 25 +++++++------ src/status/signals/wallet.nim | 7 ++-- ui/app/AppLayouts/Wallet/WalletLayout.qml | 16 +++++++++ ui/imports/Constants.qml | 2 ++ ui/onboarding/CreatePasswordModal.qml | 1 + vendor/status-go | 2 +- 11 files changed, 92 insertions(+), 39 deletions(-) diff --git a/src/app/onboarding/view.nim b/src/app/onboarding/view.nim index 926358c460..be83544620 100644 --- a/src/app/onboarding/view.nim +++ b/src/app/onboarding/view.nim @@ -17,6 +17,7 @@ QtObject: currentAccount*: AccountInfoView status*: Status isCurrentFlow*: bool + isFirstTimeLogin*: bool proc setup(self: OnboardingView) = self.QAbstractListModel.setup @@ -125,3 +126,17 @@ QtObject: read = isCurrentFlow write = setCurrentFlow notify = currentFlowChanged + + proc firstTimeLoginChanged*(self: OnboardingView, v: bool) {.signal.} + + proc setFirstTimeLogin*(self: OnboardingView, v: bool) {.slot.} = + if self.isFirstTimeLogin == v: return + self.isFirstTimeLogin = v + self.firstTimeLoginChanged(v) + + proc `isFirstTimeLogin=`*(self: OnboardingView, v: bool) = self.setFirstTimeLogin(v) + + QtProperty[bool] isFirstTimeLogin: + read = isFirstTimeLogin + write = setFirstTimeLogin + notify = firstTimeLoginChanged \ No newline at end of file diff --git a/src/app/wallet/core.nim b/src/app/wallet/core.nim index 25a41a07f7..8a29c26e73 100644 --- a/src/app/wallet/core.nim +++ b/src/app/wallet/core.nim @@ -10,6 +10,9 @@ import ../../status/[status, wallet] import ../../status/wallet/account as WalletTypes import ../../eventemitter +logScope: + topics = "wallet-core" + type WalletController* = ref object status: Status view*: WalletView @@ -58,10 +61,18 @@ proc init*(self: WalletController) = self.view.updateView() # TODO: show notification + + of "new-transfers": + for acc in data.accounts: + self.view.loadTransactionsForAccount(acc) + self.view.initBalances(false) of "recent-history-fetching": self.view.setHistoryFetchState(data.accounts, true) of "recent-history-ready": self.view.setHistoryFetchState(data.accounts, false) + self.view.initBalances(false) + else: + error "Unhandled wallet signal", eventType=data.eventType # TODO: handle these data.eventType: history, reorg # see status-react/src/status_im/ethereum/subscriptions.cljs @@ -72,6 +83,3 @@ proc init*(self: WalletController) = proc checkPendingTransactions*(self: WalletController) = self.status.wallet.checkPendingTransactions() # TODO: consider doing this in a threadpool task - -proc start*(self: WalletController) = - status_wallet.startWallet(false) diff --git a/src/app/wallet/view.nim b/src/app/wallet/view.nim index 527b3f84bc..4f45805957 100644 --- a/src/app/wallet/view.nim +++ b/src/app/wallet/view.nim @@ -136,17 +136,16 @@ const loadTransactionsTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} arg = decode[LoadTransactionsTaskArg](argEncoded) output = %*{ "address": arg.address, - "history": getTransfersByAddress(arg.address, arg.blockNumber) + "history": getTransfersByAddress(arg.address) } arg.finish(output) -proc loadTransactions[T](self: T, slot: string, address: string, blockNumber: string) = +proc loadTransactions[T](self: T, slot: string, address: string) = let arg = LoadTransactionsTaskArg( tptr: cast[ByteAddress](loadTransactionsTask), vptr: cast[ByteAddress](self.vptr), slot: slot, - address: address, - blockNumber: blockNumber + address: address ) self.status.tasks.threadpool.start(arg) @@ -514,11 +513,11 @@ QtObject: self.accounts.forceUpdate() self.setCurrentAssetList(self.currentAccount.account.assetList) - proc initBalances*(self: WalletView) = + proc checkRecentHistory*(self:WalletView) {.slot.} = + var addresses:seq[string] = @[] for acc in self.status.wallet.accounts: - let accountAddress = acc.address - let tokenList = acc.assetList.filter(proc(x:Asset): bool = x.address != "").map(proc(x: Asset): string = x.address) - self.initBalances("getAccountBalanceSuccess", accountAddress, tokenList) + addresses.add(acc.address) + discard status_wallet.checkRecentHistory(addresses) proc getAccountBalanceSuccess*(self: WalletView, jsonResponse: string) {.slot.} = let jsonObj = jsonResponse.parseJson() @@ -526,6 +525,7 @@ QtObject: self.setTotalFiatBalance(self.status.wallet.getTotalFiatBalance()) self.accounts.forceUpdate() self.currentAccountChanged() + self.updateView() proc loadCollectiblesForAccount*(self: WalletView, address: string, currentCollectiblesList: seq[CollectibleList]) = @@ -636,9 +636,6 @@ QtObject: QtProperty[QVariant] customTokenList: read = getCustomTokenList - - - proc isKnownTokenContract*(self: WalletView, address: string): bool {.slot.} = return self.status.wallet.getKnownTokenContract(parseAddress(address)) != nil @@ -665,20 +662,26 @@ QtObject: return self.fetchingHistoryState[address] return true - proc isHistoryFetched*(self: WalletView, address: string): bool {.slot.} = return self.currentTransactions.rowCount() > 0 proc loadingTrxHistoryChanged*(self: WalletView, isLoading: bool) {.signal.} proc loadTransactionsForAccount*(self: WalletView, address: string) {.slot.} = - var bn = "latest" - if self.currentTransactions.rowCount() > 0: - bn = self.currentTransactions.getLastTxBlockNumber() - # spawn'ed function cannot have a 'var' parameter - let blockNumber = bn self.loadingTrxHistoryChanged(true) - self.loadTransactions("setTrxHistoryResult", address, bn) + self.loadTransactions("setTrxHistoryResult", address) + + proc getLatestTransactionHistory*(self: WalletView, accounts: seq[string]) = + for acc in accounts: + self.loadTransactionsForAccount(acc) + + proc initBalances*(self: WalletView, loadTransactions: bool = true) = + for acc in self.status.wallet.accounts: + let accountAddress = acc.address + let tokenList = acc.assetList.filter(proc(x:Asset): bool = x.address != "").map(proc(x: Asset): string = x.address) + self.initBalances("getAccountBalanceSuccess", accountAddress, tokenList) + if loadTransactions: + self.loadTransactionsForAccount(accountAddress) proc setTrxHistoryResult(self: WalletView, historyJSON: string) {.slot.} = let historyData = parseJson(historyJSON) @@ -686,7 +689,7 @@ QtObject: let address = historyData["address"].getStr let index = self.accounts.getAccountindexByAddress(address) if index == -1: return - self.accounts.getAccount(index).transactions.add(transactions) + self.accounts.getAccount(index).transactions = transactions if address == self.currentAccount.address: self.setCurrentTransactions( self.accounts.getAccount(index).transactions) @@ -727,3 +730,6 @@ QtObject: QtProperty[QVariant] dappBrowserAccount: read = getDappBrowserAccount notify = dappBrowserAccountChanged + + proc setInitialRange*(self: WalletView) {.slot.} = + discard status_wallet.setInitialBlocksRange() diff --git a/src/nim_status_client.nim b/src/nim_status_client.nim index feb611a934..0dceabab3b 100644 --- a/src/nim_status_client.nim +++ b/src/nim_status_client.nim @@ -148,7 +148,6 @@ proc mainProc() = browserController.init() wallet.checkPendingTransactions() - wallet.start() engine.setRootContextProperty("loginModel", login.variant) engine.setRootContextProperty("onboardingModel", onboarding.variant) diff --git a/src/status/libstatus/core.nim b/src/status/libstatus/core.nim index 9d8e27fb2c..f773d67d20 100644 --- a/src/status/libstatus/core.nim +++ b/src/status/libstatus/core.nim @@ -47,8 +47,8 @@ proc getContactByID*(id: string): string = proc getBlockByNumber*(blockNumber: string): string = result = callPrivateRPC("eth_getBlockByNumber", %* [blockNumber, false]) -proc getTransfersByAddress*(address: string, toBlock: string, limit: string): string = - result = callPrivateRPC("wallet_getTransfersByAddress", %* [address, toBlock, limit]) +proc getTransfersByAddress*(address: string, limit: string, fetchMore: bool = false): string = + result = callPrivateRPC("wallet_getTransfersByAddress", %* [address, newJNull(), limit, fetchMore]) proc signMessage*(rpcParams: string): string = return $status_go.signMessage(rpcParams) diff --git a/src/status/libstatus/wallet.nim b/src/status/libstatus/wallet.nim index a9d1789233..4868f416d5 100644 --- a/src/status/libstatus/wallet.nim +++ b/src/status/libstatus/wallet.nim @@ -33,12 +33,9 @@ proc getWalletAccounts*(): seq[WalletAccount] = proc getTransactionReceipt*(transactionHash: string): string = result = callPrivateRPC("eth_getTransactionReceipt", %* [transactionHash]) -proc getTransfersByAddress*(address: string, blockNumber: string = "latest"): seq[types.Transaction] = +proc getTransfersByAddress*(address: string): seq[types.Transaction] = try: - let response = getBlockByNumber(blockNumber) - let latestBlock = parseJson(response)["result"] - - let transactionsResponse = getTransfersByAddress(address, latestBlock["number"].getStr, "0x14") + let transactionsResponse = getTransfersByAddress(address, "0x14") let transactions = parseJson(transactionsResponse)["result"] var accountTransactions: seq[types.Transaction] = @[] @@ -59,11 +56,7 @@ proc getTransfersByAddress*(address: string, blockNumber: string = "latest"): se fromAddress: transaction["from"].getStr, to: transaction["to"].getStr )) - # if we feching more trxs, we should skip the first trx as its already saved in the - # existing list - if blockNumber == "latest": - return accountTransactions - return accountTransactions[1 .. ^1] + return accountTransactions except: let msg = getCurrentExceptionMsg() error "Failed getting wallet account transactions", msg @@ -122,3 +115,15 @@ proc getPendingOutboundTransactionsByAddress*(address: string): string = proc deletePendingTransaction*(transactionHash: string) = let payload = %* [transactionHash] discard callPrivateRPC("wallet_deletePendingTransaction", payload) + +proc setInitialBlocksRange*(): string = + let payload = %* [] + result = callPrivateRPC("wallet_setInitialBlocksRange", payload) + +proc watchTransaction*(transactionHash: string): string = + let payload = %* [transactionHash] + result = callPrivateRPC("wallet_watchTransaction", payload) + +proc checkRecentHistory*(addresses: seq[string]): string = + let payload = %* [addresses] + result = callPrivateRPC("wallet_checkRecentHistory", payload) diff --git a/src/status/signals/wallet.nim b/src/status/signals/wallet.nim index 86f9ecded7..bd97136342 100644 --- a/src/status/signals/wallet.nim +++ b/src/status/signals/wallet.nim @@ -3,10 +3,11 @@ import types proc fromEvent*(jsonSignal: JsonNode): Signal = var signal:WalletSignal = WalletSignal() - if jsonSignal["event"].kind != JNull and jsonSignal["event"]{"blockNumber"}.kind != JNull: + signal.content = $jsonSignal + if jsonSignal["event"].kind != JNull: signal.eventType = jsonSignal["event"]["type"].getStr - signal.blockNumber = jsonSignal["event"]["blockNumber"].getInt - signal.erc20 = jsonSignal["event"]["erc20"].getBool + signal.blockNumber = jsonSignal["event"]{"blockNumber"}.getInt + signal.erc20 = jsonSignal["event"]{"erc20"}.getBool signal.accounts = @[] for account in jsonSignal["event"]["accounts"]: signal.accounts.add(account.getStr) diff --git a/ui/app/AppLayouts/Wallet/WalletLayout.qml b/ui/app/AppLayouts/Wallet/WalletLayout.qml index 2d127440a5..222243ce4a 100644 --- a/ui/app/AppLayouts/Wallet/WalletLayout.qml +++ b/ui/app/AppLayouts/Wallet/WalletLayout.qml @@ -17,6 +17,22 @@ ColumnLayout { signPhrasePopup.open(); } } + + Component.onCompleted: { + if(onboardingModel.firstTimeLogin){ + onboardingModel.firstTimeLogin = false + walletModel.setInitialRange() + } + walletModel.checkRecentHistory() + } + + Timer { + id: recentHistoryTimer + interval: Constants.walletFetchRecentHistoryInterval + running: true + repeat: true + onTriggered: walletModel.checkRecentHistory() + } SeedPhraseBackupWarning { } diff --git a/ui/imports/Constants.qml b/ui/imports/Constants.qml index 6366bb7191..b92008812c 100644 --- a/ui/imports/Constants.qml +++ b/ui/imports/Constants.qml @@ -19,6 +19,8 @@ QtObject { readonly property int fetchRangeLast3Days: 259200 readonly property int fetchRangeLast7Days: 604800 + readonly property int walletFetchRecentHistoryInterval: 1200000 // 20 mins + readonly property int limitLongChatText: 500 readonly property int limitLongChatTextCompactMode: 1000 diff --git a/ui/onboarding/CreatePasswordModal.qml b/ui/onboarding/CreatePasswordModal.qml index 550bfbc62d..7311d02e2f 100644 --- a/ui/onboarding/CreatePasswordModal.qml +++ b/ui/onboarding/CreatePasswordModal.qml @@ -153,6 +153,7 @@ ModalPopup { importError.text += error return importError.open() } + onboardingModel.firstTimeLogin = true } } } diff --git a/vendor/status-go b/vendor/status-go index 8a370c1c41..fd49876a47 160000 --- a/vendor/status-go +++ b/vendor/status-go @@ -1 +1 @@ -Subproject commit 8a370c1c41bfe953ee34f841593952c6391d771b +Subproject commit fd49876a475d6366c1d915933048f1f916f78b70