Skip to content

Commit

Permalink
feat(dapps) attempt to fix the wallet connect abstraction
Browse files Browse the repository at this point in the history
Spent too much time figuring out the puzzle of
service->module->view->QML just to call a
status-go function.

Keeping this attempt for later while moving to a
simplified Controller/Provider approach. I will
come back to the abstraction when we add tests
to use it.

Updates: #14615
  • Loading branch information
stefandunca committed May 30, 2024
1 parent ea2b6b2 commit 07bc6c4
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/app/boot/app_controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ proc newAppController*(statusFoundation: StatusFoundation): AppController =
result.generalService,
result.keycardService,
result.networkConnectionService,
result.sharedUrlsService
result.sharedUrlsService,
statusFoundation.threadpool
)

# Do connections
Expand Down
7 changes: 5 additions & 2 deletions src/app/modules/main/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ import ../../core/custom_urls/urls_manager

export io_interface

import app/core/tasks/threadpool

const TOAST_MESSAGE_VISIBILITY_DURATION_IN_MS = 5000 # 5 seconds
const STATUS_URL_ENS_RESOLVE_REASON = "StatusUrl"

Expand Down Expand Up @@ -165,7 +167,8 @@ proc newModule*[T](
generalService: general_service.Service,
keycardService: keycard_service.Service,
networkConnectionService: network_connection_service.Service,
sharedUrlsService: urls_service.Service
sharedUrlsService: urls_service.Service,
threadpool: ThreadPool
): Module[T] =
result = Module[T]()
result.delegate = delegate
Expand Down Expand Up @@ -212,7 +215,7 @@ proc newModule*[T](
transactionService, walletAccountService,
settingsService, savedAddressService, networkService, accountsService,
keycardService, nodeService, networkConnectionService, devicesService,
communityTokensService
communityTokensService, threadpool
)
result.browserSectionModule = browser_section_module.newModule(
result, events, bookmarkService, settingsService, networkService,
Expand Down
19 changes: 13 additions & 6 deletions src/app/modules/main/wallet_section/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import ./send/module as send_module

import ./activity/controller as activityc
import ./activity/details_controller as activity_detailsc
import ./poc_wallet_connect/controller as wcc

import app/modules/shared_modules/collectible_details/controller as collectible_detailsc

import app/global/global_singleton
import app/core/eventemitter
import app/modules/shared_modules/add_account/module as add_account_module
import app/modules/shared_modules/keypair_import/module as keypair_import_module
import app/modules/shared_modules/wallet_connect/module as wc_module
import app_service/service/keycard/service as keycard_service
import app_service/service/token/service as token_service
import app_service/service/collectible/service as collectible_service
Expand All @@ -38,9 +38,12 @@ import app_service/service/node/service as node_service
import app_service/service/network_connection/service as network_connection_service
import app_service/service/devices/service as devices_service
import app_service/service/community_tokens/service as community_tokens_service
import app_service/service/wallet_connect/service as wc_service

import backend/collectibles as backend_collectibles

import app/core/tasks/threadpool

logScope:
topics = "wallet-section-module"

Expand All @@ -58,6 +61,7 @@ type
# shared modules
addAccountModule: add_account_module.AccessInterface
keypairImportModule: keypair_import_module.AccessInterface
walletConnectModule: wc_module.AccessInterface
# modules
accountsModule: accounts_module.AccessInterface
allTokensModule: all_tokens_module.AccessInterface
Expand All @@ -75,6 +79,7 @@ type
walletAccountService: wallet_account_service.Service
savedAddressService: saved_address_service.Service
devicesService: devices_service.Service
walletConnectService: wc_service.Service

activityController: activityc.Controller
collectibleDetailsController: collectible_detailsc.Controller
Expand All @@ -85,7 +90,7 @@ type
tmpActivityControllers: ActivityControllerArray
activityDetailsController: activity_detailsc.Controller

wcController: wcc.Controller
threadpool: ThreadPool

## Forward declaration
proc onUpdatedKeypairsOperability*(self: Module, updatedKeypairs: seq[KeypairDto])
Expand All @@ -107,7 +112,8 @@ proc newModule*(
nodeService: node_service.Service,
networkConnectionService: network_connection_service.Service,
devicesService: devices_service.Service,
communityTokensService: community_tokens_service.Service
communityTokensService: community_tokens_service.Service,
threadpool: ThreadPool
): Module =
result = Module()
result.delegate = delegate
Expand All @@ -119,6 +125,7 @@ proc newModule*(
result.devicesService = devicesService
result.moduleLoaded = false
result.controller = newController(result, settingsService, walletAccountService, currencyService, networkService)
result.threadpool = threadpool

result.accountsModule = accounts_module.newModule(result, events, walletAccountService, networkService, currencyService)
result.allTokensModule = all_tokens_module.newModule(result, events, tokenService, walletAccountService, settingsService, communityTokensService)
Expand Down Expand Up @@ -160,9 +167,10 @@ proc newModule*(
result.collectibleDetailsController = collectible_detailsc.newController(int32(backend_collectibles.CollectiblesRequestID.WalletAccount), networkService, events)
result.filter = initFilter(result.controller)

result.wcController = wcc.newController(events, walletAccountService)
result.walletConnectService = wc_service.newService(result.events, result.threadpool)
result.walletConnectModule = wc_module.newModule(result, result.events, result.walletAccountService, result.walletConnectService)

result.view = newView(result, result.activityController, result.tmpActivityControllers, result.activityDetailsController, result.collectibleDetailsController, result.wcController)
result.view = newView(result, result.activityController, result.tmpActivityControllers, result.activityDetailsController, result.collectibleDetailsController, result.walletConnectModule)

method delete*(self: Module) =
self.accountsModule.delete
Expand All @@ -179,7 +187,6 @@ method delete*(self: Module) =
self.tmpActivityControllers[i].delete
self.activityDetailsController.delete
self.collectibleDetailsController.delete
self.wcController.delete

if not self.addAccountModule.isNil:
self.addAccountModule.delete
Expand Down
21 changes: 12 additions & 9 deletions src/app/modules/main/wallet_section/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import ./activity/controller as activityc
import ./activity/details_controller as activity_detailsc
import app/modules/shared_modules/collectible_details/controller as collectible_detailsc
import ./io_interface
import ../../shared_models/currency_amount
import ./poc_wallet_connect/controller as wcc
import app/modules/shared_models/currency_amount
import app/modules/shared_modules/wallet_connect/io_interface as wc_module

type
ActivityControllerArray* = array[2, activityc.Controller]
Expand All @@ -25,7 +25,7 @@ QtObject:
collectibleDetailsController: collectible_detailsc.Controller
isNonArchivalNode: bool
keypairOperabilityForObservedAccount: string
wcController: wcc.Controller
wcModule: wc_module.AccessInterface
walletReady: bool
addressFilters: string
currentCurrency: string
Expand All @@ -41,14 +41,14 @@ QtObject:
tmpActivityControllers: ActivityControllerArray,
activityDetailsController: activity_detailsc.Controller,
collectibleDetailsController: collectible_detailsc.Controller,
wcController: wcc.Controller): View =
wcModule: wc_module.AccessInterface): View =
new(result, delete)
result.delegate = delegate
result.activityController = activityController
result.tmpActivityControllers = tmpActivityControllers
result.activityDetailsController = activityDetailsController
result.collectibleDetailsController = collectibleDetailsController
result.wcController = wcController
result.wcModule = wcModule

result.setup()

Expand Down Expand Up @@ -236,10 +236,13 @@ QtObject:
proc emitDestroyKeypairImportPopup*(self: View) =
self.destroyKeypairImportPopup()

proc getWalletConnectController(self: View): QVariant {.slot.} =
return newQVariant(self.wcController)
QtProperty[QVariant] walletConnectController:
read = getWalletConnectController
proc getWalletConnectModule(self: View): QVariant {.slot.} =
if self.wcModule == nil:
return newQVariant()
return self.wcModule.getModuleAsVariant()

QtProperty[QVariant] walletConnectModule:
read = getWalletConnectModule

proc walletReadyChanged*(self: View) {.signal.}

Expand Down
9 changes: 5 additions & 4 deletions src/app/modules/shared_modules/wallet_connect/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ proc newController*(delegate: io_interface.AccessInterface,
result.walletAccountService = walletAccountService
result.walletConnectService = walletConnectService

proc delete*(self: Controller) =
self.disconnectAll()

proc init*(self: Controller) =
discard
discard

proc addWalletConnectSession*(self: Controller, session_json: string): bool =
echo "@ddd Controller.addWalletConnectSession", session_json.len
return self.walletConnectService.addSession(session_json)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ method delete*(self: AccessInterface) {.base.} =
method getModuleAsVariant*(self: AccessInterface): QVariant {.base.} =
raise newException(ValueError, "No implementation available")

method addWalletConnectSession*(self: AccessInterface, session_json: string): bool {.base.} =
raise newException(ValueError, "No implementation available")

type
DelegateInterface* = concept c
8 changes: 5 additions & 3 deletions src/app/modules/shared_modules/wallet_connect/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type
controller: Controller

proc newModule*[T](delegate: T,
uniqueIdentifier: string,
events: EventEmitter,
walletAccountService: wallet_account_service.Service,
walletConnectService: wallet_connect_service.Service):
Expand All @@ -30,14 +29,17 @@ proc newModule*[T](delegate: T,
result.delegate = delegate
result.view = view.newView(result)
result.viewVariant = newQVariant(result.view)
result.controller = controller.newController(result, walletAccountService, walletConnectService)
result.controller = controller.newController(result, events, walletAccountService, walletConnectService)

proc addWalletConnectSession*[T](self: Module[T], session_json: string): bool =
echo "@dd Module.addWalletConnectSession: ", session_json
return self.controller.addWalletConnectSession(session_json)

{.push warning[Deprecated]: off.}

method delete*[T](self: Module[T]) =
self.view.delete
self.viewVariant.delete
self.controller.delete

proc init[T](self: Module[T], fullConnect = true) =
self.controller.init()
Expand Down
10 changes: 9 additions & 1 deletion src/app/modules/shared_modules/wallet_connect/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ QtObject:
proc newView*(delegate: io_interface.AccessInterface): View =
new(result, delete)
result.QObject.setup
result.delegate = delegate
result.delegate = delegate

proc addWalletConnectSession*(self: View, session_json: string): bool {.slot.} =
echo "@dd Vew.addWalletConnectSession: ", session_json, "; self.delegate.isNil: ", self.delegate.isNil
try:
return self.delegate.addWalletConnectSession(session_json)
except Exception as e:
echo "@dd Error in View.addWalletConnectSession: ", e.msg
return false
7 changes: 5 additions & 2 deletions src/app_service/service/wallet_connect/service.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NimQml, chronicles

# import backend/wallet_connect as status_go_wallet_connect
import backend/wallet_connect as status_go

import app/global/global_singleton

Expand Down Expand Up @@ -31,4 +31,7 @@ QtObject:
result.threadpool = threadpool

proc init*(self: Service) =
discard
discard

proc addSession*(self: Service, session_json: string): bool =
return status_go.addSession(session_json)
20 changes: 20 additions & 0 deletions src/backend/wallet_connect.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import options, logging
import json
import core, response_type

from gen import rpc
import backend

rpc(addWalletConnectSession, "wallet"):
sessionJson: string

proc isErrorResponse(rpcResponse: RpcResponse[JsonNode]): bool =
return not rpcResponse.error.isNil

proc addSession*(sessionJson: string): bool =
try:
let rpcRes = addWalletConnectSession(sessionJson)
return isErrorResponse(rpcRes):
except Exception as e:
warn "AddWalletConnectSession failed: ", "msg", e.msg
return false
5 changes: 4 additions & 1 deletion storybook/pages/DAppsWorkflowPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ Item {
projectId: projectIdText.projectId
}

dappsStore: DAppsStore {
store: DAppsStore {
function addWalletConnectSession(sessionJson) {
console.info("Persist Session", sessionJson)
}
}

walletStore: WalletStore {
Expand Down
13 changes: 11 additions & 2 deletions ui/app/AppLayouts/Wallet/services/dapps/WalletConnectService.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ QtObject {
id: root

required property WalletConnectSDK wcSDK
required property DAppsStore dappsStore
required property DAppsStore store
required property WalletStore walletStore

readonly property var validAccounts: SortFilterProxyModel {
Expand Down Expand Up @@ -75,10 +75,19 @@ QtObject {
}

function onApproveSessionResult(session, err) {
// Notify client
root.approveSessionResult(session, err)

if (err) {
return
}

// TODO #14754: implement custom dApp notification
let app_url = _d.currentSessionProposal ? _d.currentSessionProposal.params.proposer.metadata.url : "-"
Global.displayToastMessage(qsTr("Connected to %1 via WalletConnect").arg(app_url), "", "checkmark-circle", false, Constants.ephemeralNotificationType.success, "")
root.approveSessionResult(session, err)

// Persist session
store.addWalletConnectSession(JSON.stringify(session))
}

function onRejectSessionResult(err) {
Expand Down
1 change: 1 addition & 0 deletions ui/app/AppLayouts/Wallet/stores/RootStore.qml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ QtObject {
property var activityDetailsController: walletSectionInst.activityDetailsController
property string signingPhrase: walletSectionInst.signingPhrase
property string mnemonicBackedUp: walletSectionInst.isMnemonicBackedUp
property var walletConnectModule: walletSectionInst.walletConnectModule

property CollectiblesStore collectiblesStore: CollectiblesStore {}

Expand Down
3 changes: 2 additions & 1 deletion ui/app/mainui/AppMain.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,8 @@ Item {

projectId: WalletStore.RootStore.appSettings.walletConnectProjectID
}
dappsStore: DAppsStore {
store: DAppsStore {
module: WalletStore.RootStore.walletConnectModule
}
walletStore: appMain.rootStore.profileSectionStore.walletStore

Expand Down
8 changes: 7 additions & 1 deletion ui/imports/shared/stores/DAppsStore.qml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import QtQuick 2.15

QtObject {}
QtObject {
required property var module

function addWalletConnectSession(sessionJson) {
module.addWalletConnectSession(sessionJson)
}
}

0 comments on commit 07bc6c4

Please sign in to comment.