-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adopt native TON send to new Send module
- Loading branch information
Showing
6 changed files
with
326 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
UnstoppableWallet/UnstoppableWallet/Modules/SendNew/TonPresendHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import BigInt | ||
import Combine | ||
import Foundation | ||
import MarketKit | ||
import RxSwift | ||
import TonKit | ||
|
||
class TonPreSendHandler { | ||
private let adapter: ISendTonAdapter & IBalanceAdapter | ||
|
||
private let stateSubject = PassthroughSubject<AdapterState, Never>() | ||
private let balanceSubject = PassthroughSubject<Decimal, Never>() | ||
|
||
private let disposeBag = DisposeBag() | ||
|
||
init(adapter: ISendTonAdapter & IBalanceAdapter) { | ||
self.adapter = adapter | ||
|
||
adapter.balanceStateUpdatedObservable | ||
.observeOn(ConcurrentDispatchQueueScheduler(qos: .userInitiated)) | ||
.subscribe { [weak self] state in | ||
self?.stateSubject.send(state) | ||
} | ||
.disposed(by: disposeBag) | ||
|
||
adapter.balanceDataUpdatedObservable | ||
.observeOn(ConcurrentDispatchQueueScheduler(qos: .userInitiated)) | ||
.subscribe { [weak self] balanceData in | ||
self?.balanceSubject.send(balanceData.available) | ||
} | ||
.disposed(by: disposeBag) | ||
} | ||
} | ||
|
||
extension TonPreSendHandler: IPreSendHandler { | ||
var state: AdapterState { | ||
adapter.balanceState | ||
} | ||
|
||
var statePublisher: AnyPublisher<AdapterState, Never> { | ||
stateSubject.eraseToAnyPublisher() | ||
} | ||
|
||
var balance: Decimal { | ||
adapter.balanceData.available | ||
} | ||
|
||
var balancePublisher: AnyPublisher<Decimal, Never> { | ||
balanceSubject.eraseToAnyPublisher() | ||
} | ||
|
||
func hasMemo(address _: String?) -> Bool { | ||
true | ||
} | ||
|
||
func sendData(amount: Decimal, address: String, memo: String?) -> SendDataResult { | ||
do { | ||
try TonKit.Kit.validate(address: address) | ||
} catch { | ||
return .invalid(cautions: [CautionNew(text: error.smartDescription, type: .error)]) | ||
} | ||
|
||
return .valid(sendData: .ton(amount: amount, address: address, memo: memo)) | ||
} | ||
} |
Oops, something went wrong.