mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-08-11 08:47:33 +00:00
feat: execute Podcasting-2.0 value-for-value (V4V) Lightning splits
Adds payment execution to the V4V value blocks that were previously display-only. A "Send value" button on the value card opens the account's zap-amount picker; choosing an amount fans the weighted shares out to every recipient, mirroring how a NIP-57 zap-split is paid. quartz (pure, tested): - PodcastValue.computeShares() — splits a total across recipients by relative weight, honoring `fee` recipients that take their split as a percent off the top. Returns PodcastValueShare (recipient + millisats). - PodcastBoostagram — the satoshis.stream keysend metadata blob carried in TLV record 7629169, with the registered field names and unset fields omitted. - PODCAST_TLV_RECORD / TYPE_NODE / TYPE_LNADDRESS constants. amethyst: - V4VPaymentHandler — the execution engine. lnaddress recipients resolve to a BOLT-11 via LNURL-pay and pay through the user's default source (NWC, CLINK debit, or external wallet intent), same rails as a zap. node recipients pay by NWC keysend (pay_keysend) carrying the boostagram TLV plus any per-recipient custom TLV; keysend is NWC-only, so node recipients are skipped with a clear error when no NWC wallet is configured. - AccountViewModel.payV4V() wrapper + the "Send value" amount picker on the value card, wired for both episode and show value blocks. V4V recipients are raw Lightning destinations, not Nostr users, so there is no zap request and no zap receipt — just the payment. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JGa1EM5KWyDo1o5Yr6sS18
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.podcasts
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.JsonMapper
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* The Podcasting-2.0 keysend metadata blob ("boostagram") carried in TLV record
|
||||
* [PodcastValue.PODCAST_TLV_RECORD] (7629169). It tells the receiving node which podcast/episode the
|
||||
* payment is for and how much was sent in total. Field names follow the satoshis.stream convention
|
||||
* (<https://github.com/satoshisstream/satoshis.stream/blob/main/TLV_registry.md>).
|
||||
*
|
||||
* Unset fields are omitted from the JSON ([JsonMapper] does not encode defaults), keeping the record
|
||||
* small enough to fit comfortably inside a keysend onion.
|
||||
*/
|
||||
@Serializable
|
||||
class PodcastBoostagram(
|
||||
val podcast: String? = null,
|
||||
val episode: String? = null,
|
||||
/** "stream" for per-minute streaming sats, "boost" for a deliberate lump-sum tip. */
|
||||
val action: String? = null,
|
||||
@SerialName("app_name")
|
||||
val appName: String? = null,
|
||||
/** Total sats (not millisats) the listener sent across all splits. */
|
||||
@SerialName("value_msat_total")
|
||||
val valueMsatTotal: Long? = null,
|
||||
val message: String? = null,
|
||||
@SerialName("sender_name")
|
||||
val senderName: String? = null,
|
||||
) {
|
||||
fun toJson(): String = JsonMapper.toJson(this)
|
||||
|
||||
companion object {
|
||||
const val ACTION_STREAM = "stream"
|
||||
const val ACTION_BOOST = "boost"
|
||||
}
|
||||
}
|
||||
@@ -46,8 +46,79 @@ class PodcastValue(
|
||||
) {
|
||||
/** Sum of recipient splits, used to turn each [PodcastValueRecipient.split] into a share. */
|
||||
fun totalSplit(): Int = recipients.sumOf { it.split }
|
||||
|
||||
/**
|
||||
* Splits [totalMilliSats] across the recipients per the Podcasting-2.0 value rules and returns
|
||||
* the non-zero shares (recipient + amount in millisats), preserving recipient order.
|
||||
*
|
||||
* - A recipient with [PodcastValueRecipient.fee] = true takes its [PodcastValueRecipient.split]
|
||||
* as a **percentage of the total**, off the top (e.g. an app/host fee).
|
||||
* - The remainder is divided among the non-fee recipients **by relative weight**
|
||||
* ([PodcastValueRecipient.split] / sum of non-fee splits).
|
||||
*
|
||||
* Recipients without a payable [PodcastValueRecipient.address] or with a non-positive split are
|
||||
* ignored. Integer division floors each share, so a few millisats may go unallocated (dust) —
|
||||
* acceptable for value-for-value streaming.
|
||||
*/
|
||||
fun computeShares(totalMilliSats: Long): List<PodcastValueShare> {
|
||||
if (totalMilliSats <= 0) return emptyList()
|
||||
|
||||
val active = recipients.filter { it.split > 0 && !it.address.isNullOrBlank() }
|
||||
if (active.isEmpty()) return emptyList()
|
||||
|
||||
var feeTotalMillis = 0L
|
||||
val feeAmounts = HashMap<PodcastValueRecipient, Long>()
|
||||
for (recipient in active) {
|
||||
if (recipient.fee == true) {
|
||||
val millis = totalMilliSats * recipient.split / 100
|
||||
if (millis > 0) {
|
||||
feeAmounts[recipient] = millis
|
||||
feeTotalMillis += millis
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val remainder = (totalMilliSats - feeTotalMillis).coerceAtLeast(0)
|
||||
val sharedWeight = active.filter { it.fee != true }.sumOf { it.split }
|
||||
|
||||
val shares = ArrayList<PodcastValueShare>(active.size)
|
||||
for (recipient in active) {
|
||||
val millis =
|
||||
if (recipient.fee == true) {
|
||||
feeAmounts[recipient] ?: 0L
|
||||
} else if (sharedWeight > 0 && remainder > 0) {
|
||||
remainder * recipient.split / sharedWeight
|
||||
} else {
|
||||
0L
|
||||
}
|
||||
if (millis > 0) shares.add(PodcastValueShare(recipient, millis))
|
||||
}
|
||||
return shares
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* TLV record type for the Podcasting-2.0 keysend metadata blob (the "boostagram"), a JSON
|
||||
* object carrying podcast/episode/app/value context. Registered value, used by the whole
|
||||
* Podcasting-2.0 ecosystem. See <https://github.com/satoshisstream/satoshis.stream>.
|
||||
*/
|
||||
const val PODCAST_TLV_RECORD: Long = 7629169L
|
||||
|
||||
/** Recipient [PodcastValueRecipient.type] for a keysend to a raw Lightning node pubkey. */
|
||||
const val TYPE_NODE = "node"
|
||||
|
||||
/** Recipient [PodcastValueRecipient.type] for an LNURL-pay to a lightning address. */
|
||||
const val TYPE_LNADDRESS = "lnaddress"
|
||||
}
|
||||
}
|
||||
|
||||
/** One recipient's resolved share of a [PodcastValue] split, in millisats. */
|
||||
@Immutable
|
||||
class PodcastValueShare(
|
||||
val recipient: PodcastValueRecipient,
|
||||
val amountMilliSats: Long,
|
||||
)
|
||||
|
||||
/** One destination in a [PodcastValue] split. */
|
||||
@Immutable
|
||||
@Serializable
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.podcasts
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.JsonMapper
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class PodcastBoostagramTest {
|
||||
@Test
|
||||
fun `uses satoshis-stream field names and omits unset fields`() {
|
||||
val json =
|
||||
PodcastBoostagram(
|
||||
podcast = "My Show",
|
||||
episode = "Ep 1",
|
||||
action = PodcastBoostagram.ACTION_BOOST,
|
||||
appName = "Amethyst",
|
||||
valueMsatTotal = 21_000_000L,
|
||||
).toJson()
|
||||
|
||||
assertTrue(json.contains("\"podcast\":\"My Show\""))
|
||||
assertTrue(json.contains("\"app_name\":\"Amethyst\""))
|
||||
assertTrue(json.contains("\"value_msat_total\":21000000"))
|
||||
assertTrue(json.contains("\"action\":\"boost\""))
|
||||
// Unset optionals (message, sender_name) must not appear.
|
||||
assertFalse(json.contains("message"))
|
||||
assertFalse(json.contains("sender_name"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `round-trips through json`() {
|
||||
val original =
|
||||
PodcastBoostagram(
|
||||
podcast = "Show",
|
||||
action = PodcastBoostagram.ACTION_STREAM,
|
||||
valueMsatTotal = 1000L,
|
||||
senderName = "alice",
|
||||
)
|
||||
val parsed = JsonMapper.fromJson<PodcastBoostagram>(original.toJson())
|
||||
|
||||
assertEquals("Show", parsed.podcast)
|
||||
assertEquals(PodcastBoostagram.ACTION_STREAM, parsed.action)
|
||||
assertEquals(1000L, parsed.valueMsatTotal)
|
||||
assertEquals("alice", parsed.senderName)
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.podcasts
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class PodcastValueShareTest {
|
||||
private fun node(
|
||||
name: String,
|
||||
split: Int,
|
||||
fee: Boolean? = null,
|
||||
) = PodcastValueRecipient(name = name, type = PodcastValue.TYPE_NODE, address = "node-$name", split = split, fee = fee)
|
||||
|
||||
@Test
|
||||
fun `weighted split with no fees divides by relative weight`() {
|
||||
val value =
|
||||
PodcastValue(
|
||||
recipients = listOf(node("host", 90), node("producer", 10)),
|
||||
)
|
||||
// 100k sats total in millisats.
|
||||
val shares = value.computeShares(100_000_000L).associate { it.recipient.name to it.amountMilliSats }
|
||||
|
||||
assertEquals(90_000_000L, shares["host"])
|
||||
assertEquals(10_000_000L, shares["producer"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `fee recipient takes its split as a percent off the top, remainder split by weight`() {
|
||||
val value =
|
||||
PodcastValue(
|
||||
recipients =
|
||||
listOf(
|
||||
node("app", 5, fee = true), // 5% fee off the top
|
||||
node("host", 80),
|
||||
node("cohost", 20),
|
||||
),
|
||||
)
|
||||
val shares = value.computeShares(1_000_000L).associate { it.recipient.name to it.amountMilliSats }
|
||||
|
||||
// 5% of 1,000,000 = 50,000 fee. Remainder 950,000 split 80/20.
|
||||
assertEquals(50_000L, shares["app"])
|
||||
assertEquals(760_000L, shares["host"])
|
||||
assertEquals(190_000L, shares["cohost"])
|
||||
// No more than the total is ever allocated.
|
||||
assertTrue(shares.values.sum() <= 1_000_000L)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `recipients without an address or with non-positive split are ignored`() {
|
||||
val value =
|
||||
PodcastValue(
|
||||
recipients =
|
||||
listOf(
|
||||
node("host", 100),
|
||||
PodcastValueRecipient(name = "noaddr", type = PodcastValue.TYPE_NODE, address = null, split = 50),
|
||||
node("zero", 0),
|
||||
),
|
||||
)
|
||||
val shares = value.computeShares(10_000L)
|
||||
|
||||
assertEquals(1, shares.size)
|
||||
assertEquals("host", shares.single().recipient.name)
|
||||
assertEquals(10_000L, shares.single().amountMilliSats)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `non-positive total or empty recipients yields no shares`() {
|
||||
val value = PodcastValue(recipients = listOf(node("host", 100)))
|
||||
assertTrue(value.computeShares(0L).isEmpty())
|
||||
assertTrue(value.computeShares(-5L).isEmpty())
|
||||
assertTrue(PodcastValue(recipients = emptyList()).computeShares(1_000L).isEmpty())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user