-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3efdba5
commit 5f04591
Showing
8 changed files
with
66 additions
and
26 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
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
48 changes: 48 additions & 0 deletions
48
app/src/main/java/io/horizontalsystems/bankwallet/core/managers/SpamManager.kt
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,48 @@ | ||
package io.horizontalsystems.bankwallet.core.managers | ||
|
||
import io.horizontalsystems.bankwallet.entities.TransactionValue | ||
import io.horizontalsystems.bankwallet.entities.transactionrecords.evm.TransferEvent | ||
import java.math.BigDecimal | ||
|
||
class SpamManager( | ||
private val marketKitWrapper: MarketKitWrapper | ||
) { | ||
private val negligibleValue = BigDecimal("0.01") | ||
private var cachedUsdPrices = mutableMapOf<String, BigDecimal?>() | ||
|
||
fun isSpam( | ||
incomingEvents: List<TransferEvent>, | ||
outgoingEvents: List<TransferEvent> | ||
): Boolean { | ||
val allEvents = incomingEvents + outgoingEvents | ||
return allEvents.all { spamEvent(it) } | ||
} | ||
|
||
private fun spamEvent(event: TransferEvent): Boolean { | ||
return when (val eventValue = event.value) { | ||
is TransactionValue.CoinValue -> { | ||
spamValue(eventValue.coinUid, eventValue.value) | ||
} | ||
|
||
is TransactionValue.NftValue -> { | ||
spamValue(eventValue.coinUid, eventValue.value) | ||
} | ||
|
||
else -> true | ||
} | ||
} | ||
|
||
private fun spamValue(coinUid: String, value: BigDecimal): Boolean { | ||
return cachedUsdPrice(coinUid)?.let { usdPrice -> | ||
usdPrice * value < negligibleValue | ||
} ?: run { | ||
value <= BigDecimal.ZERO | ||
} | ||
} | ||
|
||
private fun cachedUsdPrice(coinUid: String): BigDecimal? { | ||
return cachedUsdPrices.getOrPut(coinUid) { | ||
marketKitWrapper.coinPrice(coinUid, "USD")?.value | ||
} | ||
} | ||
} |
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