Skip to content

Commit

Permalink
rfqmsg: populate transfer type field in request message
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Nov 15, 2024
1 parent b4e92eb commit df78d25
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
57 changes: 57 additions & 0 deletions rfqmsg/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,63 @@ func (id *ID) Record() tlv.Record {
return tlv.MakeStaticRecord(0, id, recordSize, IdEncoder, IdDecoder)
}

// TransferType defines the type of transaction which will be performed if the
// quote request leads to an accepted agreement.
type TransferType uint8

const (
// UnspecifiedTransferType represents an undefined or transfer type.
UnspecifiedTransferType TransferType = iota

// PayInvoiceTransferType indicates that the requesting peer wants to
// pay a Lightning Network invoice using a taproot asset.
PayInvoiceTransferType

// RecvPaymentTransferType indicates that the requesting peer wants
// to receive taproot asset funds linked to a Lightning Network invoice.
RecvPaymentTransferType
)

// Record returns a TLV record that can be used to encode/decode a transfer type
// to/from a TLV stream.
//
// NOTE: This is part of the tlv.RecordProducer interface.
func (t *TransferType) Record() tlv.Record {
// Note that we set the type here as zero, as when used with a
// tlv.RecordT, the type param will be used as the type.
return tlv.MakeStaticRecord(
0, t, 1, TransferTypeEncoder, TransferTypeDecoder,
)
}

// TransferTypeEncoder is a function that can be used to encode a TransferType
// to a writer.
func TransferTypeEncoder(w io.Writer, val any, buf *[8]byte) error {
if transferType, ok := val.(*TransferType); ok {
transferTypeInt := uint8(*transferType)
return tlv.EUint8(w, &transferTypeInt, buf)
}

return tlv.NewTypeForEncodingErr(val, "TransferType")
}

// TransferTypeDecoder is a function that can be used to decode a TransferType
// from a reader.
func TransferTypeDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
if transferType, ok := val.(*TransferType); ok {
var transferTypeInt uint8
err := tlv.DUint8(r, &transferTypeInt, buf, l)
if err != nil {
return err
}

*transferType = TransferType(transferTypeInt)
return nil
}

return tlv.NewTypeForDecodingErr(val, "TransferType", l, 8)
}

// AssetRate represents the exchange rate of an asset to BTC, encapsulating
// both the rate in fixed-point format and an expiration timestamp.
//
Expand Down
10 changes: 9 additions & 1 deletion rfqmsg/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ type requestWireMsgData struct {
// ID is the unique identifier of the quote request.
ID tlv.RecordT[tlv.TlvType2, ID]

// TODO(ffranr): Add transfer type field with TLV type 4.
// TransferType defines the type of transaction which will be performed
// if the quote request leads to an accepted agreement.
TransferType tlv.RecordT[tlv.TlvType4, TransferType]

// Expiry is the Unix timestamp (in seconds) when the quote expires.
// The quote becomes invalid after this time.
Expand Down Expand Up @@ -129,6 +131,7 @@ type requestWireMsgData struct {
func newRequestWireMsgDataFromBuy(q BuyRequest) (requestWireMsgData, error) {
version := tlv.NewRecordT[tlv.TlvType0](q.Version)
id := tlv.NewRecordT[tlv.TlvType2](q.ID)
transferType := tlv.NewRecordT[tlv.TlvType4](RecvPaymentTransferType)

// Set the expiry to the default request lifetime unless an asset rate
// hint is provided.
Expand Down Expand Up @@ -179,6 +182,7 @@ func newRequestWireMsgDataFromBuy(q BuyRequest) (requestWireMsgData, error) {
return requestWireMsgData{
Version: version,
ID: id,
TransferType: transferType,
Expiry: expiryTlv,
InAssetID: inAssetID,
InAssetGroupKey: inAssetGroupKey,
Expand All @@ -194,6 +198,7 @@ func newRequestWireMsgDataFromBuy(q BuyRequest) (requestWireMsgData, error) {
func newRequestWireMsgDataFromSell(q SellRequest) (requestWireMsgData, error) {
version := tlv.NewPrimitiveRecord[tlv.TlvType0](q.Version)
id := tlv.NewRecordT[tlv.TlvType2](q.ID)
transferType := tlv.NewRecordT[tlv.TlvType4](PayInvoiceTransferType)

// Set the expiry to the default request lifetime unless an asset rate
// hint is provided.
Expand Down Expand Up @@ -247,6 +252,7 @@ func newRequestWireMsgDataFromSell(q SellRequest) (requestWireMsgData, error) {
return requestWireMsgData{
Version: version,
ID: id,
TransferType: transferType,
Expiry: expiryTlv,
InAssetID: inAssetID,
OutAssetID: outAssetID,
Expand Down Expand Up @@ -328,6 +334,7 @@ func (m *requestWireMsgData) Encode(w io.Writer) error {
records := []tlv.Record{
m.Version.Record(),
m.ID.Record(),
m.TransferType.Record(),
m.Expiry.Record(),
m.MaxInAsset.Record(),
}
Expand Down Expand Up @@ -408,6 +415,7 @@ func (m *requestWireMsgData) Decode(r io.Reader) error {
tlvStream, err := tlv.NewStream(
m.Version.Record(),
m.ID.Record(),
m.TransferType.Record(),
m.Expiry.Record(),

inAssetID.Record(),
Expand Down

0 comments on commit df78d25

Please sign in to comment.