graphql: return decimal for estimateGas and cumulativeGas queries (#22126)

* estimateGas, cumulativeGas
* linted
* add test for estimateGas
This commit is contained in:
rene 2021-01-06 17:19:16 +01:00 committed by GitHub
parent d667ee2d10
commit 072fd96254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 13 deletions

@ -301,21 +301,21 @@ func (t *Transaction) Status(ctx context.Context) (*hexutil.Uint64, error) {
return &ret, nil
}
func (t *Transaction) GasUsed(ctx context.Context) (*hexutil.Uint64, error) {
func (t *Transaction) GasUsed(ctx context.Context) (*Long, error) {
receipt, err := t.getReceipt(ctx)
if err != nil || receipt == nil {
return nil, err
}
ret := hexutil.Uint64(receipt.GasUsed)
ret := Long(receipt.GasUsed)
return &ret, nil
}
func (t *Transaction) CumulativeGasUsed(ctx context.Context) (*hexutil.Uint64, error) {
func (t *Transaction) CumulativeGasUsed(ctx context.Context) (*Long, error) {
receipt, err := t.getReceipt(ctx)
if err != nil || receipt == nil {
return nil, err
}
ret := hexutil.Uint64(receipt.CumulativeGasUsed)
ret := Long(receipt.CumulativeGasUsed)
return &ret, nil
}
@ -811,7 +811,7 @@ type CallData struct {
// CallResult encapsulates the result of an invocation of the `call` accessor.
type CallResult struct {
data hexutil.Bytes // The return data from the call
gasUsed hexutil.Uint64 // The amount of gas used
gasUsed Long // The amount of gas used
status hexutil.Uint64 // The return status of the call - 0 for failure or 1 for success.
}
@ -819,7 +819,7 @@ func (c *CallResult) Data() hexutil.Bytes {
return c.data
}
func (c *CallResult) GasUsed() hexutil.Uint64 {
func (c *CallResult) GasUsed() Long {
return c.gasUsed
}
@ -847,22 +847,22 @@ func (b *Block) Call(ctx context.Context, args struct {
return &CallResult{
data: result.ReturnData,
gasUsed: hexutil.Uint64(result.UsedGas),
gasUsed: Long(result.UsedGas),
status: status,
}, nil
}
func (b *Block) EstimateGas(ctx context.Context, args struct {
Data ethapi.CallArgs
}) (hexutil.Uint64, error) {
}) (Long, error) {
if b.numberOrHash == nil {
_, err := b.resolveHeader(ctx)
if err != nil {
return hexutil.Uint64(0), err
return 0, err
}
}
gas, err := ethapi.DoEstimateGas(ctx, b.backend, args.Data, *b.numberOrHash, b.backend.RPCGasCap())
return gas, err
return Long(gas), err
}
type Pending struct {
@ -917,16 +917,17 @@ func (p *Pending) Call(ctx context.Context, args struct {
return &CallResult{
data: result.ReturnData,
gasUsed: hexutil.Uint64(result.UsedGas),
gasUsed: Long(result.UsedGas),
status: status,
}, nil
}
func (p *Pending) EstimateGas(ctx context.Context, args struct {
Data ethapi.CallArgs
}) (hexutil.Uint64, error) {
}) (Long, error) {
pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
return ethapi.DoEstimateGas(ctx, p.backend, args.Data, pendingBlockNr, p.backend.RPCGasCap())
gas, err := ethapi.DoEstimateGas(ctx, p.backend, args.Data, pendingBlockNr, p.backend.RPCGasCap())
return Long(gas), err
}
// Resolver is the top-level object in the GraphQL hierarchy.

@ -117,6 +117,12 @@ func TestGraphQLBlockSerialization(t *testing.T) {
want: `{"errors":[{"message":"Cannot query field \"bleh\" on type \"Query\".","locations":[{"line":1,"column":2}]}]}`,
code: 400,
},
// should return `estimateGas` as decimal
{
body: `{"query": "{block{ estimateGas(data:{}) }}"}`,
want: `{"data":{"block":{"estimateGas":53000}}}`,
code: 200,
},
} {
resp, err := http.Post(fmt.Sprintf("http://%s/graphql", "127.0.0.1:9393"), "application/json", strings.NewReader(tt.body))
if err != nil {