Skip to content

Commit

Permalink
feat(@desktop/wallet): implement unified currency formatting for tran…
Browse files Browse the repository at this point in the history
…saction details

Fixes #9019
  • Loading branch information
dlipicar committed Jan 20, 2023
1 parent 8736dd8 commit 53ee992
Show file tree
Hide file tree
Showing 18 changed files with 239 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/app/modules/main/wallet_section/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ proc newModule*(
result.allTokensModule = all_tokens_module.newModule(result, events, tokenService, walletAccountService)
result.collectiblesModule = collectibles_module.newModule(result, events, collectibleService, walletAccountService, networkService)
result.currentAccountModule = current_account_module.newModule(result, events, walletAccountService, networkService, tokenService, currencyService)
result.transactionsModule = transactions_module.newModule(result, events, transactionService, walletAccountService, networkService)
result.transactionsModule = transactions_module.newModule(result, events, transactionService, walletAccountService, networkService, currencyService)
result.savedAddressesModule = saved_addresses_module.newModule(result, events, savedAddressService)
result.buySellCryptoModule = buy_sell_crypto_module.newModule(result, events, transactionService)

Expand Down
10 changes: 10 additions & 0 deletions src/app/modules/main/wallet_section/transactions/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import io_interface
import ../../../../../app_service/service/transaction/service as transaction_service
import ../../../../../app_service/service/network/service as network_service
import ../../../../../app_service/service/wallet_account/service as wallet_account_service
import ../../../../../app_service/service/currency/service as currency_service
import ../../../shared_modules/keycard_popup/io_interface as keycard_shared_module

import ../../../../core/[main]
Expand All @@ -17,6 +18,7 @@ type
transactionService: transaction_service.Service
networkService: network_service.Service
walletAccountService: wallet_account_service.Service
currencyService: currency_service.Service

# Forward declaration
proc loadTransactions*(self: Controller, address: string, toBlock: Uint256, limit: int = 20, loadMore: bool = false)
Expand All @@ -28,13 +30,15 @@ proc newController*(
transactionService: transaction_service.Service,
walletAccountService: wallet_account_service.Service,
networkService: network_service.Service,
currencyService: currency_service.Service,
): Controller =
result = Controller()
result.events = events
result.delegate = delegate
result.transactionService = transactionService
result.walletAccountService = walletAccountService
result.networkService = networkService
result.currencyService = currencyService

proc delete*(self: Controller) =
discard
Expand Down Expand Up @@ -133,3 +137,9 @@ proc authenticateUser*(self: Controller, keyUid = "") =
let data = SharedKeycarModuleAuthenticationArgs(uniqueIdentifier: UNIQUE_WALLET_SECTION_TRANSACTION_MODULE_IDENTIFIER,
keyUid: keyUid)
self.events.emit(SIGNAL_SHARED_KEYCARD_MODULE_AUTHENTICATE_USER, data)

proc getCurrencyFormat*(self: Controller, symbol: string): CurrencyFormatDto =
return self.currencyService.getCurrencyFormat(symbol)

proc findTokenSymbolByAddress*(self: Controller, address: string): string =
return self.walletAccountService.findTokenSymbolByAddress(address)
68 changes: 41 additions & 27 deletions src/app/modules/main/wallet_section/transactions/item.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import strformat
import ../../../shared_models/currency_amount

type
Item* = object
Expand All @@ -8,25 +9,25 @@ type
blockNumber: string
blockHash: string
timestamp: int
gasPrice: string
gasLimit: string
gasUsed: string
gasPrice: CurrencyAmount
gasLimit: int
gasUsed: int
nonce: string
txStatus: string
value: string
value: CurrencyAmount
fro: string
to: string
contract: string
chainId: int
maxFeePerGas: string
maxPriorityFeePerGas: string
maxFeePerGas: CurrencyAmount
maxPriorityFeePerGas: CurrencyAmount
input: string
txHash: string
multiTransactionID: int
isTimeStamp: bool
baseGasFees: string
totalFees: string
maxTotalFees: string
baseGasFees: CurrencyAmount
totalFees: CurrencyAmount
maxTotalFees: CurrencyAmount
symbol: string

proc initItem*(
Expand All @@ -36,25 +37,25 @@ proc initItem*(
blockNumber: string,
blockHash: string,
timestamp: int,
gasPrice: string,
gasLimit: string,
gasUsed: string,
gasPrice: CurrencyAmount,
gasLimit: int,
gasUsed: int,
nonce: string,
txStatus: string,
value: string,
value: CurrencyAmount,
fro: string,
to: string,
contract: string,
chainId: int,
maxFeePerGas: string,
maxPriorityFeePerGas: string,
maxFeePerGas: CurrencyAmount,
maxPriorityFeePerGas: CurrencyAmount,
input: string,
txHash: string,
multiTransactionID: int,
isTimeStamp: bool,
baseGasFees: string,
totalFees: string,
maxTotalFees: string,
baseGasFees: CurrencyAmount,
totalFees: CurrencyAmount,
maxTotalFees: CurrencyAmount,
symbol: string
): Item =
result.id = id
Expand Down Expand Up @@ -84,6 +85,19 @@ proc initItem*(
result.maxTotalFees = maxTotalFees
result.symbol = symbol

proc initTimestampItem*(timestamp: int): Item =
result.timestamp = timestamp
result.gasPrice = newCurrencyAmount()
result.value = newCurrencyAmount()
result.chainId = 0
result.maxFeePerGas = newCurrencyAmount()
result.maxPriorityFeePerGas = newCurrencyAmount()
result.multiTransactionID = 0
result.isTimeStamp = true
result.baseGasFees = newCurrencyAmount()
result.totalFees = newCurrencyAmount()
result.maxTotalFees = newCurrencyAmount()

proc `$`*(self: Item): string =
result = fmt"""AllTokensItem(
id: {self.id},
Expand Down Expand Up @@ -132,13 +146,13 @@ proc getBlockHash*(self: Item): string =
proc getTimestamp*(self: Item): int =
return self.timestamp

proc getGasPrice*(self: Item): string =
proc getGasPrice*(self: Item): CurrencyAmount =
return self.gasPrice

proc getGasLimit*(self: Item): string =
proc getGasLimit*(self: Item): int =
return self.gasLimit

proc getGasUsed*(self: Item): string =
proc getGasUsed*(self: Item): int =
return self.gasUsed

proc getNonce*(self: Item): string =
Expand All @@ -147,7 +161,7 @@ proc getNonce*(self: Item): string =
proc getTxStatus*(self: Item): string =
return self.txStatus

proc getValue*(self: Item): string =
proc getValue*(self: Item): CurrencyAmount =
return self.value

proc getfrom*(self: Item): string =
Expand All @@ -162,10 +176,10 @@ proc getContract*(self: Item): string =
proc getChainId*(self: Item): int =
return self.chainId

proc getMaxFeePerGas*(self: Item): string =
proc getMaxFeePerGas*(self: Item): CurrencyAmount =
return self.maxFeePerGas

proc getMaxPriorityFeePerGas*(self: Item): string =
proc getMaxPriorityFeePerGas*(self: Item): CurrencyAmount =
return self.maxPriorityFeePerGas

proc getInput*(self: Item): string =
Expand All @@ -180,13 +194,13 @@ proc getMultiTransactionID*(self: Item): int =
proc getIsTimeStamp*(self: Item): bool =
return self.isTimeStamp

proc getBaseGasFees*(self: Item): string =
proc getBaseGasFees*(self: Item): CurrencyAmount =
return self.baseGasFees

proc getTotalFees*(self: Item): string =
proc getTotalFees*(self: Item): CurrencyAmount =
return self.totalFees

proc getMaxTotalFees*(self: Item): string =
proc getMaxTotalFees*(self: Item): CurrencyAmount =
return self.maxTotalFees

proc getSymbol*(self: Item): string =
Expand Down
38 changes: 4 additions & 34 deletions src/app/modules/main/wallet_section/transactions/model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import NimQml, Tables, strutils, strformat, sequtils, tables, sugar, algorithm,

import ./item
import ../../../../../app_service/service/eth/utils as eth_service_utils
import ../../../../../app_service/service/transaction/dto

type
ModelRole {.pure.} = enum
Expand Down Expand Up @@ -197,41 +196,12 @@ QtObject:
if result == 0:
result = cmp(x.getNonce(), y.getNonce())

proc addNewTransactions*(self: Model, transactions: seq[TransactionDto], wasFetchMore: bool) =
proc addNewTransactions*(self: Model, transactions: seq[Item], wasFetchMore: bool) =
let existingTxIds = self.items.map(tx => tx.getId())
let hasNewTxs = transactions.len > 0 and transactions.any(tx => not existingTxIds.contains(tx.id))
let hasNewTxs = transactions.len > 0 and transactions.any(tx => not existingTxIds.contains(tx.getId()))

if hasNewTxs or not wasFetchMore:
let newTxItems = transactions.map(t => initItem(
t.id,
t.typeValue,
t.address,
t.blockNumber,
t.blockHash,
toInt(t.timestamp),
t.gasPrice,
t.gasLimit,
t.gasUsed,
t.nonce,
t.txStatus,
t.value,
t.fromAddress,
t.to,
t.contract,
t.chainId,
t.maxFeePerGas,
t.maxPriorityFeePerGas,
t.input,
t.txHash,
t.multiTransactionID,
false,
t.baseGasFees,
t.totalFees,
t.maxTotalFees,
t.symbol
))

var allTxs = self.items.concat(newTxItems)
var allTxs = self.items.concat(transactions)
allTxs.sort(cmpTransactions, SortOrder.Descending)
eth_service_utils.deduplicate(allTxs, tx => tx.getTxHash())

Expand All @@ -241,7 +211,7 @@ QtObject:
for tx in allTxs:
let durationInDays = (tempTimeStamp.toTimeInterval() - fromUnix(tx.getTimestamp()).toTimeInterval()).days
if(durationInDays != 0):
itemsWithDateHeaders.add(initItem("", "", "", "", "", tx.getTimestamp(), "", "", "", "", "", "", "", "", "", 0, "", "", "", "", 0, true, "", "", "", ""))
itemsWithDateHeaders.add(initTimestampItem(tx.getTimestamp()))
itemsWithDateHeaders.add(tx)
tempTimeStamp = fromUnix(tx.getTimestamp())

Expand Down
36 changes: 30 additions & 6 deletions src/app/modules/main/wallet_section/transactions/module.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import NimQml, stint, json
import NimQml, stint, json, sequtils, sugar

import ./io_interface, ./view, ./controller
import ./io_interface, ./view, ./controller, ./item, ./utils
import ../io_interface as delegate_interface
import ../../../../global/global_singleton
import ../../../../core/eventemitter
import ../../../../../app_service/service/transaction/service as transaction_service
import ../../../../../app_service/service/wallet_account/service as wallet_account_service
import ../../../../../app_service/service/network/service as network_service
import ../../../../../app_service/service/currency/service as currency_service

export io_interface

Expand Down Expand Up @@ -39,11 +40,12 @@ proc newModule*(
transactionService: transaction_service.Service,
walletAccountService: wallet_account_service.Service,
networkService: network_service.Service,
currencyService: currency_service.Service,
): Module =
result = Module()
result.delegate = delegate
result.view = newView(result)
result.controller = controller.newController(result, events, transactionService, walletAccountService, networkService)
result.controller = controller.newController(result, events, transactionService, walletAccountService, networkService, currencyService)
result.moduleLoaded = false

method delete*(self: Module) =
Expand All @@ -58,13 +60,35 @@ method load*(self: Module) =
method isLoaded*(self: Module): bool =
return self.moduleLoaded

proc getResolvedSymbol*(self: Module, transaction: TransactionDto): string =
if transaction.symbol != "":
result = transaction.symbol
else:
let contractSymbol = self.controller.findTokenSymbolByAddress(transaction.contract)
if contractSymbol != "":
result = contractSymbol
else:
result = "ETH"

proc transactionsToItems(self: Module, transactions: seq[TransactionDto]) : seq[Item] =
let gweiFormat = self.controller.getCurrencyFormat("Gwei")
let ethFormat = self.controller.getCurrencyFormat("ETH")

transactions.map(t => (block:
let resolvedSymbol = self.getResolvedSymbol(t)
transactionToItem(t, resolvedSymbol, self.controller.getCurrencyFormat(resolvedSymbol), ethFormat, gweiFormat)
))

proc setPendingTx(self: Module) =
self.view.setPendingTx(self.transactionsToItems(self.controller.checkPendingTransactions()))

method viewDidLoad*(self: Module) =
let accounts = self.getWalletAccounts()

self.moduleLoaded = true
self.delegate.transactionsModuleDidLoad()

self.view.setPendingTx(self.controller.checkPendingTransactions())
self.setPendingTx()

method switchAccount*(self: Module, accountIndex: int) =
let walletAccount = self.controller.getWalletAccount(accountIndex)
Expand All @@ -86,7 +110,7 @@ method loadTransactions*(self: Module, address: string, toBlock: string = "0x0",
self.controller.loadTransactions(address, toBlockParsed, txLimit, loadMore)

method setTrxHistoryResult*(self: Module, transactions: seq[TransactionDto], address: string, wasFetchMore: bool) =
self.view.setTrxHistoryResult(transactions, address, wasFetchMore)
self.view.setTrxHistoryResult(self.transactionsToItems(transactions), address, wasFetchMore)

method setHistoryFetchState*(self: Module, addresses: seq[string], isFetching: bool) =
self.view.setHistoryFetchStateForAccounts(addresses, isFetching)
Expand Down Expand Up @@ -144,7 +168,7 @@ method onUserAuthenticated*(self: Module, password: string) =

method transactionWasSent*(self: Module, result: string) =
self.view.transactionWasSent(result)
self.view.setPendingTx(self.controller.checkPendingTransactions())
self.setPendingTx()

method suggestedFees*(self: Module, chainId: int): string =
return self.controller.suggestedFees(chainId)
Expand Down
Loading

0 comments on commit 53ee992

Please sign in to comment.