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 Igor Mandrigin
parent 9f29fe3ea3
commit 49f677bc1d
2 changed files with 20 additions and 13 deletions

View File

@ -305,21 +305,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
}
@ -815,7 +815,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.
}
@ -823,7 +823,7 @@ func (c *CallResult) Data() hexutil.Bytes {
return c.data
}
func (c *CallResult) GasUsed() hexutil.Uint64 {
func (c *CallResult) GasUsed() Long {
return c.gasUsed
}
@ -851,22 +851,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 {
@ -921,16 +921,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.

View File

@ -118,6 +118,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 {