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

A bunch of bugs #1054

Merged
merged 13 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied the proper forcerRefresh.

Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ extension AccountPortfoliosClient: DependencyKey {

/// Fetches the pool and stake units details for a given account; Will update the portfolio accordingly
@Sendable
func fetchPoolAndStakeUnitsDetails(_ account: OnLedgerEntity.Account) async {
func fetchPoolAndStakeUnitsDetails(_ account: OnLedgerEntity.Account, cachingStrategy: OnLedgerEntitiesClient.CachingStrategy) async {
async let poolDetailsFetch = Task {
do {
let poolUnitDetails = try await onLedgerEntitiesClient.getOwnedPoolUnitsDetails(account)
let poolUnitDetails = try await onLedgerEntitiesClient.getOwnedPoolUnitsDetails(account, cachingStrategy: cachingStrategy)
await state.set(poolDetails: .success(poolUnitDetails), forAccount: account.address)
} catch {
await state.set(poolDetails: .failure(error), forAccount: account.address)
}
}.result
async let stakeUnitDetails = Task {
do {
let stakeUnitDetails = try await onLedgerEntitiesClient.getOwnedStakesDetails(account: account)
let stakeUnitDetails = try await onLedgerEntitiesClient.getOwnedStakesDetails(account: account, cachingStrategy: cachingStrategy)
await state.set(stakeUnitDetails: .success(stakeUnitDetails.asIdentifiable()), forAccount: account.address)
} catch {
await state.set(stakeUnitDetails: .failure(error), forAccount: account.address)
Expand All @@ -48,6 +48,23 @@ extension AccountPortfoliosClient: DependencyKey {
_ = await (poolDetailsFetch, stakeUnitDetails)
}

@Sendable
func applyTokenPrices(_ resources: [ResourceAddress], forceRefresh: Bool) async {
if !resources.isEmpty {
let prices = try? await tokenPricesClient.getTokenPrices(
.init(
tokens: Array(resources.uniqued()),
currency: state.selectedCurrency
),
forceRefresh
)

if let prices {
await state.setTokenPrices(prices)
}
}
}

return AccountPortfoliosClient(
fetchAccountPortfolios: { accountAddresses, forceRefresh in
let gateway = await gatewaysClient.getCurrentGateway()
Expand All @@ -74,12 +91,7 @@ extension AccountPortfoliosClient: DependencyKey {
let allResources: [ResourceAddress] = {
if gateway == .mainnet {
/// Only Mainnet resources have prices
return (currentAccounts + accounts)
.flatMap {
$0.allFungibleResourceAddresses +
$0.poolUnitResources.poolUnits.flatMap(\.poolResources) +
[.mainnetXRDAddress]
}
return (currentAccounts + accounts).flatMap(\.resourcesWithPrices) + [.mainnetXRDAddress]
} else {
#if DEBUG
/// Helpful for testing on stokenet
Expand All @@ -105,22 +117,12 @@ extension AccountPortfoliosClient: DependencyKey {
}
}()

if !allResources.isEmpty {
let prices = try? await tokenPricesClient.getTokenPrices(
.init(
tokens: Array(allResources),
currency: preferences.fiatCurrencyPriceTarget
),
forceRefresh
)

if let prices {
await state.setTokenPrices(prices)
}
}
await applyTokenPrices(Array(allResources), forceRefresh: forceRefresh)

// Load additional details
_ = await accounts.parallelMap(fetchPoolAndStakeUnitsDetails)
_ = await accounts.parallelMap {
await fetchPoolAndStakeUnitsDetails($0, cachingStrategy: forceRefresh ? .forceUpdate : .useCache)
}

return Array(state.portfoliosSubject.value.wrappedValue!.values)
},
Expand All @@ -132,8 +134,14 @@ extension AccountPortfoliosClient: DependencyKey {
let account = try await onLedgerEntitiesClient.getAccount(accountAddress)
let portfolio = AccountPortfolio(account: account)

let currentResources = await state.tokenPrices.keys
await applyTokenPrices(
currentResources + account.resourcesWithPrices,
forceRefresh: forceRefresh
)

await state.handlePortfolioUpdate(portfolio)
await fetchPoolAndStakeUnitsDetails(account)
await fetchPoolAndStakeUnitsDetails(account, cachingStrategy: forceRefresh ? .forceUpdate : .useCache)

return portfolio
},
Expand All @@ -149,3 +157,10 @@ extension AccountPortfoliosClient: DependencyKey {
)
}()
}

extension OnLedgerEntity.Account {
/// The resources which can have prices
fileprivate var resourcesWithPrices: [ResourceAddress] {
allFungibleResourceAddresses + poolUnitResources.poolUnits.flatMap(\.poolResources)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extension OnLedgerEntitiesClient {
let isXRD = resourceAddress.isXRD(on: networkID)
let details: ResourceBalance.Fungible = .init(isXRD: isXRD, amount: .init(nominalAmount: amount), guarantee: guarantee)

return .init(resource: resource, details: .fungible(details))
return .init(resource: resource, details: .fungible(details), id: resource.resourceAddress)
}

private func poolUnit(
Expand Down Expand Up @@ -115,7 +115,8 @@ extension OnLedgerEntitiesClient {
nonXrdResources: nonXrdResources
),
guarantee: guarantee
))
)),
id: resource.resourceAddress
)
} else {
guard let details = try await getPoolUnitDetails(resource, forAmount: amount) else {
Expand All @@ -127,7 +128,8 @@ extension OnLedgerEntitiesClient {
details: .poolUnit(.init(
details: details,
guarantee: guarantee
))
)),
id: resource.resourceAddress
)
}
}
Expand Down Expand Up @@ -170,7 +172,7 @@ extension OnLedgerEntitiesClient {
guarantee: guarantee
)

return .init(resource: resource, details: .liquidStakeUnit(details))
return .init(resource: resource, details: .liquidStakeUnit(details), id: resource.resourceAddress)
}

