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

     1  package wallet
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  	"os"
     9  	"sync"
    10  
    11  	"github.com/0chain/gosdk/zcncore"
    12  )
    13  
    14  // ZCNStatus represents the status of a ZCN operation.
    15  type ZCNStatus struct {
    16  	walletString string
    17  	balance      int64
    18  	value        interface{}
    19  	Wg           *sync.WaitGroup
    20  	Success      bool
    21  	Err          error
    22  }
    23  
    24  // NewZCNStatus creates a new ZCNStatus instance.
    25  //   - value: value to be stored in the ZCNStatus instance
    26  func NewZCNStatus(value interface{}) (zcns *ZCNStatus) {
    27  	return &ZCNStatus{
    28  		Wg:    new(sync.WaitGroup),
    29  		value: value,
    30  	}
    31  }
    32  
    33  // Begin starts the wait group
    34  func (zcn *ZCNStatus) Begin() {
    35  	zcn.Wg.Add(1)
    36  }
    37  
    38  // Wait waits for the wait group to finish
    39  func (zcn *ZCNStatus) Wait() error {
    40  	zcn.Wg.Wait()
    41  	return zcn.Err
    42  }
    43  
    44  // OnBalanceAvailable callback when balance is available
    45  //   - status: status of the operation
    46  //   - value: balance value
    47  //   - third parameter is not used, it is kept for compatibility
    48  func (zcn *ZCNStatus) OnBalanceAvailable(status int, value int64, _ string) {
    49  	defer zcn.Wg.Done()
    50  	if status == zcncore.StatusSuccess {
    51  		zcn.Success = true
    52  	} else {
    53  		zcn.Success = false
    54  	}
    55  	zcn.balance = value
    56  }
    57  
    58  // OnTransactionComplete callback when a transaction is completed
    59  //   - t: transaction object
    60  //   - status: status of the transaction
    61  func (zcn *ZCNStatus) OnTransactionComplete(t *zcncore.Transaction, status int) {
    62  	defer zcn.Wg.Done()
    63  	if status == zcncore.StatusSuccess {
    64  		zcn.Success = true
    65  	} else {
    66  		zcn.Err = errors.New(t.GetTransactionError())
    67  	}
    68  }
    69  
    70  // OnVerifyComplete callback when a transaction is verified
    71  //   - t: transaction object
    72  //   - status: status of the transaction
    73  func (zcn *ZCNStatus) OnVerifyComplete(t *zcncore.Transaction, status int) {
    74  	defer zcn.Wg.Done()
    75  	if status == zcncore.StatusSuccess {
    76  		zcn.Success = true
    77  	} else {
    78  		zcn.Err = errors.New(t.GetVerifyError())
    79  	}
    80  }
    81  
    82  // OnTransferComplete callback when a transfer is completed. Not used in this implementation
    83  func (zcn *ZCNStatus) OnAuthComplete(_ *zcncore.Transaction, status int) {
    84  	Logger.Info("Authorization complete with status: ", status)
    85  }
    86  
    87  // OnWalletCreateComplete callback when a wallet is created
    88  //   - status: status of the operation
    89  //   - wallet: wallet json string
    90  func (zcn *ZCNStatus) OnWalletCreateComplete(status int, wallet string, err string) {
    91  	defer zcn.Wg.Done()
    92  	if status != zcncore.StatusSuccess {
    93  		zcn.Success = false
    94  		zcn.Err = errors.New(err)
    95  		zcn.walletString = ""
    96  		return
    97  	}
    98  	zcn.Success = true
    99  	zcn.Err = nil
   100  	zcn.walletString = wallet
   101  }
   102  
   103  // OnInfoAvailable callback when information is available
   104  //   - op`: operation type (check `zcncore.Op* constants)
   105  //   - status: status of the operation
   106  //   - info: information represneted as a string
   107  //   - err: error message
   108  func (zcn *ZCNStatus) OnInfoAvailable(op int, status int, info string, err string) {
   109  	defer zcn.Wg.Done()
   110  
   111  	// If status is 400 for OpGetMintNonce, mintNonce is considered as 0
   112  	if op == zcncore.OpGetMintNonce && status == http.StatusBadRequest {
   113  		zcn.Err = nil
   114  		zcn.Success = true
   115  		return
   116  	}
   117  
   118  	if status != zcncore.StatusSuccess {
   119  		zcn.Err = errors.New(err)
   120  		zcn.Success = false
   121  		return
   122  	}
   123  
   124  	if info == "" || info == "{}" {
   125  		zcn.Err = errors.New("empty response")
   126  		zcn.Success = false
   127  		return
   128  	}
   129  
   130  	var errm error
   131  	if errm = json.Unmarshal([]byte(info), zcn.value); errm != nil {
   132  		zcn.Err = fmt.Errorf("decoding response: %v", errm)
   133  		zcn.Success = false
   134  		return
   135  	}
   136  
   137  	zcn.Err = nil
   138  	zcn.Success = true
   139  }
   140  
   141  // OnSetupComplete callback when setup is completed.
   142  // Paramters are not used in this implementation,
   143  // just kept for compatibility.
   144  func (zcn *ZCNStatus) OnSetupComplete(_ int, _ string) {
   145  	defer zcn.Wg.Done()
   146  }
   147  
   148  // OnAuthorizeSendComplete callback when authorization is completed
   149  //   - status: status of the operation
   150  //   - 2nd parameter is not used, it is kept for compatibility
   151  //   - 3rd parameter is not used, it is kept for compatibility
   152  //   - 4th parameter is not used, it is kept for compatibility
   153  //   - creationDate: timestamp of the creation date
   154  //   - signature: signature of the operation
   155  func (zcn *ZCNStatus) OnAuthorizeSendComplete(status int, _ string, _ int64, _ string, creationDate int64, signature string) {
   156  	defer zcn.Wg.Done()
   157  
   158  	Logger.Info("Status: ", status)
   159  	Logger.Info("Timestamp:", creationDate)
   160  	Logger.Info("Signature:", signature)
   161  }
   162  
   163  // OnVoteComplete callback when a multisig vote is completed
   164  //   - status: status of the operation
   165  //   - proposal: proposal json string
   166  //   - err: error message
   167  func (zcn *ZCNStatus) OnVoteComplete(status int, proposal string, err string) {
   168  	defer zcn.Wg.Done()
   169  	if status != zcncore.StatusSuccess {
   170  		zcn.Success = false
   171  		zcn.Err = errors.New(err)
   172  		zcn.walletString = ""
   173  		return
   174  	}
   175  	zcn.Success = true
   176  	zcn.Err = nil
   177  	zcn.walletString = proposal
   178  }
   179  
   180  //goland:noinspection ALL
   181  func PrintError(v ...interface{}) {
   182  	fmt.Fprintln(os.Stderr, v...)
   183  }
   184  
   185  //goland:noinspection ALL
   186  func ExitWithError(v ...interface{}) {
   187  	fmt.Fprintln(os.Stderr, v...)
   188  	os.Exit(1)
   189  }