Skip to content

Commit 072fd96

Browse files
authored
graphql: return decimal for estimateGas and cumulativeGas queries (#22126)
* estimateGas, cumulativeGas * linted * add test for estimateGas
1 parent d667ee2 commit 072fd96

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

‎graphql/graphql.go‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,21 +301,21 @@ func (t *Transaction) Status(ctx context.Context) (*hexutil.Uint64, error) {
301301
return &ret, nil
302302
}
303303

304-
func (t *Transaction) GasUsed(ctx context.Context) (*hexutil.Uint64, error) {
304+
func (t *Transaction) GasUsed(ctx context.Context) (*Long, error) {
305305
receipt, err := t.getReceipt(ctx)
306306
if err != nil || receipt == nil {
307307
return nil, err
308308
}
309-
ret := hexutil.Uint64(receipt.GasUsed)
309+
ret := Long(receipt.GasUsed)
310310
return &ret, nil
311311
}
312312

313-
func (t *Transaction) CumulativeGasUsed(ctx context.Context) (*hexutil.Uint64, error) {
313+
func (t *Transaction) CumulativeGasUsed(ctx context.Context) (*Long, error) {
314314
receipt, err := t.getReceipt(ctx)
315315
if err != nil || receipt == nil {
316316
return nil, err
317317
}
318-
ret := hexutil.Uint64(receipt.CumulativeGasUsed)
318+
ret := Long(receipt.CumulativeGasUsed)
319319
return &ret, nil
320320
}
321321

@@ -811,15 +811,15 @@ type CallData struct {
811811
// CallResult encapsulates the result of an invocation of the `call` accessor.
812812
type CallResult struct {
813813
data hexutil.Bytes // The return data from the call
814-
gasUsed hexutil.Uint64 // The amount of gas used
814+
gasUsed Long // The amount of gas used
815815
status hexutil.Uint64 // The return status of the call - 0 for failure or 1 for success.
816816
}
817817

818818
func (c *CallResult) Data() hexutil.Bytes {
819819
return c.data
820820
}
821821

822-
func (c *CallResult) GasUsed() hexutil.Uint64 {
822+
func (c *CallResult) GasUsed() Long {
823823
return c.gasUsed
824824
}
825825

@@ -847,22 +847,22 @@ func (b *Block) Call(ctx context.Context, args struct {
847847

848848
return &CallResult{
849849
data: result.ReturnData,
850-
gasUsed: hexutil.Uint64(result.UsedGas),
850+
gasUsed: Long(result.UsedGas),
851851
status: status,
852852
}, nil
853853
}
854854

855855
func (b *Block) EstimateGas(ctx context.Context, args struct {
856856
Data ethapi.CallArgs
857-
}) (hexutil.Uint64, error) {
857+
}) (Long, error) {
858858
if b.numberOrHash == nil {
859859
_, err := b.resolveHeader(ctx)
860860
if err != nil {
861-
return hexutil.Uint64(0), err
861+
return 0, err
862862
}
863863
}
864864
gas, err := ethapi.DoEstimateGas(ctx, b.backend, args.Data, *b.numberOrHash, b.backend.RPCGasCap())
865-
return gas, err
865+
return Long(gas), err
866866
}
867867

868868
type Pending struct {
@@ -917,16 +917,17 @@ func (p *Pending) Call(ctx context.Context, args struct {
917917

918918
return &CallResult{
919919
data: result.ReturnData,
920-
gasUsed: hexutil.Uint64(result.UsedGas),
920+
gasUsed: Long(result.UsedGas),
921921
status: status,
922922
}, nil
923923
}
924924

925925
func (p *Pending) EstimateGas(ctx context.Context, args struct {
926926
Data ethapi.CallArgs
927-
}) (hexutil.Uint64, error) {
927+
}) (Long, error) {
928928
pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
929-
return ethapi.DoEstimateGas(ctx, p.backend, args.Data, pendingBlockNr, p.backend.RPCGasCap())
929+
gas, err := ethapi.DoEstimateGas(ctx, p.backend, args.Data, pendingBlockNr, p.backend.RPCGasCap())
930+
return Long(gas), err
930931
}
931932

932933
// Resolver is the top-level object in the GraphQL hierarchy.

‎graphql/graphql_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ func TestGraphQLBlockSerialization(t *testing.T) {
117117
want: `{"errors":[{"message":"Cannot query field \"bleh\" on type \"Query\".","locations":[{"line":1,"column":2}]}]}`,
118118
code: 400,
119119
},
120+
// should return `estimateGas` as decimal
121+
{
122+
body: `{"query": "{block{ estimateGas(data:{}) }}"}`,
123+
want: `{"data":{"block":{"estimateGas":53000}}}`,
124+
code: 200,
125+
},
120126
} {
121127
resp, err := http.Post(fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), "application/json", strings.NewReader(tt.body))
122128
if err != nil {

0 commit comments

Comments
 (0)