Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add address tags #304

Merged
merged 6 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ class ApproveEip20Decoration(
val contractAddress: Address,
val spender: Address,
val value: BigInteger
) : TransactionDecoration() {
) : TransactionDecoration {

override fun tags(): List<String> =
listOf(contractAddress.hex, TransactionTag.EIP20_APPROVE)
override fun tags() = listOf(contractAddress.hex, TransactionTag.EIP20_APPROVE)

companion object {
val signature = ContractEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ class OutgoingEip20Decoration(
val value: BigInteger,
val sentToSelf: Boolean,
val tokenInfo: TokenInfo?
) : TransactionDecoration() {
) : TransactionDecoration {

override fun tags(): List<String> =
listOf(contractAddress.hex, TransactionTag.EIP20_TRANSFER, TransactionTag.tokenOutgoing(contractAddress.hex), TransactionTag.OUTGOING)
override fun tags() = listOf(
contractAddress.hex,
TransactionTag.EIP20_TRANSFER,
TransactionTag.tokenOutgoing(contractAddress.hex),
TransactionTag.OUTGOING,
TransactionTag.toAddress(to.hex)
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class TransferEventInstance(
tags.add(TransactionTag.INCOMING)
}

tags.add(TransactionTag.fromAddress(from.hex))
tags.add(TransactionTag.toAddress(to.hex))

return tags
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.horizontalsystems.ethereumkit.core

import io.horizontalsystems.ethereumkit.decorations.DecorationManager
import io.horizontalsystems.ethereumkit.models.*
import io.horizontalsystems.ethereumkit.models.Address
import io.horizontalsystems.ethereumkit.models.FullRpcTransaction
import io.horizontalsystems.ethereumkit.models.FullTransaction
import io.horizontalsystems.ethereumkit.models.Transaction
import io.horizontalsystems.ethereumkit.models.TransactionData
import io.horizontalsystems.ethereumkit.models.TransactionTag
import io.reactivex.BackpressureStrategy
import io.reactivex.Flowable
import io.reactivex.Single
Expand Down Expand Up @@ -91,12 +96,15 @@ class TransactionManager(
val fullTransactions = decorationManager.decorateTransactions(transactions + failedTransactions)

val transactionWithTags = mutableListOf<TransactionWithTags>()
val allTags: MutableList<TransactionTag> = mutableListOf()
val allTags = mutableListOf<TransactionTag>()

fullTransactions.forEach { fullTransaction ->
val tags = fullTransaction.decoration.tags().map { TransactionTag(it, fullTransaction.transaction.hash) }
allTags.addAll(tags)
transactionWithTags.add(TransactionWithTags(fullTransaction, tags.map { it.name }))
val tags = fullTransaction.decoration.tags()
val transactionHash = fullTransaction.transaction.hash
val transactionTags = tags.map { TransactionTag(it, transactionHash) }

allTags.addAll(transactionTags)
transactionWithTags.add(TransactionWithTags(fullTransaction, tags))
}

storage.saveTags(allTags)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.horizontalsystems.ethereumkit.core.storage

import android.content.Context
import androidx.room.*
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverter
import androidx.room.TypeConverters
import io.horizontalsystems.ethereumkit.api.storage.RoomTypeConverters
import io.horizontalsystems.ethereumkit.models.InternalTransaction
import io.horizontalsystems.ethereumkit.models.Transaction
Expand All @@ -15,7 +19,7 @@ import io.horizontalsystems.ethereumkit.models.TransactionTag
TransactionTag::class,
TransactionSyncerState::class
],
version = 12,
version = 13,
exportSchema = false
)
@TypeConverters(RoomTypeConverters::class, TransactionDatabase.TypeConverters::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.horizontalsystems.ethereumkit.decorations

class ContractCreationDecoration : TransactionDecoration() {

override fun tags(): List<String> = listOf("contractCreation")

class ContractCreationDecoration : TransactionDecoration {
override fun tags() = listOf("contractCreation")
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import java.math.BigInteger
class IncomingDecoration(
val from: Address,
val value: BigInteger
) : TransactionDecoration() {
) : TransactionDecoration {

override fun tags(): List<String> =
listOf(TransactionTag.EVM_COIN, TransactionTag.EVM_COIN_INCOMING, TransactionTag.INCOMING)
override fun tags() = listOf(
TransactionTag.EVM_COIN,
TransactionTag.EVM_COIN_INCOMING,
TransactionTag.INCOMING,
TransactionTag.fromAddress(from.hex)
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ class OutgoingDecoration(
val to: Address,
val value: BigInteger,
val sentToSelf: Boolean
) : TransactionDecoration() {
) : TransactionDecoration {

override fun tags(): List<String> {
val tags = mutableListOf(TransactionTag.EVM_COIN, TransactionTag.EVM_COIN_OUTGOING, TransactionTag.OUTGOING)
override fun tags() = buildList {
addAll(listOf(TransactionTag.EVM_COIN, TransactionTag.EVM_COIN_OUTGOING, TransactionTag.OUTGOING))

if (sentToSelf) {
tags += listOf(TransactionTag.EVM_COIN_INCOMING, TransactionTag.INCOMING)
addAll(listOf(TransactionTag.EVM_COIN_INCOMING, TransactionTag.INCOMING))
}

return tags
add(TransactionTag.toAddress(to.hex))
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.horizontalsystems.ethereumkit.decorations

open class TransactionDecoration {
open fun tags(): List<String> = listOf()
interface TransactionDecoration {
fun tags(): List<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,37 @@ open class UnknownTransactionDecoration(
private val value: BigInteger?,
open val internalTransactions: List<InternalTransaction>,
open val eventInstances: List<ContractEventInstance>
) : TransactionDecoration() {
) : TransactionDecoration {

override fun tags(): List<String> = (tagsFromInternalTransactions + tagsFromEventInstances).toSet().toList()
override fun tags(): List<String> = (tagsFromInternalTransactions + tagsFromEventInstances).distinct()

private val tagsFromInternalTransactions: List<String>
get() {
var outgoingValue = if (fromAddress == userAddress) value ?: BigInteger.ZERO else BigInteger.ZERO
for (internalTx in internalTransactions.filter { it.from == userAddress }) {
outgoingValue += internalTx.value
}

var incomingValue = if (toAddress == userAddress) value ?: BigInteger.ZERO else BigInteger.ZERO
for (internalTx in internalTransactions.filter { it.to == userAddress }) {
incomingValue += internalTx.value
}
private val tagsFromInternalTransactions: List<String> by lazy {
val incomingTxs = internalTransactions.filter { it.to == userAddress }
val outgoingTxs = internalTransactions.filter { it.from == userAddress }

if (incomingValue == BigInteger.ZERO && outgoingValue == BigInteger.ZERO) return listOf()

val tags = mutableListOf(TransactionTag.EVM_COIN)

if (incomingValue > outgoingValue) {
tags.add(TransactionTag.EVM_COIN_INCOMING)
tags.add(TransactionTag.INCOMING)
}
var incomingValue = incomingTxs.sumOf { it.value }
var outgoingValue = outgoingTxs.sumOf { it.value }

if (outgoingValue > incomingValue) {
tags.add(TransactionTag.EVM_COIN_OUTGOING)
tags.add(TransactionTag.OUTGOING)
value?.let {
when (userAddress) {
toAddress -> incomingValue += value
fromAddress -> outgoingValue += value
}

return tags
}

private val tagsFromEventInstances: List<String>
get() {
val tags: MutableList<String> = mutableListOf()

for (eventInstance in eventInstances) {
tags.addAll(eventInstance.tags(userAddress))
when {
incomingValue > outgoingValue -> {
listOf(TransactionTag.EVM_COIN_INCOMING, TransactionTag.INCOMING)
}

return tags
incomingValue < outgoingValue -> {
listOf(TransactionTag.EVM_COIN_OUTGOING, TransactionTag.OUTGOING)
}
else -> listOf()
}
}

private val tagsFromEventInstances: List<String> by lazy {
eventInstances.map { it.tags(userAddress) }.flatten()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class TransactionTag(

fun tokenIncoming(contractAddress: String): String = "${contractAddress}_$INCOMING"
fun tokenOutgoing(contractAddress: String): String = "${contractAddress}_$OUTGOING"

fun fromAddress(address: String): String = "from_$address"
fun toAddress(address: String): String = "to_$address"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ class OutgoingEip1155Decoration(
val value: BigInteger,
val sentToSelf: Boolean,
val tokenInfo: TokenInfo?,
) : TransactionDecoration() {
) : TransactionDecoration {

override fun tags(): List<String> =
listOf(contractAddress.hex, EIP1155_TRANSFER, TransactionTag.tokenOutgoing(contractAddress.hex), TransactionTag.OUTGOING)
override fun tags() = listOf(
contractAddress.hex,
EIP1155_TRANSFER,
TransactionTag.tokenOutgoing(contractAddress.hex),
TransactionTag.OUTGOING,
TransactionTag.toAddress(to.hex)
)

companion object {
const val EIP1155_TRANSFER = "eip1155Transfer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ class OutgoingEip721Decoration(
val tokenId: BigInteger,
val sentToSelf: Boolean,
val tokenInfo: TokenInfo?,
) : TransactionDecoration() {
) : TransactionDecoration {

override fun tags(): List<String> =
listOf(contractAddress.hex, EIP721_TRANSFER, TransactionTag.tokenOutgoing(contractAddress.hex), TransactionTag.OUTGOING)
override fun tags() = listOf(
contractAddress.hex,
EIP721_TRANSFER,
TransactionTag.tokenOutgoing(contractAddress.hex),
TransactionTag.OUTGOING,
TransactionTag.toAddress(to.hex)
)

companion object {
const val EIP721_TRANSFER = "eip721Transfer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.horizontalsystems.nftkit.events
import io.horizontalsystems.ethereumkit.contracts.ContractEvent
import io.horizontalsystems.ethereumkit.contracts.ContractEventInstance
import io.horizontalsystems.ethereumkit.models.Address
import io.horizontalsystems.ethereumkit.models.TransactionTag
import io.horizontalsystems.nftkit.models.TokenInfo
import java.math.BigInteger

Expand All @@ -20,14 +21,17 @@ class Eip1155TransferEventInstance(
add(contractAddress.hex)

if (from == userAddress) {
add("${contractAddress.hex}_outgoing")
add("outgoing")
add(TransactionTag.tokenOutgoing(contractAddress.hex))
add(TransactionTag.OUTGOING)
}

if (to == userAddress) {
add("${contractAddress.hex}_incoming")
add("incoming")
add(TransactionTag.tokenIncoming(contractAddress.hex))
add(TransactionTag.INCOMING)
}

add(TransactionTag.fromAddress(from.hex))
add(TransactionTag.toAddress(to.hex))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.horizontalsystems.nftkit.events
import io.horizontalsystems.ethereumkit.contracts.ContractEvent
import io.horizontalsystems.ethereumkit.contracts.ContractEventInstance
import io.horizontalsystems.ethereumkit.models.Address
import io.horizontalsystems.ethereumkit.models.TransactionTag
import io.horizontalsystems.nftkit.models.TokenInfo
import java.math.BigInteger

Expand All @@ -18,14 +19,17 @@ class Eip721TransferEventInstance(
add(contractAddress.hex)

if (from == userAddress) {
add("${contractAddress.hex}_outgoing")
add("outgoing")
add(TransactionTag.tokenOutgoing(contractAddress.hex))
add(TransactionTag.OUTGOING)
}

if (to == userAddress) {
add("${contractAddress.hex}_incoming")
add("incoming")
add(TransactionTag.tokenIncoming(contractAddress.hex))
add(TransactionTag.INCOMING)
}

add(TransactionTag.fromAddress(from.hex))
add(TransactionTag.toAddress(to.hex))
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.math.BigInteger

abstract class OneInchDecoration(
open val contractAddress: Address
) : TransactionDecoration() {
) : TransactionDecoration {

sealed class Amount(val value: BigInteger) {
class Exact(value: BigInteger) : Amount(value)
Expand All @@ -26,13 +26,16 @@ abstract class OneInchDecoration(
}
}

override fun tags(): List<String> =
listOf(contractAddress.hex, "swap")
override fun tags() = listOf(contractAddress.hex, TransactionTag.SWAP)

internal fun getTags(token: Token, type: String): List<String> =
when (token) {
is Token.EvmCoin -> listOf("${TransactionTag.EVM_COIN}_$type", TransactionTag.EVM_COIN, type)
is Token.Eip20Coin -> listOf("${token.address.hex}_$type", token.address.hex, type)
}
protected fun getTags(token: Token, type: String) = when (token) {
is Token.EvmCoin -> listOf(
"${TransactionTag.EVM_COIN}_$type",
TransactionTag.EVM_COIN,
type
)

}
is Token.Eip20Coin -> listOf("${token.address.hex}_$type", token.address.hex, type)
}

}
Loading
Loading