Skip to content

Commit 85247b0

Browse files
committed
feat: enhance general configuration with excess tolerance fields and update related tests
1 parent 399cd23 commit 85247b0

File tree

7 files changed

+133
-28
lines changed

7 files changed

+133
-28
lines changed

internal/adapters/dataproviders/database/mongo/liquidity_provider_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ var generalTestConfig = &entities.Signed[liquidity_provider.GeneralConfiguration
6060
"5": 6,
6161
"7": 8,
6262
},
63-
PublicLiquidityCheck: false,
64-
MaxLiquidity: entities.NewWei(123),
63+
PublicLiquidityCheck: false,
64+
MaxLiquidity: entities.NewWei(123),
65+
ExcessToleranceFixed: entities.NewWei(0),
66+
ExcessTolerancePercentage: utils.NewBigFloat64(0),
6567
},
6668
Signature: "general signature",
6769
Hash: "general hash",
@@ -152,7 +154,7 @@ func TestLpMongoRepository_GetGeneralConfiguration(t *testing.T) {
152154
filter := bson.D{primitive.E{Key: "name", Value: mongo.ConfigurationName("general")}}
153155
log.SetLevel(log.DebugLevel)
154156
t.Run("general configuration read successfully", func(t *testing.T) {
155-
const expectedLog = "READ interaction with db: {Value:{RskConfirmations:map[1:2 3:4] BtcConfirmations:map[5:6 7:8] PublicLiquidityCheck:false MaxLiquidity:123} Signature:general signature Hash:general hash}"
157+
const expectedLog = "READ interaction with db: {Value:{RskConfirmations:map[1:2 3:4] BtcConfirmations:map[5:6 7:8] PublicLiquidityCheck:false MaxLiquidity:123 ExcessToleranceFixed:0 ExcessTolerancePercentage:0} Signature:general signature Hash:general hash}"
156158
client, collection := getClientAndCollectionMocks(mongo.LiquidityProviderCollection)
157159
repo := mongo.NewLiquidityProviderRepository(mongo.NewConnection(client, time.Duration(1)))
158160
collection.On("FindOne", mock.Anything, filter).
@@ -276,7 +278,7 @@ func TestLpMongoRepository_UpsertGeneralConfiguration(t *testing.T) {
276278
configName := mongo.ConfigurationName("general")
277279
filter := bson.D{primitive.E{Key: "name", Value: configName}}
278280
t.Run("general configuration upserted successfully", func(t *testing.T) {
279-
const expectedLog = "INSERT interaction with db: {Signed:{Value:{RskConfirmations:map[1:2 3:4] BtcConfirmations:map[5:6 7:8] PublicLiquidityCheck:false MaxLiquidity:123} Signature:general signature Hash:general hash} Name:general}"
281+
const expectedLog = "INSERT interaction with db: {Signed:{Value:{RskConfirmations:map[1:2 3:4] BtcConfirmations:map[5:6 7:8] PublicLiquidityCheck:false MaxLiquidity:123 ExcessToleranceFixed:0 ExcessTolerancePercentage:0} Signature:general signature Hash:general hash} Name:general}"
280282
client, collection := getClientAndCollectionMocks(mongo.LiquidityProviderCollection)
281283
repo := mongo.NewLiquidityProviderRepository(mongo.NewConnection(client, time.Duration(1)))
282284
collection.On("ReplaceOne", mock.Anything, filter, mongo.StoredConfiguration[liquidity_provider.GeneralConfiguration]{

internal/adapters/dataproviders/liquidity_provider_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,13 @@ func getGeneralConfigurationMock() *entities.Signed[liquidity_provider.GeneralCo
667667
"8000000000000000000": 40,
668668
"9000000000000000001": 45,
669669
},
670-
PublicLiquidityCheck: false,
671-
MaxLiquidity: entities.NewWei(100000000000000000),
670+
PublicLiquidityCheck: false,
671+
MaxLiquidity: entities.NewWei(100000000000000000),
672+
ExcessToleranceFixed: entities.NewWei(0),
673+
ExcessTolerancePercentage: utils.NewBigFloat64(0),
672674
},
673-
Signature: "034a913d067f54f3c096d569f7913836f7d451a81f3fb6cbfbe0177758a1c22603173d0c26c0a3507310fad61bd7275e5b326eb7f3ccfb56d8b13bbab8d8a32100",
674-
Hash: "89b4fbdf620883d99d77324428f5375e2baf4b05c10d12f2a6ee81a5ea0cc956",
675+
Signature: "2512fe10fd083332e072215a708b3341d465ec4177f6a95c34147927dc9a11337e9135ab20f96c0d72d16402c65554cfcc32feca8a649458491bf4f0dbae2a7e00",
676+
Hash: "daa2faa6aec7abd10f4607e8749e872078bea8d68b82ad5e00bbcd51ae100def",
675677
}
676678
}
677679
func getPeginConfigurationMock() *entities.Signed[liquidity_provider.PeginConfiguration] {

internal/adapters/entrypoints/rest/handlers/set_general_config_test.go

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestSetGeneralConfigHandler(t *testing.T) {
1919
useCase.EXPECT().Run(mock.Anything, mock.Anything).Return(nil)
2020

2121
handler := handlers.NewSetGeneralConfigHandler(useCase)
22-
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000"}}`
22+
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000", "excessToleranceFixed": "0", "excessTolerancePercentage": 0}}`
2323
req := httptest.NewRequest(http.MethodPost, "/configuration", strings.NewReader(reqBody))
2424
req.Header.Set("Content-Type", "application/json")
2525
w := httptest.NewRecorder()
@@ -116,7 +116,7 @@ func TestSetGeneralConfigHandler(t *testing.T) {
116116

117117
handler := handlers.NewSetGeneralConfigHandler(useCase)
118118
// 1 RBTC in wei (18 decimal places) - valid large integer
119-
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000000000000000000"}}`
119+
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000000000000000000", "excessToleranceFixed": "0", "excessTolerancePercentage": 0}}`
120120
req := httptest.NewRequest(http.MethodPost, "/configuration", strings.NewReader(reqBody))
121121
req.Header.Set("Content-Type", "application/json")
122122
w := httptest.NewRecorder()
@@ -129,12 +129,98 @@ func TestSetGeneralConfigHandler(t *testing.T) {
129129
useCase.EXPECT().Run(mock.Anything, mock.Anything).Return(assert.AnError)
130130

131131
handler := handlers.NewSetGeneralConfigHandler(useCase)
132-
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000"}}`
132+
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000", "excessToleranceFixed": "0", "excessTolerancePercentage": 0}}`
133133
req := httptest.NewRequest(http.MethodPost, "/configuration", strings.NewReader(reqBody))
134134
req.Header.Set("Content-Type", "application/json")
135135
w := httptest.NewRecorder()
136136
handler(w, req)
137137
assert.Equal(t, http.StatusInternalServerError, w.Code)
138138
useCase.AssertExpectations(t)
139139
})
140+
t.Run("should return success with valid excessToleranceFixed", func(t *testing.T) {
141+
useCase := new(mocks.SetGeneralConfigUseCaseMock)
142+
useCase.EXPECT().Run(mock.Anything, mock.Anything).Return(nil)
143+
144+
handler := handlers.NewSetGeneralConfigHandler(useCase)
145+
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000", "excessToleranceFixed": "5000000000000000000", "excessTolerancePercentage": 0}}`
146+
req := httptest.NewRequest(http.MethodPost, "/configuration", strings.NewReader(reqBody))
147+
req.Header.Set("Content-Type", "application/json")
148+
w := httptest.NewRecorder()
149+
handler(w, req)
150+
assert.Equal(t, http.StatusNoContent, w.Code)
151+
useCase.AssertExpectations(t)
152+
})
153+
t.Run("should return success with valid excessTolerancePercentage", func(t *testing.T) {
154+
useCase := new(mocks.SetGeneralConfigUseCaseMock)
155+
useCase.EXPECT().Run(mock.Anything, mock.Anything).Return(nil)
156+
157+
handler := handlers.NewSetGeneralConfigHandler(useCase)
158+
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000", "excessToleranceFixed": "0", "excessTolerancePercentage": 5.5}}`
159+
req := httptest.NewRequest(http.MethodPost, "/configuration", strings.NewReader(reqBody))
160+
req.Header.Set("Content-Type", "application/json")
161+
w := httptest.NewRecorder()
162+
handler(w, req)
163+
assert.Equal(t, http.StatusNoContent, w.Code)
164+
useCase.AssertExpectations(t)
165+
})
166+
t.Run("should return bad request if excessToleranceFixed is negative", func(t *testing.T) {
167+
useCase := new(mocks.SetGeneralConfigUseCaseMock)
168+
169+
handler := handlers.NewSetGeneralConfigHandler(useCase)
170+
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000", "excessToleranceFixed": "-100", "excessTolerancePercentage": 0}}`
171+
req := httptest.NewRequest(http.MethodPost, "/configuration", strings.NewReader(reqBody))
172+
req.Header.Set("Content-Type", "application/json")
173+
w := httptest.NewRecorder()
174+
handler(w, req)
175+
assert.Equal(t, http.StatusBadRequest, w.Code)
176+
useCase.AssertNotCalled(t, "Run")
177+
})
178+
t.Run("should return bad request if excessToleranceFixed is not a number", func(t *testing.T) {
179+
useCase := new(mocks.SetGeneralConfigUseCaseMock)
180+
181+
handler := handlers.NewSetGeneralConfigHandler(useCase)
182+
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000", "excessToleranceFixed": "not_a_number", "excessTolerancePercentage": 0}}`
183+
req := httptest.NewRequest(http.MethodPost, "/configuration", strings.NewReader(reqBody))
184+
req.Header.Set("Content-Type", "application/json")
185+
w := httptest.NewRecorder()
186+
handler(w, req)
187+
assert.Equal(t, http.StatusBadRequest, w.Code)
188+
useCase.AssertNotCalled(t, "Run")
189+
})
190+
t.Run("should return bad request if excessToleranceFixed has decimal places", func(t *testing.T) {
191+
useCase := new(mocks.SetGeneralConfigUseCaseMock)
192+
193+
handler := handlers.NewSetGeneralConfigHandler(useCase)
194+
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000", "excessToleranceFixed": "100.5", "excessTolerancePercentage": 0}}`
195+
req := httptest.NewRequest(http.MethodPost, "/configuration", strings.NewReader(reqBody))
196+
req.Header.Set("Content-Type", "application/json")
197+
w := httptest.NewRecorder()
198+
handler(w, req)
199+
assert.Equal(t, http.StatusBadRequest, w.Code)
200+
useCase.AssertNotCalled(t, "Run")
201+
})
202+
t.Run("should return bad request if excessTolerancePercentage is negative", func(t *testing.T) {
203+
useCase := new(mocks.SetGeneralConfigUseCaseMock)
204+
205+
handler := handlers.NewSetGeneralConfigHandler(useCase)
206+
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000", "excessToleranceFixed": "0", "excessTolerancePercentage": -5}}`
207+
req := httptest.NewRequest(http.MethodPost, "/configuration", strings.NewReader(reqBody))
208+
req.Header.Set("Content-Type", "application/json")
209+
w := httptest.NewRecorder()
210+
handler(w, req)
211+
assert.Equal(t, http.StatusBadRequest, w.Code)
212+
useCase.AssertNotCalled(t, "Run")
213+
})
214+
t.Run("should return bad request if excessTolerancePercentage exceeds 100", func(t *testing.T) {
215+
useCase := new(mocks.SetGeneralConfigUseCaseMock)
216+
217+
handler := handlers.NewSetGeneralConfigHandler(useCase)
218+
reqBody := `{"configuration": {"btcConfirmations": {"5": 10}, "rskConfirmations": {"10": 20}, "publicLiquidityCheck": true, "maxLiquidity": "1000", "excessToleranceFixed": "0", "excessTolerancePercentage": 150}}`
219+
req := httptest.NewRequest(http.MethodPost, "/configuration", strings.NewReader(reqBody))
220+
req.Header.Set("Content-Type", "application/json")
221+
w := httptest.NewRecorder()
222+
handler(w, req)
223+
assert.Equal(t, http.StatusBadRequest, w.Code)
224+
useCase.AssertNotCalled(t, "Run")
225+
})
140226
}

internal/entities/common_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/stretchr/testify/require"
1414
)
1515

16+
//nolint:funlen
1617
func TestSigned_CheckIntegrity(t *testing.T) {
1718
peginConfig := liquidity_provider.PeginConfiguration{
1819
TimeForDeposit: 3600,
@@ -51,8 +52,10 @@ func TestSigned_CheckIntegrity(t *testing.T) {
5152
"100000000000000000": 3,
5253
"2000000000000000000": 10,
5354
},
54-
PublicLiquidityCheck: true,
55-
MaxLiquidity: entities.NewUWei(10000000000000000000),
55+
PublicLiquidityCheck: true,
56+
MaxLiquidity: entities.NewUWei(10000000000000000000),
57+
ExcessToleranceFixed: entities.NewWei(0),
58+
ExcessTolerancePercentage: utils.NewBigFloat64(0),
5659
}
5760

5861
tests := []struct {
@@ -61,7 +64,7 @@ func TestSigned_CheckIntegrity(t *testing.T) {
6164
}{
6265
{signed: entities.Signed[any]{Value: peginConfig, Hash: "5ab75cad18e0ad640908a3b70d6bf2e3cdca66bb53544e91833c942c4f5430af"}},
6366
{signed: entities.Signed[any]{Value: pegoutConfig, Hash: "35a51729bb71bb891db62dd968f33ea2479ddb17143da32ca6bb55142a488052"}},
64-
{signed: entities.Signed[any]{Value: generalConfig, Hash: "9765cfc5c4ba0fa42bc6617149fff51c65a7a3d7fa8667fefa4c36564067c080"}},
67+
{signed: entities.Signed[any]{Value: generalConfig, Hash: "cf7fdad2417ab998539130dacb9d9673da54f0aacc8c64cc5f3395e597b7eee1"}},
6568
{signed: entities.Signed[any]{Value: peginConfig, Hash: "f3daab424654d2eeb2b50dc00b3e453e24ca1c690d80015f5f54d5f1fefaf900"}, err: entities.IntegrityError},
6669
{signed: entities.Signed[any]{Value: pegoutConfig, Hash: "3b3e7b075eb60b8c249f44a117f406c64992bafda1273f540277448abd14077e"}, err: entities.IntegrityError},
6770
{signed: entities.Signed[any]{Value: generalConfig, Hash: "3fecc42296c21a63dff80885f972ea88caf5038e47f014b1c91bb9b80529b757"}, err: entities.IntegrityError},

internal/entities/liquidity_provider/default_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ func TestDefaultBtcConfirmationsPerAmount_Max(t *testing.T) {
7171
func TestDefaultGeneralConfiguration(t *testing.T) {
7272
config := liquidity_provider.DefaultGeneralConfiguration()
7373
assert.Equal(t, liquidity_provider.GeneralConfiguration{
74-
RskConfirmations: liquidity_provider.DefaultRskConfirmationsPerAmount(),
75-
BtcConfirmations: liquidity_provider.DefaultBtcConfirmationsPerAmount(),
76-
PublicLiquidityCheck: false,
77-
MaxLiquidity: entities.NewWei(1000000000000000000),
74+
RskConfirmations: liquidity_provider.DefaultRskConfirmationsPerAmount(),
75+
BtcConfirmations: liquidity_provider.DefaultBtcConfirmationsPerAmount(),
76+
PublicLiquidityCheck: false,
77+
MaxLiquidity: entities.NewWei(1000000000000000000),
78+
ExcessToleranceFixed: entities.NewWei(0),
79+
ExcessTolerancePercentage: utils.NewBigFloat64(0),
7880
}, config)
7981
}

internal/entities/liquidity_provider/liquidity_provider_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
ethcrypto "github.com/ethereum/go-ethereum/crypto"
1010
"github.com/rsksmart/liquidity-provider-server/internal/entities"
1111
"github.com/rsksmart/liquidity-provider-server/internal/entities/liquidity_provider"
12+
"github.com/rsksmart/liquidity-provider-server/internal/entities/utils"
1213
"github.com/rsksmart/liquidity-provider-server/test"
1314
"github.com/rsksmart/liquidity-provider-server/test/mocks"
1415
"github.com/stretchr/testify/require"
@@ -90,10 +91,12 @@ func TestValidateConfiguration(t *testing.T) {
9091
"10": 5,
9192
"20": 10,
9293
},
93-
PublicLiquidityCheck: true,
94-
MaxLiquidity: entities.NewWei(1000000),
94+
PublicLiquidityCheck: true,
95+
MaxLiquidity: entities.NewWei(1000000),
96+
ExcessToleranceFixed: entities.NewWei(0),
97+
ExcessTolerancePercentage: utils.NewBigFloat64(0),
9598
}
96-
mockConfigBytes := []byte(`{"rskConfirmations":{"10":100,"20":200},"btcConfirmations":{"10":5,"20":10},"publicLiquidityCheck":true,"maxLiquidity":1000000}`)
99+
mockConfigBytes := []byte(`{"rskConfirmations":{"10":100,"20":200},"btcConfirmations":{"10":5,"20":10},"publicLiquidityCheck":true,"maxLiquidity":1000000,"excessToleranceFixed":0,"excessTolerancePercentage":0}`)
97100

98101
hash := ethcrypto.Keccak256(mockConfigBytes)
99102
hashHex := hex.EncodeToString(hash)
@@ -185,10 +188,12 @@ func TestValidateConfiguration(t *testing.T) {
185188
"10": 5,
186189
"20": 10,
187190
},
188-
PublicLiquidityCheck: true,
189-
MaxLiquidity: entities.NewWei(1000000),
191+
PublicLiquidityCheck: true,
192+
MaxLiquidity: entities.NewWei(1000000),
193+
ExcessToleranceFixed: entities.NewWei(0),
194+
ExcessTolerancePercentage: utils.NewBigFloat64(0),
190195
}
191-
mockConfigBytes := []byte(`{"rskConfirmations":{"10":100,"20":200},"btcConfirmations":{"10":5,"20":10},"publicLiquidityCheck":true,"maxLiquidity":1000000}`)
196+
mockConfigBytes := []byte(`{"rskConfirmations":{"10":100,"20":200},"btcConfirmations":{"10":5,"20":10},"publicLiquidityCheck":true,"maxLiquidity":1000000,"excessToleranceFixed":0,"excessTolerancePercentage":0}`)
192197

193198
hash := ethcrypto.Keccak256(mockConfigBytes)
194199
hashHex := hex.EncodeToString(hash)

pkg/liquidity_provider_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,17 +630,22 @@ func TestToTrustedAccountsDTO(t *testing.T) {
630630
func TestFromGeneralConfigurationDTO(t *testing.T) {
631631
t.Run("converts valid configuration", func(t *testing.T) {
632632
dto := pkg.GeneralConfigurationDTO{
633-
RskConfirmations: map[string]uint16{"1000000000000000000": 5, "2000000000000000000": 10},
634-
BtcConfirmations: map[string]uint16{"3000000000000000000": 15, "4000000000000000000": 20},
635-
PublicLiquidityCheck: true,
636-
MaxLiquidity: "12345678901234567890",
633+
RskConfirmations: map[string]uint16{"1000000000000000000": 5, "2000000000000000000": 10},
634+
BtcConfirmations: map[string]uint16{"3000000000000000000": 15, "4000000000000000000": 20},
635+
PublicLiquidityCheck: true,
636+
MaxLiquidity: "12345678901234567890",
637+
ExcessToleranceFixed: "1000000000000000000",
638+
ExcessTolerancePercentage: 5.5,
637639
}
638640
config, err := pkg.FromGeneralConfigurationDTO(dto)
639641
require.NoError(t, err)
640642
assert.Equal(t, dto.RskConfirmations, map[string]uint16(config.RskConfirmations))
641643
assert.Equal(t, dto.BtcConfirmations, map[string]uint16(config.BtcConfirmations))
642644
assert.Equal(t, dto.PublicLiquidityCheck, config.PublicLiquidityCheck)
643645
assert.Equal(t, "12345678901234567890", config.MaxLiquidity.String())
646+
assert.Equal(t, "1000000000000000000", config.ExcessToleranceFixed.String())
647+
percentage, _ := config.ExcessTolerancePercentage.Native().Float64()
648+
assert.InDelta(t, 5.5, percentage, 0.0001)
644649
test.AssertNonZeroValues(t, dto)
645650
})
646651
t.Run("returns error on invalid max liquidity", func(t *testing.T) {

0 commit comments

Comments
 (0)