Added test for allEthHeaderCids query

This commit is contained in:
2022-06-03 15:05:46 +05:30
parent 80413211c2
commit 0c44882cb2
2 changed files with 205 additions and 0 deletions
+106
View File
@@ -36,6 +36,48 @@ type GetLogs struct {
Responses []LogResponse `json:"getLogs"`
}
type IPFSBlockResp struct {
Key string `json:"key"`
Data string `json:"data"`
}
type EthTransactionCidResp struct {
Cid string `json:"cid"`
TxHash string `json:"txHash"`
Index int32 `json:"index"`
Src string `json:"src"`
Dst string `json:"dst"`
BlockByMhKey IPFSBlockResp `json:"blockByMhKey"`
}
type EthTransactionCidsByHeaderIdResp struct {
Nodes []EthTransactionCidResp `json:"nodes"`
}
type EthHeaderCidResp 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 EthTransactionCidsByHeaderIdResp `json:"ethTransactionCidsByHeaderId"`
BlockByMhKey IPFSBlockResp `json:"blockByMhKey"`
}
type AllEthHeaderCidsResp struct {
Nodes []EthHeaderCidResp `json:"nodes"`
}
type AllEthHeaderCids struct {
Response AllEthHeaderCidsResp `json:"allEthHeaderCids"`
}
type Client struct {
client *gqlclient.Client
}
@@ -117,3 +159,67 @@ func (c *Client) GetStorageAt(ctx context.Context, hash common.Hash, address com
}
return &storageAt.Response, nil
}
func (c *Client) AllEthHeaderCids(ctx context.Context, condition EthHeaderCidCondition) (*AllEthHeaderCidsResp, error) {
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())
}
getLogsQuery := fmt.Sprintf(`
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)
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 allEthHeaderCids AllEthHeaderCids
err = json.Unmarshal(jsonStr, &allEthHeaderCids)
if err != nil {
return nil, err
}
return &allEthHeaderCids.Response, nil
}