-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
utils.go
87 lines (80 loc) · 2.08 KB
/
utils.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
75
76
77
78
79
80
81
82
83
84
85
86
87
package gethwrappers
import (
"crypto/sha256"
"fmt"
"os"
"path/filepath"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
// VersionHash is the hash used to detect changes in the underlying contract
func VersionHash(abiPath string, binPath string) (hash string) {
abi, err := os.ReadFile(abiPath)
if err != nil {
Exit("Could not read abi path to create version hash", err)
}
bin := []byte("")
if binPath != "-" {
bin, err = os.ReadFile(binPath)
if err != nil {
Exit("Could not read abi path to create version hash", err)
}
}
hashMsg := string(abi) + string(bin) + "\n"
return fmt.Sprintf("%x", sha256.Sum256([]byte(hashMsg)))
}
func Exit(msg string, err error) {
if err != nil {
fmt.Println(msg+":", err)
} else {
fmt.Println(msg)
}
os.Exit(1)
}
// GetProjectRoot returns the root of the chainlink project
func GetProjectRoot() (rootPath string) {
root, err := os.Getwd()
if err != nil {
Exit("could not get current working directory while seeking project root",
err)
}
for root != "/" { // Walk up path to find dir containing go.mod
if _, err := os.Stat(filepath.Join(root, "go.mod")); os.IsNotExist(err) {
root = filepath.Dir(root)
} else {
return root
}
}
Exit("could not find project root", nil)
panic("can't get here")
}
func TempDir(dirPrefix string) (string, func()) {
tmpDir, err := os.MkdirTemp("", dirPrefix+"-contractWrapper")
if err != nil {
Exit("failed to create temporary working directory", err)
}
return tmpDir, func() {
if err := os.RemoveAll(tmpDir); err != nil {
fmt.Println("failure while cleaning up temporary working directory:", err)
}
}
}
func DeepCopyLog(l types.Log) types.Log {
var cpy types.Log
cpy.Address = l.Address
if l.Topics != nil {
cpy.Topics = make([]common.Hash, len(l.Topics))
copy(cpy.Topics, l.Topics)
}
if l.Data != nil {
cpy.Data = make([]byte, len(l.Data))
copy(cpy.Data, l.Data)
}
cpy.BlockNumber = l.BlockNumber
cpy.TxHash = l.TxHash
cpy.TxIndex = l.TxIndex
cpy.BlockHash = l.BlockHash
cpy.Index = l.Index
cpy.Removed = l.Removed
return cpy
}