From 1deb5e86e584456d0209128491cd8c63ae2c64e1 Mon Sep 17 00:00:00 2001 From: Cuteivist Date: Mon, 25 Mar 2024 18:31:01 +0100 Subject: [PATCH] fix(wallet): Add token data when new token is added (#14061) --- .../main/communities/tokens/controller.nim | 2 + .../wallet_section/all_tokens/controller.nim | 13 ++++++ .../service/community_tokens/service.nim | 44 ++++++++++++------- src/app_service/service/token/service.nim | 41 ++++++++++++++++- src/backend/community_tokens_types.nim | 6 +++ ui/imports/shared/controls/TokenDelegate.qml | 3 ++ 6 files changed, 91 insertions(+), 18 deletions(-) diff --git a/src/app/modules/main/communities/tokens/controller.nim b/src/app/modules/main/communities/tokens/controller.nim index 9bda7d781a..db126fbba3 100644 --- a/src/app/modules/main/communities/tokens/controller.nim +++ b/src/app/modules/main/communities/tokens/controller.nim @@ -88,6 +88,8 @@ proc init*(self: Controller) = self.communityTokensModule.onOwnerTokenReceived(args.communityId, args.communityName, args.chainId, args.contractAddress) self.events.on(SIGNAL_COMMUNITY_TOKEN_RECEIVED) do(e: Args): let args = CommunityTokenReceivedArgs(e) + if args.isWatchOnlyAccount: + return self.communityTokensModule.onCommunityTokenReceived(args.name, args.symbol, args.image, args.communityId, args.communityName, $args.amount, args.chainId, args.txHash, args.isFirst, args.tokenType, args.accountName, args.accountAddress) self.events.on(SIGNAL_SET_SIGNER_STATUS) do(e: Args): let args = SetSignerArgs(e) diff --git a/src/app/modules/main/wallet_section/all_tokens/controller.nim b/src/app/modules/main/wallet_section/all_tokens/controller.nim index 387563a205..813563e303 100644 --- a/src/app/modules/main/wallet_section/all_tokens/controller.nim +++ b/src/app/modules/main/wallet_section/all_tokens/controller.nim @@ -48,6 +48,19 @@ proc init*(self: Controller) = let args = TokenBalanceHistoryDataArgs(e) self.delegate.tokenBalanceHistoryDataResolved(args.result) + self.events.on(SIGNAL_COMMUNITY_TOKEN_RECEIVED) do(e: Args): + let args = CommunityTokenReceivedArgs(e) + let token = TokenDto( + address: args.address, + name: args.name, + symbol: args.symbol, + decimals: args.decimals, + chainID: args.chainId, + communityID: args.communityId, + image: args.image, + ) + self.tokenService.addNewCommunityToken(token) + self.tokenService.getSupportedTokensList() proc getHistoricalDataForToken*(self: Controller, symbol: string, currency: string, range: int) = diff --git a/src/app_service/service/community_tokens/service.nim b/src/app_service/service/community_tokens/service.nim index ed9d451fbd..e20845acb0 100644 --- a/src/app_service/service/community_tokens/service.nim +++ b/src/app_service/service/community_tokens/service.nim @@ -174,6 +174,7 @@ type CommunityTokenReceivedArgs* = ref object of Args name*: string image*: string + address*: string collectibleId*: CollectibleUniqueID communityId*: string communityName*: string @@ -181,25 +182,33 @@ type amount*: float64 txHash*: string symbol*: string + decimals*: int + verified*: bool + tokenListID*: string isFirst*: bool tokenType*: int accountAddress*: string accountName*: string + isWatchOnlyAccount*: bool proc `$`*(self: CommunityTokenReceivedArgs): string = return fmt"""CommunityTokenReceivedArgs( - name: {self.name} - image: {self.image} - communityId: {self.communityId} - communityName: {self.communityName} - chainId: {self.chainId} - amount: {self.amount} - txHash: {self.txHash} - symbol: {self.symbol} - isFirst: {self.isFirst} - tokenType: {self.tokenType} - accountAddress: {self.accountAddress} - accountName: {self.accountName} + name: {self.name}, + image: {self.image}, + communityId: {self.communityId}, + communityName: {self.communityName}, + chainId: {self.chainId}, + amount: {self.amount}, + decimals: {self.decimals}, + verified: {self.verified}, + tokenListID: {self.tokenListID}, + txHash: {self.txHash}, + symbol: {self.symbol}, + isFirst: {self.isFirst}, + tokenType: {self.tokenType}, + accountAddress: {self.accountAddress}, + accountName: {self.accountName}, + isWatchOnlyAccount: {self.isWatchOnlyAccount} )""" proc toTokenData*(self: CommunityTokenReceivedArgs): string = @@ -413,10 +422,6 @@ QtObject: return let watchOnlyAccounts = self.walletAccountService.getWatchOnlyAccounts() - if any(watchOnlyAccounts, proc (x: WalletAccountDto): bool = x.address == accounts[0]): - # skip events on watch-only accounts - return - var accountName, accountAddress: string if len(accounts) > 0: accountAddress = accounts[0] @@ -429,14 +434,19 @@ QtObject: communityName: tokenDataPayload.communityName, chainId: tokenDataPayload.chainId, txHash: tokenDataPayload.txHash, + address: "0x" & tokenDataPayload.address.toHex(), name: tokenDataPayload.name, amount: tokenDataPayload.amount, + decimals: tokenDataPayload.decimals, + verified: tokenDataPayload.verified, + tokenListID: tokenDataPayload.tokenListID, image: tokenDataPayload.image, symbol: tokenDataPayload.symbol, isFirst: tokenDataPayload.isFirst, tokenType: int(TokenType.ERC20), accountAddress: accountAddress, - accountName: accountName + accountName: accountName, + isWatchOnlyAccount: any(watchOnlyAccounts, proc (x: WalletAccountDto): bool = x.address == accounts[0]) ) self.events.emit(SIGNAL_COMMUNITY_TOKEN_RECEIVED, tokenReceivedArgs) diff --git a/src/app_service/service/token/service.nim b/src/app_service/service/token/service.nim index 2d9e4c8539..478d139499 100644 --- a/src/app_service/service/token/service.nim +++ b/src/app_service/service/token/service.nim @@ -219,6 +219,45 @@ QtObject: let errDesription = e.msg error "error: ", errDesription + proc addNewCommunityToken*(self: Service, token: TokenDto) = + let sourceName = "custom" + let tokenType = TokenType.ERC20 + + var updated = false + let unique_key = $token.chainID & token.address + if not any(self.flatTokenList, proc (x: TokenItem): bool = x.key == unique_key): + self.flatTokenList.add(TokenItem( + key: unique_key, + name: token.name, + symbol: token.symbol, + sources: @[sourceName], + chainID: token.chainID, + address: token.address, + decimals: token.decimals, + image: token.image, + `type`: tokenType, + communityId: token.communityID)) + self.flatTokenList.sort(cmpTokenItem) + updated = true + + let token_by_symbol_key = token.address + if not any(self.tokenBySymbolList, proc (x: TokenBySymbolItem): bool = x.key == token_by_symbol_key): + self.tokenBySymbolList.add(TokenBySymbolItem( + key: token_by_symbol_key, + name: token.name, + symbol: token.symbol, + sources: @[sourceName], + addressPerChainId: @[AddressPerChain(chainId: token.chainID, address: token.address)], + decimals: token.decimals, + image: token.image, + `type`: tokenType, + communityId: token.communityID)) + self.tokenBySymbolList.sort(cmpTokenBySymbolItem) + updated = true + + if updated: + self.events.emit(SIGNAL_TOKENS_LIST_UPDATED, Args()) + # Callback to process the response of getSupportedTokensList call proc supportedTokensListRetrieved(self: Service, response: string) {.slot.} = # this is emited so that the models can know that the seq it depends on has been updated @@ -329,7 +368,7 @@ QtObject: var data = WalletSignal(e) case data.eventType: of "wallet-tick-reload": - self.rebuildMarketData() + self.rebuildMarketData() # update and populate internal list and then emit signal when new custom token detected? proc getCurrency*(self: Service): string = diff --git a/src/backend/community_tokens_types.nim b/src/backend/community_tokens_types.nim index 21df931786..1ba99c53fd 100644 --- a/src/backend/community_tokens_types.nim +++ b/src/backend/community_tokens_types.nim @@ -14,6 +14,9 @@ type symbol*: string image*: string chainId*: int + decimals*: int + verified*: bool + tokenListID*: string communityId*: string communityName*: string communityColor*: string @@ -28,6 +31,9 @@ proc fromJson*(t: JsonNode, T: typedesc[CommunityTokenReceivedPayload]): Communi discard t.getProp("symbol", result.symbol) discard t.getProp("image", result.image) discard t.getProp("chainId", result.chainId) + discard t.getProp("decimals", result.decimals) + discard t.getProp("verified", result.verified) + discard t.getProp("tokenListID", result.tokenListID) discard t.getProp("txHash", result.txHash) discard t.getProp("isFirst", result.isFirst) discard t.getProp("amount", result.amount) diff --git a/ui/imports/shared/controls/TokenDelegate.qml b/ui/imports/shared/controls/TokenDelegate.qml index 045e9de6a7..1efbf6085e 100644 --- a/ui/imports/shared/controls/TokenDelegate.qml +++ b/ui/imports/shared/controls/TokenDelegate.qml @@ -55,6 +55,7 @@ StatusListItem { return "▴" return "" } + readonly property bool isUndefined: modelData && !modelData.marketDetailsLoading && title === "" signal switchToCommunityRequested(string communityId) @@ -65,6 +66,8 @@ StatusListItem { asset.width: 32 asset.height: 32 errorIcon.tooltip.maxWidth: 300 + height: isUndefined ? 0 : implicitHeight + visible: !isUndefined statusListItemTitleIcons.sourceComponent: StatusFlatRoundButton { width: 14