From 40c856fc2a4c90ec5a318e404cab478e751eddcb Mon Sep 17 00:00:00 2001 From: Anjan Roy Date: Tue, 20 Jul 2021 21:57:40 +0530 Subject: [PATCH] added graphql field for querying gasprice as Float ( in Gwei ) --- app/data/common.go | 16 ++++++ app/data/tx.go | 2 + app/graph/generated/generated.go | 92 +++++++++++++++++++++++++++----- app/graph/model/models_gen.go | 29 +++++----- app/graph/schema.graphqls | 1 + go.sum | 9 ++++ 6 files changed, 121 insertions(+), 28 deletions(-) diff --git a/app/data/common.go b/app/data/common.go index 8647977..95f3b8d 100644 --- a/app/data/common.go +++ b/app/data/common.go @@ -159,3 +159,19 @@ func HumanReadableGasPrice(num *hexutil.Big) string { return fmt.Sprintf("%s Gwei", _res.String()) } + +// NumericGasPriceGwei - GasPrice in Gwei unit represented as +// floating point ( double precision ) +func NumericGasPriceGwei(num *hexutil.Big) float64 { + _num, err := BigHexToBigFloat(num) + if err != nil { + return 0.0 + } + + _den := big.NewFloat(1_000_000_000) + _res := big.NewFloat(0) + _res.Quo(_num, _den) + + gp, _ := _res.Float64() + return gp +} diff --git a/app/data/tx.go b/app/data/tx.go index f516c22..1e57584 100644 --- a/app/data/tx.go +++ b/app/data/tx.go @@ -304,8 +304,10 @@ func (m *MemPoolTx) ToGraphQL() *model.MemPoolTx { if m.GasPrice != nil { gqlTx.GasPrice = HumanReadableGasPrice(m.GasPrice) + gqlTx.GasPriceGwei = NumericGasPriceGwei(m.GasPrice) } else { gqlTx.GasPrice = "0" + gqlTx.GasPriceGwei = 0.0 } if m.Value != nil { diff --git a/app/graph/generated/generated.go b/app/graph/generated/generated.go index 61265aa..4a4f959 100644 --- a/app/graph/generated/generated.go +++ b/app/graph/generated/generated.go @@ -45,20 +45,21 @@ type DirectiveRoot struct { type ComplexityRoot struct { MemPoolTx struct { - From func(childComplexity int) int - Gas func(childComplexity int) int - GasPrice func(childComplexity int) int - Hash func(childComplexity int) int - Input func(childComplexity int) int - Nonce func(childComplexity int) int - PendingFor func(childComplexity int) int - Pool func(childComplexity int) int - QueuedFor func(childComplexity int) int - R func(childComplexity int) int - S func(childComplexity int) int - To func(childComplexity int) int - V func(childComplexity int) int - Value func(childComplexity int) int + From func(childComplexity int) int + Gas func(childComplexity int) int + GasPrice func(childComplexity int) int + GasPriceGwei func(childComplexity int) int + Hash func(childComplexity int) int + Input func(childComplexity int) int + Nonce func(childComplexity int) int + PendingFor func(childComplexity int) int + Pool func(childComplexity int) int + QueuedFor func(childComplexity int) int + R func(childComplexity int) int + S func(childComplexity int) int + To func(childComplexity int) int + V func(childComplexity int) int + Value func(childComplexity int) int } Query struct { @@ -181,6 +182,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.MemPoolTx.GasPrice(childComplexity), true + case "MemPoolTx.gasPriceGwei": + if e.complexity.MemPoolTx.GasPriceGwei == nil { + break + } + + return e.complexity.MemPoolTx.GasPriceGwei(childComplexity), true + case "MemPoolTx.hash": if e.complexity.MemPoolTx.Hash == nil { break @@ -726,6 +734,7 @@ var sources = []*ast.Source{ from: String! gas: String! gasPrice: String! + gasPriceGwei: Float! hash: String! input: String! nonce: String! @@ -1397,6 +1406,41 @@ func (ec *executionContext) _MemPoolTx_gasPrice(ctx context.Context, field graph return ec.marshalNString2string(ctx, field.Selections, res) } +func (ec *executionContext) _MemPoolTx_gasPriceGwei(ctx context.Context, field graphql.CollectedField, obj *model.MemPoolTx) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "MemPoolTx", + Field: field, + Args: nil, + IsMethod: false, + IsResolver: false, + } + + ctx = graphql.WithFieldContext(ctx, fc) + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.GasPriceGwei, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(float64) + fc.Result = res + return ec.marshalNFloat2float64(ctx, field.Selections, res) +} + func (ec *executionContext) _MemPoolTx_hash(ctx context.Context, field graphql.CollectedField, obj *model.MemPoolTx) (ret graphql.Marshaler) { defer func() { if r := recover(); r != nil { @@ -4657,6 +4701,11 @@ func (ec *executionContext) _MemPoolTx(ctx context.Context, sel ast.SelectionSet if out.Values[i] == graphql.Null { invalids++ } + case "gasPriceGwei": + out.Values[i] = ec._MemPoolTx_gasPriceGwei(ctx, field, obj) + if out.Values[i] == graphql.Null { + invalids++ + } case "hash": out.Values[i] = ec._MemPoolTx_hash(ctx, field, obj) if out.Values[i] == graphql.Null { @@ -5271,6 +5320,21 @@ func (ec *executionContext) marshalNBoolean2bool(ctx context.Context, sel ast.Se return res } +func (ec *executionContext) unmarshalNFloat2float64(ctx context.Context, v interface{}) (float64, error) { + res, err := graphql.UnmarshalFloat(v) + return res, graphql.ErrorOnPath(ctx, err) +} + +func (ec *executionContext) marshalNFloat2float64(ctx context.Context, sel ast.SelectionSet, v float64) graphql.Marshaler { + res := graphql.MarshalFloat(v) + if res == graphql.Null { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "must not be null") + } + } + return res +} + func (ec *executionContext) unmarshalNInt2int(ctx context.Context, v interface{}) (int, error) { res, err := graphql.UnmarshalInt(v) return res, graphql.ErrorOnPath(ctx, err) diff --git a/app/graph/model/models_gen.go b/app/graph/model/models_gen.go index cba0ee1..93c8276 100644 --- a/app/graph/model/models_gen.go +++ b/app/graph/model/models_gen.go @@ -3,18 +3,19 @@ package model type MemPoolTx struct { - From string `json:"from"` - Gas string `json:"gas"` - GasPrice string `json:"gasPrice"` - Hash string `json:"hash"` - Input string `json:"input"` - Nonce string `json:"nonce"` - To string `json:"to"` - Value string `json:"value"` - V string `json:"v"` - R string `json:"r"` - S string `json:"s"` - PendingFor string `json:"pendingFor"` - QueuedFor string `json:"queuedFor"` - Pool string `json:"pool"` + From string `json:"from"` + Gas string `json:"gas"` + GasPrice string `json:"gasPrice"` + GasPriceGwei float64 `json:"gasPriceGwei"` + Hash string `json:"hash"` + Input string `json:"input"` + Nonce string `json:"nonce"` + To string `json:"to"` + Value string `json:"value"` + V string `json:"v"` + R string `json:"r"` + S string `json:"s"` + PendingFor string `json:"pendingFor"` + QueuedFor string `json:"queuedFor"` + Pool string `json:"pool"` } diff --git a/app/graph/schema.graphqls b/app/graph/schema.graphqls index 3f4a9bd..6311ce4 100644 --- a/app/graph/schema.graphqls +++ b/app/graph/schema.graphqls @@ -2,6 +2,7 @@ type MemPoolTx { from: String! gas: String! gasPrice: String! + gasPriceGwei: Float! hash: String! input: String! nonce: String! diff --git a/go.sum b/go.sum index 5285bcf..77d770d 100644 --- a/go.sum +++ b/go.sum @@ -115,8 +115,10 @@ github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmf github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -630,6 +632,7 @@ github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP github.com/magiconair/properties v1.8.4 h1:8KGKTcQQGm0Kv7vEbKFErAoAOFyyacLStRtQSeYtvkY= github.com/magiconair/properties v1.8.4/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/matryer/moq v0.0.0-20200106131100-75d0ddfc0007 h1:reVOUXwnhsYv/8UqjvhrMOu5CNT9UapHFLbQ2JcXsmg= github.com/matryer/moq v0.0.0-20200106131100-75d0ddfc0007/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.0/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= @@ -816,7 +819,9 @@ github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521/go.mod h1:RvLn4FgxWubrpZHtQLnOf6EwhN2hEMusxZOhcW9H3UQ= +github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= @@ -829,6 +834,7 @@ github.com/shirou/gopsutil v2.20.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu github.com/shirou/gopsutil v3.21.2+incompatible h1:U+YvJfjCh6MslYlIAXvPtzhW3YZEtc9uncueUNpD/0A= github.com/shirou/gopsutil v3.21.2+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/shurcooL/vfsgen v0.0.0-20180121065927-ffb13db8def0/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= @@ -893,13 +899,16 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1 github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZW24CsNJDfeh9Ex6Pm0Rcpc7qrgKBiL44vF4= github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4= github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e h1:+w0Zm/9gaWpEAyDlU1eKOuk5twTjAjuevXqcJJw8hrg= github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e/go.mod h1:/HUdMve7rvxZma+2ZELQeNh88+003LL7Pf/CZ089j8U= github.com/vektah/gqlparser/v2 v2.1.0 h1:uiKJ+T5HMGGQM2kRKQ8Pxw8+Zq9qhhZhz/lieYvCMns= github.com/vektah/gqlparser/v2 v2.1.0/go.mod h1:SyUiHgLATUR8BiYURfTirrTcGpcE+4XkV2se04Px1Ms=