2021-07-02 09:00:48 +00:00
|
|
|
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 {
|
2022-06-06 10:06:29 +00:00
|
|
|
Topics []common.Hash `json:"topics"`
|
|
|
|
Data hexutil.Bytes `json:"data"`
|
|
|
|
Transaction TransactionResponse `json:"transaction"`
|
|
|
|
ReceiptCID string `json:"receiptCID"`
|
|
|
|
Status int32 `json:"status"`
|
2021-07-26 10:13:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 10:06:29 +00:00
|
|
|
type TransactionResponse struct {
|
2021-07-26 10:13:38 +00:00
|
|
|
Hash common.Hash `json:"hash"`
|
2021-07-02 09:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GetLogs struct {
|
|
|
|
Responses []LogResponse `json:"getLogs"`
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:06:29 +00:00
|
|
|
type IPFSBlockResponse struct {
|
2022-06-01 09:15:48 +00:00
|
|
|
Key string `json:"key"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:06:29 +00:00
|
|
|
type EthTransactionCidResponse struct {
|
|
|
|
Cid string `json:"cid"`
|
|
|
|
TxHash string `json:"txHash"`
|
|
|
|
Index int32 `json:"index"`
|
|
|
|
Src string `json:"src"`
|
|
|
|
Dst string `json:"dst"`
|
|
|
|
BlockByMhKey IPFSBlockResponse `json:"blockByMhKey"`
|
2022-06-01 09:15:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 11:15:02 +00:00
|
|
|
type EthTransactionCidByTxHash struct {
|
2022-06-06 10:06:29 +00:00
|
|
|
Response EthTransactionCidResponse `json:"ethTransactionCidByTxHash"`
|
2022-06-01 11:15:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 10:06:29 +00:00
|
|
|
type EthTransactionCidsByHeaderIdResponse struct {
|
|
|
|
Nodes []EthTransactionCidResponse `json:"nodes"`
|
2022-06-01 09:15:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 10:06:29 +00:00
|
|
|
type EthHeaderCidResponse struct {
|
|
|
|
Cid string `json:"cid"`
|
|
|
|
BlockNumber BigInt `json:"blockNumber"`
|
|
|
|
BlockHash string `json:"blockHash"`
|
|
|
|
ParentHash string `json:"parentHash"`
|
|
|
|
Timestamp BigInt `json:"timestamp"`
|
|
|
|
StateRoot string `json:"stateRoot"`
|
|
|
|
Td BigInt `json:"td"`
|
|
|
|
TxRoot string `json:"txRoot"`
|
|
|
|
ReceiptRoot string `json:"receiptRoot"`
|
|
|
|
UncleRoot string `json:"uncleRoot"`
|
|
|
|
Bloom string `json:"bloom"`
|
|
|
|
EthTransactionCidsByHeaderId EthTransactionCidsByHeaderIdResponse `json:"ethTransactionCidsByHeaderId"`
|
|
|
|
BlockByMhKey IPFSBlockResponse `json:"blockByMhKey"`
|
2022-06-01 09:15:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 10:06:29 +00:00
|
|
|
type AllEthHeaderCidsResponse struct {
|
|
|
|
Nodes []EthHeaderCidResponse `json:"nodes"`
|
2022-06-01 09:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AllEthHeaderCids struct {
|
2022-06-06 10:06:29 +00:00
|
|
|
Response AllEthHeaderCidsResponse `json:"allEthHeaderCids"`
|
2022-06-01 09:15:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:00:48 +00:00
|
|
|
type Client struct {
|
|
|
|
client *gqlclient.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(endpoint string) *Client {
|
|
|
|
client := gqlclient.NewClient(endpoint)
|
|
|
|
return &Client{client: client}
|
|
|
|
}
|
|
|
|
|
2021-09-02 13:42:33 +00:00
|
|
|
func (c *Client) GetLogs(ctx context.Context, hash common.Hash, address *common.Address) ([]LogResponse, error) {
|
|
|
|
params := fmt.Sprintf(`blockHash: "%s"`, hash.String())
|
|
|
|
if address != nil {
|
|
|
|
params += fmt.Sprintf(`, contract: "%s"`, address.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
getLogsQuery := fmt.Sprintf(`query{
|
|
|
|
getLogs(%s) {
|
2021-07-02 09:00:48 +00:00
|
|
|
data
|
|
|
|
topics
|
2021-07-26 10:13:38 +00:00
|
|
|
transaction {
|
|
|
|
hash
|
|
|
|
}
|
2021-08-30 15:29:54 +00:00
|
|
|
status
|
2021-09-01 07:02:28 +00:00
|
|
|
receiptCID
|
2021-07-02 09:00:48 +00:00
|
|
|
}
|
2021-09-02 13:42:33 +00:00
|
|
|
}`, params)
|
2021-07-02 09:00:48 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-06-01 09:15:48 +00:00
|
|
|
|
2022-06-06 10:06:29 +00:00
|
|
|
func (c *Client) AllEthHeaderCids(ctx context.Context, condition EthHeaderCidCondition) (*AllEthHeaderCidsResponse, error) {
|
2022-06-01 09:15:48 +00:00
|
|
|
var params string
|
|
|
|
if condition.BlockHash != nil {
|
|
|
|
params = fmt.Sprintf(`blockHash: "%s"`, *condition.BlockHash)
|
|
|
|
}
|
|
|
|
if condition.BlockNumber != nil {
|
|
|
|
params += fmt.Sprintf(`blockNumber: "%s"`, condition.BlockNumber.String())
|
|
|
|
}
|
|
|
|
|
2022-06-01 11:15:02 +00:00
|
|
|
getHeadersQuery := fmt.Sprintf(`
|
2022-06-01 09:15:48 +00:00
|
|
|
query{
|
|
|
|
allEthHeaderCids(condition: { %s }) {
|
|
|
|
nodes {
|
|
|
|
cid
|
|
|
|
blockNumber
|
|
|
|
blockHash
|
|
|
|
parentHash
|
|
|
|
timestamp
|
|
|
|
stateRoot
|
|
|
|
td
|
|
|
|
txRoot
|
|
|
|
receiptRoot
|
|
|
|
uncleRoot
|
|
|
|
bloom
|
|
|
|
blockByMhKey {
|
|
|
|
key
|
|
|
|
data
|
|
|
|
}
|
|
|
|
ethTransactionCidsByHeaderId {
|
|
|
|
nodes {
|
|
|
|
cid
|
|
|
|
txHash
|
|
|
|
index
|
|
|
|
src
|
|
|
|
dst
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, params)
|
|
|
|
|
2022-06-01 11:15:02 +00:00
|
|
|
req := gqlclient.NewRequest(getHeadersQuery)
|
2022-06-01 09:15:48 +00:00
|
|
|
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 allEthHeaderCids AllEthHeaderCids
|
|
|
|
err = json.Unmarshal(jsonStr, &allEthHeaderCids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &allEthHeaderCids.Response, nil
|
|
|
|
}
|
2022-06-01 11:15:02 +00:00
|
|
|
|
2022-06-06 10:06:29 +00:00
|
|
|
func (c *Client) EthTransactionCidByTxHash(ctx context.Context, txHash string) (*EthTransactionCidResponse, error) {
|
2022-06-01 11:15:02 +00:00
|
|
|
getTxQuery := fmt.Sprintf(`
|
|
|
|
query{
|
|
|
|
ethTransactionCidByTxHash(txHash: "%s") {
|
|
|
|
cid
|
|
|
|
txHash
|
|
|
|
index
|
|
|
|
src
|
|
|
|
dst
|
|
|
|
blockByMhKey {
|
|
|
|
data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, txHash)
|
|
|
|
|
|
|
|
req := gqlclient.NewRequest(getTxQuery)
|
|
|
|
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 ethTxCid EthTransactionCidByTxHash
|
|
|
|
err = json.Unmarshal(jsonStr, ðTxCid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ðTxCid.Response, nil
|
|
|
|
}
|