github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/pkg/client/client.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package client
    18  
    19  import (
    20  	"context"
    21  	"log"
    22  
    23  	"github.com/stafiprotocol/go-substrate-rpc-client/config"
    24  	gethrpc "github.com/stafiprotocol/go-substrate-rpc-client/pkg/gethrpc"
    25  	"github.com/stafiprotocol/go-substrate-rpc-client/types"
    26  )
    27  
    28  type Client interface {
    29  	Call(result interface{}, method string, args ...interface{}) error
    30  
    31  	Subscribe(ctx context.Context, namespace, subscribeMethodSuffix, unsubscribeMethodSuffix,
    32  		notificationMethodSuffix string, channel interface{}, args ...interface{}) (
    33  		*gethrpc.ClientSubscription, error)
    34  
    35  	URL() string
    36  
    37  	Close()
    38  }
    39  
    40  type client struct {
    41  	gethrpc.Client
    42  
    43  	url string
    44  }
    45  
    46  // URL returns the URL the client connects to
    47  func (c client) URL() string {
    48  	return c.url
    49  }
    50  
    51  func (c client) Close() {
    52  	c.Client.Close()
    53  }
    54  
    55  // Connect connects to the provided url
    56  func Connect(url string) (Client, error) {
    57  	log.Printf("Connecting to %v...", url)
    58  
    59  	ctx, cancel := context.WithTimeout(context.Background(), config.Default().DialTimeout)
    60  	defer cancel()
    61  
    62  	c, err := gethrpc.DialContext(ctx, url)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	cc := client{*c, url}
    67  	return &cc, nil
    68  }
    69  
    70  func CallWithBlockHash(c Client, target interface{}, method string, blockHash *types.Hash, args ...interface{}) error {
    71  	if blockHash == nil {
    72  		err := c.Call(target, method, args...)
    73  		if err != nil {
    74  			return err
    75  		}
    76  		return nil
    77  	}
    78  	hexHash, err := types.Hex(*blockHash)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	hargs := append(args, hexHash)
    83  	err = c.Call(target, method, hargs...)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	return nil
    88  }