github.com/gagliardetto/solana-go@v1.11.0/rpc/ws/types.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  // This file has been modified by github.com/gagliardetto
     3  //
     4  // Copyright 2020 dfuse Platform Inc.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //      http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package ws
    19  
    20  import (
    21  	stdjson "encoding/json"
    22  	"fmt"
    23  	"math/rand"
    24  	"net/http"
    25  	"time"
    26  )
    27  
    28  type request struct {
    29  	Version string      `json:"jsonrpc"`
    30  	Method  string      `json:"method"`
    31  	Params  interface{} `json:"params,omitempty"`
    32  	ID      uint64      `json:"id"`
    33  }
    34  
    35  func newRequest(params []interface{}, method string, configuration map[string]interface{}) *request {
    36  	if params != nil && configuration != nil {
    37  		params = append(params, configuration)
    38  	}
    39  	return &request{
    40  		Version: "2.0",
    41  		Method:  method,
    42  		Params:  params,
    43  		ID:      uint64(rand.Int63()),
    44  	}
    45  }
    46  
    47  func (c *request) encode() ([]byte, error) {
    48  	data, err := json.Marshal(c)
    49  	if err != nil {
    50  		return nil, fmt.Errorf("encode request: json marshall: %w", err)
    51  	}
    52  	return data, nil
    53  }
    54  
    55  type response struct {
    56  	Version string              `json:"jsonrpc"`
    57  	Params  *params             `json:"params"`
    58  	Error   *stdjson.RawMessage `json:"error"`
    59  }
    60  
    61  type params struct {
    62  	Result       *stdjson.RawMessage `json:"result"`
    63  	Subscription int                 `json:"subscription"`
    64  }
    65  
    66  type Options struct {
    67  	HttpHeader       http.Header
    68  	HandshakeTimeout time.Duration
    69  }
    70  
    71  var DefaultHandshakeTimeout = 45 * time.Second