Skip to content

fix(wallet): Transaction Details status not updated automatically on iOS #22405

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

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import SwiftUI

class TransactionDetailsStore: ObservableObject, WalletObserverStore {

let transaction: BraveWallet.TransactionInfo
var transaction: BraveWallet.TransactionInfo
@Published private(set) var parsedTransaction: ParsedTransaction?
@Published private(set) var network: BraveWallet.NetworkInfo?

Expand Down Expand Up @@ -81,14 +81,14 @@ class TransactionDetailsStore: ObservableObject, WalletObserverStore {
guard !isObserving else { return }
self.txServiceObserver = TxServiceObserver(
txService: txService,
_onNewUnapprovedTx: { [weak self] _ in
self?.update()
_onNewUnapprovedTx: { [weak self] transaction in
self?.updateTransaction(transaction)
},
_onUnapprovedTxUpdated: { [weak self] _ in
self?.update()
_onUnapprovedTxUpdated: { [weak self] transaction in
self?.updateTransaction(transaction)
},
_onTransactionStatusChanged: { [weak self] _ in
self?.update()
_onTransactionStatusChanged: { [weak self] transaction in
self?.updateTransaction(transaction)
},
_onTxServiceReset: { [weak self] in
self?.update()
Expand All @@ -100,6 +100,15 @@ class TransactionDetailsStore: ObservableObject, WalletObserverStore {
txServiceObserver = nil
}

func updateTransaction(_ transaction: BraveWallet.TransactionInfo) {
guard transaction.id == self.transaction.id else {
// not the transaction currently open
return
}
self.transaction = transaction
self.update()
}

func update() {
Task { @MainActor in
let coin = transaction.coin
Expand All @@ -113,7 +122,7 @@ class TransactionDetailsStore: ObservableObject, WalletObserverStore {
return
}
self.network = network
var allTokens: [BraveWallet.BlockchainToken] = await blockchainRegistry.allTokens(
let allTokens: [BraveWallet.BlockchainToken] = await blockchainRegistry.allTokens(
chainId: network.chainId,
coin: network.coin
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright 2021 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import BraveCore
import Combine
import XCTest

@testable import BraveWallet

class TransactionDetailsStoreTests: XCTestCase {

private var cancellables: Set<AnyCancellable> = []

private func setupServices() -> (
BraveWalletKeyringService,
BraveWalletBraveWalletService,
BraveWalletJsonRpcService,
BraveWalletAssetRatioService,
BraveWalletBlockchainRegistry,
BraveWalletTxService,
BraveWalletSolanaTxManagerProxy,
IpfsAPI,
WalletUserAssetManagerType
) {
let keyringService = BraveWallet.TestKeyringService()
keyringService._allAccounts = { completion in
let allAccounts = [BraveWallet.AccountInfo.previewAccount]
let allAccountsInfo: BraveWallet.AllAccountsInfo = .init(
accounts: allAccounts,
selectedAccount: allAccounts.first,
ethDappSelectedAccount: allAccounts.first(where: { $0.coin == .eth }),
solDappSelectedAccount: allAccounts.first(where: { $0.coin == .sol })
)
completion(allAccountsInfo)
}
let walletService = BraveWallet.TestBraveWalletService()
walletService._defaultBaseCurrency = { $0(CurrencyCode.usd.code) }
let rpcService = BraveWallet.TestJsonRpcService()
rpcService._allNetworks = { $1([.mockSepolia]) }
let assetRatioService = BraveWallet.TestAssetRatioService()
assetRatioService._price = { _, _, _, completion in
let mockAssetPrices: [BraveWallet.AssetPrice] = [
.init(fromAsset: "eth", toAsset: "usd", price: "3059.99", assetTimeframeChange: "-57.23")
]
completion(true, mockAssetPrices)
}
let blockchainRegistry = BraveWallet.TestBlockchainRegistry()
blockchainRegistry._allTokens = { _, _, completion in
completion([])
}
let txService = BraveWallet.TestTxService()
txService._addObserver = { _ in }
let solTxManagerProxy = BraveWallet.TestSolanaTxManagerProxy()
let userAssetManager = TestableWalletUserAssetManager()
userAssetManager._getAllUserAssetsInNetworkAssetsByVisibility = { _, _ in
return [
NetworkAssets(
network: .mockSepolia,
tokens: [
.previewToken.copy(asVisibleAsset: true).then {
$0.chainId = BraveWallet.SepoliaChainId
}
],
sortOrder: 0
)
]
}
let ipfsApi = TestIpfsAPI()

return (
keyringService, walletService, rpcService, assetRatioService, blockchainRegistry, txService,
solTxManagerProxy, ipfsApi, userAssetManager
)
}

/// Test `update()` will populate `parsedTransaction`
func testUpdate() {
let transaction: BraveWallet.TransactionInfo = .previewConfirmedSend.then {
$0.chainId = BraveWallet.SepoliaChainId
}
let (
keyringService, walletService, rpcService, assetRatioService, blockchainRegistry, txService,
solTxManagerProxy, ipfsApi, userAssetManager
) = setupServices()
let store = TransactionDetailsStore(
transaction: transaction,
parsedTransaction: nil,
keyringService: keyringService,
walletService: walletService,
rpcService: rpcService,
assetRatioService: assetRatioService,
blockchainRegistry: blockchainRegistry,
txService: txService,
solanaTxManagerProxy: solTxManagerProxy,
ipfsApi: ipfsApi,
userAssetManager: userAssetManager
)
let parsedTransactionExpectation = expectation(description: "update-parsedTransaction")
store.$parsedTransaction
.dropFirst()
.first()
.sink { parsedTransaction in
defer { parsedTransactionExpectation.fulfill() }
XCTAssertNotNil(parsedTransaction)
XCTAssertEqual(parsedTransaction?.transaction.txHash, transaction.txHash)
}
.store(in: &cancellables)
let networkExpectation = expectation(description: "update-network")
store.$network
.dropFirst()
.first()
.sink { network in
defer { networkExpectation.fulfill() }
XCTAssertEqual(network, .mockSepolia)
}
.store(in: &cancellables)
store.update()
waitForExpectations(timeout: 1) { error in
XCTAssertNil(error)
}
}

/// Test `updateTransaction(_:)` will update `parsedTransaction` with the new transaction
func testUpdateTransaction() {
let submittedTx: BraveWallet.TransactionInfo = .previewConfirmedSend.then {
$0.chainId = BraveWallet.SepoliaChainId
$0.txStatus = .submitted
}
let confirmedTx: BraveWallet.TransactionInfo = .previewConfirmedSend.then {
$0.chainId = BraveWallet.SepoliaChainId
$0.txStatus = .confirmed
}
XCTAssertEqual(submittedTx.txHash, confirmedTx.txHash)
let (
keyringService, walletService, rpcService, assetRatioService, blockchainRegistry, txService,
solTxManagerProxy, ipfsApi, userAssetManager
) = setupServices()
let store = TransactionDetailsStore(
transaction: submittedTx,
parsedTransaction: nil,
keyringService: keyringService,
walletService: walletService,
rpcService: rpcService,
assetRatioService: assetRatioService,
blockchainRegistry: blockchainRegistry,
txService: txService,
solanaTxManagerProxy: solTxManagerProxy,
ipfsApi: ipfsApi,
userAssetManager: userAssetManager
)
let parsedTxExpectation = expectation(description: "update-parsedTx")
store.$parsedTransaction
.dropFirst()
.sink { parsedTransaction in
defer { parsedTxExpectation.fulfill() }
XCTAssertNotNil(parsedTransaction)
XCTAssertEqual(parsedTransaction?.transaction.txHash, submittedTx.txHash)
XCTAssertEqual(parsedTransaction?.transaction.txStatus, .submitted)
}
.store(in: &cancellables)
store.update()
wait(for: [parsedTxExpectation], timeout: 1)
cancellables.removeAll()
let confirmedParsedTxExpectation = expectation(description: "update-parsedTx-confirmed")
store.$parsedTransaction
.dropFirst()
.sink { parsedTransaction in
defer { confirmedParsedTxExpectation.fulfill() }
XCTAssertNotNil(parsedTransaction)
XCTAssertEqual(parsedTransaction?.transaction.txHash, confirmedTx.txHash)
XCTAssertEqual(parsedTransaction?.transaction.txStatus, .confirmed)
}
.store(in: &cancellables)
store.updateTransaction(confirmedTx)
waitForExpectations(timeout: 1) { error in
XCTAssertNil(error)
}
}
}