github.com/diadata-org/diadata@v1.4.593/pkg/dia/helpers/substrate-helper/gsrpc/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 2019 Centrifuge GmbH
     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  
    22  	"github.com/diadata-org/diadata/pkg/dia/helpers/substrate-helper/gsrpc/config"
    23  	gethrpc "github.com/diadata-org/diadata/pkg/dia/helpers/substrate-helper/gsrpc/gethrpc"
    24  	"github.com/diadata-org/diadata/pkg/dia/helpers/substrate-helper/gsrpc/types"
    25  	"github.com/diadata-org/diadata/pkg/dia/helpers/substrate-helper/gsrpc/types/codec"
    26  )
    27  
    28  //go:generate mockery --name Client
    29  
    30  type Client interface {
    31  	// Call makes the call to RPC method with the provided args
    32  	// args must be encoded in the format RPC understands
    33  	Call(result interface{}, method string, args ...interface{}) error
    34  
    35  	CallContext(
    36  		ctx context.Context,
    37  		result interface{},
    38  		method string,
    39  		args ...interface{},
    40  	) error
    41  
    42  	Subscribe(
    43  		ctx context.Context,
    44  		namespace, subscribeMethodSuffix, unsubscribeMethodSuffix,
    45  		notificationMethodSuffix string,
    46  		channel interface{},
    47  		args ...interface{},
    48  	) (*gethrpc.ClientSubscription, error)
    49  
    50  	URL() string
    51  
    52  	Close()
    53  }
    54  
    55  type client struct {
    56  	gethrpc.Client
    57  
    58  	url string
    59  }
    60  
    61  // URL returns the URL the client connects to
    62  func (c client) URL() string {
    63  	return c.url
    64  }
    65  
    66  func (c client) Close() {
    67  	c.Client.Close()
    68  }
    69  
    70  // Connect connects to the provided url
    71  func Connect(url string) (Client, error) {
    72  	ctx, cancel := context.WithTimeout(context.Background(), config.Default().DialTimeout)
    73  	defer cancel()
    74  
    75  	c, err := gethrpc.DialContext(ctx, url)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	cc := client{*c, url}
    80  	return &cc, nil
    81  }
    82  
    83  func CallWithBlockHash(c Client, target interface{}, method string, blockHash *types.Hash, args ...interface{}) error {
    84  	ctx := context.Background()
    85  
    86  	return CallWithBlockHashContext(ctx, c, target, method, blockHash, args...)
    87  }
    88  
    89  func CallWithBlockHashContext(
    90  	ctx context.Context,
    91  	c Client,
    92  	target interface{},
    93  	method string,
    94  	blockHash *types.Hash,
    95  	args ...interface{},
    96  ) error {
    97  	if blockHash == nil {
    98  		err := c.CallContext(ctx, target, method, args...)
    99  		if err != nil {
   100  			return err
   101  		}
   102  		return nil
   103  	}
   104  	hexHash, err := codec.Hex(*blockHash)
   105  	if err != nil {
   106  		return err
   107  	}
   108  	args = append(args, hexHash)
   109  	err = c.CallContext(ctx, target, method, args...)
   110  	if err != nil {
   111  		return err
   112  	}
   113  	return nil
   114  }