Skip to content

Commit

Permalink
rfqmsg: add request fields MinInAsset and MinOutAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Oct 26, 2024
1 parent de341b3 commit e1e8268
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions rfqmsg/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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(),
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
24 changes: 24 additions & 0 deletions rfqmsg/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -101,6 +118,8 @@ func (tc testCaseEncodeDecode) Request() requestWireMsgData {
MaxInAsset: maxInAsset,
InAssetRateHint: inAssetRateHint,
OutAssetRateHint: outAssetRateHint,
MinInAsset: minInAsset,
MinOutAsset: minOutAsset,
}
}

Expand Down Expand Up @@ -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, " +
Expand All @@ -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 " +
Expand Down

0 comments on commit e1e8268

Please sign in to comment.