Skip to content

Commit

Permalink
Add method to get transactions after some hash
Browse files Browse the repository at this point in the history
  • Loading branch information
omurovch committed Jan 21, 2025
1 parent 0c770e3 commit 027165f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ class EthereumKit(
return transactionManager.getFullTransactionSingle(hash)
}

fun getFullTransactionsAfterSingle(hash: ByteArray?): Single<List<FullTransaction>> {
return transactionManager.getFullTransactionsAfterSingle(hash)
}

fun estimateGas(to: Address?, value: BigInteger, gasPrice: GasPrice): Single<Long> {
// without address - provide default gas limit
if (to == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ interface ITransactionStorage {

fun saveTags(tags: List<TransactionTag>)
fun getDistinctTokenContractAddresses(): List<String>

fun getTransactionsAfterSingle(hash: ByteArray?): Single<List<Transaction>>
}

interface IEip20Storage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ class TransactionManager(
return fullRpcTransactionSingle.map { decorationManager.decorateFullRpcTransaction(it) }
}

fun getFullTransactionsAfterSingle(fromHash: ByteArray? = null): Single<List<FullTransaction>> =
storage.getTransactionsAfterSingle(fromHash)
.map { transactions ->
decorationManager.decorateTransactions(transactions)
}

private fun failPendingTransactions(): List<Transaction> {
val pendingTransactions = storage.getPendingTransactions()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface TransactionDao {
fun getTransactions(hashes: List<ByteArray>): List<Transaction>

@RawQuery
fun getTransactionsBeforeAsync(query: SupportSQLiteQuery): Single<List<Transaction>>
fun getTransactionsByRawQuery(query: SupportSQLiteQuery): Single<List<Transaction>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(transactions: List<Transaction>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class TransactionStorage(database: TransactionDatabase) : ITransactionStorage {
$limitClause
"""

return transactionDao.getTransactionsBeforeAsync(SimpleSQLiteQuery(sqlQuery))
return transactionDao.getTransactionsByRawQuery(SimpleSQLiteQuery(sqlQuery))
}

override fun save(transactions: List<Transaction>) {
Expand Down Expand Up @@ -143,4 +143,39 @@ class TransactionStorage(database: TransactionDatabase) : ITransactionStorage {
override fun getDistinctTokenContractAddresses(): List<String> {
return tagsDao.getDistinctTokenContractAddresses()
}

override fun getTransactionsAfterSingle(hash: ByteArray?): Single<List<Transaction>> {
val whereConditions = mutableListOf<String>()
hash?.let { transactionDao.getTransaction(hash) }?.let { fromTransaction ->
val transactionIndex = fromTransaction.transactionIndex ?: 0
val fromCondition = """
(
tx.timestamp > ${fromTransaction.timestamp} OR
(
tx.timestamp = ${fromTransaction.timestamp} AND
tx.transactionIndex > $transactionIndex
) OR
(
tx.timestamp = ${fromTransaction.timestamp} AND
tx.transactionIndex = $transactionIndex AND
HEX(tx.hash) > "${fromTransaction.hash.toRawHexString().uppercase()}"
)
)
"""

whereConditions.add(fromCondition)
}

val whereClause = if (whereConditions.isNotEmpty()) "WHERE ${whereConditions.joinToString(" AND ")}" else ""
val orderClause = "ORDER BY tx.timestamp, tx.transactionIndex, HEX(tx.hash)"

val sqlQuery = """
SELECT tx.*
FROM `Transaction` as tx
$whereClause
$orderClause
"""

return transactionDao.getTransactionsByRawQuery(SimpleSQLiteQuery(sqlQuery))
}
}

0 comments on commit 027165f

Please sign in to comment.