-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add types and codecs for the extensible liquidity ads format proposed in lightning/bolts#1153.
- Loading branch information
Showing
7 changed files
with
249 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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
156 changes: 156 additions & 0 deletions
156
eclair-core/src/main/scala/fr/acinq/eclair/wire/protocol/LiquidityAds.scala
This file contains 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,156 @@ | ||
/* | ||
* Copyright 2024 ACINQ SAS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package fr.acinq.eclair.wire.protocol | ||
|
||
import com.google.common.base.Charsets | ||
import fr.acinq.bitcoin.scalacompat.Crypto.PrivateKey | ||
import fr.acinq.bitcoin.scalacompat.{ByteVector64, Crypto, Satoshi} | ||
import fr.acinq.eclair.blockchain.fee.FeeratePerKw | ||
import fr.acinq.eclair.transactions.Transactions | ||
import fr.acinq.eclair.wire.protocol.CommonCodecs._ | ||
import fr.acinq.eclair.wire.protocol.TlvCodecs.{tlvField, tlvStream} | ||
import fr.acinq.eclair.{MilliSatoshi, ToMilliSatoshiConversion, UInt64} | ||
import scodec.Codec | ||
import scodec.bits.ByteVector | ||
import scodec.codecs._ | ||
|
||
/** | ||
* Created by t-bast on 12/04/2024. | ||
*/ | ||
|
||
/** | ||
* Liquidity ads create a decentralized market for channel liquidity. | ||
* Nodes advertise fee rates for their available liquidity using the gossip protocol. | ||
* Other nodes can then pay the advertised rate to get inbound liquidity allocated towards them. | ||
*/ | ||
object LiquidityAds { | ||
|
||
/** | ||
* Liquidity fees are paid using the following : | ||
* | ||
* - the buyer pays [[leaseFeeBase]] regardless of the amount contributed by the seller | ||
* - the buyer pays [[leaseFeeProportional]] (expressed in basis points) based on the amount contributed by the seller | ||
* - the seller will have to add inputs/outputs to the transaction and pay on-chain fees for them, but the buyer | ||
* refunds [[fundingWeight]] vbytes of those on-chain fees | ||
*/ | ||
case class FundingLeaseFee(fundingWeight: Int, leaseFeeProportional: Int, leaseFeeBase: Satoshi) { | ||
/** | ||
* Fees paid by the liquidity buyer: the resulting amount must be added to the seller's output in the corresponding | ||
* commitment transaction. | ||
*/ | ||
def fees(feerate: FeeratePerKw, requestedAmount: Satoshi, contributedAmount: Satoshi): LeaseFees = { | ||
val onChainFees = Transactions.weight2fee(feerate, fundingWeight) | ||
// If the seller adds more liquidity than requested, the buyer doesn't pay for that extra liquidity. | ||
val proportionalFee = requestedAmount.min(contributedAmount).toMilliSatoshi * leaseFeeProportional / 10_000 | ||
LeaseFees(onChainFees, leaseFeeBase + proportionalFee.truncateToSatoshi) | ||
} | ||
} | ||
|
||
// @formatter:off | ||
sealed trait FundingLease { def fundingFee: FundingLeaseFee } | ||
case class BasicFundingLease(minAmount: Satoshi, maxAmount: Satoshi, fundingFee: FundingLeaseFee) extends FundingLease | ||
case class DurationBasedFundingLease(leaseDuration: Int, minAmount: Satoshi, maxAmount: Satoshi, fundingFee: FundingLeaseFee, maxRelayFeeProportional: Int, maxRelayFeeBase: MilliSatoshi) extends FundingLease | ||
// @formatter:on | ||
|
||
// @formatter:off | ||
sealed trait LeaseRatesTlv extends Tlv | ||
case class BasicFundingLeaseRates(rates: List[BasicFundingLease]) extends LeaseRatesTlv | ||
case class DurationBasedFundingLeaseRates(rates: List[DurationBasedFundingLease]) extends LeaseRatesTlv | ||
// @formatter:on | ||
|
||
// @formatter:off | ||
sealed trait FundingLeaseWitness | ||
case class BasicFundingLeaseWitness(fundingScript: ByteVector) extends FundingLeaseWitness | ||
case class DurationBasedFundingLeaseWitness(leaseExpiry: Long, fundingScript: ByteVector, maxRelayFeeProportional: Int, maxRelayFeeBase: MilliSatoshi) extends FundingLeaseWitness | ||
// @formatter:on | ||
|
||
case class RequestFunds(requestedAmount: Satoshi, fundingLease: FundingLease) | ||
|
||
case class WillFund(leaseWitness: FundingLeaseWitness, signature: ByteVector64) | ||
|
||
case class LeaseFees(miningFee: Satoshi, serviceFee: Satoshi) { | ||
val total: Satoshi = miningFee + serviceFee | ||
} | ||
|
||
def signLease(request: RequestFunds, nodeKey: PrivateKey, fundingScript: ByteVector, currentBlockHeight: Long): WillFund = { | ||
val witness = request.fundingLease match { | ||
case _: BasicFundingLease => BasicFundingLeaseWitness(fundingScript) | ||
case l: DurationBasedFundingLease => DurationBasedFundingLeaseWitness(currentBlockHeight + l.leaseDuration, fundingScript, l.maxRelayFeeProportional, l.maxRelayFeeBase) | ||
} | ||
val toSign = witness match { | ||
case w: BasicFundingLeaseWitness => Crypto.sha256(ByteVector("basic_funding_lease".getBytes(Charsets.US_ASCII)) ++ Codecs.basicFundingLeaseWitness.encode(w).require.bytes) | ||
case w: DurationBasedFundingLeaseWitness => Crypto.sha256(ByteVector("duration_based_funding_lease".getBytes(Charsets.US_ASCII)) ++ Codecs.durationBasedFundingLeaseWitness.encode(w).require.bytes) | ||
} | ||
WillFund(witness, Crypto.sign(toSign, nodeKey)) | ||
} | ||
|
||
object Codecs { | ||
private val fundingLeaseFee: Codec[FundingLeaseFee] = ( | ||
("fundingWeight" | uint16) :: | ||
("leaseFeeBasis" | uint16) :: | ||
("leaseFeeBase" | satoshi32) | ||
).as[FundingLeaseFee] | ||
|
||
private val basicFundingLease: Codec[BasicFundingLease] = ( | ||
("minLeaseAmount" | satoshi32) :: | ||
("maxLeaseAmount" | satoshi32) :: | ||
("leaseFee" | fundingLeaseFee) | ||
).as[BasicFundingLease] | ||
|
||
private val durationBasedFundingLease: Codec[DurationBasedFundingLease] = ( | ||
("leaseDuration" | uint16) :: | ||
("minLeaseAmount" | satoshi32) :: | ||
("maxLeaseAmount" | satoshi32) :: | ||
("leaseFee" | fundingLeaseFee) :: | ||
("maxChannelFeeBasis" | uint16) :: | ||
("maxChannelFeeBase" | millisatoshi32) | ||
).as[DurationBasedFundingLease] | ||
|
||
private val fundingLease: Codec[FundingLease] = discriminated[FundingLease].by(byte) | ||
.typecase(1, basicFundingLease) | ||
.typecase(3, durationBasedFundingLease) | ||
|
||
val basicFundingLeaseWitness: Codec[BasicFundingLeaseWitness] = ("fundingScript" | varsizebinarydata).as[BasicFundingLeaseWitness] | ||
|
||
val durationBasedFundingLeaseWitness: Codec[DurationBasedFundingLeaseWitness] = ( | ||
("leaseExpiry" | uint32) :: | ||
("fundingScript" | varsizebinarydata) :: | ||
("maxChannelFeeBasis" | uint16) :: | ||
("maxChannelFeeBase" | millisatoshi32) | ||
).as[DurationBasedFundingLeaseWitness] | ||
|
||
private val fundingLeaseWitness: Codec[FundingLeaseWitness] = discriminated[FundingLeaseWitness].by(byte) | ||
.typecase(1, basicFundingLeaseWitness) | ||
.typecase(3, durationBasedFundingLeaseWitness) | ||
|
||
val requestFunds: Codec[RequestFunds] = ( | ||
("requestedAmount" | satoshi) :: | ||
("fundingLease" | fundingLease) | ||
).as[RequestFunds] | ||
|
||
val willFund: Codec[WillFund] = ( | ||
("leaseWitness" | fundingLeaseWitness) :: | ||
("signature" | bytes64) | ||
).as[WillFund] | ||
|
||
val leaseRates: Codec[TlvStream[LeaseRatesTlv]] = tlvStream(discriminated[LeaseRatesTlv].by(varint) | ||
.typecase(UInt64(1), tlvField(list(basicFundingLease).as[BasicFundingLeaseRates])) | ||
.typecase(UInt64(3), tlvField(list(durationBasedFundingLease).as[DurationBasedFundingLeaseRates])) | ||
) | ||
} | ||
|
||
} |
This file contains 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 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.