2020-11-09 13:14:52 -08:00
|
|
|
package ws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
|
2020-11-24 05:43:37 -08:00
|
|
|
"github.com/dfuse-io/solana-go"
|
|
|
|
|
2020-11-09 13:14:52 -08:00
|
|
|
"github.com/dfuse-io/solana-go/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type request struct {
|
|
|
|
Version string `json:"jsonrpc"`
|
|
|
|
Method string `json:"method"`
|
2020-11-11 07:24:58 -08:00
|
|
|
Params interface{} `json:"params,omitempty"`
|
2020-11-09 13:14:52 -08:00
|
|
|
ID uint64 `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRequest(params []interface{}, method string, configuration map[string]interface{}) *request {
|
2020-11-11 07:24:58 -08:00
|
|
|
if params != nil && configuration != nil {
|
|
|
|
params = append(params, configuration)
|
|
|
|
}
|
2020-11-09 13:14:52 -08:00
|
|
|
return &request{
|
|
|
|
Version: "2.0",
|
|
|
|
Method: method,
|
|
|
|
Params: params,
|
|
|
|
ID: uint64(rand.Int63()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *request) encode() ([]byte, error) {
|
|
|
|
data, err := json.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("encode request: json marshall: %w", err)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Version string `json:"jsonrpc"`
|
|
|
|
Params *params `json:"params"`
|
|
|
|
Error *json.RawMessage `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type params struct {
|
|
|
|
Result *json.RawMessage `json:"result"`
|
|
|
|
Subscription int `json:"subscription"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProgramResult struct {
|
|
|
|
Context struct {
|
|
|
|
Slot uint64
|
|
|
|
} `json:"context"`
|
|
|
|
Value struct {
|
2020-12-08 09:28:42 -08:00
|
|
|
PubKey solana.PublicKey `json:"pubkey"`
|
2020-11-24 05:43:37 -08:00
|
|
|
Account rpc.Account `json:"account"`
|
2020-11-09 13:14:52 -08:00
|
|
|
} `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AccountResult struct {
|
|
|
|
Context struct {
|
|
|
|
Slot uint64
|
|
|
|
} `json:"context"`
|
|
|
|
Value struct {
|
2020-12-07 10:36:27 -08:00
|
|
|
rpc.Account
|
2020-11-09 13:14:52 -08:00
|
|
|
} `json:"value"`
|
|
|
|
}
|
2020-11-11 07:24:58 -08:00
|
|
|
|
|
|
|
type SlotResult struct {
|
|
|
|
Parent uint64
|
|
|
|
Root uint64
|
|
|
|
Slot uint64
|
|
|
|
}
|