-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from celenium-io/feature/gas-estimation
Feature: gas tracker endpoint
- Loading branch information
Showing
20 changed files
with
837 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// SPDX-FileCopyrightText: 2023 PK Lab AG <contact@pklab.io> | ||
// 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-FileCopyrightText: 2023 PK Lab AG <contact@pklab.io> | ||
// 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) | ||
} |
Oops, something went wrong.