From e1e8268049f1dcc46b29df48c699ad5891bfa3c4 Mon Sep 17 00:00:00 2001 From: ffranr Date: Sat, 26 Oct 2024 01:54:45 +0100 Subject: [PATCH] rfqmsg: add request fields MinInAsset and MinOutAsset --- rfqmsg/request.go | 34 ++++++++++++++++++++++++++++++++++ rfqmsg/request_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/rfqmsg/request.go b/rfqmsg/request.go index ad577b3d9..8cffafb08 100644 --- a/rfqmsg/request.go +++ b/rfqmsg/request.go @@ -113,6 +113,16 @@ type requestWireMsgData struct { // // NOTE: This field is optional. OutAssetRateHint requestOutAssetRateHint + + // MinInAsset is an optional minimum quantity of in-asset that may be + // transferred under the terms of the quote, applicable whether the + // asset is BTC or any other. + MinInAsset tlv.OptionalRecordT[tlv.TlvType23, uint64] + + // MinOutAsset is an optional minimum quantity of out-asset that may be + // transferred under the terms of the quote, applicable whether the + // asset is BTC or any other. + MinOutAsset tlv.OptionalRecordT[tlv.TlvType25, uint64] } // newRequestWireMsgDataFromBuy creates a new requestWireMsgData from a buy @@ -352,6 +362,17 @@ func (m *requestWireMsgData) Encode(w io.Writer) error { }, ) + m.MinInAsset.WhenSome( + func(r tlv.RecordT[tlv.TlvType23, uint64]) { + records = append(records, r.Record()) + }, + ) + m.MinOutAsset.WhenSome( + func(r tlv.RecordT[tlv.TlvType25, uint64]) { + records = append(records, r.Record()) + }, + ) + tlv.SortRecords(records) // Create the tlv stream. @@ -375,6 +396,9 @@ func (m *requestWireMsgData) Decode(r io.Reader) error { inAssetRateHint := m.InAssetRateHint.Zero() outAssetRateHint := m.OutAssetRateHint.Zero() + minInAsset := m.MinInAsset.Zero() + minOutAsset := m.MinOutAsset.Zero() + // Create a tlv stream with all the fields. tlvStream, err := tlv.NewStream( m.Version.Record(), @@ -391,6 +415,9 @@ func (m *requestWireMsgData) Decode(r io.Reader) error { inAssetRateHint.Record(), outAssetRateHint.Record(), + + minInAsset.Record(), + minOutAsset.Record(), ) if err != nil { return err @@ -424,6 +451,13 @@ func (m *requestWireMsgData) Decode(r io.Reader) error { m.OutAssetRateHint = tlv.SomeRecordT(outAssetRateHint) } + if _, ok := tlvMap[minInAsset.TlvType()]; ok { + m.MinInAsset = tlv.SomeRecordT(minInAsset) + } + if _, ok := tlvMap[minOutAsset.TlvType()]; ok { + m.MinOutAsset = tlv.SomeRecordT(minOutAsset) + } + return nil } diff --git a/rfqmsg/request_test.go b/rfqmsg/request_test.go index e14334743..20f5bca69 100644 --- a/rfqmsg/request_test.go +++ b/rfqmsg/request_test.go @@ -30,6 +30,9 @@ type testCaseEncodeDecode struct { maxInAsset uint64 inAssetRateHint *uint64 outAssetRateHint *uint64 + + minInAsset *uint64 + minOutAsset *uint64 } // Request generates a requestWireMsgData instance from the test case. @@ -90,6 +93,20 @@ func (tc testCaseEncodeDecode) Request() requestWireMsgData { ) } + var minInAsset tlv.OptionalRecordT[tlv.TlvType23, uint64] + if tc.minInAsset != nil { + minInAsset = tlv.SomeRecordT[tlv.TlvType23]( + tlv.NewPrimitiveRecord[tlv.TlvType23](*tc.minInAsset), + ) + } + + var minOutAsset tlv.OptionalRecordT[tlv.TlvType25, uint64] + if tc.minOutAsset != nil { + minOutAsset = tlv.SomeRecordT[tlv.TlvType25]( + tlv.NewPrimitiveRecord[tlv.TlvType25](*tc.minOutAsset), + ) + } + return requestWireMsgData{ Version: version, ID: id, @@ -101,6 +118,8 @@ func (tc testCaseEncodeDecode) Request() requestWireMsgData { MaxInAsset: maxInAsset, InAssetRateHint: inAssetRateHint, OutAssetRateHint: outAssetRateHint, + MinInAsset: minInAsset, + MinOutAsset: minOutAsset, } } @@ -129,6 +148,9 @@ func TestRequestMsgDataEncodeDecode(t *testing.T) { inAssetRateHint := uint64(1000) outAssetRateHint := uint64(2000) + minInAsset := uint64(1) + minOutAsset := uint64(10) + testCases := []testCaseEncodeDecode{ { testName: "in asset ID, out asset ID zero, " + @@ -144,6 +166,8 @@ func TestRequestMsgDataEncodeDecode(t *testing.T) { maxInAsset: 1000, inAssetRateHint: &inAssetRateHint, outAssetRateHint: &outAssetRateHint, + minInAsset: &minInAsset, + minOutAsset: &minOutAsset, }, { testName: "in asset ID, out asset ID zero, no asset " +