Skip to content

Commit

Permalink
Fix reSync logic (from background and change internet state)
Browse files Browse the repository at this point in the history
  • Loading branch information
ant013 committed Jun 14, 2024
1 parent 30f24bc commit 6b2738b
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@
11B354B5E42290EE934C428E /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B359697FC3E92D4111ED5D /* String.swift */; };
11B354B8BD1C3C036F6DE16A /* LitecoinAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B356861F703A5A5C6630B6 /* LitecoinAdapter.swift */; };
11B354BC4D954CCDA2E75C68 /* AddEvmSyncSourceViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B350B29037572DDAAF9E16 /* AddEvmSyncSourceViewModel.swift */; };
11B354C1218C0776499FAA5E /* Kmm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35D813B2B43683404CCD6 /* Kmm.swift */; };
11B354CAD4BC4FAB3889838D /* EvmSyncSourceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35D9C2409FD9060974F67 /* EvmSyncSourceManager.swift */; };
11B354CF393A2EAFDABE1C47 /* WalletTokenListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B357D222B4819BE881E182 /* WalletTokenListViewController.swift */; };
11B354D628AADF3AFD9123E1 /* SingleCoinPriceWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B351F5E57874D4517F67B7 /* SingleCoinPriceWidget.swift */; };
Expand Down Expand Up @@ -781,7 +780,6 @@
11B358C4D4C466ACCEF0E4C7 /* MultiSwapMainField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35D0D43137223A01FC2DA /* MultiSwapMainField.swift */; };
11B358C72B4E7F70331084AA /* SendEvmViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35113CB935A0E54504C1C /* SendEvmViewController.swift */; };
11B358D01760F90518DA612F /* SendHandlerFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B353B02ADF5EC5CC83FB33 /* SendHandlerFactory.swift */; };
11B358D0D4AE015DC9FECF29 /* Kmm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35D813B2B43683404CCD6 /* Kmm.swift */; };
11B358D1687049E5DACEBC96 /* AppManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B352884D47E0B23DCF2C2C /* AppManager.swift */; };
11B358D35D2270FD78C6EF82 /* AutoLockPeriod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B35E41142BD3D2FF59BAE7 /* AutoLockPeriod.swift */; };
11B358D519ACFE88A7823C7E /* ApiProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11B3531363949F235A210921 /* ApiProvider.swift */; };
Expand Down Expand Up @@ -3777,7 +3775,6 @@
11B35D6FC62F4797DEE1C419 /* StatStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatStorage.swift; sourceTree = "<group>"; };
11B35D747108CE6727D3103D /* HsToolKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HsToolKit.swift; sourceTree = "<group>"; };
11B35D805327837A9E81801C /* ManageAccountsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ManageAccountsViewController.swift; sourceTree = "<group>"; };
11B35D813B2B43683404CCD6 /* Kmm.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Kmm.swift; sourceTree = "<group>"; };
11B35D8AF9D337A98530548D /* Auditor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Auditor.swift; sourceTree = "<group>"; };
11B35D8B730D82D948B27210 /* ISendHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ISendHandler.swift; sourceTree = "<group>"; };
11B35D96B8963CDC30DC5643 /* NftViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NftViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5169,7 +5166,6 @@
ABC9A9B35C58F6525F3B2D5C /* FullCoin.swift */,
ABC9A830FE79DBF62FD63CC4 /* ThemeMode.swift */,
ABC9A3DFC1E03CB2E6C12F2C /* Encodable.swift */,
11B35D813B2B43683404CCD6 /* Kmm.swift */,
D00DAE442B626C2900F48E1D /* GasPrice.swift */,
ABC9A448ABC30B93088DE978 /* Binding.swift */,
11B35ED0A8819AB7EA27D368 /* StatExtensions.swift */,
Expand Down Expand Up @@ -10501,7 +10497,6 @@
11B35ACE7B126DCF9F7F1A19 /* SearchBar.swift in Sources */,
11B3546CB3C043D22A5F7A88 /* TransactionsViewModel.swift in Sources */,
11B35071705455BC25C214D2 /* TonAdapter.swift in Sources */,
11B358D0D4AE015DC9FECF29 /* Kmm.swift in Sources */,
11B3535D6A72ED1D564A0F7C /* TonOutgoingTransactionRecord.swift in Sources */,
11B35C60FE9B94994FCCB0CB /* TonTransactionRecord.swift in Sources */,
11B3589CF4D819A0430DE3D9 /* TonIncomingTransactionRecord.swift in Sources */,
Expand Down Expand Up @@ -11957,7 +11952,6 @@
11B358781EBEFCE7CED000F0 /* SearchBar.swift in Sources */,
11B358D91C9D8102C46B97ED /* TransactionsViewModel.swift in Sources */,
11B35D88633A14FD13E91702 /* TonAdapter.swift in Sources */,
11B354C1218C0776499FAA5E /* Kmm.swift in Sources */,
11B3589C124F6BBDDBB144F4 /* TonOutgoingTransactionRecord.swift in Sources */,
D389BC4F2C0DEF1800724504 /* MarketAdvancedSearchResultsView.swift in Sources */,
11B35EB4CAA93773DF09B479 /* TonTransactionRecord.swift in Sources */,
Expand Down Expand Up @@ -12870,7 +12864,7 @@
repositoryURL = "https://github.com/horizontalsystems/TonKit.Swift";
requirement = {
kind = exactVersion;
version = 0.1.0;
version = 0.2.0;
};
};
6BF66DD82BA1A73300963242 /* XCRemoteSwiftPackageReference "ObjectMapper" */ = {
Expand Down
76 changes: 39 additions & 37 deletions UnstoppableWallet/UnstoppableWallet/Core/Adapters/TonAdapter.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import BigInt
import Combine
import Foundation
import HdWalletKit
import HsToolKit
import MarketKit
import RxSwift
import TonKit
import TweetNacl
import TonSwift
import BigInt
import TweetNacl

class TonAdapter {
private static let coinRate: Decimal = 1_000_000_000
Expand All @@ -18,11 +18,15 @@ class TonAdapter {
private let transactionSource: TransactionSource
private let baseToken: Token
private let reachabilityManager = App.shared.reachabilityManager
private let appManager = App.shared.appManager

private var cancellables = Set<AnyCancellable>()

private var adapterStarted = false
private var kitStarted = false

private let logger: Logger?

private let adapterStateSubject = PublishSubject<AdapterState>()
private(set) var adapterState: AdapterState {
didSet {
Expand All @@ -43,12 +47,15 @@ class TonAdapter {
transactionSource = wallet.transactionSource
self.baseToken = baseToken

// logger = Logger(minLogLevel: .debug)
logger = App.shared.logger.scoped(with: "TonKit")

switch wallet.account.type {
case .mnemonic:
guard let seed = wallet.account.type.mnemonicSeed else {
throw AdapterError.unsupportedAccount
}

let hdWallet = HDWallet(seed: seed, coinType: 607, xPrivKey: 0, curve: .ed25519)
let privateKey = try hdWallet.privateKey(account: 0)
let privateRaw = Data(privateKey.raw.bytes)
Expand All @@ -61,7 +68,7 @@ class TonAdapter {
network: .mainNet,
walletId: wallet.account.id,
apiKey: nil,
minLogLevel: .debug
logger: logger
)

case let .tonAddress(address):
Expand All @@ -71,15 +78,14 @@ class TonAdapter {
network: .mainNet,
walletId: wallet.account.id,
apiKey: nil,
minLogLevel: .debug
logger: logger
)
default:
throw AdapterError.unsupportedAccount
}

ownAddress = tonKit.address


adapterState = Self.adapterState(kitSyncState: tonKit.syncState)
balanceData = BalanceData(available: Self.amount(kitAmount: tonKit.balance))

Expand All @@ -95,23 +101,17 @@ class TonAdapter {
}
.store(in: &cancellables)

reachabilityManager.$isReachable
.sink { [weak self] isReachable in
self?.handle(isReachable: isReachable)
appManager.didEnterBackgroundPublisher
.sink { [weak self] in
self?.stop()
}
.store(in: &cancellables)
}

private func handle(isReachable: Bool) {
guard adapterStarted else {
return
}

if isReachable, !kitStarted {
startKit()
} else if !isReachable, kitStarted {
stopKit()
}
appManager.willEnterForegroundPublisher
.sink { [weak self] in
self?.start()
}
.store(in: &cancellables)
}

private func handle(tonTransactions: [TonKit.FullTransaction]) {
Expand All @@ -136,7 +136,7 @@ class TonAdapter {
}

static func amount(kitAmount: Decimal) -> Decimal {
return kitAmount / coinRate
kitAmount / coinRate
}

private func transactionRecord(tonTransaction tx: TonKit.FullTransaction) -> TonTransactionRecord {
Expand All @@ -159,15 +159,15 @@ class TonAdapter {
)

default:
return TonTransactionRecord(
source: .init(blockchainType: .ton, meta: nil),
event: tx.event,
feeToken: baseToken
)
return TonTransactionRecord(
source: .init(blockchainType: .ton, meta: nil),
event: tx.event,
feeToken: baseToken
)
}
}
private func tagQuery(token: MarketKit.Token?, filter: TransactionTypeFilter, address: String?) -> TransactionTagQuery {

private func tagQuery(token _: MarketKit.Token?, filter: TransactionTypeFilter, address: String?) -> TransactionTagQuery {
var type: TransactionTag.TagType?

switch filter {
Expand All @@ -182,11 +182,13 @@ class TonAdapter {
}

private func startKit() {
logger?.log(level: .debug, message: "TonAdapter, start kit.")
tonKit.start()
kitStarted = true
}

private func stopKit() {
logger?.log(level: .debug, message: "TonAdapter, stop kit.")
tonKit.stop()
kitStarted = false
}
Expand Down Expand Up @@ -220,7 +222,7 @@ extension TonAdapter: IAdapter {
}

var statusInfo: [(String, Any)] {
[]
[] // tonKit.statusInfo()
}

var debugInfo: String {
Expand All @@ -236,7 +238,7 @@ extension TonAdapter: IBalanceAdapter {
var balanceDataUpdatedObservable: Observable<BalanceData> {
balanceDataSubject.asObservable()
}

var balanceState: AdapterState {
adapterState
}
Expand Down Expand Up @@ -295,23 +297,23 @@ extension TonAdapter: ITransactionsAdapter {

Task { [weak self] in
let address = address.flatMap { try? FriendlyAddress(string: $0) }?.address.toRaw()
let beforeLt = (from as? TonTransactionRecord).map { $0.lt }

let beforeLt = (from as? TonTransactionRecord).map(\.lt)
var tagQueries = [TransactionTagQuery]()
switch filter {
case .all: ()
case .incoming: tagQueries.append(.init(type: .incoming, address: address))
case .outgoing: tagQueries.append(.init(type: .outgoing, address: address))
default: observer(.success([]))
}

let txs = (self?.tonKit
.transactions(tagQueries: tagQueries, beforeLt: beforeLt, limit: limit)
.compactMap { self?.transactionRecord(tonTransaction: $0) }) ?? []

observer(.success(txs))
}

return Disposables.create()
}
}
Expand All @@ -332,11 +334,11 @@ extension TonAdapter: ISendTonAdapter {

func estimateFee(recipient: String, amount: Decimal, memo: String?) async throws -> Decimal {
let amount = (amount * Self.coinRate).rounded(decimal: 0)

let kitAmount = try await tonKit.estimateFee(recipient: recipient, amount: amount, comment: memo)
return Self.amount(kitAmount: kitAmount)
}

func send(recipient: String, amount: Decimal, memo: String?) async throws {
let amount = (amount * Self.coinRate).rounded(decimal: 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class AppManager {

private let didBecomeActiveSubject = PublishSubject<Void>()
private let willEnterForegroundSubjectOld = PublishSubject<Void>()
private let didEnterBackgroundSubject = PassthroughSubject<Void, Never>()
private let willEnterForegroundSubject = PassthroughSubject<Void, Never>()

init(accountManager: AccountManager, walletManager: WalletManager, adapterManager: AdapterManager, lockManager: LockManager,
Expand Down Expand Up @@ -97,6 +98,8 @@ extension AppManager {
func didEnterBackground() {
debugBackgroundLogger?.logEnterBackground()

didEnterBackgroundSubject.send()

lockManager.didEnterBackground()
walletConnectSocketConnectionService.didEnterBackground()
balanceHiddenManager.didEnterBackground()
Expand Down Expand Up @@ -133,6 +136,10 @@ extension AppManager {
}

extension AppManager {
var didEnterBackgroundPublisher: AnyPublisher<Void, Never> {
didEnterBackgroundSubject.eraseToAnyPublisher()
}

var willEnterForegroundPublisher: AnyPublisher<Void, Never> {
willEnterForegroundSubject.eraseToAnyPublisher()
}
Expand Down
Loading

0 comments on commit 6b2738b

Please sign in to comment.