From 8c5ee2f48c641af79bc51eadf0769d9dbf50360c Mon Sep 17 00:00:00 2001 From: dragosrebegea Date: Wed, 28 Aug 2024 10:54:26 +0300 Subject: [PATCH] gas below 1 --- clients/gasManagement/gasStation.go | 6 +++++ clients/gasManagement/gasStation_test.go | 29 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/clients/gasManagement/gasStation.go b/clients/gasManagement/gasStation.go index a2b54762..03cfb9f7 100644 --- a/clients/gasManagement/gasStation.go +++ b/clients/gasManagement/gasStation.go @@ -20,6 +20,7 @@ const minPollingInterval = time.Second const minRequestTime = time.Millisecond const logPath = "EthClient/gasStation" const minGasPriceMultiplier = 1 +const minGasPriceValue = 1 const minFetchRetries = 2 // ArgsGasStation is the DTO used for the creating a new gas handler instance @@ -47,6 +48,7 @@ type gasStation struct { gasPriceSelector core.EthGasPriceSelector loopStatus *atomic.Flag gasPriceMultiplier *big.Int + minGasPriceValue *big.Int mut sync.RWMutex latestGasPrice int @@ -71,6 +73,7 @@ func NewGasStation(args ArgsGasStation) (*gasStation, error) { gasPriceSelector: args.GasPriceSelector, loopStatus: &atomic.Flag{}, gasPriceMultiplier: big.NewInt(int64(args.GasPriceMultiplier)), + minGasPriceValue: big.NewInt(minGasPriceValue), latestGasPrice: -1, fetchRetries: 0, } @@ -221,6 +224,9 @@ func (gs *gasStation) GetCurrentGasPrice() (*big.Int, error) { } result := big.NewInt(int64(gs.latestGasPrice)) + if result.Cmp(gs.minGasPriceValue) < 0 { + result.Set(gs.minGasPriceValue) + } return result.Mul(result, gs.gasPriceMultiplier), nil } diff --git a/clients/gasManagement/gasStation_test.go b/clients/gasManagement/gasStation_test.go index cb6d9c8a..e0d0f9f7 100644 --- a/clients/gasManagement/gasStation_test.go +++ b/clients/gasManagement/gasStation_test.go @@ -407,6 +407,35 @@ func TestGasStation_GetCurrentGasPriceExceededMaximum(t *testing.T) { _ = gs.Close() } +func TestGasStation_GetCurrentGasPriceBelowMin(t *testing.T) { + t.Parallel() + + gsResponse := createMockGasStationResponse() + gsResponse.Result.SafeGasPrice = "0.944851822" + args := createMockArgsGasStation() + httpServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + rw.WriteHeader(http.StatusOK) + + resp, _ := json.Marshal(&gsResponse) + _, _ = rw.Write(resp) + })) + defer httpServer.Close() + + args.RequestURL = httpServer.URL + + gs, err := NewGasStation(args) + require.Nil(t, err) + expectedPrice := big.NewInt(0).Mul(gs.minGasPriceValue, gs.gasPriceMultiplier) + + time.Sleep(time.Second * 2) + assert.True(t, gs.loopStatus.IsSet()) + + price, err := gs.GetCurrentGasPrice() + require.Nil(t, err) + assert.Equal(t, expectedPrice, price) + _ = gs.Close() +} + func createMockGasStationResponse() gasStationResponse { return gasStationResponse{ Status: "1",