/
contract_map.go
52 lines (41 loc) · 988 Bytes
/
contract_map.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
package seth
import (
"io"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/pelletier/go-toml/v2"
)
func SaveDeployedContract(filename, contractName, address string) error {
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()
v := map[string]string{
address: contractName,
}
marhalled, err := toml.Marshal(v)
if err != nil {
return err
}
_, err = file.WriteString(string(marhalled))
return err
}
func LoadDeployedContracts(filename string) (map[string]string, error) {
tomlFile, err := os.Open(filename)
if err != nil {
return map[string]string{}, nil
}
defer tomlFile.Close()
b, _ := io.ReadAll(tomlFile)
rawContracts := map[common.Address]string{}
err = toml.Unmarshal(b, &rawContracts)
if err != nil {
return map[string]string{}, err
}
contracts := map[string]string{}
for k, v := range rawContracts {
contracts[k.Hex()] = v
}
return contracts, nil
}