Skip to content

Commit

Permalink
fix(@wallet): Fix event for transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
alaibe committed Jan 27, 2023
1 parent 64d6c48 commit 7744d20
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 158 deletions.
52 changes: 24 additions & 28 deletions src/app/modules/main/wallet_section/transactions/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,25 @@ proc delete*(self: Controller) =
discard

proc init*(self: Controller) =
self.events.on(SignalType.Wallet.event) do(e:Args):
var data = WalletSignal(e)
case data.eventType:
of "new-transfers":
for account in data.accounts:
# TODO find a way to use data.blockNumber
self.loadTransactions(account, stint.fromHex(Uint256, "0x0"))
of "recent-history-fetching":
self.delegate.setHistoryFetchState(data.accounts, isFetching = true)
of "recent-history-ready":
for account in data.accounts:
self.loadTransactions(account, stint.fromHex(Uint256, "0x0"))
self.delegate.setHistoryFetchState(data.accounts, isFetching = false)
of "non-archival-node-detected":
let accounts = self.getWalletAccounts()
let addresses = accounts.map(account => account.address)
self.delegate.setHistoryFetchState(addresses, isFetching = false)
self.delegate.setIsNonArchivalNode(true)
of "fetching-history-error":
let accounts = self.getWalletAccounts()
let addresses = accounts.map(account => account.address)
self.delegate.setHistoryFetchState(addresses, isFetching = false)

self.events.on(SIGNAL_HISTORY_FETCHING) do (e:Args):
let args = HistoryArgs(e)
self.delegate.setHistoryFetchState(args.addresses, isFetching = true)

self.events.on(SIGNAL_HISTORY_READY) do (e:Args):
let args = HistoryArgs(e)
self.delegate.setHistoryFetchState(args.addresses, isFetching = false)

self.events.on(SIGNAL_HISTORY_NON_ARCHIVAL_NODE) do (e:Args):
let accounts = self.getWalletAccounts()
let addresses = accounts.map(account => account.address)
self.delegate.setHistoryFetchState(addresses, isFetching = false)
self.delegate.setIsNonArchivalNode(true)

self.events.on(SIGNAL_HISTORY_ERROR) do (e:Args):
let accounts = self.getWalletAccounts()
let addresses = accounts.map(account => account.address)
self.delegate.setHistoryFetchState(addresses, isFetching = false)

self.events.on(SIGNAL_TRANSACTIONS_LOADED) do(e:Args):
let args = TransactionsLoadedArgs(e)
self.delegate.setTrxHistoryResult(args.transactions, args.address, args.wasFetchMore)
Expand All @@ -83,15 +79,15 @@ proc init*(self: Controller) =
self.events.on(SIGNAL_SUGGESTED_ROUTES_READY) do(e:Args):
self.delegate.suggestedRoutesReady(SuggestedRoutesArgs(e).suggestedRoutes)

self.events.on(SIGNAL_PENDING_TX_COMPLETED) do(e:Args):
self.walletAccountService.checkRecentHistory()

self.events.on(SIGNAL_TRANSACTION_LOADING_COMPLETED_FOR_ALL_NETWORKS) do(e:Args):
let args = TransactionsLoadedArgs(e)
self.delegate.setHistoryFetchState(args.address, isFetching = false)

proc checkPendingTransactions*(self: Controller): seq[TransactionDto] =
return self.transactionService.checkPendingTransactions()
proc watchPendingTransactions*(self: Controller): seq[TransactionDto] =
return self.transactionService.watchPendingTransactions()

proc getPendingTransactions*(self: Controller): seq[TransactionDto] =
return self.transactionService.getPendingTransactions()

proc getWalletAccounts*(self: Controller): seq[WalletAccountDto] =
self.walletAccountService.getWalletAccounts()
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/main/wallet_section/transactions/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ proc transactionsToItems(self: Module, transactions: seq[TransactionDto]) : seq[
))

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

method viewDidLoad*(self: Module) =
let accounts = self.getWalletAccounts()
Expand Down Expand Up @@ -168,7 +168,7 @@ method onUserAuthenticated*(self: Module, password: string) =

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

method suggestedFees*(self: Module, chainId: int): string =
return self.controller.suggestedFees(chainId)
Expand Down
18 changes: 6 additions & 12 deletions src/app_service/service/ens/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,9 @@ QtObject:
proc init*(self: Service) =
self.doConnect()

# Response of `transactionService.getPendingTransactions()` should be appropriate DTO, that's not added at the moment
# but once we add it, need to update this block here, since we won't need to parse json manually here.
let pendingTransactions = self.transactionService.getPendingTransactions()
if (pendingTransactions.kind == JArray and pendingTransactions.len > 0):
for trx in pendingTransactions.getElems():
let transactionType = trx["type"].getStr
if transactionType == $PendingTransactionTypeDto.RegisterENS or
transactionType == $PendingTransactionTypeDto.SetPubKey:
self.pendingEnsUsernames.incl trx["additionalData"].getStr
for trx in self.transactionService.getPendingTransactions():
if trx.typeValue == $PendingTransactionTypeDto.RegisterENS or trx.typeValue == $PendingTransactionTypeDto.SetPubKey:
self.pendingEnsUsernames.incl trx.additionalData

proc getMyPendingEnsUsernames*(self: Service): seq[string] =
for i in self.pendingEnsUsernames.items:
Expand Down Expand Up @@ -260,7 +254,7 @@ QtObject:
let hash = resp.result.getStr

