github.com/0chain/gosdk@v1.17.11/zcncore/transaction_query_base.go (about)

     1  package zcncore
     2  
     3  import (
     4  	"encoding/json"
     5  	stderrors "errors"
     6  
     7  	thrown "github.com/0chain/errors"
     8  )
     9  
    10  // GetUserLockedTotal get total token user locked
    11  // # Inputs
    12  //   - clientID wallet id
    13  func GetUserLockedTotal(clientID string) (int64, error) {
    14  
    15  	err := checkSdkInit()
    16  	if err != nil {
    17  		return 0, err
    18  	}
    19  
    20  	var url = withParams(STORAGESC_GET_USER_LOCKED_TOTAL, Params{
    21  		"client_id": clientID,
    22  	})
    23  	cb := createGetInfoCallback()
    24  	go GetInfoFromSharders(url, OpStorageSCGetStakePoolInfo, cb)
    25  	info, err := cb.Wait()
    26  	if err != nil {
    27  		return 0, err
    28  	}
    29  
    30  	result := make(map[string]int64)
    31  
    32  	err = json.Unmarshal([]byte(info), &result)
    33  	if err != nil {
    34  		return 0, thrown.Throw(err, "invalid json format")
    35  	}
    36  
    37  	total, ok := result["total"]
    38  	if ok {
    39  		return total, nil
    40  	}
    41  
    42  	return 0, stderrors.New("invalid result")
    43  
    44  }
    45  
    46  func createGetInfoCallback() *getInfoCallback {
    47  	return &getInfoCallback{
    48  		callback: make(chan bool),
    49  	}
    50  }
    51  
    52  type getInfoCallback struct {
    53  	callback chan bool
    54  	status   int
    55  	info     string
    56  	err      string
    57  }
    58  
    59  func (cb *getInfoCallback) OnInfoAvailable(op int, status int, info string, err string) {
    60  
    61  	// if status == StatusSuccess then info is valid
    62  	// is status != StatusSuccess then err will give the reason
    63  
    64  	cb.status = status
    65  	if status == StatusSuccess {
    66  		cb.info = info
    67  	} else {
    68  		cb.err = err
    69  	}
    70  
    71  	cb.callback <- true
    72  }
    73  
    74  func (cb *getInfoCallback) Wait() (string, error) {
    75  	<-cb.callback
    76  	if cb.err == "" {
    77  		return cb.info, nil
    78  	}
    79  
    80  	return "", stderrors.New(cb.err)
    81  }