Skip to content

Commit

Permalink
Merge branch 'main' into merge-main-feat-v3-2024.09.03
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Sep 3, 2024
2 parents f9622f2 + 2f32d82 commit 9f57069
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clients/gasManagement/gasStation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,6 +48,7 @@ type gasStation struct {
gasPriceSelector core.EthGasPriceSelector
loopStatus *atomic.Flag
gasPriceMultiplier *big.Int
minGasPriceValue *big.Int

mut sync.RWMutex
latestGasPrice int
Expand All @@ -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,
}
Expand Down Expand Up @@ -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
}

Expand Down
29 changes: 29 additions & 0 deletions clients/gasManagement/gasStation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,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",
Expand Down

0 comments on commit 9f57069

Please sign in to comment.