Skip to content
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

MOB-1892 - Load SOL and BTC balances on public profile #444

Merged
merged 1 commit into from
Mar 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ final class PreviewWalletsDataService: WalletsDataServiceProtocol {
func loadBalanceFor(walletAddress: HexAddress) async throws -> [WalletTokenPortfolio] {
MockEntitiesFabric.Wallet.mockEntities()[0].balance
}

func loadAdditionalBalancesFor(domainName: DomainName) async -> [WalletTokenPortfolio] {
[]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,12 @@ extension PublicProfileView {
private func loadProfileTokens() {
Task {
await performAsyncErrorCatchingBlock {
let balances = try await appContext.walletsDataService.loadBalanceFor(walletAddress: domain.walletAddress)
tokens = balances.map { BalanceTokenUIDescription.extractFrom(walletBalance: $0) }.flatMap({ $0 }).sorted(by: { $0.balanceUsd > $1.balanceUsd })
async let balancesTask = appContext.walletsDataService.loadBalanceFor(walletAddress: domain.walletAddress)
async let additionalBalancesTask = appContext.walletsDataService.loadAdditionalBalancesFor(domainName: domain.name)
let (balances, additionalBalances) = try await (balancesTask, additionalBalancesTask)
let allBalances = balances + additionalBalances

tokens = allBalances.map { BalanceTokenUIDescription.extractFrom(walletBalance: $0) }.flatMap({ $0 }).sorted(by: { $0.balanceUsd > $1.balanceUsd })
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ extension WalletsDataService: WalletsDataServiceProtocol {
func loadBalanceFor(walletAddress: HexAddress) async throws -> [WalletTokenPortfolio] {
try await networkService.fetchCryptoPortfolioFor(wallet: walletAddress)
}

func loadAdditionalBalancesFor(domainName: DomainName) async -> [WalletTokenPortfolio] {
do {
let additionalAddresses = try await getAdditionalWalletAddressesToLoadBalanceFor(domainName: domainName)
guard !additionalAddresses.isEmpty else { return [] }

let balances = await loadAdditionalBalancesFor(addresses: additionalAddresses)
return balances
} catch {
Debugger.printFailure("Failed to load additional tokens for domain: \(domainName)")
return []
}
}
}

// MARK: - UDWalletsServiceListener
Expand Down Expand Up @@ -503,16 +516,10 @@ private extension WalletsDataService {
}

func loadAdditionalBalancesFor(wallet: WalletEntity) async -> [WalletTokenPortfolio] {
do {
let additionalAddresses = try await getAdditionalWalletAddressesToLoadBalanceFor(wallet: wallet)
guard !additionalAddresses.isEmpty else { return [] }

let balances = await loadAdditionalBalancesFor(addresses: additionalAddresses)
return balances
} catch {
Debugger.printFailure("Failed to load additional tokens for wallet: \(wallet.address)")
return []
}
guard let profileDomainName = wallet.profileDomainName else { return [] }

let balances = await loadAdditionalBalancesFor(domainName: profileDomainName)
return balances
}

func loadAdditionalBalancesFor(addresses: Set<String>) async -> [WalletTokenPortfolio] {
Expand All @@ -539,10 +546,8 @@ private extension WalletsDataService {
return balances
}

func getAdditionalWalletAddressesToLoadBalanceFor(wallet: WalletEntity) async throws -> Set<String> {
guard let profileDomainName = wallet.profileDomainName else { return [] }

let records = try await networkService.fetchProfileRecordsFor(domainName: profileDomainName)
func getAdditionalWalletAddressesToLoadBalanceFor(domainName: DomainName) async throws -> Set<String> {
let records = try await networkService.fetchProfileRecordsFor(domainName: domainName)
let additionalAddresses = Set(Constants.additionalSupportedTokens.compactMap({ records[$0] }))

return additionalAddresses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ protocol WalletsDataServiceProtocol {
func didMintDomainsWith(domainNames: [String],
to wallet: WalletEntity) -> [MintingDomain]
func loadBalanceFor(walletAddress: HexAddress) async throws -> [WalletTokenPortfolio]
func loadAdditionalBalancesFor(domainName: DomainName) async -> [WalletTokenPortfolio]
}