-
Notifications
You must be signed in to change notification settings - Fork 4
fix(receive): handle additional receive liquidity edge cases #1222
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
13 commits
Select commit
Hold shift + click to select a range
450a4b8
fix(receive): handle additional receive liquidity edge cases
pwltr a932f77
fix: refresh cjit max limits
pwltr 685f849
fix: refresh cjit limit tests
pwltr d9c603a
fix: address cjit review nits
pwltr 11b9807
Merge branch 'master' into fix/additional-cjit
piotr-iohk ef90676
fix: address receive cjit review
pwltr 47b6550
test: fix receive deeplink assertions
pwltr ebb48d3
fix: preserve trezor receive tab
pwltr 78fa7d4
Merge remote-tracking branch 'origin/master' into fix/additional-cjit
pwltr 7fca106
fix: refresh receive channel state
pwltr 3c21400
fix: use usable receive liquidity
pwltr 10355fa
fix: use usable receive liquidity
pwltr 07ec441
fix: move receive liquidity read off main
pwltr 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
85 changes: 85 additions & 0 deletions
85
app/src/main/java/to/bitkit/models/ReceiveLiquidityDecision.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,85 @@ | ||
| package to.bitkit.models | ||
|
|
||
| enum class ReceiveLiquiditySource { | ||
| SAVINGS, | ||
| AUTO, | ||
| SPENDING, | ||
| } | ||
|
|
||
| sealed interface ReceiveAdditionalLiquidityAction { | ||
| data object None : ReceiveAdditionalLiquidityAction | ||
| data object ChooseAmount : ReceiveAdditionalLiquidityAction | ||
| data class CreateCjit(val amountSats: ULong) : ReceiveAdditionalLiquidityAction | ||
| data object GeoBlocked : ReceiveAdditionalLiquidityAction | ||
| } | ||
|
|
||
| data class ReceiveAdditionalLiquidityParams( | ||
| val source: ReceiveLiquiditySource, | ||
| val invoiceAmountSats: ULong, | ||
| val inboundCapacitySats: ULong?, | ||
| val minCjitSats: ULong?, | ||
| val maxCjitAmountSats: ULong?, | ||
| val isGeoBlocked: Boolean, | ||
| ) | ||
|
|
||
| object ReceiveLiquidityDecision { | ||
| fun canCreateLightningInvoice( | ||
| hasUsableChannels: Boolean, | ||
| inboundCapacitySats: ULong?, | ||
| invoiceAmountSats: ULong?, | ||
| ): Boolean { | ||
| if (!hasUsableChannels || inboundCapacitySats == null) return false | ||
|
|
||
| if (invoiceAmountSats == null || invoiceAmountSats == 0uL) { | ||
| return inboundCapacitySats > 0uL | ||
| } | ||
|
|
||
| return invoiceAmountSats <= inboundCapacitySats | ||
| } | ||
|
|
||
| fun additionalLiquidityAction(params: ReceiveAdditionalLiquidityParams): ReceiveAdditionalLiquidityAction { | ||
| return when { | ||
| params.source != ReceiveLiquiditySource.SPENDING -> ReceiveAdditionalLiquidityAction.None | ||
| !needsInboundLiquidity(params.invoiceAmountSats, params.inboundCapacitySats) -> | ||
| ReceiveAdditionalLiquidityAction.None | ||
| (params.inboundCapacitySats ?: 0uL) == 0uL -> ReceiveAdditionalLiquidityAction.None | ||
| params.isGeoBlocked -> ReceiveAdditionalLiquidityAction.GeoBlocked | ||
| shouldChooseAmount(params) -> ReceiveAdditionalLiquidityAction.ChooseAmount | ||
| else -> ReceiveAdditionalLiquidityAction.CreateCjit(params.invoiceAmountSats) | ||
| } | ||
| } | ||
|
|
||
| fun needsCjitLimitsForAdditionalLiquidity( | ||
| source: ReceiveLiquiditySource, | ||
| invoiceAmountSats: ULong, | ||
| inboundCapacitySats: ULong?, | ||
| isGeoBlocked: Boolean, | ||
| ): Boolean { | ||
| if (source != ReceiveLiquiditySource.SPENDING) return false | ||
| if (!needsInboundLiquidity(invoiceAmountSats, inboundCapacitySats)) return false | ||
| if ((inboundCapacitySats ?: 0uL) == 0uL) return false | ||
|
|
||
| return !isGeoBlocked | ||
| } | ||
|
|
||
| fun needsInboundLiquidity( | ||
| invoiceAmountSats: ULong, | ||
| inboundCapacitySats: ULong?, | ||
| ): Boolean { | ||
| val inbound = inboundCapacitySats ?: 0uL | ||
|
|
||
| if (invoiceAmountSats == 0uL) { | ||
| return inbound == 0uL | ||
| } | ||
|
|
||
| return invoiceAmountSats > inbound | ||
| } | ||
|
|
||
| private fun shouldChooseAmount(params: ReceiveAdditionalLiquidityParams): Boolean { | ||
| val min = params.minCjitSats ?: 0uL | ||
| val max = params.maxCjitAmountSats?.takeIf { it > 0uL } ?: return true | ||
| if (params.invoiceAmountSats == 0uL || min == 0uL) return true | ||
|
|
||
| return params.invoiceAmountSats < min || params.invoiceAmountSats > max | ||
| } | ||
| } |
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.