/
bridge_helper.go
74 lines (59 loc) · 1.74 KB
/
bridge_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package zcnbridge
import (
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
//. "github.com/0chain/gosdk/zcnbridge/log"
"github.com/0chain/gosdk/zcncore"
"github.com/pkg/errors"
)
func GetTransactionStatus(hash string) (int, error) {
_, err := zcncore.GetEthClient()
if err != nil {
return -1, err
}
return zcncore.CheckEthHashStatus(hash), nil
}
func ConfirmEthereumTransaction(hash string, times int, duration time.Duration) (int, error) {
var (
res = 0
err error
)
if hash == "" {
return -1, errors.New("transaction hash should not be empty")
}
for i := 0; i < times; i++ {
res, err = GetTransactionStatus(hash)
if err != nil {
Logger.Info(fmt.Sprintf("confirmation of Ethereum transaction %s [ERROR]", hash))
return -1, err
}
if res == 1 {
Logger.Info(fmt.Sprintf("confirmation of Ethereum transaction %s [OK]", hash))
return res, nil
}
if res == 0 {
Logger.Info(fmt.Sprintf("confirmation of Ethereum transaction %s [FAILED]", hash))
return res, nil
}
Logger.Info(fmt.Sprintf("Try confirming Ethereum transaction %s # %d", hash, i))
time.Sleep(duration)
}
Logger.Info(fmt.Sprintf("Verification of transaction %s is still pending after %d efforts, try checking it later", hash, times))
return res, nil
}
func addPercents(gasLimitUnits uint64, percents int) *big.Int {
gasLimitBig := big.NewInt(int64(gasLimitUnits))
factorBig := big.NewInt(int64(percents))
deltaBig := gasLimitBig.Div(gasLimitBig, factorBig)
origin := big.NewInt(int64(gasLimitUnits))
gasLimitBig = origin.Add(origin, deltaBig)
return gasLimitBig
}
func CreateHash(message string) common.Hash {
data := []byte(message)
hash := crypto.Keccak256Hash(data)
return hash
}