Repository navigation
fix: apply fixed btc prices with paykit rc71 #1437
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a35f724
chore: update paykit to rc71
ben-kaufman d1f8c74
fix: apply fixed bitcoin request prices
ben-kaufman 077f83e
chore: align fixed-price docs and tests
ben-kaufman 36c9294
fix: stop preparing unpayable payment requests
ben-kaufman dc54e6e
fix: bound fixed-price decimal precision
ben-kaufman 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
58 changes: 58 additions & 0 deletions
58
app/src/main/java/to/bitkit/repositories/PaykitBitcoinRequestPricing.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,58 @@ | ||
| package to.bitkit.repositories | ||
|
|
||
| import com.synonym.paykit.PaymentConversion | ||
| import com.synonym.paykit.PaymentRequestTerms | ||
| import java.math.BigDecimal | ||
| import java.math.RoundingMode | ||
|
|
||
| internal object PaykitBitcoinRequestPricing { | ||
| /** Maximum input precision and combined normalized operand precision. */ | ||
| private const val MAX_SIGNIFICANT_DIGITS = 38 | ||
|
|
||
| /** Maximum length of an amount or rate before decimal parsing. */ | ||
| private const val MAX_DECIMAL_LENGTH = 80 | ||
|
|
||
| /** Millisatoshis per Bitcoin, expressed as a decimal scale. */ | ||
| private const val MILLISATOSHI_SCALE = 11 | ||
|
|
||
| data class BitcoinPayment(val amountSats: ULong, val endpointIdentifiers: List<String>) | ||
|
|
||
| @Suppress("ReturnCount") | ||
| fun bitcoinPayment(terms: PaymentRequestTerms, endpoints: List<String>): BitcoinPayment? { | ||
| val rates = (terms.conversion as? PaymentConversion.Fixed)?.rates ?: return null | ||
| if (rates.isEmpty() || rates.distinctBy { it.asset }.size != rates.size) return null | ||
| val requestedAmount = positiveDecimal(terms.amount.value) ?: return null | ||
| val amounts = mutableSetOf<ULong>() | ||
| val payableEndpoints = mutableListOf<String>() | ||
| for (endpoint in endpoints) { | ||
| val selector = endpoint.split('-').take(2).joinToString("-") | ||
| val rate = rates.firstOrNull { it.asset == selector } | ||
| ?: rates.firstOrNull { it.asset == PaykitIssuerInterop.BITCOIN_ASSET } | ||
| val rateValue = rate?.value | ||
| ?: "1".takeIf { terms.amount.asset == PaykitIssuerInterop.BITCOIN_ASSET } ?: continue | ||
| val multiplier = positiveDecimal(rateValue) ?: return null | ||
| val sats = sats(requestedAmount, multiplier, lightning = selector == "btc-lightning") ?: return null | ||
| amounts.add(sats) | ||
| payableEndpoints.add(endpoint) | ||
| } | ||
| return amounts.singleOrNull()?.let { BitcoinPayment(it, payableEndpoints) } | ||
| } | ||
|
|
||
| private fun sats(amount: BigDecimal, rate: BigDecimal, lightning: Boolean): ULong? { | ||
| val combinedPrecision = amount.stripTrailingZeros().precision() + rate.stripTrailingZeros().precision() | ||
| if (combinedPrecision > MAX_SIGNIFICANT_DIGITS) return null | ||
| val bitcoin = amount.multiply(rate).stripTrailingZeros() | ||
| val units = bitcoin.movePointRight(if (lightning) MILLISATOSHI_SCALE else 8).setScale(0, RoundingMode.CEILING) | ||
| val integer = units.toPlainString().toULongOrNull()?.takeIf { it > 0uL } ?: return null | ||
| if (lightning && integer % 1000uL != 0uL) return null | ||
| val sats = if (lightning) integer / 1000uL else integer | ||
| return sats.takeIf { it <= ULong.MAX_VALUE / 1000uL } | ||
| } | ||
|
|
||
| private fun positiveDecimal(value: String): BigDecimal? { | ||
| if (value.length > MAX_DECIMAL_LENGTH) return null | ||
| if (!value.matches(Regex("(?:[0-9]+(?:\\.[0-9]*)?|\\.[0-9]+)"))) return null | ||
| if (value.replace(".", "").trimStart('0').length > MAX_SIGNIFICANT_DIGITS) return null | ||
| return value.toBigDecimalOrNull()?.takeIf { it.signum() > 0 } | ||
| } | ||
| } |
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
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.