forked from ethereum-optimism/superchain-registry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
229 lines (197 loc) · 7.25 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/ethereum-optimism/optimism/op-e2e/bindings"
"github.com/ethereum-optimism/superchain-registry/add-chain/cmd"
"github.com/ethereum-optimism/superchain-registry/add-chain/config"
"github.com/ethereum-optimism/superchain-registry/add-chain/flags"
"github.com/ethereum-optimism/superchain-registry/superchain"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
)
var app = &cli.App{
Name: "add-chain",
Usage: "Add a new chain to the superchain-registry",
Flags: []cli.Flag{
flags.PublicRpcFlag,
flags.SequencerRpcFlag,
flags.ExplorerFlag,
flags.SuperchainTargetFlag,
flags.MonorepoDirFlag,
flags.ChainNameFlag,
flags.ChainShortNameFlag,
flags.RollupConfigFlag,
flags.DeploymentsDirFlag,
flags.StandardChainCandidateFlag,
},
Action: entrypoint,
Commands: []*cli.Command{
&cmd.PromoteToStandardCmd,
&cmd.CheckRollupConfigCmd,
&cmd.CompressGenesisCmd,
&cmd.CheckGenesisCmd,
&cmd.UpdateConfigsCmd,
},
}
func main() {
if err := runApp(os.Args); err != nil {
fmt.Println(err)
fmt.Println("*********************")
fmt.Printf("FAILED: %s\n", app.Name)
os.Exit(1)
}
fmt.Println("*********************")
fmt.Printf("SUCCESS: %s\n", app.Name)
}
func runApp(args []string) error {
// Load the appropriate .env file
var err error
if runningTests := os.Getenv("SCR_RUN_TESTS"); runningTests == "true" {
fmt.Println("Loading .env.test")
err = godotenv.Load("./testdata/.env.test")
} else {
fmt.Println("Loading .env")
err = godotenv.Load()
}
if err != nil {
panic("error loading .env file")
}
return app.Run(args)
}
func entrypoint(ctx *cli.Context) error {
standardChainCandidate := ctx.Bool(flags.StandardChainCandidateFlag.Name)
superchainLevel := superchain.Frontier // All chains enter as frontier chains
publicRPC := ctx.String(flags.PublicRpcFlag.Name)
sequencerRPC := ctx.String(flags.SequencerRpcFlag.Name)
explorer := ctx.String(flags.ExplorerFlag.Name)
superchainTarget := ctx.String(flags.SuperchainTargetFlag.Name)
monorepoDir := ctx.String(flags.MonorepoDirFlag.Name)
chainName := ctx.String(flags.ChainNameFlag.Name)
rollupConfigPath := ctx.String(flags.RollupConfigFlag.Name)
deploymentsDir := ctx.String(flags.DeploymentsDirFlag.Name)
chainShortName := ctx.String(flags.ChainShortNameFlag.Name)
if chainShortName == "" {
return fmt.Errorf("must set chain-short-name (SCR_CHAIN_SHORT_NAME)")
}
// Get the current script filepath
_, thisFile, _, ok := runtime.Caller(0)
if !ok {
panic("error getting current filepath")
}
superchainRepoRoot := filepath.Dir(filepath.Dir(thisFile))
fmt.Printf("Chain Name: %s\n", chainName)
fmt.Printf("Chain Short Name: %s\n", chainShortName)
fmt.Printf("Superchain target: %s\n", superchainTarget)
fmt.Printf("Superchain-registry repo dir: %s\n", superchainRepoRoot)
fmt.Printf("Monorepo dir: %s\n", monorepoDir)
fmt.Printf("Deployments directory: %s\n", deploymentsDir)
fmt.Printf("Rollup config filepath: %s\n", rollupConfigPath)
fmt.Printf("Public RPC endpoint: %s\n", publicRPC)
fmt.Printf("Sequencer RPC endpoint: %s\n", sequencerRPC)
fmt.Printf("Block Explorer: %s\n", explorer)
fmt.Println()
// Check if superchain target directory exists
targetDir := filepath.Join(superchainRepoRoot, "superchain", "configs", superchainTarget)
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
return fmt.Errorf("superchain target directory not found. Please follow instructions to add a superchain target in CONTRIBUTING.md: %s", targetDir)
}
l1RpcUrl, err := config.GetL1RpcUrl(superchainTarget)
if err != nil {
return fmt.Errorf("failed to retrieve L1 rpc url: %w", err)
}
var addresses superchain.AddressList
err = readAddressesFromJSON(&addresses, deploymentsDir)
if err != nil {
return fmt.Errorf("failed to read addresses from JSON files: %w", err)
}
isFaultProofs, err := inferIsFaultProofs(addresses.SystemConfigProxy, addresses.OptimismPortalProxy, l1RpcUrl)
if err != nil {
return fmt.Errorf("failed to infer fault proofs status of chain: %w", err)
}
rollupConfig, err := config.ConstructChainConfig(rollupConfigPath, chainName, publicRPC, sequencerRPC, explorer, superchainLevel, standardChainCandidate)
if err != nil {
return fmt.Errorf("failed to construct rollup config: %w", err)
}
err = readAddressesFromChain(&addresses, l1RpcUrl, isFaultProofs)
if err != nil {
return fmt.Errorf("failed to read addresses from chain: %w", err)
}
if rollupConfig.AltDA != nil {
addresses.DAChallengeAddress = *rollupConfig.AltDA.DAChallengeAddress
}
rollupConfig.Addresses = addresses
l1RpcUrl, err = config.GetL1RpcUrl(superchainTarget)
if err != nil {
return fmt.Errorf("error getting l1RpcUrl: %w", err)
}
gpt, err := getGasPayingToken(l1RpcUrl, addresses.SystemConfigProxy)
if err != nil {
return fmt.Errorf("error inferring gas paying token: %w", err)
}
rollupConfig.GasPayingToken = gpt
targetFilePath := filepath.Join(targetDir, chainShortName+".toml")
err = config.WriteChainConfigTOML(rollupConfig, targetFilePath)
if err != nil {
return fmt.Errorf("error generating chain config .yaml file: %w", err)
}
fmt.Printf("Wrote config for new chain with identifier %s", rollupConfig.Identifier())
return nil
}
func inferIsFaultProofs(systemConfigProxyAddress, optimismPortalProxyAddress superchain.Address, l1RpcUrl string) (bool, error) {
tokenAddress, err := getGasPayingToken(l1RpcUrl, systemConfigProxyAddress)
if err != nil {
return false, fmt.Errorf("failed to query for gasPayingToken: %w", err)
}
if tokenAddress != nil {
return false, nil
}
// Portal version `3` is the first version of the `OptimismPortal` that supported the fault proof system.
version, err := castCall(optimismPortalProxyAddress, "version()(string)", l1RpcUrl)
if err != nil {
return false, fmt.Errorf("failed to get OptimismPortalProxy.version(): %w", err)
}
version, err = strconv.Unquote(version)
if err != nil {
return false, fmt.Errorf("failed to parse OptimismPortalProxy.version(): %w", err)
}
majorVersion, err := strconv.ParseInt(strings.Split(version, ".")[0], 10, 32)
if err != nil {
return false, fmt.Errorf("failed to parse OptimismPortalProxy.version(): %w", err)
}
return majorVersion >= 3, nil
}
func getGasPayingToken(l1rpcURl string, SystemConfigAddress superchain.Address) (*superchain.Address, error) {
client, err := ethclient.Dial(l1rpcURl)
if err != nil {
return nil, err
}
sc, err := bindings.NewSystemConfig(common.Address(SystemConfigAddress), client)
if err != nil {
return nil, err
}
opts := bind.CallOpts{}
result, err := sc.GasPayingToken(&opts)
if err != nil {
if strings.Contains(err.Error(), "execution reverted") {
// This happens when the SystemConfig contract
// does not yet have the CGT functionality.
return nil, nil
}
return nil, err
}
if (result.Addr == common.Address{}) {
// This happens with the SystemConfig contract
// does have the CGT functionality, but it has
// not been enabled.
return nil, nil
}
return (*superchain.Address)(&result.Addr), nil
}