Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from itzmeanjan/graphql-add-query
Browse files Browse the repository at this point in the history
GraphQL New Queries
  • Loading branch information
itzmeanjan committed Jul 23, 2021
2 parents 430ce31 + ef745a6 commit 4572d37
Show file tree
Hide file tree
Showing 8 changed files with 621 additions and 0 deletions.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Reduce Chaos in MemPool 😌
- [Inspecting tx(s) in pending pool](#pending-pool)
- [Pending For >= `X`](#pending-for-more-than-X)
- [Pending For <= `X`](#pending-for-less-than-X)
- [Pending With >= `X` ( Gwei )](#pending-with-more-than-X)
- [Pending With <= `X` ( Gwei )](#pending-with-less-than-X)
- [Pending From Address `A`](#pending-from-A)
- [Pending To Address `A`](#pending-to-A)
- [Top `X` Pending Tx(s)](#top-X-pending)
Expand All @@ -37,6 +39,8 @@ Reduce Chaos in MemPool 😌
- [Inspecting tx(s) in queued pool](#queued-pool)
- [Queued For >= `X`](#queued-for-more-than-X)
- [Queued For <= `X`](#queued-for-less-than-X)
- [Queued With >= `X` ( Gwei )](#queued-with-more-than-X)
- [Queued With <= `X` ( Gwei )](#queued-with-less-than-X)
- [Queued From Address `A`](#queued-from-A)
- [Queued To Address `A`](#queued-to-A)
- [Top `X` Queued Tx(s)](#top-X-queued)
Expand Down Expand Up @@ -434,6 +438,48 @@ query {

---

### Pending with more than `X`

For listing all tx(s) pending with gas price >= `x` GWei, send graphQL query

Method : **POST**

URL : **/v1/graphql**


```graphql
query {
pendingWithMoreThan(x: 20.1) {
from
hash
gasPriceGwei
}
}
```

---

### Pending with less than `X`

For listing all tx(s) pending with gas price <= `x` GWei, send graphQL query

Method : **POST**

URL : **/v1/graphql**


```graphql
query {
pendingWithLessThan(x: 10.1) {
from
hash
gasPriceGwei
}
}
```

---

### Pending from `A`

For getting a list of all pending tx(s) `from` specific address, send a graphQL query like 👇
Expand Down Expand Up @@ -846,6 +892,48 @@ query {

---

### Queued with more than `X`

For listing all tx(s) queued with gas price >= `x` GWei, send graphQL query

Method : **POST**

URL : **/v1/graphql**


```graphql
query {
queuedWithMoreThan(x: 20.1) {
from
hash
gasPriceGwei
}
}
```

---

### Queued with less than `X`

For listing all tx(s) queued with gas price <= `x` GWei, send graphQL query

Method : **POST**

URL : **/v1/graphql**


```graphql
query {
queuedWithLessThan(x: 10.1) {
from
hash
gasPriceGwei
}
}
```

---

### Queued from `A`

For getting a list of all queued tx(s) `from` specific address, send a graphQL query like 👇
Expand Down
52 changes: 52 additions & 0 deletions app/data/pending.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,58 @@ func (p *PendingPool) FresherThanX(x time.Duration) []*MemPoolTx {

}

// HigherThanX - Returns a list of pending txs which are paid with
// gas price >= `X`
func (p *PendingPool) HigherThanX(x float64) []*MemPoolTx {
txs := p.DescListTxs()
if txs == nil {
return nil
}

txCount := uint64(len(txs))
result := make([]*MemPoolTx, 0, txCount)

for i := 0; i < len(txs); i++ {
// Stop ASAP, because iterating over
// descending sorted ( w.r.t. gas price )
// tx list
if !txs[i].HasGasPriceMoreThan(x) {
break
}

result = append(result, txs[i])
}

CleanSlice(txs)
return result
}

// LowerThanX - Returns a list of pending txs which are paid with
// gas price <= `X`
func (p *PendingPool) LowerThanX(x float64) []*MemPoolTx {
txs := p.AscListTxs()
if txs == nil {
return nil
}

txCount := uint64(len(txs))
result := make([]*MemPoolTx, 0, txCount)

for i := 0; i < len(txs); i++ {
// Stop ASAP, because iterating over
// ascending sorted ( w.r.t. gas price )
// tx list
if !txs[i].HasGasPriceLessThan(x) {
break
}

result = append(result, txs[i])
}

CleanSlice(txs)
return result
}

// Add - Attempts to add new tx found in pending pool into
// harmony mempool, so that further manipulation can be performed on it
//
Expand Down
20 changes: 20 additions & 0 deletions app/data/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ func (m *MemPool) QueuedForLTE(x time.Duration) []*MemPoolTx {
return m.Queued.FresherThanX(x)
}

// PendingWithGTE - Returns list of tx(s), pending with gas price >= `X`
func (m *MemPool) PendingWithGTE(x float64) []*MemPoolTx {
return m.Pending.HigherThanX(x)
}

// PendingWithLTE - Returns list of tx(s), pending with gas price <= `X`
func (m *MemPool) PendingWithLTE(x float64) []*MemPoolTx {
return m.Pending.LowerThanX(x)
}

// QueuedWithGTE - Returns list of tx(s), queued with gas price >= `X`
func (m *MemPool) QueuedWithGTE(x float64) []*MemPoolTx {
return m.Queued.HigherThanX(x)
}

// QueuedWithLTE - Returns list of tx(s), queued with gas price <= `X`
func (m *MemPool) QueuedWithLTE(x float64) []*MemPoolTx {
return m.Queued.LowerThanX(x)
}

// PendingFrom - List of tx(s) pending from address
//
// @note These are going to be same nonce tx(s), only one of them will
Expand Down
52 changes: 52 additions & 0 deletions app/data/queued.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,58 @@ func (q *QueuedPool) FresherThanX(x time.Duration) []*MemPoolTx {

}

// HigherThanX - Returns a list of queued txs which are paid with
// gas price >= `X`
func (q *QueuedPool) HigherThanX(x float64) []*MemPoolTx {
txs := q.DescListTxs()
if txs == nil {
return nil
}

txCount := uint64(len(txs))
result := make([]*MemPoolTx, 0, txCount)

for i := 0; i < len(txs); i++ {
// Stop ASAP, because iterating over
// descending sorted ( w.r.t. gas price )
// tx list
if !txs[i].HasGasPriceMoreThan(x) {
break
}

result = append(result, txs[i])
}

CleanSlice(txs)
return result
}

// LowerThanX - Returns a list of queued txs which are paid with
// gas price <= `X`
func (q *QueuedPool) LowerThanX(x float64) []*MemPoolTx {
txs := q.AscListTxs()
if txs == nil {
return nil
}

txCount := uint64(len(txs))
result := make([]*MemPoolTx, 0, txCount)

for i := 0; i < len(txs); i++ {
// Stop ASAP, because iterating over
// ascending sorted ( w.r.t. gas price )
// tx list
if !txs[i].HasGasPriceLessThan(x) {
break
}

result = append(result, txs[i])
}

CleanSlice(txs)
return result
}

// Add - Attempts to add new tx found in pending pool into
// harmony mempool, so that further manipulation can be performed on it
//
Expand Down
25 changes: 25 additions & 0 deletions app/data/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package data

import (
"context"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -186,6 +187,30 @@ func (m *MemPoolTx) IsUnstuck(ctx context.Context, rpc *rpc.Client) (bool, error

}

// HasGasPriceMoreThan - Returns true if gas price of this tx
// is more than or equals to `X`
func (m *MemPoolTx) HasGasPriceMoreThan(x float64) bool {
gp, err := BigHexToBigFloat(m.GasPrice)
if err != nil {
return false
}

given := big.NewFloat(x * 1_000_000_000)
return gp.Cmp(given) >= 0
}

// HasGasPriceLessThan - Returns true if gas price of this tx
// is less than or equals to `X`
func (m *MemPoolTx) HasGasPriceLessThan(x float64) bool {
gp, err := BigHexToBigFloat(m.GasPrice)
if err != nil {
return false
}

given := big.NewFloat(x * 1_000_000_000)
return gp.Cmp(given) <= 0
}

// ToMessagePack - Serialize to message pack encoded byte array format
func (m *MemPoolTx) ToMessagePack() ([]byte, error) {

Expand Down
Loading

0 comments on commit 4572d37

Please sign in to comment.