|
| 1 | +/* |
| 2 | + * Copyright 2024 ACINQ SAS |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package fr.acinq.eclair.wire.protocol |
| 18 | + |
| 19 | +import com.google.common.base.Charsets |
| 20 | +import fr.acinq.bitcoin.scalacompat.Crypto.PrivateKey |
| 21 | +import fr.acinq.bitcoin.scalacompat.{ByteVector64, Crypto, Satoshi} |
| 22 | +import fr.acinq.eclair.blockchain.fee.FeeratePerKw |
| 23 | +import fr.acinq.eclair.transactions.Transactions |
| 24 | +import fr.acinq.eclair.wire.protocol.CommonCodecs._ |
| 25 | +import fr.acinq.eclair.wire.protocol.TlvCodecs.{tlvField, tlvStream} |
| 26 | +import fr.acinq.eclair.{MilliSatoshi, ToMilliSatoshiConversion, UInt64} |
| 27 | +import scodec.Codec |
| 28 | +import scodec.bits.ByteVector |
| 29 | +import scodec.codecs._ |
| 30 | + |
| 31 | +/** |
| 32 | + * Created by t-bast on 12/04/2024. |
| 33 | + */ |
| 34 | + |
| 35 | +/** |
| 36 | + * Liquidity ads create a decentralized market for channel liquidity. |
| 37 | + * Nodes advertise fee rates for their available liquidity using the gossip protocol. |
| 38 | + * Other nodes can then pay the advertised rate to get inbound liquidity allocated towards them. |
| 39 | + */ |
| 40 | +object LiquidityAds { |
| 41 | + |
| 42 | + /** |
| 43 | + * Liquidity fees are paid using the following : |
| 44 | + * |
| 45 | + * - the buyer pays [[leaseFeeBase]] regardless of the amount contributed by the seller |
| 46 | + * - the buyer pays [[leaseFeeProportional]] (expressed in basis points) based on the amount contributed by the seller |
| 47 | + * - the seller will have to add inputs/outputs to the transaction and pay on-chain fees for them, but the buyer |
| 48 | + * refunds [[fundingWeight]] vbytes of those on-chain fees |
| 49 | + */ |
| 50 | + case class FundingLeaseFee(fundingWeight: Int, leaseFeeProportional: Int, leaseFeeBase: Satoshi) { |
| 51 | + /** |
| 52 | + * Fees paid by the liquidity buyer: the resulting amount must be added to the seller's output in the corresponding |
| 53 | + * commitment transaction. |
| 54 | + */ |
| 55 | + def fees(feerate: FeeratePerKw, requestedAmount: Satoshi, contributedAmount: Satoshi): LeaseFees = { |
| 56 | + val onChainFees = Transactions.weight2fee(feerate, fundingWeight) |
| 57 | + // If the seller adds more liquidity than requested, the buyer doesn't pay for that extra liquidity. |
| 58 | + val proportionalFee = requestedAmount.min(contributedAmount).toMilliSatoshi * leaseFeeProportional / 10_000 |
| 59 | + LeaseFees(onChainFees, leaseFeeBase + proportionalFee.truncateToSatoshi) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // @formatter:off |
| 64 | + sealed trait FundingLease { def fundingFee: FundingLeaseFee } |
| 65 | + case class BasicFundingLease(minAmount: Satoshi, maxAmount: Satoshi, fundingFee: FundingLeaseFee) extends FundingLease |
| 66 | + case class DurationBasedFundingLease(leaseDuration: Int, minAmount: Satoshi, maxAmount: Satoshi, fundingFee: FundingLeaseFee, maxRelayFeeProportional: Int, maxRelayFeeBase: MilliSatoshi) extends FundingLease |
| 67 | + // @formatter:on |
| 68 | + |
| 69 | + // @formatter:off |
| 70 | + sealed trait LeaseRatesTlv extends Tlv |
| 71 | + case class BasicFundingLeaseRates(rates: List[BasicFundingLease]) extends LeaseRatesTlv |
| 72 | + case class DurationBasedFundingLeaseRates(rates: List[DurationBasedFundingLease]) extends LeaseRatesTlv |
| 73 | + // @formatter:on |
| 74 | + |
| 75 | + // @formatter:off |
| 76 | + sealed trait FundingLeaseWitness |
| 77 | + case class BasicFundingLeaseWitness(fundingScript: ByteVector) extends FundingLeaseWitness |
| 78 | + case class DurationBasedFundingLeaseWitness(leaseExpiry: Long, fundingScript: ByteVector, maxRelayFeeProportional: Int, maxRelayFeeBase: MilliSatoshi) extends FundingLeaseWitness |
| 79 | + // @formatter:on |
| 80 | + |
| 81 | + case class RequestFunds(requestedAmount: Satoshi, fundingLease: FundingLease) |
| 82 | + |
| 83 | + case class WillFund(leaseWitness: FundingLeaseWitness, signature: ByteVector64) |
| 84 | + |
| 85 | + case class LeaseFees(miningFee: Satoshi, serviceFee: Satoshi) { |
| 86 | + val total: Satoshi = miningFee + serviceFee |
| 87 | + } |
| 88 | + |
| 89 | + def signLease(request: RequestFunds, nodeKey: PrivateKey, fundingScript: ByteVector, currentBlockHeight: Long): WillFund = { |
| 90 | + val witness = request.fundingLease match { |
| 91 | + case _: BasicFundingLease => BasicFundingLeaseWitness(fundingScript) |
| 92 | + case l: DurationBasedFundingLease => DurationBasedFundingLeaseWitness(currentBlockHeight + l.leaseDuration, fundingScript, l.maxRelayFeeProportional, l.maxRelayFeeBase) |
| 93 | + } |
| 94 | + val toSign = witness match { |
| 95 | + case w: BasicFundingLeaseWitness => Crypto.sha256(ByteVector("basic_funding_lease".getBytes(Charsets.US_ASCII)) ++ Codecs.basicFundingLeaseWitness.encode(w).require.bytes) |
| 96 | + case w: DurationBasedFundingLeaseWitness => Crypto.sha256(ByteVector("duration_based_funding_lease".getBytes(Charsets.US_ASCII)) ++ Codecs.durationBasedFundingLeaseWitness.encode(w).require.bytes) |
| 97 | + } |
| 98 | + WillFund(witness, Crypto.sign(toSign, nodeKey)) |
| 99 | + } |
| 100 | + |
| 101 | + object Codecs { |
| 102 | + private val fundingLeaseFee: Codec[FundingLeaseFee] = ( |
| 103 | + ("fundingWeight" | uint16) :: |
| 104 | + ("leaseFeeBasis" | uint16) :: |
| 105 | + ("leaseFeeBase" | satoshi32) |
| 106 | + ).as[FundingLeaseFee] |
| 107 | + |
| 108 | + private val basicFundingLease: Codec[BasicFundingLease] = ( |
| 109 | + ("minLeaseAmount" | satoshi32) :: |
| 110 | + ("maxLeaseAmount" | satoshi32) :: |
| 111 | + ("leaseFee" | fundingLeaseFee) |
| 112 | + ).as[BasicFundingLease] |
| 113 | + |
| 114 | + private val durationBasedFundingLease: Codec[DurationBasedFundingLease] = ( |
| 115 | + ("leaseDuration" | uint16) :: |
| 116 | + ("minLeaseAmount" | satoshi32) :: |
| 117 | + ("maxLeaseAmount" | satoshi32) :: |
| 118 | + ("leaseFee" | fundingLeaseFee) :: |
| 119 | + ("maxChannelFeeBasis" | uint16) :: |
| 120 | + ("maxChannelFeeBase" | millisatoshi32) |
| 121 | + ).as[DurationBasedFundingLease] |
| 122 | + |
| 123 | + private val fundingLease: Codec[FundingLease] = discriminated[FundingLease].by(byte) |
| 124 | + .typecase(1, basicFundingLease) |
| 125 | + .typecase(3, durationBasedFundingLease) |
| 126 | + |
| 127 | + val basicFundingLeaseWitness: Codec[BasicFundingLeaseWitness] = ("fundingScript" | varsizebinarydata).as[BasicFundingLeaseWitness] |
| 128 | + |
| 129 | + val durationBasedFundingLeaseWitness: Codec[DurationBasedFundingLeaseWitness] = ( |
| 130 | + ("leaseExpiry" | uint32) :: |
| 131 | + ("fundingScript" | varsizebinarydata) :: |
| 132 | + ("maxChannelFeeBasis" | uint16) :: |
| 133 | + ("maxChannelFeeBase" | millisatoshi32) |
| 134 | + ).as[DurationBasedFundingLeaseWitness] |
| 135 | + |
| 136 | + private val fundingLeaseWitness: Codec[FundingLeaseWitness] = discriminated[FundingLeaseWitness].by(byte) |
| 137 | + .typecase(1, basicFundingLeaseWitness) |
| 138 | + .typecase(3, durationBasedFundingLeaseWitness) |
| 139 | + |
| 140 | + val requestFunds: Codec[RequestFunds] = ( |
| 141 | + ("requestedAmount" | satoshi) :: |
| 142 | + ("fundingLease" | fundingLease) |
| 143 | + ).as[RequestFunds] |
| 144 | + |
| 145 | + val willFund: Codec[WillFund] = ( |
| 146 | + ("leaseWitness" | fundingLeaseWitness) :: |
| 147 | + ("signature" | bytes64) |
| 148 | + ).as[WillFund] |
| 149 | + |
| 150 | + val leaseRates: Codec[TlvStream[LeaseRatesTlv]] = tlvStream(discriminated[LeaseRatesTlv].by(varint) |
| 151 | + .typecase(UInt64(1), tlvField(list(basicFundingLease).as[BasicFundingLeaseRates])) |
| 152 | + .typecase(UInt64(3), tlvField(list(durationBasedFundingLease).as[DurationBasedFundingLeaseRates])) |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments