-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_utils.go
51 lines (44 loc) · 1.27 KB
/
file_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
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"fmt"
)
func loadFromFile(filename string) (string, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
return string(data), nil
}
func saveToFile(filename string, data []byte) error {
return ioutil.WriteFile(filename, data, 0644)
}
func saveContractArtifacts(bin string, abiBytes []byte) {
cacheDir := "cache"
if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
err = os.Mkdir(cacheDir, 0755)
if err != nil {
log.Fatalf("Failed to create cache directory: %v", err)
}
}
err := saveToFile(filepath.Join(cacheDir, "Greeter.bin"), []byte(bin))
if err != nil {
log.Fatalf("Failed to save bytecode: %v", err)
}
err = saveToFile(filepath.Join(cacheDir, "Greeter.abi"), abiBytes)
if err != nil {
log.Fatalf("Failed to save ABI: %v", err)
}
fmt.Println("Saved ABI and bytecode to cache files.")
}
func saveAddressToFile(address string) {
addressFilePath := "cache/address"
err := saveToFile(addressFilePath, []byte(address))
if err != nil {
log.Fatalf("Failed to save contract address: %v", err)
}
fmt.Println("Saved contract address to file.")
}