-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GQL API for getStorageAt and getLogs #77
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
18266c4
Get receipt CID and block data for logs.
ashwinphatak b90fcb5
Get logs API, with receipt CID and raw IPLD block.
ashwinphatak a284a56
Get storage API, with storage leaf CID and raw IPLD block.
ashwinphatak b2828a8
Compute leaf key from slot.
ashwinphatak a3ca08b
Return empty result instead of error if slot not found in storage table.
ashwinphatak 0a14bd9
Update method used to get storage slot.
ashwinphatak 1d18d1f
Update gopsutil to fix build on mac.
arijitAD 9a5581b
Fix failing unit tests.
arijitAD f1a61d0
Add graphql tests.
arijitAD File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package graphql | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
gqlclient "github.com/machinebox/graphql" | ||
) | ||
|
||
type StorageResponse struct { | ||
Cid string `json:"cid"` | ||
Value common.Hash `json:"value"` | ||
IpldBlock hexutil.Bytes `json:"ipldBlock"` | ||
} | ||
|
||
type GetStorageAt struct { | ||
Response StorageResponse `json:"getStorageAt"` | ||
} | ||
|
||
type LogResponse struct { | ||
Topics []common.Hash `json:"topics"` | ||
Data hexutil.Bytes `json:"data"` | ||
} | ||
|
||
type GetLogs struct { | ||
Responses []LogResponse `json:"getLogs"` | ||
} | ||
|
||
type Client struct { | ||
client *gqlclient.Client | ||
} | ||
|
||
func NewClient(endpoint string) *Client { | ||
client := gqlclient.NewClient(endpoint) | ||
return &Client{client: client} | ||
} | ||
|
||
func (c *Client) GetLogs(ctx context.Context, hash common.Hash, address common.Address) ([]LogResponse, error) { | ||
getLogsQuery := fmt.Sprintf(` | ||
query{ | ||
getLogs(blockHash: "%s", contract: "%s") { | ||
data | ||
topics | ||
} | ||
} | ||
`, hash.String(), address.String()) | ||
|
||
req := gqlclient.NewRequest(getLogsQuery) | ||
req.Header.Set("Cache-Control", "no-cache") | ||
|
||
var respData map[string]interface{} | ||
err := c.client.Run(ctx, req, &respData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
jsonStr, err := json.Marshal(respData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var logs GetLogs | ||
err = json.Unmarshal(jsonStr, &logs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return logs.Responses, nil | ||
} | ||
|
||
func (c *Client) GetStorageAt(ctx context.Context, hash common.Hash, address common.Address, slot string) (*StorageResponse, error) { | ||
getLogsQuery := fmt.Sprintf(` | ||
query{ | ||
getStorageAt(blockHash: "%s", contract: "%s",slot: "%s") { | ||
cid | ||
value | ||
ipldBlock | ||
} | ||
} | ||
`, hash.String(), address.String(), common.HexToHash(slot)) | ||
|
||
req := gqlclient.NewRequest(getLogsQuery) | ||
req.Header.Set("Cache-Control", "no-cache") | ||
|
||
var respData map[string]interface{} | ||
err := c.client.Run(ctx, req, &respData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
jsonStr, err := json.Marshal(respData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var storageAt GetStorageAt | ||
err = json.Unmarshal(jsonStr, &storageAt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &storageAt.Response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For compatibility with EIP-2718 enveloped transactions and receipts (EIP-2930 access list and EIP-1559 dynamic fee) we can no longer use RLP encoding/decoding, we instead need to use the
MarshalBinary
andUnmarshalBinary
methods for receipts and transactions.Related issue and more info here: #78 (comment).