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

     1  package zcnbridge
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"time"
     7  
     8  	"github.com/0chain/gosdk/core/sys"
     9  	"github.com/0chain/gosdk/zcncore"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  // AlchemyGasEstimationRequest describes request used for Alchemy enhanced JSON-RPC API.
    14  type AlchemyGasEstimationRequest struct {
    15  	From  string `json:"from"`
    16  	To    string `json:"to"`
    17  	Value string `json:"value"`
    18  	Data  string `json:"data"`
    19  }
    20  
    21  // GasEstimationRequest describes request used for Alchemy enhanced JSON-RPC API.
    22  type GasEstimationRequest struct {
    23  	From  string `json:"from"`
    24  	To    string `json:"to"`
    25  	Value string `json:"value"`
    26  }
    27  
    28  // GasPriceEstimationResult represents result of the gas price estimation operation execution.
    29  type GasPriceEstimationResult struct {
    30  	Value float64 `json:"value"`
    31  }
    32  
    33  // BancorTokenDetails describes Bancor ZCN zcntoken pool details
    34  type BancorTokenDetails struct {
    35  	Data struct {
    36  		Rate struct {
    37  			ETH  string `json:"eth"`
    38  			BNT  string `json:"bnt"`
    39  			USDC string `json:"usd"`
    40  			EURC string `json:"eur"`
    41  		}
    42  	} `json:"data"`
    43  }
    44  
    45  func GetTransactionStatus(hash string) (int, error) {
    46  	_, err := zcncore.GetEthClient()
    47  	if err != nil {
    48  		return -1, err
    49  	}
    50  
    51  	return zcncore.CheckEthHashStatus(hash), nil
    52  }
    53  
    54  // ConfirmEthereumTransaction confirms Ethereum transaction by hash.
    55  //   - hash is the transaction hash to confirm.
    56  //   - times is the number of times to try confirming the transaction.
    57  //   - duration is the duration to wait between each confirmation attempt.
    58  func ConfirmEthereumTransaction(hash string, times int, duration time.Duration) (int, error) {
    59  	var (
    60  		res = 0
    61  		err error
    62  	)
    63  
    64  	if hash == "" {
    65  		return -1, errors.New("transaction hash should not be empty")
    66  	}
    67  
    68  	for i := 0; i < times; i++ {
    69  		res, err = GetTransactionStatus(hash)
    70  		if err != nil {
    71  			Logger.Info(fmt.Sprintf("confirmation of Ethereum transaction %s [ERROR]", hash))
    72  			return -1, err
    73  		}
    74  		if res == 1 {
    75  			Logger.Info(fmt.Sprintf("confirmation of Ethereum transaction %s [OK]", hash))
    76  			return res, nil
    77  		}
    78  		if res == 0 {
    79  			Logger.Info(fmt.Sprintf("confirmation of Ethereum transaction %s [FAILED]", hash))
    80  			return res, nil
    81  		}
    82  		Logger.Info(fmt.Sprintf("Try confirming Ethereum transaction %s # %d", hash, i))
    83  		sys.Sleep(duration)
    84  	}
    85  
    86  	Logger.Info(fmt.Sprintf("Verification of transaction %s is still pending after %d efforts, try checking it later", hash, times))
    87  
    88  	return res, nil
    89  }
    90  
    91  func addPercents(gasLimitUnits uint64, percents int) *big.Int {
    92  	gasLimitBig := big.NewInt(int64(gasLimitUnits))
    93  	factorBig := big.NewInt(int64(percents))
    94  	deltaBig := gasLimitBig.Div(gasLimitBig, factorBig)
    95  
    96  	origin := big.NewInt(int64(gasLimitUnits))
    97  	gasLimitBig = origin.Add(origin, deltaBig)
    98  
    99  	return gasLimitBig
   100  }
   101  
   102  // ConvertIntToHex converts given int value to hex string.
   103  func ConvertIntToHex(value int64) string {
   104  	return fmt.Sprintf("%#x", value)
   105  }