Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class KNTransactionCoordinator {
self.isLoadingEnabled = true
self.startUpdatingCompletedTransactions()
self.startUpdatingPendingTransactions()

// remove some old txs
let oldKyberTxs = self.transactionStorage.kyberMinedTransactions.filter({ return Date().timeIntervalSince($0.date) >= 7.0 * 24.0 * 60.0 * 60.0 })
self.transactionStorage.delete(oldKyberTxs)
}

func stop() {
Expand Down Expand Up @@ -266,6 +270,7 @@ extension KNTransactionCoordinator {
KNAppTracker.updateInternalTransactionLastBlockLoad(lastBlockLoaded, for: self.wallet.address)
let eth = KNSupportedTokenStorage.shared.ethToken
let transactions = data.map({ KNTokenTransaction(internalDict: $0, eth: eth).toTransaction() })
self.handleReceiveEtherOrToken(transactions)
self.transactionStorage.add(transactions)
KNNotificationUtil.postNotification(for: kTokenTransactionListDidUpdateNotificationKey)
print("---- Internal Token Transactions: Loaded \(transactions.count) transactions ----")
Expand Down Expand Up @@ -304,6 +309,7 @@ extension KNTransactionCoordinator {

func updateListTokenTransactions(_ transactions: [Transaction]) {
if transactions.isEmpty { return }
self.handleReceiveEtherOrToken(transactions)
self.transactionStorage.add(transactions)
KNNotificationUtil.postNotification(for: kTokenTransactionListDidUpdateNotificationKey)
var tokenObjects: [TokenObject] = []
Expand All @@ -317,6 +323,29 @@ extension KNTransactionCoordinator {
KNNotificationUtil.postNotification(for: kTokenObjectListDidUpdateNotificationKey)
}
}

fileprivate func handleReceiveEtherOrToken(_ transactions: [Transaction]) {
if transactions.isEmpty { return }
if KNAppTracker.transactionLoadState(for: wallet.address) != .done { return }
let receivedTxs = transactions.filter({ return $0.to.lowercased() == wallet.address.description.lowercased() && $0.state == .completed }).sorted(by: { return $0.date > $1.date })
if let latestReceiveTx = receivedTxs.first {
let title = NSLocalizedString("received.token", value: "Received %@", comment: "")
let message = NSLocalizedString("successfully.received", value: "Successfully received %@ from %@", comment: "")
if self.transactionStorage.getKyberTransaction(forPrimaryKey: latestReceiveTx.id) != nil { return } // swap/transfer transaction
if self.transactionStorage.get(forPrimaryKey: latestReceiveTx.compoundKey) != nil { return } // already done transaction
let address = "\(latestReceiveTx.from.prefix(12))...\(latestReceiveTx.from.suffix(10))"

guard let symbol = latestReceiveTx.getTokenSymbol() else { return }
let amount = "\(latestReceiveTx.value) \(symbol)"
let userInfo = ["transaction_hash": "\(latestReceiveTx.id)"]

KNNotificationUtil.localPushNotification(
title: String(format: title, symbol),
body: String(format: message, arguments: [amount, address]),
userInfo: userInfo
)
}
}
}

// MARK: Pending transactions
Expand All @@ -339,9 +368,6 @@ extension KNTransactionCoordinator {

@objc func shouldUpdatePendingTransaction(_ sender: Any?) {
let objects = self.transactionStorage.kyberPendingTransactions
if objects.isEmpty {
self.transactionStorage.deleteAllKyberTransactions()
}
objects.forEach {
if self.isLoadingEnabled { self.updatePendingTransaction($0) }
}
Expand All @@ -353,7 +379,7 @@ extension KNTransactionCoordinator {
guard let `self` = self else { return }
self.externalProvider.getTransactionByHash(transaction.id, completion: { [weak self] sessionError in
guard let `self` = self else { return }
guard let trans = self.transactionStorage.get(forPrimaryKey: transaction.id) else { return }
guard let trans = self.transactionStorage.getKyberTransaction(forPrimaryKey: transaction.id) else { return }
if trans.state != .pending {
// Prevent the notification is called multiple time due to timer runs
return
Expand Down Expand Up @@ -400,7 +426,6 @@ extension KNTransactionCoordinator {
object: newTx.id,
userInfo: nil
)
self?.transactionStorage.delete([transaction])
completion(nil)
case .failure(let error):
completion(error)
Expand Down
3 changes: 3 additions & 0 deletions KyberNetwork/KyberNetwork/Utilities/KNNotificationUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ let kIEOTxListUpdateNotificationKey = "kIEOTxListUpdateNotificationKey"

let kUserWalletsListUpdatedNotificationKey = "kUserWalletsListUpdatedNotificationKey"

// Receive txs
let kNewReceivedTransactionKey = "kNewReceivedTransactionKey"

class KNNotificationUtil {

static func postNotification(for key: String, object: Any? = nil, userInfo: [AnyHashable: Any]? = nil) {
Expand Down
2 changes: 2 additions & 0 deletions KyberNetwork/Localization/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@
"can.not.decode.data" = "Can not decode data";
"something.went.wrong.please.try.again" = "Something went wrong, please try again";
"please.send.your.request.to.support" = "Please send your request to support@kyber.network";
"successfully.received" = "Successfully received %@ from %@";
"received.token" = "Received %@";

// Promo code error messages
"Promo code is invalid!" = "Promo code is invalid!";
Expand Down
2 changes: 2 additions & 0 deletions KyberNetwork/Localization/ko.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@
"new" = "새로운";
"can.not.update.exchange.rate" = "환율을 업데이트할 수 없습니다.";

"successfully.received" = "%@ 에서 %@ 성공적으로 받았습니다.";
"received.token" = "받은 %@";

// MARK: Price alert feature
"Cap reached" = "Cap reached";
Expand Down
3 changes: 3 additions & 0 deletions KyberNetwork/Localization/vi.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@
"something.went.wrong.please.try.again" = "Đã xảy ra lỗi, vui lòng thử lại";
"please.send.your.request.to.support" = "Vui lòng gửi yêu cầu của bạn đến support@kyber.network";

"successfully.received" = "Thành công nhận được %@ từ %@";
"received.token" = "Đã nhận %@";

// MARK: Price alert feature
"Cap reached" = "Đạt mức cho phép";
"You can only have maximum of 10 alerts" = "Bạn chỉ có thể có tối đa 10 thông báo";
Expand Down
3 changes: 3 additions & 0 deletions KyberNetwork/Localization/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@
"new" = "新";
"can.not.update.exchange.rate" = "无法更新汇率";

"successfully.received" = "成功接收%@从%@";
"received.token" = "收到%@";

// MARK: Price alert feature
"Cap reached" = "达到了限额";
"You can only have maximum of 10 alerts" = "您最多只能有10个提醒";
Expand Down
7 changes: 7 additions & 0 deletions KyberNetwork/Transactions/Storage/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,11 @@ extension Transaction {
isDisabled: false
)
}

func getTokenSymbol() -> String? {
guard let localObject = self.localizedOperations.first, localObject.type == "transfer" else {
return nil
}
return localObject.symbol
}
}