Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(@desktop/wallet): Unifying the various "TokensTypes" across the app #12654

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/app/modules/main/chat_section/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import ../../../../app_service/service/community_tokens/service as community_tok
import ../../../../app_service/service/shared_urls/service as shared_urls_service
import ../../../../app_service/service/visual_identity/service as visual_identity
import ../../../../app_service/service/contacts/dto/contacts as contacts_dto
import ../../../../app_service/service/community/dto/community as community_dto
import ../../../../app_service/common/types

export io_interface

Expand Down Expand Up @@ -1307,11 +1305,11 @@ method createOrEditCommunityTokenPermission*(self: Module, communityId: string,

let viewAmount = tokenCriteria{"amount"}.getFloat
var tokenCriteriaDto = tokenCriteria.toTokenCriteriaDto
if tokenCriteriaDto.`type` == community_dto.TokenType.ERC20:
if tokenCriteriaDto.`type` == TokenType.ERC20:
tokenCriteriaDto.decimals = self.controller.getTokenDecimals(tokenCriteriaDto.symbol)

let contractAddresses = self.controller.getContractAddressesForToken(tokenCriteriaDto.symbol)
if contractAddresses.len == 0 and tokenCriteriaDto.`type` != community_dto.TokenType.ENS:
if contractAddresses.len == 0 and tokenCriteriaDto.`type` != TokenType.ENS:
if permissionId == "":
self.onCommunityTokenPermissionCreationFailed(communityId)
return
Expand Down
6 changes: 3 additions & 3 deletions src/app/modules/main/communities/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ proc buildTokensAndCollectiblesFromCommunities(self: Module, communityTokens: se
infiniteSupply,
)

if tokenMetadata.tokenType == community_dto.TokenType.ERC20:
if tokenMetadata.tokenType == TokenType.ERC20:
# Community ERC20 tokens
tokenListItems.add(communityTokenItem)
else:
Expand Down Expand Up @@ -635,12 +635,12 @@ method onCommunityTokenMetadataAdded*(self: Module, communityId: string, tokenMe
infiniteSupply,
)

if tokenMetadata.tokenType == community_dto.TokenType.ERC721 and
if tokenMetadata.tokenType == TokenType.ERC721 and
not self.view.collectiblesListModel().hasItem(tokenMetadata.symbol):
self.view.collectiblesListModel.addItems(@[tokenListItem])
return

if tokenMetadata.tokenType == community_dto.TokenType.ERC20 and
if tokenMetadata.tokenType == TokenType.ERC20 and
not self.view.tokenListModel().hasItem(tokenMetadata.symbol):
self.view.tokenListModel.addItems(@[tokenListItem])

Expand Down
16 changes: 8 additions & 8 deletions src/app/modules/main/communities/tokens/controller.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import stint
import ./io_interface as community_tokens_module_interface

import ../../../../../app_service/service/community_tokens/service as community_tokens_service
import ../../../../../app_service/service/transaction/service as transaction_service
import ../../../../../app_service/service/network/service as networks_service
import ../../../../../app_service/service/community/service as community_service
import ../../../../../app_service/service/community/dto/community
import ../../../../core/signals/types
import ../../../../core/eventemitter
import app_service/service/community_tokens/service as community_tokens_service
import app_service/service/transaction/service as transaction_service
import app_service/service/network/service as networks_service
import app_service/service/community/service as community_service
import app_service/common/types
import app/core/signals/types
import app/core/eventemitter
import ../../../shared_modules/keycard_popup/io_interface as keycard_shared_module


Expand Down Expand Up @@ -165,4 +165,4 @@ proc declineOwnership*(self: Controller, communityId: string) =
self.communityTokensService.declineOwnership(communityId)

proc asyncGetOwnerTokenOwnerAddress*(self: Controller, chainId: int, contractAddress: string) =
self.communityTokensService.asyncGetOwnerTokenOwnerAddress(chainId, contractAddress)
self.communityTokensService.asyncGetOwnerTokenOwnerAddress(chainId, contractAddress)
1 change: 0 additions & 1 deletion src/app/modules/main/communities/tokens/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ../../../../../app_service/service/community_tokens/service as community_
import ../../../../../app_service/service/transaction/service as transaction_service
import ../../../../../app_service/service/network/service as networks_service
import ../../../../../app_service/service/community/service as community_service
import ../../../../../app_service/service/community/dto/community
import ../../../../../app_service/service/accounts/utils as utl
import ../../../../../app_service/common/types
import ../../../../core/eventemitter
Expand Down
8 changes: 4 additions & 4 deletions src/app/modules/main/communities/tokens/view.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import NimQml, json, strutils, sequtils

import ./io_interface as community_tokens_module_interface
import ../../../shared_models/currency_amount
import ../../../../../app_service/common/conversion
import ../../../../../app_service/service/community/dto/community
import app/modules/shared_models/currency_amount
import app_service/common/conversion
import app_service/common/types

QtObject:
type
Expand Down Expand Up @@ -133,4 +133,4 @@ QtObject:

QtProperty[string] ownerTokenDetails:
read = getOwnerTokenDetails
notify = ownerTokenDetailsChanged
notify = ownerTokenDetailsChanged
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import logging
import ./collectibles_item
import web3/ethtypes as eth
import backend/activity as backend_activity
import app_service/common/types

type
CollectibleRole* {.pure.} = enum
Expand Down Expand Up @@ -187,7 +188,7 @@ QtObject:
proc getActivityToken*(self: CollectiblesModel, id: string): backend_activity.Token =
for item in self.items:
if(cmpIgnoreCase(item.getId(), id) == 0):
result.tokenType = backend_activity.TokenType.Erc721
result.tokenType = TokenType.ERC721
result.chainId = backend_activity.ChainId(item.getChainId())
var contract = item.getContractAddress()
if len(contract) > 0:
Expand Down Expand Up @@ -216,4 +217,4 @@ QtObject:
# Fallback, create uid from data, because it still might not be fetched
if chainId > 0 and len(tokenAddress) > 0 and len(tokenId) > 0:
return $chainId & "+" & tokenAddress & "+" & tokenId
return ""
return ""
4 changes: 2 additions & 2 deletions src/app/modules/main/wallet_section/activity/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import app/core/eventemitter
import app/core/signals/types

import backend/activity as backend_activity
import backend/backend as backend

import app_service/common/conversion
import app_service/common/types
import app_service/service/currency/service as currency_service
import app_service/service/transaction/service as transaction_service
import app_service/service/token/service as token_service
Expand Down Expand Up @@ -358,7 +358,7 @@ QtObject:
for chainId in self.chainIds:
let token = self.tokenService.findTokenBySymbol(chainId, tokenCode)
if token != nil:
let tokenType = if token.symbol == "ETH": backend_activity.TokenType.Native else: backend_activity.TokenType.Erc20
let tokenType = if token.symbol == "ETH": TokenType.Native else: TokenType.ERC20
assets.add(backend_activity.Token(
tokenType: tokenType,
chainId: backend_activity.ChainId(token.chainId),
Expand Down
5 changes: 3 additions & 2 deletions src/app/modules/shared_models/collectibles_model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import logging
import ./collectibles_item
import web3/ethtypes as eth
import backend/activity as backend_activity
import app_service/common/utils as common_utils
import app_service/common/types

type
CollectibleRole* {.pure.} = enum
Expand Down Expand Up @@ -350,7 +352,7 @@ QtObject:
proc getActivityToken*(self: Model, id: string): backend_activity.Token =
for item in self.items:
if(cmpIgnoreCase(item.getId(), id) == 0):
result.tokenType = backend_activity.TokenType.Erc721
result.tokenType = TokenType.ERC721
result.chainId = backend_activity.ChainId(item.getChainId())
var contract = item.getContractAddress()
if len(contract) > 0:
Expand Down Expand Up @@ -380,4 +382,3 @@ QtObject:
if chainId > 0 and len(tokenAddress) > 0 and len(tokenId) > 0:
return $chainId & "+" & tokenAddress & "+" & tokenId
return ""

4 changes: 3 additions & 1 deletion src/app/modules/shared_models/token_criteria_model.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import NimQml, Tables

import app_service/common/types

import token_criteria_item
import ../../../app_service/service/community/dto/community

type
ModelRole {.pure.} = enum
Expand Down
7 changes: 4 additions & 3 deletions src/app_service/common/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ type Shard* = ref object
cluster*: int
index*: int

# ToDo: Will be streamlined to single TokenType under https://github.com/status-im/status-desktop/pull/12654/files
type NewTokenType* {.pure.} = enum
type TokenType* {.pure.} = enum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about how to avoid growing these catch-all types files. One way could be to follow the domain-driven design and not mix different topics (messaging with wallet). Does it make sense to have this into a blockchain_types.nim and the current types.nim into messaging_types.nim. Will this bring some clarity to the code for a newcomer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stefandunca the reasoning for this is that eventually community tokens and wallet tokens are "tokens" and we are treating them even more so after the are listing the community tokens with account assets. hope that makes sense

Native = 0
ERC20 = 1,
ERC721 = 2,
ERC1155
ERC1155 = 3,
Unknown = 4,
ENS = 5
6 changes: 0 additions & 6 deletions src/app_service/service/community/dto/community.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ type TokenPermissionState* {.pure.}= enum
UpdatePending = 2,
RemovalPending = 3,

type TokenType* {.pure.}= enum
Unknown = 0,
ERC20 = 1,
ERC721 = 2,
ENS = 3 # ENS is also ERC721 but we want to distinguish without heuristics

type TokenCriteriaDto* = object
contractAddresses* {.serializedFieldName("contract_addresses").}: Table[int, string]
`type`* {.serializedFieldName("type").}: TokenType
Expand Down
8 changes: 4 additions & 4 deletions src/app_service/service/token/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ QtObject:
if flatTokensList.hasKey(unique_key):
flatTokensList[unique_key].sources.add(s.name)
else:
let tokenType = if s.name == "native" : NewTokenType.Native
else: NewTokenType.ERC20
let tokenType = if s.name == "native" : TokenType.Native
else: TokenType.ERC20
flatTokensList[unique_key] = TokenItem(
key: unique_key,
name: token.name,
Expand Down Expand Up @@ -159,8 +159,8 @@ QtObject:
if not addedChains.contains(token.chainID):
tokenBySymbolList[token_by_symbol_key].addressPerChainId.add(AddressPerChain(chainId: token.chainID, address: token.address))
else:
let tokenType = if s.name == "native": NewTokenType.Native
else: NewTokenType.ERC20
let tokenType = if s.name == "native": TokenType.Native
else: TokenType.ERC20
tokenBySymbolList[token_by_symbol_key] = TokenBySymbolItem(
key: token_by_symbol_key,
name: token.name,
Expand Down
2 changes: 1 addition & 1 deletion src/app_service/service/token/service_items.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type
decimals*: int
# will remain empty until backend provides us this data
image*: string
`type`*: common_types.NewTokenType
`type`*: common_types.TokenType
communityId*: string

proc `$`*(self: TokenItem): string =
Expand Down
19 changes: 10 additions & 9 deletions src/backend/activity.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import stint
import web3/ethtypes as eth
import web3/conversions

import app_service/common/types
from gen import rpc
import backend

Expand Down Expand Up @@ -35,10 +36,6 @@ type
ActivityStatus* {.pure.} = enum
Failed, Pending, Complete, Finalized

# see status-go/services/wallet/activity/filter.go TokenType
TokenType* {.pure.} = enum
Native, Erc20, Erc721, Erc1155

# see status-go/services/wallet/activity/filter.go TokenID
TokenId* = distinct string

Expand Down Expand Up @@ -94,14 +91,18 @@ proc `%`*(tt: TokenType): JsonNode {.inline.} =

proc `$`*(tt: TokenType): string {.inline.} =
case tt:
of Native:
of TokenType.Native:
return "ETH"
of Erc20:
of TokenType.ERC20:
return "ERC-20"
of Erc721:
of TokenType.ERC721:
return "ERC-721"
of Erc1155:
of TokenType.ERC1155:
return "ERC-1155"
of TokenType.Unknown:
return "Unknown"
of TokenType.ENS:
return "ENS"

proc fromJson*(jn: JsonNode, T: typedesc[TokenType]): TokenType {.inline.} =
return cast[TokenType](jn.getInt())
Expand Down Expand Up @@ -593,4 +594,4 @@ rpc(getMultiTxDetails, "wallet"):
id: int

rpc(getTxDetails, "wallet"):
id: string
id: string
11 changes: 6 additions & 5 deletions storybook/pages/CommunityPermissionsHoldingItemEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14

import AppLayouts.Communities.controls 1.0
import utils 1.0

ColumnLayout {
id: root
Expand All @@ -17,12 +18,12 @@ ColumnLayout {
QtObject {
id: d

readonly property bool ensLayout: root.type === HoldingTypes.Type.Ens
readonly property bool ensLayout: root.type === Constants.TokenType.ENS

readonly property var holdingTypesModel: [
{ value: HoldingTypes.Type.Asset, text: "Asset" },
{ value: HoldingTypes.Type.Collectible, text: "Collectible" },
{ value: HoldingTypes.Type.Ens, text: "ENS" }
{ value: Constants.TokenType.ERC20, text: "Asset" },
{ value: Constants.TokenType.ERC721, text: "Collectible" },
{ value: Constants.TokenType.ENS, text: "ENS" }
]
}

Expand Down Expand Up @@ -72,7 +73,7 @@ ColumnLayout {
Layout.fillWidth: true

visible: !d.ensLayout
model: root.type === HoldingTypes.Type.Asset
model: root.type === Constants.TokenType.ERC20
? root.assetKeys : root.collectibleKeys

onActivated: root.key = currentText
Expand Down
4 changes: 3 additions & 1 deletion storybook/pages/CommunityPermissionsSettingsPanelEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import AppLayouts.Communities.controls 1.0

import Models 1.0

import utils 1.0

Flickable {
id: root

Expand Down Expand Up @@ -132,7 +134,7 @@ Flickable {
}

Button {
enabled: d.newKey && (d.newAmount || d.newType === HoldingTypes.Type.Ens)
enabled: d.newKey && (d.newAmount || d.newType === Constants.TokenType.ENS)
Layout.fillWidth: true
text: "Add new holding"

Expand Down
2 changes: 1 addition & 1 deletion storybook/pages/HoldingsDropdownPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ SplitView {
text: "Update"
onClicked: {
holdingsDropdown.close()
holdingsDropdown.setActiveTab(HoldingTypes.Type.Ens)
holdingsDropdown.setActiveTab(Constants.TokenType.ENS)
holdingsDropdown.openUpdateFlow()
}
}
Expand Down
11 changes: 7 additions & 4 deletions storybook/pages/SendModalPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ SplitView {

property var preSelectedAccount: txStore.selectedSenderAccount
property string preSelectedHoldingID
property int preSelectedHoldingType
property int preSelectedHoldingType: Constants.TokenType.Unknown
property int preSelectedSendType: Constants.SendType.Unknown
property bool onlyAssets: false
property string preDefinedAmountToSend
Expand Down Expand Up @@ -204,13 +204,16 @@ SplitView {
}
ComboBox {
id: tokenType
model: ["Unknown", "Asset", "Collectible"]
onCurrentIndexChanged: loader.preSelectedHoldingType = currentIndex
model: ["Native", "Asset", "Collectible", "Fungible Token", "Unknown", "ENS"]
currentIndex: 4
onCurrentIndexChanged: {
loader.preSelectedHoldingType = currentIndex
}
}
}

StatusInput {
enabled: tokenType.currentIndex !== 0
enabled: tokenType.currentIndex > 0 && tokenType.currentIndex < 3
label: "preSelectedHoldingID (case sensitive)"
onTextChanged: loader.preSelectedHoldingID = text
}
Expand Down
Loading