solana_exporter/pkg/rpc/responses.go

128 lines
3.3 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-06-14 02:09:39 -07:00
response[T any] struct {
2024-10-06 07:52:18 -07:00
jsonrpc string
Result T `json:"result"`
Error RPCError `json:"error"`
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 {
// Current absolute slot in epoch
AbsoluteSlot int64 `json:"absoluteSlot"`
// Current block height
BlockHeight int64 `json:"blockHeight"`
// Current epoch number
Epoch int64 `json:"epoch"`
// Current slot relative to the start of the current epoch
SlotIndex int64 `json:"slotIndex"`
// Number of slots in this epoch
SlotsInEpoch int64 `json:"slotsInEpoch"`
2024-06-14 03:58:32 -07:00
// Total number of transactions
2024-06-14 02:09:39 -07:00
TransactionCount int64 `json:"transactionCount"`
}
VoteAccount struct {
ActivatedStake int64 `json:"activatedStake"`
Commission int `json:"commission"`
EpochCredits [][]int `json:"epochCredits"`
EpochVoteAccount bool `json:"epochVoteAccount"`
LastVote int `json:"lastVote"`
NodePubkey string `json:"nodePubkey"`
RootSlot int `json:"rootSlot"`
VotePubkey string `json:"votePubkey"`
}
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 {
Amount int64 `json:"amount"`
EffectiveSlot int64 `json:"effectiveSlot"`
Epoch int64 `json:"epoch"`
PostBalance int64 `json:"postBalance"`
}
2024-10-07 05:49:37 -07:00
Block struct {
2024-10-15 08:50:04 -07:00
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime,omitempty"`
Blockhash string `json:"blockhash"`
ParentSlot int64 `json:"parentSlot"`
PreviousBlockhash string `json:"previousBlockhash"`
Rewards []BlockReward `json:"rewards"`
Transactions []map[string]any `json:"transactions"`
2024-10-07 05:49:37 -07:00
}
BlockReward struct {
Pubkey string `json:"pubkey"`
Lamports int64 `json:"lamports"`
PostBalance int64 `json:"postBalance"`
RewardType string `json:"rewardType"`
Commission uint8 `json:"commission"`
}
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
}