mirror of
https://github.com/status-im/eth-rpc-proxy.git
synced 2026-08-30 19:21:09 +00:00
75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"go-proxy-cache/internal/models"
|
|
)
|
|
|
|
// ParseJSONRPCRequest parses raw JSON-RPC request body
|
|
func ParseJSONRPCRequest(rawBody string) (*models.JSONRPCRequest, error) {
|
|
if rawBody == "" {
|
|
return nil, fmt.Errorf("empty request body")
|
|
}
|
|
|
|
var request models.JSONRPCRequest
|
|
if err := json.Unmarshal([]byte(rawBody), &request); err != nil {
|
|
return nil, fmt.Errorf("failed to parse JSON-RPC request: %w", err)
|
|
}
|
|
|
|
if request.Method == "" {
|
|
return nil, fmt.Errorf("missing method in JSON-RPC request")
|
|
}
|
|
|
|
return &request, nil
|
|
}
|
|
|
|
// FixResponseID fixes the ID in cached response to match the current request
|
|
func FixResponseID(cachedResponse string, requestID interface{}) string {
|
|
if cachedResponse == "" || requestID == nil {
|
|
return cachedResponse
|
|
}
|
|
|
|
var responseData map[string]interface{}
|
|
if err := json.Unmarshal([]byte(cachedResponse), &responseData); err != nil {
|
|
return cachedResponse
|
|
}
|
|
|
|
// If ID already matches, return original response
|
|
if responseData["id"] == requestID {
|
|
return cachedResponse
|
|
}
|
|
|
|
// Update the ID to match the current request
|
|
responseData["id"] = requestID
|
|
|
|
if fixedBytes, err := json.Marshal(responseData); err == nil {
|
|
return string(fixedBytes)
|
|
}
|
|
|
|
return cachedResponse
|
|
}
|
|
|
|
// IsNullResult checks if the JSON-RPC response has a null result
|
|
// This is used to skip caching null results for methods like eth_getTransactionReceipt
|
|
// where null indicates a pending/non-existent transaction that may appear later
|
|
func IsNullResult(responseData string) bool {
|
|
if responseData == "" {
|
|
return false
|
|
}
|
|
|
|
var response map[string]interface{}
|
|
if err := json.Unmarshal([]byte(responseData), &response); err != nil {
|
|
return false
|
|
}
|
|
|
|
// Check if result field exists and is null
|
|
result, exists := response["result"]
|
|
if !exists {
|
|
return false
|
|
}
|
|
|
|
return result == nil
|
|
}
|