Skip to content

Commit

Permalink
Integrate Ton transactions with several transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Nov 29, 2023
1 parent 30738ac commit f160a45
Show file tree
Hide file tree
Showing 16 changed files with 40,598 additions and 40,403 deletions.

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,19 @@ class TonAdapter {

private func transactionRecord(tonTransaction tx: TonTransaction) -> TonTransactionRecord {
switch tx.type {
case "Incoming":
case TransactionType.incoming:
return TonIncomingTransactionRecord(
source: transactionSource,
transaction: tx,
feeToken: baseToken,
token: baseToken
)
case "Outgoing":
case TransactionType.outgoing:
return TonOutgoingTransactionRecord(
source: transactionSource,
transaction: tx,
feeToken: baseToken,
token: baseToken,
sentToSelf: ownAddress == tx.dest
token: baseToken
)
default:
return TonTransactionRecord(
Expand Down Expand Up @@ -278,7 +277,6 @@ extension TonAdapter: ITransactionsAdapter {
case (_, .all): return transaction
case (is TonIncomingTransactionRecord, .incoming): return transaction
case (is TonOutgoingTransactionRecord, .outgoing): return transaction
case let (tx as TonOutgoingTransactionRecord, .incoming): return tx.sentToSelf ? transaction : nil
default: return nil
}
}
Expand Down
269 changes: 118 additions & 151 deletions UnstoppableWallet/UnstoppableWallet/Core/App.swift

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import MarketKit
import TonKitKmm

class TonIncomingTransactionRecord: TonTransactionRecord {
let value: TransactionValue
let from: String
let transfer: Transfer?

init(source: TransactionSource, transaction: TonTransaction, feeToken: Token, token: Token) {
let tonValue: Decimal = transaction.value_.map { TonAdapter.amount(kitAmount: $0) } ?? 0
value = .coinValue(token: token, value: tonValue)
from = transaction.src ?? ""
transfer = transaction.transfers.first.map { transfer in
Transfer(
address: transfer.src,
value: .coinValue(token: token, value: TonAdapter.amount(kitAmount: transfer.amount))
)
}

super.init(source: source, transaction: transaction, feeToken: feeToken)
}

override var mainValue: TransactionValue? {
value
transfer?.value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@ import MarketKit
import TonKitKmm

class TonOutgoingTransactionRecord: TonTransactionRecord {
let value: TransactionValue
let to: String
let sentToSelf: Bool
let transfers: [Transfer]
let totalValue: TransactionValue

init(source: TransactionSource, transaction: TonTransaction, feeToken: Token, token: Token, sentToSelf: Bool) {
let tonValue: Decimal = transaction.value_.map { TonAdapter.amount(kitAmount: $0) } ?? 0
value = .coinValue(token: token, value: Decimal(sign: .minus, exponent: tonValue.exponent, significand: tonValue.significand))
to = transaction.dest ?? ""
self.sentToSelf = sentToSelf
init(source: TransactionSource, transaction: TonTransaction, feeToken: Token, token: Token) {
var totalAmount: Decimal = 0

transfers = transaction.transfers.map { transfer in
let tonValue = TonAdapter.amount(kitAmount: transfer.amount)
let value = Decimal(sign: .minus, exponent: tonValue.exponent, significand: tonValue.significand)

totalAmount += value

return Transfer(
address: transfer.dest,
value: .coinValue(token: token, value: value)
)
}

totalValue = .coinValue(token: token, value: totalAmount)

super.init(source: source, transaction: transaction, feeToken: feeToken)
}

override var mainValue: TransactionValue? {
value
totalValue
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ class TonTransactionRecord: TransactionRecord {
.completed
}
}

extension TonTransactionRecord {
struct Transfer {
let address: String
let value: TransactionValue
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ class TransactionInfoService {
tokens.append(tx.value.token)

case let tx as TonIncomingTransactionRecord:
tokens.append(tx.value.token)
tokens.append(tx.transfer?.value.token)
case let tx as TonOutgoingTransactionRecord:
tx.fee.flatMap { tokens.append($0.token) }
tokens.append(tx.value.token)
tokens.append(tx.fee?.token)
tx.transfers.forEach { tokens.append($0.value.token) }
case let tx as TonTransactionRecord:
tx.fee.flatMap { tokens.append($0.token) }
tokens.append(tx.fee?.token)

default: ()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,13 @@ class TransactionInfoViewItemFactory {
feeViewItem = .fee(title: "tx_info.fee".localized, value: feeString(transactionValue: record.fee, rate: _rate(record.fee)))

case let record as TonIncomingTransactionRecord:
sections.append(receiveSection(source: record.source, transactionValue: record.value, from: record.from, rates: item.rates, balanceHidden: balanceHidden))
if let transfer = record.transfer {
sections.append(receiveSection(source: record.source, transactionValue: transfer.value, from: transfer.address, rates: item.rates, balanceHidden: balanceHidden))
}

case let record as TonOutgoingTransactionRecord:
sections.append(sendSection(source: record.source, transactionValue: record.value, to: record.to, rates: item.rates, sentToSelf: record.sentToSelf, balanceHidden: balanceHidden))

if record.sentToSelf {
sections.append([.sentToSelf])
for transfer in record.transfers {
sections.append(sendSection(source: record.source, transactionValue: transfer.value, to: transfer.address, rates: item.rates, balanceHidden: balanceHidden))
}

feeViewItem = record.fee.map { .fee(title: "tx_info.fee".localized, value: feeString(transactionValue: $0, rate: _rate($0))) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,28 +394,42 @@ class TransactionsViewItemFactory {
subTitle = "transactions.unknown_transaction.description".localized()

case let record as TonIncomingTransactionRecord:
iconType = singleValueIconType(source: record.source, value: record.value)
title = "transactions.receive".localized
subTitle = "transactions.from".localized(mapped(address: record.from, blockchainType: item.record.source.blockchainType))
if let transfer = record.transfer {
iconType = singleValueIconType(source: record.source, value: transfer.value)
subTitle = "transactions.from".localized(mapped(address: transfer.address, blockchainType: item.record.source.blockchainType))
primaryValue = BaseTransactionsViewModel.Value(text: coinString(from: transfer.value), type: type(value: transfer.value, .incoming))
} else {
iconType = .localIcon(imageName: item.record.source.blockchainType.iconPlain32)
subTitle = ""
}

primaryValue = BaseTransactionsViewModel.Value(text: coinString(from: record.value), type: type(value: record.value, .incoming))
if let currencyValue = item.currencyValue {
secondaryValue = BaseTransactionsViewModel.Value(text: currencyString(from: currencyValue), type: .secondary)
}

case let record as TonOutgoingTransactionRecord:
iconType = singleValueIconType(source: record.source, value: record.value)
title = "transactions.send".localized
subTitle = "transactions.to".localized(mapped(address: record.to, blockchainType: item.record.source.blockchainType))

primaryValue = BaseTransactionsViewModel.Value(text: coinString(from: record.value, showSign: !record.sentToSelf), type: type(value: record.value, condition: record.sentToSelf, .neutral, .outgoing))
if !record.transfers.isEmpty {
iconType = singleValueIconType(source: record.source, value: record.transfers[0].value)

if record.transfers.count == 1 {
subTitle = "transactions.to".localized(mapped(address: record.transfers[0].address, blockchainType: item.record.source.blockchainType))
} else {
subTitle = "transactions.multiple".localized
}

primaryValue = BaseTransactionsViewModel.Value(text: coinString(from: record.totalValue), type: type(value: record.totalValue, .outgoing))
} else {
iconType = .localIcon(imageName: item.record.source.blockchainType.iconPlain32)
subTitle = ""
}

if let currencyValue = item.currencyValue {
secondaryValue = BaseTransactionsViewModel.Value(text: currencyString(from: currencyValue), type: .secondary)
}

sentToSelf = record.sentToSelf

case is TonTransactionRecord:
iconType = .localIcon(imageName: item.record.source.blockchainType.iconPlain32)
title = "transactions.unknown_transaction.title".localized
Expand Down

0 comments on commit f160a45

Please sign in to comment.