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

[trello.com/c/9nKvz1hT] Balance invalidation timer fix #607

Merged
merged 3 commits into from
Dec 2, 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
16 changes: 9 additions & 7 deletions Adamant/Modules/Wallets/Adamant/AdmWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, @unchecked Sendable

// MARK: - State
@Atomic private(set) var state: WalletServiceState = .upToDate
@Atomic private(set) var wallet: WalletAccount?
@Atomic private(set) var admWallet: AdmWallet?

var wallet: WalletAccount? { admWallet }

// MARK: - Logic
override init() {
Expand Down Expand Up @@ -140,22 +142,22 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, @unchecked Sendable
NotificationCenter.default
.notifications(named: .AdamantAccountService.userLoggedOut, object: nil)
.sink { @MainActor [weak self] _ in
self?.wallet = nil
self?.admWallet = nil
}
.store(in: &subscriptions)
}

func update() {
guard let accountService = accountService, let account = accountService.account else {
wallet = nil
admWallet = nil
return
}

let notify: Bool

let isRaised: Bool

if let wallet = wallet as? AdmWallet {
if let wallet = admWallet {
isRaised = (wallet.balance < account.balance) && wallet.isBalanceInitialized
if wallet.balance != account.balance || wallet.isBalanceInitialized != !accountService.isBalanceExpired {
wallet.balance = account.balance
Expand All @@ -165,17 +167,17 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, @unchecked Sendable
} else {
notify = false
}
wallet.isBalanceInitialized = !accountService.isBalanceExpired
} else {
let wallet = AdmWallet(unicId: tokenUnicID, address: account.address)
wallet.isBalanceInitialized = !accountService.isBalanceExpired
wallet.balance = account.balance

self.wallet = wallet
admWallet = wallet
notify = true
isRaised = false
}

admWallet?.isBalanceInitialized = !accountService.isBalanceExpired

if isRaised {
Task { @MainActor in vibroService.applyVibration(.success) }
}
Expand Down
38 changes: 12 additions & 26 deletions Adamant/Modules/Wallets/Bitcoin/BtcWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,6 @@ final class BtcWalletService: WalletCoreProtocol, @unchecked Sendable {
self?.balanceInvalidationSubscription = nil
}
.store(in: &subscriptions)

NotificationCenter.default
.notifications(named: UIApplication.didBecomeActiveNotification, object: nil)
.sink { [weak self] _ in self?.setBalanceInvalidationSubscription() }
.store(in: &subscriptions)

NotificationCenter.default
.notifications(named: UIApplication.willResignActiveNotification, object: nil)
.sink { [weak self] _ in self?.balanceInvalidationSubscription = nil }
.store(in: &subscriptions)
}

func addTransactionObserver() {
Expand Down Expand Up @@ -292,7 +282,7 @@ final class BtcWalletService: WalletCoreProtocol, @unchecked Sendable {
setState(.updating)

if let balance = try? await getBalance() {
setBalanceInvalidationSubscription()
markBalanceAsFresh()
let notification: Notification.Name?

let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized
Expand All @@ -306,8 +296,6 @@ final class BtcWalletService: WalletCoreProtocol, @unchecked Sendable {
notification = nil
}

wallet.isBalanceInitialized = true

if isRaised {
await vibroService.applyVibration(.success)
}
Expand Down Expand Up @@ -387,23 +375,21 @@ final class BtcWalletService: WalletCoreProtocol, @unchecked Sendable {
return output
}

private func setBalanceInvalidationSubscription() {
private func markBalanceAsFresh() {
btcWallet?.isBalanceInitialized = true

balanceInvalidationSubscription = Task { [weak self] in
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
self?.resetBalance()
guard let self, let wallet = btcWallet else { return }
wallet.isBalanceInitialized = false

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}.eraseToAnyCancellable()
}

private func resetBalance() {
btcWallet?.isBalanceInitialized = false
guard let wallet = btcWallet else { return }

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}

public func isValid(bitcoinAddress address: String) -> Bool {
(try? addressConverter.convert(address: address)) != nil
Expand Down
38 changes: 12 additions & 26 deletions Adamant/Modules/Wallets/Dash/DashWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,6 @@ final class DashWalletService: WalletCoreProtocol, @unchecked Sendable {
self?.balanceInvalidationSubscription = nil
}
.store(in: &subscriptions)

NotificationCenter.default
.notifications(named: UIApplication.didBecomeActiveNotification, object: nil)
.sink { [weak self] _ in self?.setBalanceInvalidationSubscription() }
.store(in: &subscriptions)

NotificationCenter.default
.notifications(named: UIApplication.willResignActiveNotification, object: nil)
.sink { [weak self] _ in self?.balanceInvalidationSubscription = nil }
.store(in: &subscriptions)
}

func addTransactionObserver() {
Expand Down Expand Up @@ -265,7 +255,7 @@ final class DashWalletService: WalletCoreProtocol, @unchecked Sendable {
setState(.updating)

if let balance = try? await getBalance() {
setBalanceInvalidationSubscription()
markBalanceAsFresh()
let notification: Notification.Name?
let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized

Expand All @@ -278,8 +268,6 @@ final class DashWalletService: WalletCoreProtocol, @unchecked Sendable {
notification = nil
}

wallet.isBalanceInitialized = true

if isRaised {
await vibroService.applyVibration(.success)
}
Expand Down Expand Up @@ -307,23 +295,21 @@ final class DashWalletService: WalletCoreProtocol, @unchecked Sendable {
}
}

private func setBalanceInvalidationSubscription() {
private func markBalanceAsFresh() {
dashWallet?.isBalanceInitialized = true

balanceInvalidationSubscription = Task { [weak self] in
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
self?.resetBalance()
guard let self, let wallet = dashWallet else { return }
wallet.isBalanceInitialized = false

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}.eraseToAnyCancellable()
}

private func resetBalance() {
dashWallet?.isBalanceInitialized = false
guard let wallet = dashWallet else { return }

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}
}

// MARK: - WalletInitiatedWithPassphrase
Expand Down
38 changes: 12 additions & 26 deletions Adamant/Modules/Wallets/Doge/DogeWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,6 @@ final class DogeWalletService: WalletCoreProtocol, @unchecked Sendable {
self?.balanceInvalidationSubscription = nil
}
.store(in: &subscriptions)

NotificationCenter.default
.notifications(named: UIApplication.didBecomeActiveNotification, object: nil)
.sink { [weak self] _ in self?.setBalanceInvalidationSubscription() }
.store(in: &subscriptions)

NotificationCenter.default
.notifications(named: UIApplication.willResignActiveNotification, object: nil)
.sink { [weak self] _ in self?.balanceInvalidationSubscription = nil }
.store(in: &subscriptions)
}

func addTransactionObserver() {
Expand Down Expand Up @@ -262,7 +252,7 @@ final class DogeWalletService: WalletCoreProtocol, @unchecked Sendable {
setState(.updating)

if let balance = try? await getBalance() {
setBalanceInvalidationSubscription()
markBalanceAsFresh()
let notification: Notification.Name?
let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized

Expand All @@ -275,8 +265,6 @@ final class DogeWalletService: WalletCoreProtocol, @unchecked Sendable {
notification = nil
}

wallet.isBalanceInitialized = true

if isRaised {
await vibroService.applyVibration(.success)
}
Expand All @@ -300,23 +288,21 @@ final class DogeWalletService: WalletCoreProtocol, @unchecked Sendable {
}
}

private func setBalanceInvalidationSubscription() {
private func markBalanceAsFresh() {
dogeWallet?.isBalanceInitialized = true

balanceInvalidationSubscription = Task { [weak self] in
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
self?.resetBalance()
guard let self, let wallet = dogeWallet else { return }
wallet.isBalanceInitialized = false

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}.eraseToAnyCancellable()
}

private func resetBalance() {
dogeWallet?.isBalanceInitialized = false
guard let wallet = dogeWallet else { return }

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}
}

// MARK: - WalletInitiatedWithPassphrase
Expand Down
50 changes: 18 additions & 32 deletions Adamant/Modules/Wallets/ERC20/ERC20WalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
EthWalletService.explorerAddress
}

private (set) var blockchainSymbol: String = "ETH"
private (set) var isDynamicFee: Bool = true
private (set) var transactionFee: Decimal = 0.0
private (set) var gasPrice: BigUInt = 0
private (set) var gasLimit: BigUInt = 0
private (set) var isWarningGasPrice = false
private(set) var blockchainSymbol: String = "ETH"
private(set) var isDynamicFee: Bool = true
private(set) var transactionFee: Decimal = 0.0
private(set) var gasPrice: BigUInt = 0
private(set) var gasLimit: BigUInt = 0
private(set) var isWarningGasPrice = false

var isTransactionFeeValid: Bool {
return ethWallet?.balance ?? 0 > transactionFee
Expand Down Expand Up @@ -229,16 +229,6 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
self?.balanceInvalidationSubscription = nil
}
.store(in: &subscriptions)

NotificationCenter.default
.notifications(named: UIApplication.didBecomeActiveNotification, object: nil)
.sink { [weak self] _ in self?.setBalanceInvalidationSubscription() }
.store(in: &subscriptions)

NotificationCenter.default
.notifications(named: UIApplication.willResignActiveNotification, object: nil)
.sink { [weak self] _ in self?.balanceInvalidationSubscription = nil }
.store(in: &subscriptions)
}

func addTransactionObserver() {
Expand Down Expand Up @@ -271,7 +261,7 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
setState(.updating)

if let balance = try? await getBalance(forAddress: wallet.ethAddress) {
setBalanceInvalidationSubscription()
markBalanceAsFresh()
let notification: Notification.Name?
let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized

Expand All @@ -284,8 +274,6 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
notification = nil
}

wallet.isBalanceInitialized = true

if isRaised {
await vibroService.applyVibration(.success)
}
Expand Down Expand Up @@ -367,23 +355,21 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
}.get()
}

private func setBalanceInvalidationSubscription() {
private func markBalanceAsFresh() {
ethWallet?.isBalanceInitialized = true

balanceInvalidationSubscription = Task { [weak self] in
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
self?.resetBalance()
guard let self, let wallet = ethWallet else { return }
wallet.isBalanceInitialized = false

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}.eraseToAnyCancellable()
}

private func resetBalance() {
ethWallet?.isBalanceInitialized = false
guard let wallet = ethWallet else { return }

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}
}

// MARK: - WalletInitiatedWithPassphrase
Expand Down
Loading