Skip to content

Commit

Permalink
graphql: return decimal for estimateGas and cumulativeGas queries (
Browse files Browse the repository at this point in the history
…#22126)

* estimateGas, cumulativeGas
* linted
* add test for estimateGas
  • Loading branch information
renaynay committed Jan 6, 2021
1 parent d667ee2 commit 072fd96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
27 changes: 14 additions & 13 deletions graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -811,15 +811,15 @@ 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.
}

func (c *CallResult) Data() hexutil.Bytes {
return c.data
}

func (c *CallResult) GasUsed() hexutil.Uint64 {
func (c *CallResult) GasUsed() Long {
return c.gasUsed
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 072fd96

Please sign in to comment.