"All <token symbol>" in activity screen after tapping an activity in Activity tab which is for approving large amounts of ERC20 transfers. Also fixes not using decimals correctly when the amount *is* shown

pull/2347/head
Hwee-Boon Yar 4 years ago
parent a47e772c6a
commit 69fb7561d7
  1. 27
      AlphaWallet/Activities/ViewModels/DefaultActivityViewModel.swift

@ -37,11 +37,19 @@ struct DefaultActivityViewModel {
let string: String
switch activity.nativeViewType {
case .erc20Sent, .erc20Received, .erc20OwnerApproved, .erc20ApprovalObtained, .nativeCryptoSent, .nativeCryptoReceived:
case .erc20Sent, .erc20Received, .nativeCryptoSent, .nativeCryptoReceived:
if let value = cardAttributes["amount"]?.uintValue {
let formatter = EtherNumberFormatter.short
let value = formatter.string(from: BigInt(value))
string = "\(sign)\(value) \(activity.tokenObject.symbol)"
string = stringFromFungibleAmount(sign: sign, amount: value)
} else {
string = ""
}
case .erc20OwnerApproved, .erc20ApprovalObtained:
if let value = cardAttributes["amount"]?.uintValue {
if doesApprovedAmountLookReallyBig(value, decimals: activity.tokenObject.decimals) {
string = R.string.localizable.activityApproveAmountAll(activity.tokenObject.symbol)
} else {
string = stringFromFungibleAmount(sign: sign, amount: value)
}
} else {
string = ""
}
@ -64,4 +72,15 @@ struct DefaultActivityViewModel {
return NSAttributedString(string: string, attributes: [.font: Fonts.regular(size: 28), .foregroundColor: R.color.silver()!, .strikethroughStyle: NSUnderlineStyle.single.rawValue])
}
}
private func stringFromFungibleAmount(sign: String, amount: BigUInt) -> String {
let formatter = EtherNumberFormatter.short
let value = formatter.string(from: BigInt(amount), decimals: activity.tokenObject.decimals)
return "\(sign)\(value) \(activity.tokenObject.symbol)"
}
private func doesApprovedAmountLookReallyBig(_ amount: BigUInt, decimals: Int) -> Bool {
let empiricallyBigLimit: Double = 90_000_000
return Double(amount) / pow(10, activity.tokenObject.decimals).doubleValue > empiricallyBigLimit
}
}

Loading…
Cancel
Save