let resolverAddress = status_ens.resolver(chainId, ensUsername.addDomain()).result.getStr
self.transactionService.trackPendingTransaction(
self.transactionService.watchTransaction(
hash, $address, resolverAddress,
$PendingTransactionTypeDto.SetPubKey, ensUsername, chainId
)
Expand Down Expand Up @@ -317,7 +311,7 @@ QtObject:
let hash = resp.result.getStr

let ensUsernamesAddress = self.getEnsRegisteredAddress()
self.transactionService.trackPendingTransaction(
self.transactionService.watchTransaction(
hash, address, ensUsernamesAddress,
$PendingTransactionTypeDto.ReleaseENS, ensUsername, chainId
)
Expand Down Expand Up @@ -368,7 +362,7 @@ QtObject:
let hash = resp.result.getStr
let sntContract = self.getStatusToken()
let ensUsername = self.formatUsername(username, true)
self.transactionService.trackPendingTransaction(
self.transactionService.watchTransaction(
hash, address, $sntContract.address,
$PendingTransactionTypeDto.RegisterEns, ensUsername,
chainId
Expand Down
2 changes: 1 addition & 1 deletion src/app_service/service/stickers/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ QtObject:
let transactionResponse = status_stickers.buy(chainId, %txData, packId, password)
let transactionHash = transactionResponse.result.getStr()
let sntContract = self.getStatusToken()
self.transactionService.trackPendingTransaction(
self.transactionService.watchTransaction(
transactionHash,
address,
$sntContract.address,
Expand Down
2 changes: 0 additions & 2 deletions src/app_service/service/token/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,11 @@ QtObject:

if found:
continue

let responseTokens = backend.getTokens(network.chainId)
let default_tokens = map(
responseTokens.result.getElems(),
proc(x: JsonNode): TokenDto = x.toTokenDto(network.enabled, hasIcon=true, isCustom=false)
)

self.tokens[network.chainId] = default_tokens.filter(
proc(x: TokenDto): bool = x.chainId == network.chainId
)
Expand Down
19 changes: 14 additions & 5 deletions src/app_service/service/transaction/async_tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,30 @@ const getSuggestedRoutesTask*: Task = proc(argEncoded: string) {.gcsafe, nimcall
}
arg.finish(output)


type
WatchTransactionTaskArg* = ref object of QObjectTaskArg
hash: string
chainId: int
txHash: string
address: string
trxType: string

const watchTransactionsTask*: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
const watchTransactionTask*: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[WatchTransactionTaskArg](argEncoded)
try:
let output = %*{
"txHash": arg.txHash,
"isSuccessfull": transactions.watchTransaction(arg.chainId, arg.txHash).error.isNil,
"hash": arg.hash,
"address": arg.address,
"chainId": arg.chainId,
"trxType": arg.trxType,
"isSuccessfull": transactions.watchTransaction(arg.chainId, arg.hash).error.isNil,
}
arg.finish(output)
except Exception as e:
let output = %* {
"hash": arg.txHash,
"hash": arg.hash,
"address": arg.address,
"chainId": arg.chainId,
"trxType": arg.trxType,
"isSuccessfull": false
}
24 changes: 17 additions & 7 deletions src/app_service/service/transaction/dto.nim
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,24 @@ proc cmpTransactions*(x, y: TransactionDto): int =

type
SuggestedFeesDto* = ref object
gasPrice*: float
baseFee*: float
maxPriorityFeePerGas*: float
maxFeePerGasL*: float
maxFeePerGasM*: float
maxFeePerGasH*: float
gasPrice*: float64
baseFee*: float64
maxPriorityFeePerGas*: float64
maxFeePerGasL*: float64
maxFeePerGasM*: float64
maxFeePerGasH*: float64
eip1559Enabled*: bool

proc decodeSuggestedFeesDto*(jsonObj: JsonNode): SuggestedFeesDto =
result = SuggestedFeesDto()
result.gasPrice = jsonObj{"gasPrice"}.getFloat
result.baseFee = jsonObj{"baseFee"}.getFloat
result.maxPriorityFeePerGas = jsonObj{"maxPriorityFeePerGas"}.getFloat
result.maxFeePerGasL = jsonObj{"maxFeePerGasL"}.getFloat
result.maxFeePerGasM = jsonObj{"maxFeePerGasM"}.getFloat
result.maxFeePerGasH = jsonObj{"maxFeePerGasH"}.getFloat
result.eip1559Enabled = jsonObj{"eip1559Enabled"}.getbool

proc toSuggestedFeesDto*(jsonObj: JsonNode): SuggestedFeesDto =
result = SuggestedFeesDto()
result.gasPrice = parseFloat(jsonObj["gasPrice"].getStr)
Expand Down Expand Up @@ -225,7 +235,7 @@ proc convertToTransactionPathDto*(jsonObj: JsonNode): TransactionPathDto =
discard jsonObj.getProp("bridgeName", result.bridgeName)
result.fromNetwork = Json.decode($jsonObj["fromNetwork"], NetworkDto, allowUnknownFields = true)
result.toNetwork = Json.decode($jsonObj["toNetwork"], NetworkDto, allowUnknownFields = true)
result.gasFees = Json.decode($jsonObj["gasFees"], SuggestedFeesDto, allowUnknownFields = true)
result.gasFees = decodeSuggestedFeesDto(jsonObj["gasFees"])
discard jsonObj.getProp("cost", result.cost)
discard jsonObj.getProp("tokenFees", result.tokenFees)
discard jsonObj.getProp("bonderFees", result.bonderFees)
Expand Down
Loading

0 comments on commit 7744d20

Please sign in to comment.