Skip to content

Commit

Permalink
[ABW-3827] Resource view Pre-Authorization states (#1377)
Browse files Browse the repository at this point in the history
Co-authored-by: Matias Bzurovski <matias.bzurovski@rdx.works>
  • Loading branch information
danvleju-rdx and matiasbzurovski authored Nov 8, 2024
1 parent 4887e80 commit e0a1e81
Show file tree
Hide file tree
Showing 47 changed files with 1,177 additions and 381 deletions.
2 changes: 1 addition & 1 deletion RadixWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9195,7 +9195,7 @@
repositoryURL = "https://github.com/radixdlt/sargon/";
requirement = {
kind = exactVersion;
version = 1.1.42;
version = 1.1.44;
};
};
5B4E1D1D2CB7FE8E002FAC2E /* XCRemoteSwiftPackageReference "sargon" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/radixdlt/sargon/",
"state" : {
"revision" : "83e13eb11037733a9edfe2c2c70d1efeb63e0831",
"version" : "1.1.42"
"revision" : "719ad87a7fd693418752c76024325b46f4b1bb4f",
"version" : "1.1.44"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ extension AccountPortfoliosClient.State {

// MARK: - Stake and Pool details handling
extension AccountPortfoliosClient.State {
func calculateWorth(_ gateway: Gateway) -> (ResourceAddress, ResourceAmount) -> FiatWorth? {
func calculateWorth(_ gateway: Gateway) -> (ResourceAddress, ExactResourceAmount) -> FiatWorth? {
{ resourceAddress, amount in
let worth: FiatWorth.Worth? = {
guard case let .success(tokenPrices) = self.tokenPrices else {
Expand Down Expand Up @@ -247,62 +247,91 @@ private extension AccountPortfoliosClient.AccountPortfolio {
}
}

mutating func updateFiatWorth(_ change: (ResourceAddress, ResourceAmount) -> FiatWorth?) {
mutating func updateFiatWorth(_ change: (ResourceAddress, ExactResourceAmount) -> FiatWorth?) {
account.fungibleResources.updateFiatWorth(change)
stakeUnitDetails.mutateValue { $0.updateFiatWorth(change) }
poolUnitDetails.mutateValue { $0.updateFiatWorth(change) }
}
}

extension ResourceAmount {
mutating func updateFiatWorth(
resourceAddress: ResourceAddress,
change: (ResourceAddress, ExactResourceAmount) -> FiatWorth?
) {
switch self {
case var .exact(exactAmount):
exactAmount.fiatWorth = change(resourceAddress, exactAmount)
self = .exact(exactAmount)
case var .atLeast(exactAmount):
exactAmount.fiatWorth = change(resourceAddress, exactAmount)
self = .atLeast(exactAmount)
case var .atMost(exactAmount):
exactAmount.fiatWorth = change(resourceAddress, exactAmount)
self = .atMost(exactAmount)
case var .between(minExactAmount, maxExactAmount):
minExactAmount.fiatWorth = change(resourceAddress, minExactAmount)
maxExactAmount.fiatWorth = change(resourceAddress, maxExactAmount)
self = .between(minimum: minExactAmount, maximum: maxExactAmount)
case var .predicted(predicted, guaranteed):
predicted.fiatWorth = change(resourceAddress, predicted)
guaranteed.fiatWorth = change(resourceAddress, guaranteed)
self = .predicted(predicted: predicted, guaranteed: guaranteed)
case .unknown:
return
}
}
}

private extension OnLedgerEntity.OwnedFungibleResources {
mutating func updateFiatWorth(_ change: (ResourceAddress, ResourceAmount) -> FiatWorth?) {
mutating func updateFiatWorth(_ change: (ResourceAddress, ExactResourceAmount) -> FiatWorth?) {
xrdResource.mutate { resource in
resource.amount.fiatWorth = change(.mainnetXRD, resource.amount)
resource.amount.updateFiatWorth(resourceAddress: .mainnetXRD, change: change)
}

nonXrdResources.mutateAll { resource in
resource.amount.fiatWorth = change(resource.resourceAddress, resource.amount)
resource.amount.updateFiatWorth(resourceAddress: resource.resourceAddress, change: change)
}

nonXrdResources.sort(by: <)
}
}

private extension MutableCollection where Element == OnLedgerEntitiesClient.OwnedResourcePoolDetails {
mutating func updateFiatWorth(_ change: (ResourceAddress, ResourceAmount) -> FiatWorth?) {
mutating func updateFiatWorth(_ change: (ResourceAddress, ExactResourceAmount) -> FiatWorth?) {
mutateAll { detail in
detail.xrdResource?.redemptionValue.mutate { amount in
amount.fiatWorth = change(.mainnetXRD, amount)
amount.updateFiatWorth(resourceAddress: .mainnetXRD, change: change)
}

detail.nonXrdResources.mutateAll { resource in
let address = resource.resource.resourceAddress
resource.redemptionValue.mutate { amount in
amount.fiatWorth = change(address, amount)
amount.updateFiatWorth(resourceAddress: address, change: change)
}
}
}
}
}

private extension MutableCollection where Element == OnLedgerEntitiesClient.OwnedStakeDetails {
mutating func updateFiatWorth(_ change: (ResourceAddress, ResourceAmount) -> FiatWorth?) {
mutating func updateFiatWorth(_ change: (ResourceAddress, ExactResourceAmount) -> FiatWorth?) {
mutateAll { detail in
let xrdRedemptionValue = detail.xrdRedemptionValue
detail.stakeUnitResource.mutate {
$0.amount.fiatWorth = change(
.mainnetXRD,
.init(
nominalAmount: xrdRedemptionValue,
fiatWorth: $0.amount.fiatWorth
var stakeUnitResource = detail.stakeUnitResource
stakeUnitResource.mutate {
$0.amount.updateFiatWorth(resourceAddress: .mainnetXRD, change: {
change(
$0,
detail.xrdRedemptionValue(exactAmount: $1)
)
)
})
}
detail.stakeClaimTokens.mutate {
$0.stakeClaims.mutateAll { token in
token.claimAmount.fiatWorth = change(.mainnetXRD, token.claimAmount)
}
}
detail.stakeUnitResource = stakeUnitResource
}
}
}
Expand All @@ -320,16 +349,16 @@ extension AccountPortfoliosClient.AccountPortfolio {

private extension OnLedgerEntity.OwnedFungibleResources {
var fiatWorth: FiatWorth.Worth {
let xrdFiatWorth = xrdResource?.amount.fiatWorth?.worth ?? .zero
let nonXrdFiatWorth = nonXrdResources.compactMap(\.amount.fiatWorth?.worth).reduce(.zero, +)
let xrdFiatWorth = xrdResource?.amount.exactAmount?.fiatWorth?.worth ?? .zero
let nonXrdFiatWorth = nonXrdResources.compactMap(\.amount.exactAmount?.fiatWorth?.worth).reduce(.zero, +)
return xrdFiatWorth + nonXrdFiatWorth
}
}

private extension Collection<OnLedgerEntitiesClient.OwnedStakeDetails> {
var fiatWorth: FiatWorth.Worth {
reduce(.zero) { partialResult, stakeUnitDetail in
let stakeUnitFiatWorth = stakeUnitDetail.stakeUnitResource?.amount.fiatWorth?.worth ?? .zero
let stakeUnitFiatWorth = stakeUnitDetail.stakeUnitResource?.amount.exactAmount?.fiatWorth?.worth ?? .zero
let stakeClaimsFiatWorth = stakeUnitDetail
.stakeClaimTokens?
.stakeClaims
Expand All @@ -343,8 +372,8 @@ private extension Collection<OnLedgerEntitiesClient.OwnedStakeDetails> {
private extension Collection<OnLedgerEntitiesClient.OwnedResourcePoolDetails> {
var fiatWorth: FiatWorth.Worth {
reduce(.zero) { partialResult, poolUnitDetail in
let xrdFiatWorth = poolUnitDetail.xrdResource?.redemptionValue?.fiatWorth?.worth ?? .zero
let nonXrdFiatWorth = poolUnitDetail.nonXrdResources.compactMap(\.redemptionValue?.fiatWorth?.worth).reduce(.zero, +)
let xrdFiatWorth = poolUnitDetail.xrdResource?.redemptionValue?.exactAmount?.fiatWorth?.worth ?? .zero
let nonXrdFiatWorth = poolUnitDetail.nonXrdResources.compactMap(\.redemptionValue?.exactAmount?.fiatWorth?.worth).reduce(.zero, +)
return partialResult + xrdFiatWorth + nonXrdFiatWorth
}
}
Expand Down
Loading

0 comments on commit e0a1e81

Please sign in to comment.