Skip to content

Commit

Permalink
Merge pull request #53 from celenium-io/feature/gas-estimation
Browse files Browse the repository at this point in the history
Feature: gas tracker endpoint
  • Loading branch information
aopoltorzhicky committed Nov 25, 2023
2 parents 62048ef + 9119801 commit 9672695
Show file tree
Hide file tree
Showing 20 changed files with 837 additions and 10 deletions.
44 changes: 41 additions & 3 deletions cmd/api/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions cmd/api/docs/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions cmd/api/docs/swagger.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions cmd/api/gas/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-FileCopyrightText: 2023 PK Lab AG <[email protected]>
// SPDX-License-Identifier: MIT

package gas

import (
"sync"

"github.com/shopspring/decimal"
)

type GasPrice struct {
Slow string
Median string
Fast string
}

type info struct {
Height uint64
Percentiles []decimal.Decimal
TxCount int64
GasUsed int64
GasWanted int64
Fee decimal.Decimal
GasUsedRatio decimal.Decimal
}

type queue struct {
data []info
capacity int
mx *sync.RWMutex
}

func newQueue(capacity int) *queue {
return &queue{
data: make([]info, 0),
capacity: capacity,
mx: new(sync.RWMutex),
}
}

func (q *queue) Push(item info) {
q.mx.Lock()
if len(q.data) == q.capacity {
q.data = q.data[:len(q.data)-2]
}
q.data = append([]info{item}, q.data...)
q.mx.Unlock()
}

func (q *queue) Range(handler func(item info) (bool, error)) error {
if handler == nil {
return nil
}

q.mx.RLock()
defer q.mx.RUnlock()

for i := range q.data {
br, err := handler(q.data[i])
if err != nil {
return err
}
if br {
return nil
}
}
return nil
}

func (q *queue) Size() int {
q.mx.RLock()
defer q.mx.RUnlock()

return len(q.data)
}
29 changes: 29 additions & 0 deletions cmd/api/gas/data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2023 PK Lab AG <[email protected]>
// SPDX-License-Identifier: MIT

package gas

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestQueue(t *testing.T) {
q := newQueue(10)

for i := 0; i < 10000; i++ {
q.Push(info{
Height: uint64(i),
TxCount: 2,
})
}

var totalTx int64
err := q.Range(func(item info) (bool, error) {
totalTx += item.TxCount
return false, nil
})
require.NoError(t, err)
require.EqualValues(t, 20, totalTx)
}
Loading

0 comments on commit 9672695

Please sign in to comment.