diff --git a/BraveWallet/Crypto/Asset Details/AssetDetailHeaderView.swift b/BraveWallet/Crypto/Asset Details/AssetDetailHeaderView.swift index a8a5ab9cdc4..df3bc0deab7 100644 --- a/BraveWallet/Crypto/Asset Details/AssetDetailHeaderView.swift +++ b/BraveWallet/Crypto/Asset Details/AssetDetailHeaderView.swift @@ -143,13 +143,36 @@ struct AssetDetailHeaderView: View { Divider() .padding(.bottom) HStack { - Button(action: { buySendSwapDestination = .buy }) { - Text(Strings.Wallet.buy) + if assetDetailStore.isBuySupported { + Button( + action: { + buySendSwapDestination = BuySendSwapDestination( + kind: .buy, + initialToken: assetDetailStore.token + ) + } + ) { + Text(Strings.Wallet.buy) + } } - Button(action: { buySendSwapDestination = .send }) { + Button( + action: { + buySendSwapDestination = BuySendSwapDestination( + kind: .send, + initialToken: assetDetailStore.token + ) + } + ) { Text(Strings.Wallet.send) } - Button(action: { buySendSwapDestination = .swap }) { + Button( + action: { + buySendSwapDestination = BuySendSwapDestination( + kind: .swap, + initialToken: assetDetailStore.token + ) + } + ) { Text(Strings.Wallet.swap) } } diff --git a/BraveWallet/Crypto/BuySendSwap/BuySendSwapDestination.swift b/BraveWallet/Crypto/BuySendSwap/BuySendSwapDestination.swift index a5c2e54972e..a3bd1df7e38 100644 --- a/BraveWallet/Crypto/BuySendSwap/BuySendSwapDestination.swift +++ b/BraveWallet/Crypto/BuySendSwap/BuySendSwapDestination.swift @@ -6,38 +6,43 @@ import Foundation import struct Shared.Strings import SwiftUI +import BraveCore /// Used to determine where a user is navigated to when they tap on a buy, send or swap button -enum BuySendSwapDestination: String, Identifiable, CaseIterable { - case buy - case send - case swap - - var id: String { - rawValue - } - - var localizedTitle: String { - switch self { - case .buy: - return Strings.Wallet.buy - case .send: - return Strings.Wallet.send - case .swap: - return Strings.Wallet.swap +struct BuySendSwapDestination: Identifiable, Equatable, Hashable { + enum Kind: String, Identifiable, CaseIterable { + case buy, send, swap + + var id: String { + rawValue } - } - - var localizedDescription: String { - switch self { - case .buy: - return Strings.Wallet.buyDescription - case .send: - return Strings.Wallet.sendDescription - case .swap: - return Strings.Wallet.swapDescription + + var localizedTitle: String { + switch self { + case .buy: + return Strings.Wallet.buy + case .send: + return Strings.Wallet.send + case .swap: + return Strings.Wallet.swap + } + } + + var localizedDescription: String { + switch self { + case .buy: + return Strings.Wallet.buyDescription + case .send: + return Strings.Wallet.sendDescription + case .swap: + return Strings.Wallet.swapDescription + } } } + + var kind: Kind + var initialToken: BraveWallet.ERCToken? + var id: String { kind.id } } private struct BuySendSwapDestinationKey: EnvironmentKey { diff --git a/BraveWallet/Crypto/BuySendSwap/BuySendSwapView.swift b/BraveWallet/Crypto/BuySendSwap/BuySendSwapView.swift index cd348982262..38ca41980b1 100644 --- a/BraveWallet/Crypto/BuySendSwap/BuySendSwapView.swift +++ b/BraveWallet/Crypto/BuySendSwap/BuySendSwapView.swift @@ -8,17 +8,19 @@ import struct Shared.Strings struct BuySendSwapView: View { var action: (BuySendSwapDestination) -> Void - + var destinations = [BuySendSwapDestination(kind: .buy), + BuySendSwapDestination(kind: .send), + BuySendSwapDestination(kind: .swap)] var body: some View { VStack(alignment: .leading, spacing: 16) { - ForEach(BuySendSwapDestination.allCases, id: \.self) { action in - Button(action: { self.action(action) }) { + ForEach(destinations, id: \.self) { destination in + Button(action: { self.action(destination) }) { VStack(alignment: .leading, spacing: 3) { - Text(action.localizedTitle) + Text(destination.kind.localizedTitle) .foregroundColor(Color(.bravePrimary)) .font(.headline) .multilineTextAlignment(.leading) - Text(action.localizedDescription) + Text(destination.kind.localizedDescription) .foregroundColor(Color(.braveLabel)) .font(.footnote) .multilineTextAlignment(.leading) @@ -26,7 +28,7 @@ struct BuySendSwapView: View { .frame(maxWidth: .infinity, alignment: .leading) .padding([.leading, .trailing], 20) } - if action != BuySendSwapDestination.allCases.last { + if destination != destinations.last { Divider() .padding(.leading, 20) } diff --git a/BraveWallet/Crypto/CryptoView.swift b/BraveWallet/Crypto/CryptoView.swift index 10fca538d9d..74f1c95f8a8 100644 --- a/BraveWallet/Crypto/CryptoView.swift +++ b/BraveWallet/Crypto/CryptoView.swift @@ -111,24 +111,24 @@ private struct CryptoContainerView: View { .background( Color.clear .sheet(item: $cryptoStore.buySendSwapDestination) { action in - switch action { + switch action.kind { case .buy: BuyTokenView( keyringStore: keyringStore, networkStore: cryptoStore.networkStore, - buyTokenStore: cryptoStore.openBuyTokenStore() + buyTokenStore: cryptoStore.openBuyTokenStore(action.initialToken) ) case .send: SendTokenView( keyringStore: keyringStore, networkStore: cryptoStore.networkStore, - sendTokenStore: cryptoStore.openSendTokenStore() + sendTokenStore: cryptoStore.openSendTokenStore(action.initialToken) ) case .swap: SwapCryptoView( keyringStore: keyringStore, ethNetworkStore: cryptoStore.networkStore, - swapTokensStore: cryptoStore.openSwapTokenStore() + swapTokensStore: cryptoStore.openSwapTokenStore(action.initialToken) ) } } diff --git a/BraveWallet/Crypto/Stores/AssetDetailStore.swift b/BraveWallet/Crypto/Stores/AssetDetailStore.swift index c258d8fe553..5239b58e421 100644 --- a/BraveWallet/Crypto/Stores/AssetDetailStore.swift +++ b/BraveWallet/Crypto/Stores/AssetDetailStore.swift @@ -37,6 +37,7 @@ class AssetDetailStore: ObservableObject { @Published private(set) var isLoadingAccountBalances: Bool = false @Published private(set) var accounts: [AccountAssetViewModel] = [] @Published private(set) var transactions: [BraveWallet.TransactionInfo] = [] + @Published private(set) var isBuySupported: Bool = true private(set) var assetPriceValue: Double = 0.0 @@ -44,6 +45,7 @@ class AssetDetailStore: ObservableObject { private let keyringController: BraveWalletKeyringController private let rpcController: BraveWalletEthJsonRpcController private let txController: BraveWalletEthTxController + private let tokenRegistry: BraveWalletERCTokenRegistry let token: BraveWallet.ERCToken @@ -52,12 +54,14 @@ class AssetDetailStore: ObservableObject { keyringController: BraveWalletKeyringController, rpcController: BraveWalletEthJsonRpcController, txController: BraveWalletEthTxController, + tokenRegistry: BraveWalletERCTokenRegistry, token: BraveWallet.ERCToken ) { self.assetRatioController = assetRatioController self.keyringController = keyringController self.rpcController = rpcController self.txController = txController + self.tokenRegistry = tokenRegistry self.token = token self.keyringController.add(self) @@ -80,6 +84,10 @@ class AssetDetailStore: ObservableObject { isLoadingPrice = true isLoadingChart = true + tokenRegistry.buyTokens { [self] tokens in + isBuySupported = tokens.first(where: { $0.symbol.lowercased() == token.symbol.lowercased() }) != nil + } + keyringController.defaultKeyringInfo { [self] keyring in accounts = keyring.accountInfos.map { .init(account: $0, decimalBalance: 0.0, balance: "", fiatBalance: "") diff --git a/BraveWallet/Crypto/Stores/BuyTokenStore.swift b/BraveWallet/Crypto/Stores/BuyTokenStore.swift index bef67538661..db407ee4a49 100644 --- a/BraveWallet/Crypto/Stores/BuyTokenStore.swift +++ b/BraveWallet/Crypto/Stores/BuyTokenStore.swift @@ -23,10 +23,12 @@ public class BuyTokenStore: ObservableObject { public init( tokenRegistry: BraveWalletERCTokenRegistry, - rpcController: BraveWalletEthJsonRpcController + rpcController: BraveWalletEthJsonRpcController, + prefilledToken: BraveWallet.ERCToken? ) { self.tokenRegistry = tokenRegistry self.rpcController = rpcController + self.selectedBuyToken = prefilledToken } func fetchBuyUrl(account: BraveWallet.AccountInfo, amount: String, completion: @escaping (_ url: String?) -> Void) { diff --git a/BraveWallet/Crypto/Stores/CryptoStore.swift b/BraveWallet/Crypto/Stores/CryptoStore.swift index 494980612e7..65746df20cb 100644 --- a/BraveWallet/Crypto/Stores/CryptoStore.swift +++ b/BraveWallet/Crypto/Stores/CryptoStore.swift @@ -66,20 +66,21 @@ public class CryptoStore: ObservableObject { } private var buyTokenStore: BuyTokenStore? - func openBuyTokenStore() -> BuyTokenStore { + func openBuyTokenStore(_ prefilledToken: BraveWallet.ERCToken?) -> BuyTokenStore { if let store = buyTokenStore { return store } let store = BuyTokenStore( tokenRegistry: tokenRegistry, - rpcController: rpcController + rpcController: rpcController, + prefilledToken: prefilledToken ) buyTokenStore = store return store } private var sendTokenStore: SendTokenStore? - func openSendTokenStore() -> SendTokenStore { + func openSendTokenStore(_ prefilledToken: BraveWallet.ERCToken?) -> SendTokenStore { if let store = sendTokenStore { return store } @@ -88,14 +89,15 @@ public class CryptoStore: ObservableObject { rpcController: rpcController, walletService: walletService, transactionController: transactionController, - tokenRegistery: tokenRegistry + tokenRegistery: tokenRegistry, + prefilledToken: prefilledToken ) sendTokenStore = store return store } private var swapTokenStore: SwapTokenStore? - func openSwapTokenStore() -> SwapTokenStore { + func openSwapTokenStore(_ prefilledToken: BraveWallet.ERCToken?) -> SwapTokenStore { if let store = swapTokenStore { return store } @@ -105,7 +107,8 @@ public class CryptoStore: ObservableObject { rpcController: rpcController, assetRatioController: assetRatioController, swapController: swapController, - transactionController: transactionController + transactionController: transactionController, + prefilledToken: prefilledToken ) swapTokenStore = store return store @@ -121,6 +124,7 @@ public class CryptoStore: ObservableObject { keyringController: keyringController, rpcController: rpcController, txController: transactionController, + tokenRegistry: tokenRegistry, token: token ) assetDetailStore = store diff --git a/BraveWallet/Crypto/Stores/SendTokenStore.swift b/BraveWallet/Crypto/Stores/SendTokenStore.swift index 1c5d331b1a0..05fc2390df7 100644 --- a/BraveWallet/Crypto/Stores/SendTokenStore.swift +++ b/BraveWallet/Crypto/Stores/SendTokenStore.swift @@ -64,13 +64,15 @@ public class SendTokenStore: ObservableObject { rpcController: BraveWalletEthJsonRpcController, walletService: BraveWalletBraveWalletService, transactionController: BraveWalletEthTxController, - tokenRegistery: BraveWalletERCTokenRegistry + tokenRegistery: BraveWalletERCTokenRegistry, + prefilledToken: BraveWallet.ERCToken? ) { self.keyringController = keyringController self.rpcController = rpcController self.walletService = walletService self.transactionController = transactionController self.tokenRegistery = tokenRegistery + self.selectedSendToken = prefilledToken self.keyringController.add(self) self.rpcController.add(self) diff --git a/BraveWallet/Crypto/Stores/SwapTokenStore.swift b/BraveWallet/Crypto/Stores/SwapTokenStore.swift index 45df8faa111..9e7e0d12c6b 100644 --- a/BraveWallet/Crypto/Stores/SwapTokenStore.swift +++ b/BraveWallet/Crypto/Stores/SwapTokenStore.swift @@ -113,6 +113,8 @@ public class SwapTokenStore: ObservableObject { } private var updatingPriceQuote = false private var timer: Timer? + private let batSymbol = "BAT" + private let daiSymbol = "DAI" enum SwapParamsBase { // calculating based on sell asset amount @@ -140,7 +142,8 @@ public class SwapTokenStore: ObservableObject { rpcController: BraveWalletEthJsonRpcController, assetRatioController: BraveWalletAssetRatioController, swapController: BraveWalletSwapController, - transactionController: BraveWalletEthTxController + transactionController: BraveWalletEthTxController, + prefilledToken: BraveWallet.ERCToken? ) { self.keyringController = keyringController self.tokenRegistry = tokenRegistry @@ -148,6 +151,7 @@ public class SwapTokenStore: ObservableObject { self.assetRatioController = assetRatioController self.swapController = swapController self.transactionController = transactionController + self.selectedFromToken = prefilledToken self.keyringController.add(self) self.rpcController.add(self) @@ -561,9 +565,17 @@ public class SwapTokenStore: ObservableObject { } } else { if chainId == BraveWallet.MainnetChainId { - selectedToToken = allTokens.first(where: { $0.symbol.uppercased() == "BAT" }) + if let fromToken = selectedFromToken, fromToken.symbol.uppercased() == batSymbol.uppercased() { + selectedToToken = allTokens.first(where: { $0.symbol.uppercased() != batSymbol.uppercased() }) + } else { + selectedToToken = allTokens.first(where: { $0.symbol.uppercased() == batSymbol.uppercased() }) + } } else if chainId == BraveWallet.RopstenChainId { - selectedToToken = allTokens.first(where: { $0.symbol.uppercased() == "DAI" }) + if let fromToken = selectedFromToken, fromToken.symbol.uppercased() == daiSymbol.uppercased() { + selectedToToken = allTokens.first(where: { $0.symbol.uppercased() != daiSymbol.uppercased() }) + } else { + selectedToToken = allTokens.first(where: { $0.symbol.uppercased() == daiSymbol.uppercased() }) + } } completion?() } diff --git a/BraveWallet/Preview Content/TestStores.swift b/BraveWallet/Preview Content/TestStores.swift index d711475b200..020ce4f1608 100644 --- a/BraveWallet/Preview Content/TestStores.swift +++ b/BraveWallet/Preview Content/TestStores.swift @@ -59,7 +59,8 @@ extension BuyTokenStore { static var previewStore: BuyTokenStore { .init( tokenRegistry: TestTokenRegistry(), - rpcController: TestEthJsonRpcController() + rpcController: TestEthJsonRpcController(), + prefilledToken: .eth ) } } @@ -71,7 +72,8 @@ extension SendTokenStore { rpcController: TestEthJsonRpcController(), walletService: TestBraveWalletService(), transactionController: TestEthTxController(), - tokenRegistery: TestTokenRegistry() + tokenRegistery: TestTokenRegistry(), + prefilledToken: .eth ) } } @@ -83,6 +85,7 @@ extension AssetDetailStore { keyringController: TestKeyringController(), rpcController: TestEthJsonRpcController(), txController: TestEthTxController(), + tokenRegistry: TestTokenRegistry(), token: .eth ) } @@ -96,7 +99,8 @@ extension SwapTokenStore { rpcController: TestEthJsonRpcController(), assetRatioController: TestAssetRatioController(), swapController: TestSwapController(), - transactionController: TestEthTxController() + transactionController: TestEthTxController(), + prefilledToken: nil ) } } diff --git a/BraveWalletTests/BuyTokenStoreTest.swift b/BraveWalletTests/BuyTokenStoreTest.swift new file mode 100644 index 00000000000..33056543a67 --- /dev/null +++ b/BraveWalletTests/BuyTokenStoreTest.swift @@ -0,0 +1,30 @@ +// 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 http://mozilla.org/MPL/2.0/. + +import Foundation + +import XCTest +import Combine +import BraveCore +@testable import BraveWallet + +class BuyTokenStoreTests: XCTestCase { + func testPrefilledToken() { + var store = BuyTokenStore( + tokenRegistry: TestTokenRegistry(), + rpcController: TestEthJsonRpcController(), + prefilledToken: nil + ) + XCTAssertNil(store.selectedBuyToken) + + store = BuyTokenStore( + tokenRegistry: TestTokenRegistry(), + rpcController: TestEthJsonRpcController(), + prefilledToken: .eth + ) + XCTAssertEqual(store.selectedBuyToken?.symbol.lowercased(), BraveWallet.ERCToken.eth.symbol.lowercased()) + } + +} diff --git a/BraveWalletTests/SendTokenStoreTests.swift b/BraveWalletTests/SendTokenStoreTests.swift index d98ddbb9edc..da823e91e54 100644 --- a/BraveWalletTests/SendTokenStoreTests.swift +++ b/BraveWalletTests/SendTokenStoreTests.swift @@ -10,6 +10,29 @@ import BraveCore class SendTokenStoreTests: XCTestCase { private var cancellables: Set = [] + private let batSymbol = "BAT" + + func testPrefilledToken() { + var store = SendTokenStore( + keyringController: TestKeyringController(), + rpcController: TestEthJsonRpcController(), + walletService: TestBraveWalletService(), + transactionController: TestEthTxController(), + tokenRegistery: TestTokenRegistry(), + prefilledToken: nil + ) + XCTAssertNil(store.selectedSendToken) + + store = SendTokenStore( + keyringController: TestKeyringController(), + rpcController: TestEthJsonRpcController(), + walletService: TestBraveWalletService(), + transactionController: TestEthTxController(), + tokenRegistery: TestTokenRegistry(), + prefilledToken: .eth + ) + XCTAssertEqual(store.selectedSendToken?.symbol.lowercased(), BraveWallet.ERCToken.eth.symbol.lowercased()) + } func testFetchAssets() { let store = SendTokenStore( @@ -17,7 +40,8 @@ class SendTokenStoreTests: XCTestCase { rpcController: TestEthJsonRpcController(), walletService: TestBraveWalletService(), transactionController: TestEthTxController(), - tokenRegistery: TestTokenRegistry() + tokenRegistery: TestTokenRegistry(), + prefilledToken: nil ) let ex = expectation(description: "fetch-assets") XCTAssertNil(store.selectedSendToken) // Initial state @@ -41,9 +65,9 @@ class SendTokenStoreTests: XCTestCase { rpcController: TestEthJsonRpcController(), walletService: TestBraveWalletService(), transactionController: TestEthTxController(), - tokenRegistery: TestTokenRegistry() + tokenRegistery: TestTokenRegistry(), + prefilledToken: .eth ) - store.selectedSendToken = .eth store.setUpTest() let ex = expectation(description: "send-eth-eip1559-transaction") store.sendToken(amount: "0.01") { success in @@ -62,9 +86,9 @@ class SendTokenStoreTests: XCTestCase { rpcController: rpcController, walletService: TestBraveWalletService(), transactionController: TestEthTxController(), - tokenRegistery: TestTokenRegistry() + tokenRegistery: TestTokenRegistry(), + prefilledToken: .eth ) - store.selectedSendToken = .eth store.setUpTest() let ex = expectation(description: "send-eth-transaction") @@ -86,9 +110,10 @@ class SendTokenStoreTests: XCTestCase { rpcController: TestEthJsonRpcController(), walletService: TestBraveWalletService(), transactionController: TestEthTxController(), - tokenRegistery: TestTokenRegistry() + tokenRegistery: TestTokenRegistry(), + prefilledToken: nil ) - let token: BraveWallet.ERCToken = .init(contractAddress: "0x0d8775f648430679a709e98d2b0cb6250d2887ef", name: "Basic Attention Token", logo: "", isErc20: true, isErc721: false, symbol: "BAT", decimals: 18, visible: true, tokenId: "") + let token: BraveWallet.ERCToken = .init(contractAddress: "0x0d8775f648430679a709e98d2b0cb6250d2887ef", name: "Basic Attention Token", logo: "", isErc20: true, isErc721: false, symbol: batSymbol, decimals: 18, visible: true, tokenId: "") store.selectedSendToken = token store.setUpTest() @@ -109,9 +134,9 @@ class SendTokenStoreTests: XCTestCase { rpcController: rpcController, walletService: TestBraveWalletService(), transactionController: TestEthTxController(), - tokenRegistery: TestTokenRegistry() + tokenRegistery: TestTokenRegistry(), + prefilledToken: .eth ) - store.selectedSendToken = .eth store.setUpTest() let ex = expectation(description: "send-bat-transaction") diff --git a/BraveWalletTests/SwapTokenStoreTests.swift b/BraveWalletTests/SwapTokenStoreTests.swift index f7a699df4e8..b43ffa1e9d1 100644 --- a/BraveWalletTests/SwapTokenStoreTests.swift +++ b/BraveWalletTests/SwapTokenStoreTests.swift @@ -9,14 +9,15 @@ import BraveCore class SendSwapStoreTests: XCTestCase { - func testDefaultSellBuyTokensOnMainnet() { + func testDefaultSellBuyTokensOnMainnetWithoutPrefilledToken() { let store = SwapTokenStore( keyringController: TestKeyringController(), tokenRegistry: TestTokenRegistry(), rpcController: TestEthJsonRpcController(), assetRatioController: TestAssetRatioController(), swapController: TestSwapController(), - transactionController: TestEthTxController() + transactionController: TestEthTxController(), + prefilledToken: nil ) let ex = expectation(description: "default-sell-buy-token-on-main") XCTAssertNil(store.selectedFromToken) @@ -32,7 +33,33 @@ class SendSwapStoreTests: XCTestCase { } } - func testDefaultSellBuyTokensOnRopsten() { + func testDefaultSellBuyTokensOnMainnetWithPrefilledToken() { + let batToken: BraveWallet.ERCToken = .init(contractAddress: "", name: "Brave BAT", logo: "", isErc20: true, isErc721: false, symbol: "BAT", decimals: 18, visible: false, tokenId: "") + let store = SwapTokenStore( + keyringController: TestKeyringController(), + tokenRegistry: TestTokenRegistry(), + rpcController: TestEthJsonRpcController(), + assetRatioController: TestAssetRatioController(), + swapController: TestSwapController(), + transactionController: TestEthTxController(), + prefilledToken: batToken + ) + let ex = expectation(description: "default-sell-buy-token-on-main") + XCTAssertNotNil(store.selectedFromToken) + XCTAssertNil(store.selectedToToken) + let testAccountInfo: BraveWallet.AccountInfo = .init() + store.prepare(with: testAccountInfo) { + defer { ex.fulfill() } + XCTAssertEqual(store.selectedFromToken?.symbol.lowercased(), batToken.symbol.lowercased()) + XCTAssertNotNil(store.selectedToToken) + XCTAssertNotEqual(store.selectedToToken!.symbol.lowercased(), batToken.symbol.lowercased()) + } + waitForExpectations(timeout: 3) { error in + XCTAssertNil(error) + } + } + + func testDefaultSellBuyTokensOnRopstenWithoutPrefilledToken() { let rpcController = TestEthJsonRpcController() let store = SwapTokenStore( keyringController: TestKeyringController(), @@ -40,7 +67,8 @@ class SendSwapStoreTests: XCTestCase { rpcController: rpcController, assetRatioController: TestAssetRatioController(), swapController: TestSwapController(), - transactionController: TestEthTxController() + transactionController: TestEthTxController(), + prefilledToken: nil ) let ex = expectation(description: "default-sell-buy-token-on-ropsten") XCTAssertNil(store.selectedFromToken) @@ -60,6 +88,37 @@ class SendSwapStoreTests: XCTestCase { } } + func testDefaultSellBuyTokensOnRopstenWithPrefilledToken() { + let daiToken: BraveWallet.ERCToken = .init(contractAddress: "", name: "DAI Stablecoin", logo: "", isErc20: true, isErc721: false, symbol: "DAI", decimals: 18, visible: false, tokenId: "") + let rpcController = TestEthJsonRpcController() + let store = SwapTokenStore( + keyringController: TestKeyringController(), + tokenRegistry: TestTokenRegistry(), + rpcController: rpcController, + assetRatioController: TestAssetRatioController(), + swapController: TestSwapController(), + transactionController: TestEthTxController(), + prefilledToken: daiToken + ) + let ex = expectation(description: "default-sell-buy-token-on-ropsten") + XCTAssertNotNil(store.selectedFromToken) + XCTAssertNil(store.selectedToToken) + + rpcController.setNetwork(BraveWallet.RopstenChainId) { success in + XCTAssertTrue(success) + let testAccountInfo: BraveWallet.AccountInfo = .init() + store.prepare(with: testAccountInfo) { + defer { ex.fulfill() } + XCTAssertEqual(store.selectedFromToken?.symbol.lowercased(), daiToken.symbol.lowercased()) + XCTAssertNotNil(store.selectedToToken) + XCTAssertNotEqual(store.selectedToToken!.symbol.lowercased(), daiToken.symbol.lowercased()) + } + } + waitForExpectations(timeout: 3) { error in + XCTAssertNil(error) + } + } + func testFetchPriceQuote() { let store = SwapTokenStore( keyringController: TestKeyringController(), @@ -67,7 +126,8 @@ class SendSwapStoreTests: XCTestCase { rpcController: TestEthJsonRpcController(), assetRatioController: TestAssetRatioController(), swapController: TestSwapController(), - transactionController: TestEthTxController() + transactionController: TestEthTxController(), + prefilledToken: nil ) let ex = expectation(description: "fetch-price-quote") store.setUpTest() @@ -88,7 +148,8 @@ class SendSwapStoreTests: XCTestCase { rpcController: TestEthJsonRpcController(), assetRatioController: TestAssetRatioController(), swapController: TestSwapController(), - transactionController: TestEthTxController() + transactionController: TestEthTxController(), + prefilledToken: nil ) let ex = expectation(description: "make-erc20-eip1559-swap-transaction") store.setUpTest() @@ -112,7 +173,8 @@ class SendSwapStoreTests: XCTestCase { rpcController: rpcController, assetRatioController: TestAssetRatioController(), swapController: TestSwapController(), - transactionController: TestEthTxController() + transactionController: TestEthTxController(), + prefilledToken: nil ) let ex = expectation(description: "make-erc20-swap-transaction") store.setUpTest() @@ -139,7 +201,8 @@ class SendSwapStoreTests: XCTestCase { rpcController: TestEthJsonRpcController(), assetRatioController: TestAssetRatioController(), swapController: TestSwapController(), - transactionController: TestEthTxController() + transactionController: TestEthTxController(), + prefilledToken: nil ) let ex = expectation(description: "make-eth-swap-eip1559-transaction") store.setUpTest() @@ -163,7 +226,8 @@ class SendSwapStoreTests: XCTestCase { rpcController: rpcController, assetRatioController: TestAssetRatioController(), swapController: TestSwapController(), - transactionController: TestEthTxController() + transactionController: TestEthTxController(), + prefilledToken: nil ) let ex = expectation(description: "make-eth-swap-eip1559-transaction") store.setUpTest() diff --git a/Client.xcodeproj/project.pbxproj b/Client.xcodeproj/project.pbxproj index b9ca15a004e..baee8d6ac9c 100644 --- a/Client.xcodeproj/project.pbxproj +++ b/Client.xcodeproj/project.pbxproj @@ -942,6 +942,7 @@ 7DCA34D427555E96001B0555 /* CryptoStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DCA34D327555E96001B0555 /* CryptoStore.swift */; }; 7DF9113D275AC86100EF0D8B /* WalletLoadingButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DF9113C275AC86100EF0D8B /* WalletLoadingButton.swift */; }; 7DF911C0276A8D1600EF0D8B /* SwapTokenStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DF911BF276A8D1600EF0D8B /* SwapTokenStoreTests.swift */; }; + 7DF911DF27726E5E00EF0D8B /* BuyTokenStoreTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DF911DE27726E5E00EF0D8B /* BuyTokenStoreTest.swift */; }; A104E199210A384400D2323E /* ShieldsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A104E198210A384400D2323E /* ShieldsViewController.swift */; }; A134B88A20DA98BB00A581D0 /* ClientPreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = A134B88920DA98BB00A581D0 /* ClientPreferences.swift */; }; A13AC72520EC12360040D4BB /* Migration.swift in Sources */ = {isa = PBXBuildFile; fileRef = A13AC72420EC12360040D4BB /* Migration.swift */; }; @@ -2877,6 +2878,7 @@ 7DCA34D327555E96001B0555 /* CryptoStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CryptoStore.swift; sourceTree = ""; }; 7DF9113C275AC86100EF0D8B /* WalletLoadingButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletLoadingButton.swift; sourceTree = ""; }; 7DF911BF276A8D1600EF0D8B /* SwapTokenStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapTokenStoreTests.swift; sourceTree = ""; }; + 7DF911DE27726E5E00EF0D8B /* BuyTokenStoreTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuyTokenStoreTest.swift; sourceTree = ""; }; A104E198210A384400D2323E /* ShieldsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShieldsViewController.swift; sourceTree = ""; }; A134B88920DA98BB00A581D0 /* ClientPreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientPreferences.swift; sourceTree = ""; }; A13AC72420EC12360040D4BB /* Migration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Migration.swift; sourceTree = ""; }; @@ -4121,6 +4123,7 @@ 277309E62721DF6D007643F6 /* BraveWalletTests */ = { isa = PBXGroup; children = ( + 7DF911DE27726E5E00EF0D8B /* BuyTokenStoreTest.swift */, 279115362755597200033843 /* SendTokenStoreTests.swift */, 7DF911BF276A8D1600EF0D8B /* SwapTokenStoreTests.swift */, 277309E72721DF6D007643F6 /* WeiFormatterTests.swift */, @@ -7865,6 +7868,7 @@ 2792CBD5275952C40055151E /* PasteboardTests.swift in Sources */, 279115422755597200033843 /* SendTokenStoreTests.swift in Sources */, 27EA1B0E2729E6C8003C5CF9 /* AddressTests.swift in Sources */, + 7DF911DF27726E5E00EF0D8B /* BuyTokenStoreTest.swift in Sources */, 7DC52B1F273D9D230067E237 /* WalletArrayExtensionTests.swift in Sources */, 7DF911C0276A8D1600EF0D8B /* SwapTokenStoreTests.swift in Sources */, 277309E82721DF6D007643F6 /* WeiFormatterTests.swift in Sources */,