forked from ethereum-optimism/superchain-registry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
addresses.go
159 lines (139 loc) · 4.95 KB
/
addresses.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/ethereum-optimism/superchain-registry/superchain"
)
type AddressData struct {
Address string `json:"address"`
}
func readAddressesFromChain(addresses *superchain.AddressList, l1RpcUrl string, isFaultProofs bool) error {
// SuperchainConfig
address, err := castCall(addresses.OptimismPortalProxy, "superchainConfig()(address)", l1RpcUrl)
if err == nil {
addresses.SuperchainConfig = superchain.MustHexToAddress(address)
}
// Guardian
address, err = castCall(addresses.SuperchainConfig, "guardian()(address)", l1RpcUrl)
if err != nil {
address, err = castCall(addresses.OptimismPortalProxy, "guardian()(address)", l1RpcUrl)
if err != nil {
return fmt.Errorf("could not retrieve address for Guardian %w", err)
}
}
addresses.Guardian = superchain.MustHexToAddress(address)
// ProxyAdminOwner
address, err = castCall(addresses.ProxyAdmin, "owner()(address)", l1RpcUrl)
if err != nil {
return fmt.Errorf("could not retrieve address for ProxyAdminOwner")
}
addresses.ProxyAdminOwner = superchain.MustHexToAddress(address)
// SystemConfigOwner
address, err = castCall(addresses.SystemConfigProxy, "owner()(address)", l1RpcUrl)
if err != nil {
return fmt.Errorf("could not retrieve address for SystemConfigOwner")
}
addresses.SystemConfigOwner = superchain.MustHexToAddress(address)
// UnsafeBlockSigner
address, err = castCall(addresses.SystemConfigProxy, "unsafeBlockSigner()(address)", l1RpcUrl)
if err != nil {
return fmt.Errorf("could not retrieve address for UnsafeBlockSigner")
}
addresses.UnsafeBlockSigner = superchain.MustHexToAddress(address)
// BatchSubmitter
hash, err := castCall(addresses.SystemConfigProxy, "batcherHash()(bytes32)", l1RpcUrl)
if err != nil {
return fmt.Errorf("could not retrieve batcherHash")
}
batchSubmitter := "0x" + hash[26:66]
addresses.BatchSubmitter = superchain.MustHexToAddress(batchSubmitter)
if isFaultProofs {
// Proposer
address, err = castCall(addresses.PermissionedDisputeGame, "proposer()(address)", l1RpcUrl)
if err != nil {
return fmt.Errorf("could not retrieve address for Proposer")
}
addresses.Proposer = superchain.MustHexToAddress(address)
// Challenger
address, err = castCall(addresses.PermissionedDisputeGame, "challenger()(address)", l1RpcUrl)
if err != nil {
return fmt.Errorf("could not retrieve address for Challenger")
}
addresses.Challenger = superchain.MustHexToAddress(address)
} else {
// Proposer
address, err = castCall(addresses.L2OutputOracleProxy, "PROPOSER()(address)", l1RpcUrl)
if err != nil {
return fmt.Errorf("could not retrieve address for Proposer")
}
addresses.Proposer = superchain.MustHexToAddress(address)
// Challenger
address, err = castCall(addresses.L2OutputOracleProxy, "CHALLENGER()(address)", l1RpcUrl)
if err != nil {
return fmt.Errorf("could not retrieve address for Challenger")
}
addresses.Challenger = superchain.MustHexToAddress(address)
}
fmt.Printf("Addresses read from chain\n")
return nil
}
func readAddressesFromJSON(addressList *superchain.AddressList, deploymentsDir string) error {
// Check for the following
// 1. filepath == deploymentsDir
// 2. filepath == deploymentsDir/.deploy
// 3. filepath == deploymentsDir/<contract-name>.json
deployFilePath := filepath.Join(deploymentsDir)
fileInfo, err := os.Stat(deployFilePath)
if err != nil {
return fmt.Errorf("invalid deployment filepath provided: %w", err)
}
if fileInfo.IsDir() {
deployFilePath = filepath.Join(deploymentsDir, ".deploy")
_, err = os.Stat(deployFilePath)
if err != nil {
// Use legacy deployment artifact schema
contractAddresses := make(map[string]string)
fmt.Printf("failed to find .deploy file. Will look for legacy .json files")
files, _ := os.ReadDir(deploymentsDir)
for _, file := range files {
if file.IsDir() {
continue
}
contractName := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
fileContents, err := os.ReadFile(filepath.Join(deploymentsDir, file.Name()))
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
var data AddressData
if err = json.Unmarshal(fileContents, &data); err != nil {
return fmt.Errorf("failed to unmarshal json: %w", err)
}
contractAddresses[contractName] = data.Address
err = mapToAddressList(contractAddresses, addressList)
if err != nil {
return fmt.Errorf("failed to convert contracts map into AddressList: %w", err)
}
}
return nil
}
}
rawData, err := os.ReadFile(deployFilePath)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
if err = json.Unmarshal(rawData, &addressList); err != nil {
return fmt.Errorf("failed to unmarshal json: %w", err)
}
return nil
}
func mapToAddressList(m map[string]string, result *superchain.AddressList) error {
out, err := toml.Marshal(m)
if err != nil {
return err
}
return toml.Unmarshal(out, result)
}