Skip to content

Commit

Permalink
Fix updating token balances
Browse files Browse the repository at this point in the history
  • Loading branch information
manhlx3006 committed Dec 30, 2019
1 parent 04c2fdd commit 171168c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,7 @@ class KNLoadBalanceCoordinator {
if isFetchingOtherTokensBalance { return }
isFetchingOtherTokensBalance = true

let tokenContracts = self.session.tokenStorage.tokens.filter({ return !$0.isETH && $0.isSupported }).sorted { (token0, token1) -> Bool in
if token0.value.isEmpty || token0.value == "0" { return false }
if token1.value.isEmpty || token1.value == "0" { return true }
return true
}.map({ $0.contract })
let tokenContracts = self.session.tokenStorage.tokens.filter({ return !$0.isETH && $0.isSupported }).map({ $0.contract })

let tokens = tokenContracts.map({ return Address(string: $0)! })

Expand All @@ -285,7 +281,11 @@ class KNLoadBalanceCoordinator {
if isFetchingOtherTokensBalance { return }
isFetchingOtherTokensBalance = true
var isBalanceChanged: Bool = false
let tokenContracts = self.session.tokenStorage.tokens.filter({ return !$0.isETH && $0.isSupported }).map({ $0.contract })
let tokenContracts = self.session.tokenStorage.tokens.filter({ return !$0.isETH && $0.isSupported }).sorted { (token0, token1) -> Bool in
if token0.value.isEmpty || token0.value == "0" { return false }
if token1.value.isEmpty || token1.value == "0" { return true }
return true
}.map({ $0.contract })
let currentWallet = self.session.wallet
let group = DispatchGroup()
var counter = 0
Expand Down Expand Up @@ -401,13 +401,16 @@ class KNLoadBalanceCoordinator {
}
var isBalanceChanged = false
self.session.externalProvider.getMultipleERC20Balances(tokens) { [weak self] result in
guard let `self` = self else { return }
guard let `self` = self else {
completion(.success(false))
return
}
switch result {
case .success(let values):
if values.count == tokens.count {
for id in 0..<values.count {
let balance = Balance(value: values[id])
let addr = tokens.description.lowercased()
let addr = tokens[id].description.lowercased()
if self.otherTokensBalance[addr] == nil || self.otherTokensBalance[addr]!.value != values[id] {
isBalanceChanged = true
}
Expand Down
2 changes: 1 addition & 1 deletion KyberNetwork/KyberNetwork/Storage/KNTokenStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class KNTokenStorage {
}

func updateBalance(for address: Address, balance: BigInt) {
if let token = self.tokens.first(where: { $0.contract == address.description.lowercased() }) {
if let token = self.tokens.first(where: { $0.contract.lowercased() == address.description.lowercased() }) {
if token.isInvalidated { return }
self.realm.beginWrite()
token.value = balance.description
Expand Down

0 comments on commit 171168c

Please sign in to comment.