diff --git a/gasstation/gasstattion.go b/gasstation/gasstattion.go index 8e9bcf06b7..4dfd0f8a92 100644 --- a/gasstation/gasstattion.go +++ b/gasstation/gasstattion.go @@ -236,8 +236,15 @@ func (gs *GasStation) FeeHistory(ctx context.Context, blocks, lastBlock uint64, func feesPercentiles(ascFees []*big.Int, percentiles []float64) []*big.Int { res := make([]*big.Int, len(percentiles)) + if len(ascFees) == 0 { + for i := range res { + res[i] = big.NewInt(0) + } + return res + } for i, p := range percentiles { - idx := int(float64(len(ascFees)) * p) + // percentiles are given in [0, 100] + idx := int(float64(len(ascFees)) * p / 100) if idx >= len(ascFees) { idx = len(ascFees) - 1 } diff --git a/gasstation/gasstattion_test.go b/gasstation/gasstattion_test.go index 9824da10b4..9626a1efe3 100644 --- a/gasstation/gasstattion_test.go +++ b/gasstation/gasstattion_test.go @@ -283,3 +283,16 @@ func prepareBlocks(r *require.Assertions, cases []testActionGas) map[uint64]*blo } return blocks } + +func TestFeesPercentiles(t *testing.T) { + r := require.New(t) + fees := make([]*big.Int, 10) + for i := range fees { + fees[i] = big.NewInt(int64(i + 1)) + } + got := feesPercentiles(fees, []float64{0, 25, 50, 75, 100}) + r.Equal([]*big.Int{big.NewInt(1), big.NewInt(3), big.NewInt(6), big.NewInt(8), big.NewInt(10)}, got) + + got = feesPercentiles(nil, []float64{10, 90}) + r.Equal([]*big.Int{big.NewInt(0), big.NewInt(0)}, got) +}