Skip to content

Commit

Permalink
Adopt tron send to new send module
Browse files Browse the repository at this point in the history
  • Loading branch information
esen committed Jun 14, 2024
1 parent 6c95c2c commit cb564c0
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 14 deletions.
12 changes: 12 additions & 0 deletions UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,10 @@
D0A6902F2C04969300E59296 /* CautionDataSourceViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A6902D2C04969300E59296 /* CautionDataSourceViewModel.swift */; };
D0A690342C05D01C00E59296 /* UIImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A690332C05D01C00E59296 /* UIImageView.swift */; };
D0A690352C05D01C00E59296 /* UIImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A690332C05D01C00E59296 /* UIImageView.swift */; };
D0A690252BFCB51800E59296 /* TronPresendHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A690242BFCB51800E59296 /* TronPresendHandler.swift */; };
D0A690262BFCB51800E59296 /* TronPresendHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A690242BFCB51800E59296 /* TronPresendHandler.swift */; };
D0A690282BFF50D900E59296 /* TronSendData.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A690272BFF50D900E59296 /* TronSendData.swift */; };
D0A690292BFF50D900E59296 /* TronSendData.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A690272BFF50D900E59296 /* TronSendData.swift */; };
D0A980A92B5E3C0900127AF4 /* StepChangeButtonsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A980A82B5E3C0900127AF4 /* StepChangeButtonsView.swift */; };
D0A980AA2B5E3C0900127AF4 /* StepChangeButtonsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A980A82B5E3C0900127AF4 /* StepChangeButtonsView.swift */; };
D0A980AF2B60E73F00127AF4 /* LegacyFeeSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A980AE2B60E73F00127AF4 /* LegacyFeeSettingsView.swift */; };
Expand Down Expand Up @@ -4508,6 +4512,8 @@
D0A6902A2C00ACF600E59296 /* CautionDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CautionDataSource.swift; sourceTree = "<group>"; };
D0A6902D2C04969300E59296 /* CautionDataSourceViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CautionDataSourceViewModel.swift; sourceTree = "<group>"; };
D0A690332C05D01C00E59296 /* UIImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIImageView.swift; sourceTree = "<group>"; };
D0A690242BFCB51800E59296 /* TronPresendHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TronPresendHandler.swift; sourceTree = "<group>"; };
D0A690272BFF50D900E59296 /* TronSendData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TronSendData.swift; sourceTree = "<group>"; };
D0A980A82B5E3C0900127AF4 /* StepChangeButtonsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StepChangeButtonsView.swift; sourceTree = "<group>"; };
D0A980AE2B60E73F00127AF4 /* LegacyFeeSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LegacyFeeSettingsView.swift; sourceTree = "<group>"; };
D0C2260F2A66A3BC007101F7 /* PersonalSupportModule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersonalSupportModule.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -8554,6 +8560,8 @@
D3A580872BE4DAA2003953F4 /* EvmSendData.swift */,
D3A5808A2BE4DB11003953F4 /* WalletConnectSendHandler.swift */,
D0E5E8542BE38AA2005080A4 /* TronSendHandler.swift */,
D0A690242BFCB51800E59296 /* TronPresendHandler.swift */,
D0A690272BFF50D900E59296 /* TronSendData.swift */,
D054DAE22BE5123F0040B7C9 /* InitialTransactionSettings.swift */,
D3A580932BE8AA80003953F4 /* BitcoinSendSettingsView.swift */,
D3A580962BE8AA90003953F4 /* BitcoinSendSettingsViewModel.swift */,
Expand Down Expand Up @@ -9179,6 +9187,7 @@
D36DE0FD272FD92F000BC916 /* SwapSelectProviderService.swift in Sources */,
D008CA67267C8E1800001E0A /* ApproveTransactionRecord.swift in Sources */,
D09D768F2A2E06D6004311E6 /* SendTronConfirmationViewController.swift in Sources */,
D0A690262BFCB51800E59296 /* TronPresendHandler.swift in Sources */,
D384057F218317DF007D50AD /* LaunchService.swift in Sources */,
D3840581218317DF007D50AD /* App.swift in Sources */,
D3840583218317DF007D50AD /* BlurManager.swift in Sources */,
Expand Down Expand Up @@ -10567,6 +10576,7 @@
11B35337D37FD03982571DF0 /* ThorChainMultiSwapProvider.swift in Sources */,
ABC9AE635D02A4E6DA719787 /* TransactionContactSelectView.swift in Sources */,
ABC9AD02AD6340C230AC4F9D /* TransactionContactSelectViewModel.swift in Sources */,
D0A690292BFF50D900E59296 /* TronSendData.swift in Sources */,
ABC9AC0F2A4A48BB223005EA /* TransactionBlockchainSelectViewModel.swift in Sources */,
ABC9A24A0A2F6C7C476D7F69 /* TransactionTokenSelectViewModel.swift in Sources */,
11B35EFEEB7F361BD5FCCC3D /* SendView.swift in Sources */,
Expand Down Expand Up @@ -10632,6 +10642,7 @@
D04D98EA268055A2001A3135 /* TransactionRecord.swift in Sources */,
D008CA66267C8E1800001E0A /* ApproveTransactionRecord.swift in Sources */,
D384066E21831B3D007D50AD /* LaunchService.swift in Sources */,
D0A690252BFCB51800E59296 /* TronPresendHandler.swift in Sources */,
D384067021831B3D007D50AD /* App.swift in Sources */,
D384067221831B3D007D50AD /* BlurManager.swift in Sources */,
D384067421831B3D007D50AD /* MainSettingsModule.swift in Sources */,
Expand Down Expand Up @@ -12020,6 +12031,7 @@
ABC9AB3C239A182E9B93AFDA /* MultiSwapApproveViewModel.swift in Sources */,
ABC9A62E858360A9964084D2 /* Binding.swift in Sources */,
ABC9AAF24F259B71E59C247D /* ErrorViewController.swift in Sources */,
D0A690282BFF50D900E59296 /* TronSendData.swift in Sources */,
11B357696EB7DB1895BF9F23 /* ThorChainMultiSwapProvider.swift in Sources */,
ABC9A5B9D81C00155732363C /* TransactionContactSelectView.swift in Sources */,
ABC9A6C99739F0AC308F0574 /* TransactionContactSelectViewModel.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extension TronTransactionsAdapter: ITransactionsAdapter {
return nil
}

return TokenQuery(blockchainType: tronKitWrapper.blockchainType, tokenType: tokenType)
return TokenQuery(blockchainType: .tron, tokenType: tokenType)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TronTransactionConverter {
}

private func eip20Value(tokenAddress: TronKit.Address, value: BigUInt, sign: FloatingPointSign, tokenInfo: TokenInfo?) -> TransactionValue {
let query = TokenQuery(blockchainType: tronKitWrapper.blockchainType, tokenType: .eip20(address: tokenAddress.base58))
let query = TokenQuery(blockchainType: .tron, tokenType: .eip20(address: tokenAddress.base58))

if let token = try? coinManager.token(query: query) {
let value = convertAmount(amount: value, decimals: token.decimals, sign: sign)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ class AdapterFactory {
}

private func tronAdapter(wallet: Wallet) -> IAdapter? {
guard let tronKitWrapper = try? tronKitManager.tronKitWrapper(account: wallet.account, blockchainType: .tron) else {
guard let tronKitWrapper = try? tronKitManager.tronKitWrapper(account: wallet.account) else {
return nil
}

return TronAdapter(tronKitWrapper: tronKitWrapper)
}

private func trc20Adapter(address: String, wallet: Wallet) -> IAdapter? {
guard let tronKitWrapper = try? tronKitManager.tronKitWrapper(account: wallet.account, blockchainType: .tron) else {
guard let tronKitWrapper = try? tronKitManager.tronKitWrapper(account: wallet.account) else {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TronKitManager {
self.testNetManager = testNetManager
}

private func _tronKitWrapper(account: Account, blockchainType: BlockchainType) throws -> TronKitWrapper {
private func _tronKitWrapper(account: Account) throws -> TronKitWrapper {
if let _tronKitWrapper, let currentAccount, currentAccount == account {
return _tronKitWrapper
}
Expand Down Expand Up @@ -52,7 +52,7 @@ class TronKitManager {

tronKit.start()

let wrapper = TronKitWrapper(blockchainType: blockchainType, tronKit: tronKit, signer: signer)
let wrapper = TronKitWrapper(tronKit: tronKit, signer: signer)

_tronKitWrapper = wrapper
currentAccount = account
Expand All @@ -74,20 +74,18 @@ extension TronKitManager {
}
}

func tronKitWrapper(account: Account, blockchainType: BlockchainType) throws -> TronKitWrapper {
func tronKitWrapper(account: Account) throws -> TronKitWrapper {
try queue.sync {
try _tronKitWrapper(account: account, blockchainType: blockchainType)
try _tronKitWrapper(account: account)
}
}
}

class TronKitWrapper {
let blockchainType: BlockchainType
let tronKit: TronKit.Kit
let signer: Signer?

init(blockchainType: BlockchainType, tronKit: TronKit.Kit, signer: Signer?) {
self.blockchainType = blockchainType
init(tronKit: TronKit.Kit, signer: Signer?) {
self.tronKit = tronKit
self.signer = signer
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ struct CoinAnalyticsView: View {
VStack(spacing: .margin12) {
cardHeader(text: title, info: info)

let value = {
let value: String = {
switch viewItem {
case .preview: return Self.placeholderText
case let .regular(viewItem): return viewItem.value
Expand Down Expand Up @@ -698,7 +698,7 @@ struct CoinAnalyticsView: View {
VStack(spacing: .margin12) {
cardHeader(text: title)

let value = {
let value: String = {
switch viewItem.value {
case .preview: return Self.placeholderText
case let .regular(value): return value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import TronKit

enum FeeData {
case evm(evmFeeData: EvmFeeData)
case bitcoin(bitcoinFeeData: BitcoinFeeData)
case tron(fees: [Fee])

var gasLimit: Int? {
switch self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import BitcoinCore
import EvmKit
import Foundation
import MarketKit
import TronKit
import ZcashLightClientKit

enum SendData {
case evm(blockchainType: BlockchainType, transactionData: TransactionData)
case bitcoin(token: Token, params: SendParameters)
case binance(token: Token, amount: Decimal, address: String, memo: String?)
case zcash(amount: Decimal, recipient: Recipient, memo: String?)
case tron(token: Token, contract: Contract)
case swap(tokenIn: Token, tokenOut: Token, amountIn: Decimal, provider: IMultiSwapProvider)
case walletConnect(request: WalletConnectRequest)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import SwiftUI
enum SendField {
case amount(title: String, token: Token, coinValueType: CoinValueType, currencyValue: CurrencyValue?, type: AmountType)
case value(title: String, description: ActionSheetView.InfoDescription?, coinValue: CoinValue?, currencyValue: CurrencyValue?, formatFull: Bool)
case doubleValue(title: String, description: ActionSheetView.InfoDescription?, value1: String, value2: String?)
case levelValue(title: String, value: String, level: ValueLevel)
case address(title: String, value: String, blockchainType: BlockchainType)
case price(title: String, tokenA: Token, tokenB: Token, amountA: Decimal, amountB: Decimal)
Expand Down Expand Up @@ -105,6 +106,31 @@ enum SendField {
}
.buttonStyle(SecondaryCircleButtonStyle(style: .default))
}
case let .doubleValue(title, description, value1, value2):
ListRow(padding: EdgeInsets(top: .margin12, leading: description == nil ? .margin16 : 0, bottom: .margin12, trailing: .margin16)) {
if let description {
Text(title)
.textSubhead2()
.modifier(Informed(description: description))
} else {
Text(title)
.textSubhead2()
}

Spacer()

VStack(alignment: .trailing, spacing: 1) {
Text(value1)
.textSubhead1(color: .themeLeah)
.multilineTextAlignment(.trailing)

if let value2 {
Text(value2)
.textSubhead1(color: .themeLeah)
.multilineTextAlignment(.trailing)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ enum SendHandlerFactory {
return BinanceSendHandler.instance(token: token, amount: amount, address: address, memo: memo)
case let .zcash(amount, recipient, memo):
return ZcashSendHandler.instance(amount: amount, recipient: recipient, memo: memo)
case let .tron(token, contract):
return TronSendHandler.instance(token: token, contract: contract)
case let .swap(tokenIn, tokenOut, amountIn, provider):
return MultiSwapSendHandler.instance(tokenIn: tokenIn, tokenOut: tokenOut, amountIn: amountIn, provider: provider)
case let .walletConnect(request):
Expand All @@ -37,6 +39,10 @@ enum SendHandlerFactory {
return ZcashPreSendHandler(token: wallet.token, adapter: adapter)
}

if let adapter = adapter as? ISendTronAdapter & IBalanceAdapter {
return TronPreSendHandler(token: wallet.token, adapter: adapter)
}

return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SendViewModel: ObservableObject {
return false
}

guard let service = transactionService, !service.cautions.contains(where: { $0.type == .error }) else {
if let service = transactionService, service.cautions.contains(where: { $0.type == .error }) {
return false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import BigInt
import Combine
import Foundation
import MarketKit
import RxSwift
import TronKit

class TronPreSendHandler {
private let token: Token
private let adapter: ISendTronAdapter & IBalanceAdapter

private let stateSubject = PassthroughSubject<AdapterState, Never>()
private let balanceSubject = PassthroughSubject<Decimal, Never>()

private let disposeBag = DisposeBag()

init(token: Token, adapter: ISendTronAdapter & IBalanceAdapter) {
self.token = token
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 TronPreSendHandler: 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 sendData(amount: Decimal, address: String, memo: String?) -> SendDataResult {
guard let amountBigUInt = BigUInt(amount.hs.roundedString(decimal: token.decimals)) else {
return .invalid(cautions: [])
}

guard let tronAddress = try? TronKit.Address(address: address) else {
return .invalid(cautions: [])
}

guard tronAddress != adapter.tronKitWrapper.tronKit.receiveAddress else {
return .invalid(cautions: [CautionNew(text: "send.address_error.own_address".localized, type: .error)])
}

let contract = adapter.contract(amount: amountBigUInt, address: tronAddress, memo: memo)

return .valid(sendData: .tron(token: token, contract: contract))
}
}
Loading

0 comments on commit cb564c0

Please sign in to comment.