Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion gasstation/gasstattion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions gasstation/gasstattion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading