github.com/0chain/gosdk@v1.17.11/zcnbridge/rest.go (about)

     1  package zcnbridge
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/0chain/gosdk/core/common"
     7  
     8  	"github.com/0chain/gosdk/zcnbridge/http"
     9  	"github.com/0chain/gosdk/zcnbridge/wallet"
    10  	"github.com/0chain/gosdk/zcncore"
    11  )
    12  
    13  // Models
    14  
    15  // AuthorizerResponse represents the response of the request to get authorizer info from the sharders.
    16  type AuthorizerResponse struct {
    17  	AuthorizerID string `json:"id"`
    18  	URL          string `json:"url"`
    19  
    20  	// Configuration
    21  	Fee common.Balance `json:"fee"`
    22  
    23  	// Geolocation
    24  	Latitude  float64 `json:"latitude"`
    25  	Longitude float64 `json:"longitude"`
    26  
    27  	// Stats
    28  	LastHealthCheck int64 `json:"last_health_check"`
    29  
    30  	// stake_pool_settings
    31  	DelegateWallet string         `json:"delegate_wallet"`
    32  	MinStake       common.Balance `json:"min_stake"`
    33  	MaxStake       common.Balance `json:"max_stake"`
    34  	NumDelegates   int            `json:"num_delegates"`
    35  	ServiceCharge  float64        `json:"service_charge"`
    36  }
    37  
    38  // AuthorizerNodesResponse represents the response of the request to get authorizers
    39  type AuthorizerNodesResponse struct {
    40  	Nodes []*AuthorizerNode `json:"nodes"`
    41  }
    42  
    43  // AuthorizerNode represents an authorizer node
    44  type AuthorizerNode struct {
    45  	ID  string `json:"id"`
    46  	URL string `json:"url"`
    47  }
    48  
    49  // Rest endpoints
    50  
    51  // getAuthorizers returns authorizers from smart contract
    52  func getAuthorizers(active bool) ([]*AuthorizerNode, error) {
    53  	var (
    54  		authorizers = new(AuthorizerNodesResponse)
    55  		cb          = wallet.NewZCNStatus(authorizers)
    56  		err         error
    57  	)
    58  
    59  	cb.Begin()
    60  
    61  	if err = GetAuthorizers(active, cb); err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	if err = cb.Wait(); err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	if len(authorizers.Nodes) == 0 {
    70  		fmt.Println("no authorizers found")
    71  		return nil, err
    72  	}
    73  
    74  	return authorizers.Nodes, nil
    75  }
    76  
    77  // GetAuthorizer returned authorizer information from Züs Blockchain by the ID
    78  //   - id is the authorizer ID
    79  //   - cb is the callback function to handle the response asynchronously
    80  func GetAuthorizer(id string, cb zcncore.GetInfoCallback) (err error) {
    81  	err = zcncore.CheckConfig()
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	go http.MakeSCRestAPICall(
    87  		zcncore.OpZCNSCGetAuthorizer,
    88  		http.PathGetAuthorizer,
    89  		http.Params{
    90  			"id": id,
    91  		},
    92  		cb,
    93  	)
    94  
    95  	return
    96  }
    97  
    98  // GetAuthorizers Returns all or only active authorizers
    99  //   - active is the flag to get only active authorizers
   100  //   - cb is the callback function to handle the response asynchronously
   101  func GetAuthorizers(active bool, cb zcncore.GetInfoCallback) (err error) {
   102  	err = zcncore.CheckConfig()
   103  	if err != nil {
   104  		return err
   105  	}
   106  	go http.MakeSCRestAPICall(zcncore.OpZCNSCGetAuthorizerNodes, fmt.Sprintf(http.PathGetAuthorizerNodes, active), nil, cb)
   107  	return
   108  }
   109  
   110  // GetGlobalConfig Returns global config
   111  //   - cb is the callback function to handle the response asynchronously
   112  func GetGlobalConfig(cb zcncore.GetInfoCallback) (err error) {
   113  	err = zcncore.CheckConfig()
   114  	if err != nil {
   115  		return err
   116  	}
   117  	go http.MakeSCRestAPICall(zcncore.OpZCNSCGetGlobalConfig, http.PathGetGlobalConfig, nil, cb)
   118  	return
   119  }