-
Notifications
You must be signed in to change notification settings - Fork 4
fix: add quickpay daily spend limit #1159
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
Open
ovitrif
wants to merge
32
commits into
master
Choose a base branch
from
fix/require-payment-pin-for-quickpay
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
068fb91
fix: require payment pin for quickpay
ovitrif 76cb9ef
chore: rename changelog fragment
ovitrif 01cc208
fix: add quickpay daily spend limit
ovitrif 99b47d8
fix: show multiplier steps as times
ovitrif fbe9321
fix: include steplider labels in layout bounds
ovitrif d90dfb1
fix: layout steplider labels from constraints
ovitrif ef76b51
refactor: measure slider as one unit
ovitrif af4163d
fix: spacing and copy
ovitrif 17f544c
fix: snap slider tap from current value
ovitrif 7385c73
fix: simplify multiplier string templates
ovitrif af959c9
fix: harden quickpay daily spend limits
ovitrif 7cdae86
fix: localize error messages and texts
ovitrif 3593678
refactor: use USD constant
ovitrif 4761c75
feat: use quickpay routing fee on ui
ovitrif b64cc53
merge: sync master into quickpay daily limit
ovitrif 07a88a2
fix: harden quickpay spend and slider
ovitrif a254ba2
test: fix quickpay day spend assertion
ovitrif c6136d7
merge: sync master into quickpay daily limit
ovitrif d98ad17
fix: wrap cache store test lines
ovitrif c874d2c
merge: sync master into quickpay daily limit
ovitrif c0806a6
fix: account quickpay daily spend in usd cents
ovitrif ddf9b71
test: fix quickpay viewmodel mock setup
ovitrif 6d7bf18
refactor: move quickpay spend ledger to repo
ovitrif 1bb0498
chore: cleanup imports
ovitrif 37017df
fix: keep quickpay events and spend in sync
ovitrif ffe28d9
fix: release spend without pending gate
ovitrif 68cba13
test: drop unshipped sat spend decode
ovitrif ef74df4
test: drop duplicate setmain in quickpay
ovitrif 5c21813
refactor: inline quickpay day key
ovitrif b0f56d5
refactor: move quickpay types to repo
ovitrif d46ab05
refactor: drop experimentaltime opt-in
ovitrif 0f59bc6
chore: fix quickpay import order
ovitrif File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
175 changes: 175 additions & 0 deletions
175
app/src/main/java/to/bitkit/repositories/QuickPayRepo.kt
This file contains hidden or 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,175 @@ | ||
| package to.bitkit.repositories | ||
|
|
||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.withContext | ||
| import kotlinx.datetime.TimeZone | ||
| import kotlinx.datetime.toLocalDateTime | ||
| import kotlinx.serialization.Serializable | ||
| import to.bitkit.data.AppCacheData | ||
| import to.bitkit.data.CacheStore | ||
| import to.bitkit.data.SettingsStore | ||
| import to.bitkit.di.IoDispatcher | ||
| import to.bitkit.ext.runSuspendCatching | ||
| import to.bitkit.models.USD | ||
| import to.bitkit.utils.AppError | ||
| import to.bitkit.utils.Logger | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import kotlin.time.Clock | ||
|
|
||
| @Singleton | ||
| class QuickPayRepo @Inject constructor( | ||
| private val cacheStore: CacheStore, | ||
| private val settingsStore: SettingsStore, | ||
| private val currencyRepo: CurrencyRepo, | ||
| @IoDispatcher private val ioDispatcher: CoroutineDispatcher, | ||
| private val clock: Clock, | ||
| ) { | ||
| companion object { | ||
| private const val TAG = "QuickPayRepo" | ||
| } | ||
|
|
||
| suspend fun spentCentsToday(): Result<Long> = withContext(ioDispatcher) { | ||
| runSuspendCatching { | ||
| cacheStore.data.first().spendFor(currentDayKey()).spentCents | ||
| } | ||
| } | ||
|
|
||
| suspend fun canApply(amountSats: ULong): Result<Boolean> = withContext(ioDispatcher) { | ||
| runSuspendCatching { | ||
| val settings = settingsStore.data.first() | ||
| if (!settings.isQuickPayEnabled || amountSats == 0uL) return@runSuspendCatching false | ||
|
|
||
| val thresholdSats = currencyRepo.convertFiatToSats( | ||
| settings.quickPayAmount.toDouble(), | ||
| USD, | ||
| ).getOrNull() ?: return@runSuspendCatching false | ||
| if (amountSats > thresholdSats) return@runSuspendCatching false | ||
|
|
||
| val converted = currencyRepo.convertSatsToFiat(amountSats.toLong(), USD).getOrNull() | ||
| ?: return@runSuspendCatching false | ||
| val reserveCents = quickPayReserveCents(converted.toUsdCents(), settings.quickPayAmount) | ||
| val capCents = quickPayCapCents(settings.quickPayAmount, settings.quickPayDailyLimitMultiplier) | ||
| val spentCentsToday = cacheStore.data.first().spendFor(currentDayKey()).spentCents | ||
| if (spentCentsToday + reserveCents <= capCents) return@runSuspendCatching true | ||
|
|
||
| Logger.info( | ||
| "Skipping QuickPay: daily spend '$spentCentsToday' + '$reserveCents' exceeds cap '$capCents'", | ||
| context = TAG, | ||
| ) | ||
| false | ||
| } | ||
| } | ||
|
|
||
| suspend fun tryReserve(amountSats: ULong): Result<QuickPaySpendReservation?> = withContext(ioDispatcher) { | ||
| runSuspendCatching { | ||
| val settings = settingsStore.data.first() | ||
| val converted = currencyRepo.convertSatsToFiat(amountSats.toLong(), USD).getOrElse { | ||
| throw QuickPayConversionError() | ||
| } | ||
| val amountCents = quickPayReserveCents(converted.toUsdCents(), settings.quickPayAmount) | ||
| val capCents = quickPayCapCents(settings.quickPayAmount, settings.quickPayDailyLimitMultiplier) | ||
| val dayKey = currentDayKey() | ||
| var reserved: QuickPaySpendReservation? = null | ||
| cacheStore.update { | ||
| val spend = it.spendFor(dayKey) | ||
| if (spend.spentCents + amountCents > capCents) return@update it | ||
| reserved = QuickPaySpendReservation(amountCents = amountCents, dayKey = spend.dayKey) | ||
| it.copy( | ||
| quickPaySpendDayKey = spend.dayKey, | ||
| quickPaySpentCentsToday = spend.spentCents + amountCents, | ||
| ) | ||
| } | ||
| reserved | ||
| } | ||
| } | ||
|
|
||
| suspend fun remember( | ||
| paymentHash: String, | ||
| reservation: QuickPaySpendReservation, | ||
| ): Result<Unit> = withContext(ioDispatcher) { | ||
| runSuspendCatching { | ||
| if (paymentHash.isBlank()) return@runSuspendCatching | ||
| cacheStore.update { | ||
| it.copy( | ||
| quickPayReservations = it.quickPayReservations + (paymentHash to reservation), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| suspend fun reservation(paymentHash: String): Result<QuickPaySpendReservation?> = withContext(ioDispatcher) { | ||
| runSuspendCatching { | ||
| if (paymentHash.isBlank()) return@runSuspendCatching null | ||
| cacheStore.data.first().quickPayReservations[paymentHash] | ||
| } | ||
| } | ||
|
|
||
| suspend fun release(paymentHash: String): Result<Unit> = withContext(ioDispatcher) { | ||
| runSuspendCatching { | ||
| if (paymentHash.isBlank()) return@runSuspendCatching | ||
| cacheStore.update { data -> | ||
| val reservation = data.quickPayReservations[paymentHash] ?: return@update data | ||
| val remaining = data.quickPayReservations - paymentHash | ||
| val spend = data.spendFor(reservation.dayKey) | ||
| if (reservation.dayKey != spend.dayKey) { | ||
| return@update data.copy(quickPayReservations = remaining) | ||
| } | ||
| data.copy( | ||
| quickPaySpentCentsToday = (spend.spentCents - reservation.amountCents).coerceAtLeast(0L), | ||
| quickPayReservations = remaining, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| suspend fun releaseUnbound(reservation: QuickPaySpendReservation): Result<Unit> = withContext(ioDispatcher) { | ||
| runSuspendCatching { | ||
| cacheStore.update { | ||
| if (reservation.dayKey != it.quickPaySpendDayKey) return@update it | ||
| it.copy( | ||
| quickPaySpentCentsToday = (it.quickPaySpentCentsToday - reservation.amountCents).coerceAtLeast(0L), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| suspend fun clear(paymentHash: String): Result<Unit> = withContext(ioDispatcher) { | ||
| runSuspendCatching { | ||
| if (paymentHash.isBlank()) return@runSuspendCatching | ||
| cacheStore.update { | ||
| if (paymentHash !in it.quickPayReservations) return@update it | ||
| it.copy(quickPayReservations = it.quickPayReservations - paymentHash) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun currentDayKey(): String = | ||
| clock.now().toLocalDateTime(TimeZone.currentSystemDefault()).date.toString() | ||
| } | ||
|
|
||
| @Serializable | ||
| data class QuickPaySpendReservation( | ||
| val amountCents: Long, | ||
| val dayKey: String, | ||
| ) | ||
|
|
||
| private data class QuickPayDaySpend( | ||
| val dayKey: String, | ||
| val spentCents: Long, | ||
| ) | ||
|
|
||
| private fun quickPayCapCents(thresholdUsd: Int, multiplier: Int): Long = | ||
| thresholdUsd.toLong() * 100L * multiplier.toLong() | ||
|
|
||
| private fun quickPayReserveCents(convertedCents: Long, thresholdUsd: Int): Long = | ||
| minOf(convertedCents, thresholdUsd.toLong() * 100L) | ||
|
|
||
| class QuickPayConversionError : AppError("Currency conversion failed") | ||
|
|
||
| private fun AppCacheData.spendFor(dayKey: String): QuickPayDaySpend = when { | ||
| quickPaySpendDayKey.isEmpty() || dayKey > quickPaySpendDayKey -> QuickPayDaySpend(dayKey, 0L) | ||
| dayKey == quickPaySpendDayKey -> QuickPayDaySpend(dayKey, quickPaySpentCentsToday) | ||
| else -> QuickPayDaySpend(quickPaySpendDayKey, quickPaySpentCentsToday) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.