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/DxdadJdD] Wallet balances update fix #622

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
15 changes: 3 additions & 12 deletions Adamant/Modules/Wallets/Adamant/AdmWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,17 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, @unchecked Sendable
admWallet = nil
return
}

let notify: Bool

let isRaised: Bool

if let wallet = admWallet {
isRaised = (wallet.balance < account.balance) && wallet.isBalanceInitialized
if wallet.balance != account.balance || wallet.isBalanceInitialized != !accountService.isBalanceExpired {
wallet.balance = account.balance
notify = true
} else if !wallet.isBalanceInitialized {
notify = true
} else {
notify = false
}
wallet.balance = account.balance
} else {
let wallet = AdmWallet(unicId: tokenUnicID, address: account.address)
wallet.balance = account.balance

admWallet = wallet
notify = true
isRaised = false
}

Expand All @@ -181,7 +171,8 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, @unchecked Sendable
if isRaised {
Task { @MainActor in vibroService.applyVibration(.success) }
}
if notify, let wallet = wallet {

if let wallet = wallet {
postUpdateNotification(with: wallet)
}
}
Expand Down
41 changes: 17 additions & 24 deletions Adamant/Modules/Wallets/Bitcoin/BtcWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,30 +283,18 @@ final class BtcWalletService: WalletCoreProtocol, @unchecked Sendable {

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

let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized

if wallet.balance != balance {
wallet.balance = balance
notification = walletUpdatedNotification
} else if !wallet.isBalanceInitialized {
notification = walletUpdatedNotification
} else {
notification = nil
}

if isRaised {
if wallet.balance < balance, wallet.isBalanceInitialized {
await vibroService.applyVibration(.success)
}

if let notification = notification {
NotificationCenter.default.post(
name: notification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}
wallet.balance = balance

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

setState(.upToDate)
Expand Down Expand Up @@ -548,11 +536,16 @@ extension BtcWalletService {
}

func getBalance(address: String) async throws -> Decimal {
let response: BtcBalanceResponse = try await btcApiService.request(waitsForConnectivity: false) { api, origin in
await api.sendRequestJsonResponse(origin: origin, path: BtcApiCommands.balance(for: address))
}.get()
do {
let response: BtcBalanceResponse = try await btcApiService.request(waitsForConnectivity: false) { api, origin in
await api.sendRequestJsonResponse(origin: origin, path: BtcApiCommands.balance(for: address))
}.get()

return response.value / BtcWalletService.multiplier
return response.value / BtcWalletService.multiplier
} catch {
print("--debug", error.localizedDescription)
return 0
}
}

func getFeeRate() async throws -> Decimal {
Expand Down
29 changes: 9 additions & 20 deletions Adamant/Modules/Wallets/Dash/DashWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,29 +256,18 @@ final class DashWalletService: WalletCoreProtocol, @unchecked Sendable {

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

if wallet.balance != balance {
wallet.balance = balance
notification = walletUpdatedNotification
} else if !wallet.isBalanceInitialized {
notification = walletUpdatedNotification
} else {
notification = nil
}

if isRaised {

if wallet.balance < balance, wallet.isBalanceInitialized {
await vibroService.applyVibration(.success)
}

if let notification = notification {
NotificationCenter.default.post(
name: notification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}
wallet.balance = balance

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

setState(.upToDate)
Expand Down
25 changes: 9 additions & 16 deletions Adamant/Modules/Wallets/Doge/DogeWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,25 +253,18 @@ final class DogeWalletService: WalletCoreProtocol, @unchecked Sendable {

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

if wallet.balance != balance {
wallet.balance = balance
notification = walletUpdatedNotification
} else if !wallet.isBalanceInitialized {
notification = walletUpdatedNotification
} else {
notification = nil
}

if isRaised {

if wallet.balance < balance, wallet.isBalanceInitialized {
await vibroService.applyVibration(.success)
}

if let notification = notification {
NotificationCenter.default.post(name: notification, object: self, userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet])
}
wallet.balance = balance

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

setState(.upToDate)
Expand Down
23 changes: 8 additions & 15 deletions Adamant/Modules/Wallets/ERC20/ERC20WalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,25 +262,18 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {

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

if wallet.balance != balance {
wallet.balance = balance
notification = walletUpdatedNotification
} else if !wallet.isBalanceInitialized {
notification = walletUpdatedNotification
} else {
notification = nil
}

if isRaised {
if wallet.balance < balance, wallet.isBalanceInitialized {
await vibroService.applyVibration(.success)
}

if let notification = notification {
NotificationCenter.default.post(name: notification, object: self, userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet])
}
wallet.balance = balance

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

setState(.upToDate)
Expand Down
27 changes: 9 additions & 18 deletions Adamant/Modules/Wallets/Ethereum/EthWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,30 +292,21 @@ final class EthWalletService: WalletCoreProtocol, @unchecked Sendable {

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

let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized

if wallet.balance != balance {
wallet.balance = balance
notification = walletUpdatedNotification
} else if !wallet.isBalanceInitialized {
notification = walletUpdatedNotification
} else {
notification = nil
}

if isRaised {

if wallet.balance < balance, wallet.isBalanceInitialized {
vibroService.applyVibration(.success)
}

if let notification = notification {
NotificationCenter.default.post(name: notification, object: self, userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet])
}
wallet.balance = balance

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

setState(.upToDate)

await calculateFee()
}

Expand Down
30 changes: 9 additions & 21 deletions Adamant/Modules/Wallets/Klayr/WalletService/KlyWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,30 +254,18 @@ private extension KlyWalletService {

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

let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized

if wallet.balance != balance {
wallet.balance = balance
notification = walletUpdatedNotification
} else if !wallet.isBalanceInitialized {
notification = walletUpdatedNotification
} else {
notification = nil
}

if isRaised {

if wallet.balance < balance, wallet.isBalanceInitialized {
await vibroService.applyVibration(.success)
}

if let notification = notification {
NotificationCenter.default.post(
name: notification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
)
}
wallet.balance = balance

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

setState(.upToDate)
Expand Down
9 changes: 5 additions & 4 deletions Adamant/Services/AdamantAccountService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,12 @@ extension AdamantAccountService {
}

markBalanceAsFresh()
self.account = account

if loggedAccount.balance != account.balance {
self.account = account
NotificationCenter.default.post(name: Notification.Name.AdamantAccountService.accountDataUpdated, object: self)
}
NotificationCenter.default.post(
name: .AdamantAccountService.accountDataUpdated,
object: self
)

state = .loggedIn
completion?(.success(account: account, alert: nil))
Expand Down