// MARK: Non-fungibles
Expand Down Expand Up @@ -220,7 +222,7 @@ extension OnLedgerEntitiesClient {
)]
} else {
result = tokens.map { token in
.init(resource: resource, details: .nonFungible(token))
.init(resource: resource, details: .nonFungible(token), id: token.id)
}

guard result.count == ids.count else {
Expand All @@ -238,7 +240,7 @@ extension OnLedgerEntitiesClient {
try NonFungibleGlobalId.fromParts(resourceAddress: resourceAddress.intoEngine(), nonFungibleLocalId: localId)
}
.map { id in
ResourceBalance(resource: resource, details: .nonFungible(.init(id: id, data: nil)))
ResourceBalance(resource: resource, details: .nonFungible(.init(id: id, data: nil)), id: id)
}

guard result.count == ids.count else {
Expand Down Expand Up @@ -296,7 +298,8 @@ extension OnLedgerEntitiesClient {
stakeClaims: stakeClaimTokens.asIdentifiable()
),
validatorName: stakeClaimValidator.metadata.name
))
)),
id: resource.id
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion RadixWallet/EngineKit/TXID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public typealias TXID = TransactionHash

extension TXID {
public func formatted(_ format: AddressFormat = .default) -> String {
bytes().hex()
asStr()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Properly formatted TXID

}

public var hex: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public struct AssetTransfer: Sendable, FeatureReducer {
let manifest = try await createManifest(accounts)
Task {
_ = try await dappInteractionClient.addWalletInteraction(
.transaction(.init(send: .init(transactionManifest: manifest))),
.transaction(.init(send: .init(transactionManifest: manifest, message: message))),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message was removed in some of older PRs

.accountTransfer
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ private extension View {
private func addAsset(with destinationStore: PresentationStoreOf<TransferAccountList.Destination>) -> some View {
sheet(store: destinationStore.scope(state: \.state.addAsset, action: \.addAsset)) { assetsStore in
AssetsView.View(store: assetsStore)
.navigationTitle(L10n.AssetTransfer.AddAssets.navigationTitle)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With latest iOS updates the order matters now.

.navigationBarTitleDisplayMode(.inline)
.withNavigationBar {
assetsStore.send(.view(.closeButtonTapped))
}
.navigationTitle(L10n.AssetTransfer.AddAssets.navigationTitle)
.navigationBarTitleDisplayMode(.inline)
}
}
}