Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix # Display custom tokens in swap token list and move those with the selected network native token to the top of the list. #5011

Merged
merged 2 commits into from
Feb 22, 2022
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
1 change: 1 addition & 0 deletions BraveWallet/Crypto/Stores/CryptoStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public class CryptoStore: ObservableObject {
assetRatioService: assetRatioService,
swapService: swapService,
txService: txService,
walletService: walletService,
prefilledToken: prefilledToken
)
swapTokenStore = store
Expand Down
37 changes: 29 additions & 8 deletions BraveWallet/Crypto/Stores/SwapTokenStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class SwapTokenStore: ObservableObject {
private let assetRatioService: BraveWalletAssetRatioService
private let swapService: BraveWalletSwapService
private let txService: BraveWalletEthTxService
private let walletService: BraveWalletBraveWalletService
private var accountInfo: BraveWallet.AccountInfo?
private var slippage = 0.005 {
didSet {
Expand Down Expand Up @@ -142,6 +143,7 @@ public class SwapTokenStore: ObservableObject {
assetRatioService: BraveWalletAssetRatioService,
swapService: BraveWalletSwapService,
txService: BraveWalletEthTxService,
walletService: BraveWalletBraveWalletService,
prefilledToken: BraveWallet.BlockchainToken?
) {
self.keyringService = keyringService
Expand All @@ -150,6 +152,7 @@ public class SwapTokenStore: ObservableObject {
self.assetRatioService = assetRatioService
self.swapService = swapService
self.txService = txService
self.walletService = walletService
self.selectedFromToken = prefilledToken

self.keyringService.add(self)
Expand Down Expand Up @@ -589,18 +592,36 @@ public class SwapTokenStore: ObservableObject {
}
}

// All tokens from token registry
blockchainRegistry.allTokens(BraveWallet.MainnetChainId) { [weak self] tokens in
guard let self = self else { return }

self.rpcService.network { network in
// Native token on the current selected network
let nativeAsset = network.nativeToken
if network.chainId == BraveWallet.RopstenChainId {
let supportedAssets = tokens.filter { BraveWallet.assetsSwapInRopsten.contains($0.symbol) } + [nativeAsset]
self.allTokens = supportedAssets.sorted(by: { $0.symbol < $1.symbol })
updateSelectedTokens(in: network)
} else {
let fullList = tokens + [nativeAsset]
self.allTokens = fullList.sorted(by: { $0.symbol < $1.symbol })
// Custom tokens added by users
self.walletService.userAssets(network.chainId) { userAssets in
let customTokens = userAssets.filter { asset in
!tokens.contains(where: { $0.contractAddress == asset.contractAddress })
}
let sortedCustomTokens = customTokens.sorted {
if $0.contractAddress == nativeAsset.contractAddress {
return true
} else {
return $0.symbol < $1.symbol
}
}
if network.chainId == BraveWallet.RopstenChainId {
self.allTokens = sortedCustomTokens + tokens.filter {
BraveWallet.assetsSwapInRopsten.contains($0.symbol)
}.sorted(by: { $0.symbol < $1.symbol })
} else {
self.allTokens = sortedCustomTokens + tokens.sorted(by: { $0.symbol < $1.symbol })
}
// Seems like user assets always include the selected network's native asset
// But let's make sure all token list includes the native asset
if !self.allTokens.contains(where: { $0.symbol.lowercased() == nativeAsset.symbol.lowercased() }) {
self.allTokens.insert(nativeAsset, at: 0)
}
updateSelectedTokens(in: network)
}
}
Expand Down
1 change: 1 addition & 0 deletions BraveWallet/Preview Content/MockStores.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ extension SwapTokenStore {
assetRatioService: MockAssetRatioService(),
swapService: MockSwapService(),
txService: MockEthTxService(),
walletService: MockBraveWalletService(),
prefilledToken: nil
)
}
Expand Down
9 changes: 9 additions & 0 deletions BraveWalletTests/SwapTokenStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SendSwapStoreTests: XCTestCase {
assetRatioService: MockAssetRatioService(),
swapService: MockSwapService(),
txService: MockEthTxService(),
walletService: MockBraveWalletService(),
prefilledToken: nil
)
let ex = expectation(description: "default-sell-buy-token-on-main")
Expand All @@ -42,6 +43,7 @@ class SendSwapStoreTests: XCTestCase {
assetRatioService: MockAssetRatioService(),
swapService: MockSwapService(),
txService: MockEthTxService(),
walletService: MockBraveWalletService(),
prefilledToken: batToken
)
let ex = expectation(description: "default-sell-buy-token-on-main")
Expand All @@ -68,6 +70,7 @@ class SendSwapStoreTests: XCTestCase {
assetRatioService: MockAssetRatioService(),
swapService: MockSwapService(),
txService: MockEthTxService(),
walletService: MockBraveWalletService(),
prefilledToken: nil
)
let ex = expectation(description: "default-sell-buy-token-on-ropsten")
Expand Down Expand Up @@ -98,6 +101,7 @@ class SendSwapStoreTests: XCTestCase {
assetRatioService: MockAssetRatioService(),
swapService: MockSwapService(),
txService: MockEthTxService(),
walletService: MockBraveWalletService(),
prefilledToken: daiToken
)
let ex = expectation(description: "default-sell-buy-token-on-ropsten")
Expand Down Expand Up @@ -127,6 +131,7 @@ class SendSwapStoreTests: XCTestCase {
assetRatioService: MockAssetRatioService(),
swapService: MockSwapService(),
txService: MockEthTxService(),
walletService: MockBraveWalletService(),
prefilledToken: nil
)
let ex = expectation(description: "fetch-price-quote")
Expand All @@ -149,6 +154,7 @@ class SendSwapStoreTests: XCTestCase {
assetRatioService: MockAssetRatioService(),
swapService: MockSwapService(),
txService: MockEthTxService(),
walletService: MockBraveWalletService(),
prefilledToken: nil
)
let ex = expectation(description: "make-erc20-eip1559-swap-transaction")
Expand All @@ -174,6 +180,7 @@ class SendSwapStoreTests: XCTestCase {
assetRatioService: MockAssetRatioService(),
swapService: MockSwapService(),
txService: MockEthTxService(),
walletService: MockBraveWalletService(),
prefilledToken: nil
)
let ex = expectation(description: "make-erc20-swap-transaction")
Expand Down Expand Up @@ -202,6 +209,7 @@ class SendSwapStoreTests: XCTestCase {
assetRatioService: MockAssetRatioService(),
swapService: MockSwapService(),
txService: MockEthTxService(),
walletService: MockBraveWalletService(),
prefilledToken: nil
)
let ex = expectation(description: "make-eth-swap-eip1559-transaction")
Expand All @@ -227,6 +235,7 @@ class SendSwapStoreTests: XCTestCase {
assetRatioService: MockAssetRatioService(),
swapService: MockSwapService(),
txService: MockEthTxService(),
walletService: MockBraveWalletService(),
prefilledToken: nil
)
let ex = expectation(description: "make-eth-swap-eip1559-transaction")
Expand Down