solana_exporter/pkg/rpc/responses.go

110 lines
2.5 KiB
Go
Raw Normal View History

2024-06-14 02:09:39 -07:00
package rpc
2024-10-02 05:47:04 -07:00
import (
"encoding/json"
"fmt"
)
2024-06-14 02:09:39 -07:00
type (
RPCError struct {
2024-10-14 08:20:34 -07:00
Message string `json:"message"`
Code int64 `json:"code"`
Data map[string]any `json:"data"`
2024-10-18 07:57:44 -07:00
// Method is not returned by the RPC, rather added by the client for visibility purposes
Method string
}
2024-10-27 09:14:08 -07:00
Response[T any] struct {
Jsonrpc string `json:"jsonrpc"`
Result T `json:"result,omitempty"`
Error RPCError `json:"error,omitempty"`
2024-10-06 07:52:18 -07:00
Id int `json:"id"`
2024-06-14 02:09:39 -07:00
}
2024-10-02 05:47:04 -07:00
contextualResult[T any] struct {
Value T `json:"value"`
Context struct {
Slot int64 `json:"slot"`
2024-10-03 21:44:35 -07:00
} `json:"context"`
2024-10-02 05:47:04 -07:00
}
2024-06-14 02:09:39 -07:00
EpochInfo struct {
2024-10-28 09:19:07 -07:00
AbsoluteSlot int64 `json:"absoluteSlot"`
BlockHeight int64 `json:"blockHeight"`
Epoch int64 `json:"epoch"`
SlotIndex int64 `json:"slotIndex"`
SlotsInEpoch int64 `json:"slotsInEpoch"`
2024-06-14 02:09:39 -07:00
TransactionCount int64 `json:"transactionCount"`
}
VoteAccount struct {
2024-10-28 09:19:07 -07:00
ActivatedStake int64 `json:"activatedStake"`
LastVote int `json:"lastVote"`
NodePubkey string `json:"nodePubkey"`
RootSlot int `json:"rootSlot"`
VotePubkey string `json:"votePubkey"`
2024-06-14 02:09:39 -07:00
}
VoteAccounts struct {
Current []VoteAccount `json:"current"`
Delinquent []VoteAccount `json:"delinquent"`
}
2024-10-02 05:47:04 -07:00
HostProduction struct {
LeaderSlots int64
BlocksProduced int64
2024-06-14 03:58:32 -07:00
}
2024-10-02 05:47:04 -07:00
BlockProductionRange struct {
FirstSlot int64 `json:"firstSlot"`
LastSlot int64 `json:"lastSlot"`
2024-06-14 03:58:32 -07:00
}
2024-10-02 05:47:04 -07:00
BlockProduction struct {
ByIdentity map[string]HostProduction `json:"byIdentity"`
Range BlockProductionRange `json:"range"`
2024-06-14 03:58:32 -07:00
}
2024-10-02 08:08:46 -07:00
InflationReward struct {
2024-10-28 09:19:07 -07:00
Amount int64 `json:"amount"`
Epoch int64 `json:"epoch"`
2024-10-02 08:08:46 -07:00
}
2024-10-07 05:49:37 -07:00
Block struct {
2024-10-28 09:19:07 -07:00
Rewards []BlockReward `json:"rewards"`
Transactions []map[string]any `json:"transactions"`
2024-10-07 05:49:37 -07:00
}
BlockReward struct {
2024-10-28 09:19:07 -07:00
Pubkey string `json:"pubkey"`
Lamports int64 `json:"lamports"`
RewardType string `json:"rewardType"`
2024-10-07 05:49:37 -07:00
}
FullTransaction struct {
Transaction struct {
Message struct {
AccountKeys []string `json:"accountKeys"`
} `json:"message"`
} `json:"transaction"`
}
2024-10-02 05:47:04 -07:00
)
2024-06-14 03:58:32 -07:00
func (e *RPCError) Error() string {
2024-10-18 07:57:44 -07:00
return fmt.Sprintf("%s rpc error (code: %d): %s (data: %v)", e.Method, e.Code, e.Message, e.Data)
}
2024-10-02 05:47:04 -07:00
func (hp *HostProduction) UnmarshalJSON(data []byte) error {
var arr []int64
if err := json.Unmarshal(data, &arr); err != nil {
return err
2024-06-14 02:09:39 -07:00
}
2024-10-01 02:52:02 -07:00
2024-10-02 05:47:04 -07:00
if len(arr) != 2 {
return fmt.Errorf("expected array of 2 integers, got %d", len(arr))
2024-10-01 02:52:02 -07:00
}
2024-10-02 05:47:04 -07:00
hp.LeaderSlots = arr[0]
hp.BlocksProduced = arr[1]
return nil
}