Skip to content

Commit

Permalink
Add block withdrawals in graphQL response (#10463)
Browse files Browse the repository at this point in the history
In the context of data collection and indexing, I need to make one
graphQL request per block to retrieve several informations. Currently
withdrawals are not implemented in graphQL. This PR adds it.

**Request**

```graphQL
{
  block(number: 19938542) {
    number
    withdrawals {
      index
      validator
      address
      amount
    }
  }
}
```

**Response**

```json
{
  "data": {
    "block": {
      "number": 19938542,
      "withdrawals": [
        {
          "index": 46457291,
          "validator": 425887,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11db8a5"
        },
        {
          "index": 46457292,
          "validator": 425888,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11e2885"
        },
        {
          "index": 46457293,
          "validator": 425889,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11e9866"
        },
        {
          "index": 46457294,
          "validator": 425890,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11e5524"
        },
        {
          "index": 46457295,
          "validator": 425891,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11e00fc"
        },
        {
          "index": 46457296,
          "validator": 425892,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11ec53c"
        },
        {
          "index": 46457297,
          "validator": 425893,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11e2f24"
        },
        {
          "index": 46457298,
          "validator": 425894,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11d866b"
        },
        {
          "index": 46457299,
          "validator": 425895,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11e4bb3"
        },
        {
          "index": 46457300,
          "validator": 425896,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11deeb5"
        },
        {
          "index": 46457301,
          "validator": 425897,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11e0e99"
        },
        {
          "index": 46457302,
          "validator": 425898,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11ee082"
        },
        {
          "index": 46457303,
          "validator": 425899,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11e4283"
        },
        {
          "index": 46457304,
          "validator": 425900,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11ed7b6"
        },
        {
          "index": 46457305,
          "validator": 425901,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x11d41ef"
        },
        {
          "index": 46457306,
          "validator": 425902,
          "address": "0x4B5E9A6Ab1B87D4Df850178AD2807c1E3E3BA33E",
          "amount": "0x3c88641"
        }
      ]
    }
  }
}
```
  • Loading branch information
scorring authored May 24, 2024
1 parent 43118df commit 28aa3ef
Show file tree
Hide file tree
Showing 7 changed files with 899 additions and 466 deletions.
637 changes: 502 additions & 135 deletions cmd/rpcdaemon/graphql/graph/generated.go

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions cmd/rpcdaemon/graphql/graph/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

671 changes: 342 additions & 329 deletions cmd/rpcdaemon/graphql/graph/schema.graphqls

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions cmd/rpcdaemon/graphql/graph/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions cmd/rpcdaemon/graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func TestGraphQLQueryBlock(t *testing.T) {
want: `{"data":{"block":{"ommerHash":"0x22f29046fa689683c504ad6fd9a7a9d5803f8e6bb66de435438b563f586651fe","ommerCount":1,"ommers":[{"hash":"0xf4af15465ca81e65866c6e64cbc446b735a06fb2118dda69a7c21d4ab0b1e217"}]}}}`,
code: 200,
},
{ // Should return withdrawals
body: `{"query": "{block{withdrawals{index,validator,address,amount}}}","variables": null}`,
want: `{"data":{"block":{"withdrawals":\[({"index":\d+,"validator":\d+,"address":"0x[0-9a-fA-F]+","amount":"0x[0-9a-fA-F]+"},?)*\]}}}`,
code: 200,
comp: "regexp",
},
// should return `estimateGas` as decimal
/*
{
Expand Down
6 changes: 6 additions & 0 deletions cmd/rpcdaemon/graphql/query_block.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@
}
status
}
withdrawals {
index
validator
address
amount
}
}
}
14 changes: 14 additions & 0 deletions turbo/jsonrpc/graphql_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ func (api *GraphQLAPIImpl) GetBlockDetails(ctx context.Context, blockNumber rpc.
response["block"] = getBlockRes
response["receipts"] = result

// Withdrawals
wresult := make([]map[string]interface{}, 0, len(block.Withdrawals()))
for _, withdrawal := range block.Withdrawals() {
wmap := make(map[string]interface{})
wmap["index"] = hexutil.Uint64(withdrawal.Index)
wmap["validator"] = hexutil.Uint64(withdrawal.Validator)
wmap["address"] = withdrawal.Address
wmap["amount"] = withdrawal.Amount

wresult = append(wresult, wmap)
}

response["withdrawals"] = wresult

return response, nil
}

Expand Down

0 comments on commit 28aa3ef

Please sign in to comment.