github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/etherman/simulated.go (about) 1 package etherman 2 3 import ( 4 "context" 5 "fmt" 6 "math/big" 7 8 "github.com/0xPolygon/supernets2-node/etherman/smartcontracts/matic" 9 "github.com/0xPolygon/supernets2-node/etherman/smartcontracts/mockverifier" 10 "github.com/0xPolygon/supernets2-node/etherman/smartcontracts/polygonzkevmbridge" 11 "github.com/0xPolygon/supernets2-node/etherman/smartcontracts/polygonzkevmglobalexitroot" 12 "github.com/0xPolygon/supernets2-node/etherman/smartcontracts/supernets2" 13 "github.com/0xPolygon/supernets2-node/etherman/smartcontracts/supernets2datacommittee" 14 "github.com/ethereum/go-ethereum/accounts/abi/bind" 15 "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" 16 "github.com/ethereum/go-ethereum/common" 17 "github.com/ethereum/go-ethereum/core" 18 "github.com/ethereum/go-ethereum/crypto" 19 ) 20 21 // NewSimulatedEtherman creates an etherman that uses a simulated blockchain. It's important to notice that the ChainID of the auth 22 // must be 1337. The address that holds the auth will have an initial balance of 10 ETH 23 func NewSimulatedEtherman(cfg Config, auth *bind.TransactOpts) ( 24 etherman *Client, 25 ethBackend *backends.SimulatedBackend, 26 maticAddr common.Address, 27 br *polygonzkevmbridge.Polygonzkevmbridge, 28 da *supernets2datacommittee.Supernets2datacommittee, 29 err error, 30 ) { 31 if auth == nil { 32 // read only client 33 return &Client{}, nil, common.Address{}, nil, nil, nil 34 } 35 // 10000000 ETH in wei 36 balance, _ := new(big.Int).SetString("10000000000000000000000000", 10) //nolint:gomnd 37 address := auth.From 38 genesisAlloc := map[common.Address]core.GenesisAccount{ 39 address: { 40 Balance: balance, 41 }, 42 } 43 blockGasLimit := uint64(999999999999999999) //nolint:gomnd 44 client := backends.NewSimulatedBackend(genesisAlloc, blockGasLimit) 45 46 // DAC Setup 47 dataCommitteeAddr, _, da, err := supernets2datacommittee.DeploySupernets2datacommittee(auth, client) 48 if err != nil { 49 return nil, nil, common.Address{}, nil, nil, err 50 } 51 _, err = da.Initialize(auth) 52 if err != nil { 53 return nil, nil, common.Address{}, nil, nil, err 54 } 55 _, err = da.SetupCommittee(auth, big.NewInt(0), []string{}, []byte{}) 56 if err != nil { 57 return nil, nil, common.Address{}, nil, nil, err 58 } 59 60 // Deploy contracts 61 const maticDecimalPlaces = 18 62 totalSupply, _ := new(big.Int).SetString("10000000000000000000000000000", 10) //nolint:gomnd 63 maticAddr, _, maticContract, err := matic.DeployMatic(auth, client, "Matic Token", "MATIC", maticDecimalPlaces, totalSupply) 64 if err != nil { 65 return nil, nil, common.Address{}, nil, nil, err 66 } 67 rollupVerifierAddr, _, _, err := mockverifier.DeployMockverifier(auth, client) 68 if err != nil { 69 return nil, nil, common.Address{}, nil, nil, err 70 } 71 nonce, err := client.PendingNonceAt(context.TODO(), auth.From) 72 if err != nil { 73 return nil, nil, common.Address{}, nil, nil, err 74 } 75 const posBridge = 1 76 calculatedBridgeAddr := crypto.CreateAddress(auth.From, nonce+posBridge) 77 const posPoE = 2 78 calculatedPoEAddr := crypto.CreateAddress(auth.From, nonce+posPoE) 79 genesis := common.HexToHash("0xfd3434cd8f67e59d73488a2b8da242dd1f02849ea5dd99f0ca22c836c3d5b4a9") // Random value. Needs to be different to 0x0 80 exitManagerAddr, _, globalExitRoot, err := polygonzkevmglobalexitroot.DeployPolygonzkevmglobalexitroot(auth, client, calculatedPoEAddr, calculatedBridgeAddr) 81 if err != nil { 82 return nil, nil, common.Address{}, nil, nil, err 83 } 84 bridgeAddr, _, br, err := polygonzkevmbridge.DeployPolygonzkevmbridge(auth, client) 85 if err != nil { 86 return nil, nil, common.Address{}, nil, nil, err 87 } 88 poeAddr, _, poe, err := supernets2.DeploySupernets2(auth, client, exitManagerAddr, maticAddr, rollupVerifierAddr, bridgeAddr, dataCommitteeAddr, 1000, 1) //nolint 89 if err != nil { 90 return nil, nil, common.Address{}, nil, nil, err 91 } 92 _, err = br.Initialize(auth, 0, exitManagerAddr, poeAddr) 93 if err != nil { 94 return nil, nil, common.Address{}, nil, nil, err 95 } 96 97 poeParams := supernets2.Supernets2InitializePackedParameters{ 98 Admin: auth.From, 99 TrustedSequencer: auth.From, 100 PendingStateTimeout: 10000, //nolint:gomnd 101 TrustedAggregator: auth.From, 102 TrustedAggregatorTimeout: 10000, //nolint:gomnd 103 } 104 _, err = poe.Initialize(auth, poeParams, genesis, "http://localhost", "L2", "v1") //nolint:gomnd 105 if err != nil { 106 return nil, nil, common.Address{}, nil, nil, err 107 } 108 109 if calculatedBridgeAddr != bridgeAddr { 110 return nil, nil, common.Address{}, nil, nil, fmt.Errorf("bridgeAddr (%s) is different from the expected contract address (%s)", 111 bridgeAddr.String(), calculatedBridgeAddr.String()) 112 } 113 if calculatedPoEAddr != poeAddr { 114 return nil, nil, common.Address{}, nil, nil, fmt.Errorf("poeAddr (%s) is different from the expected contract address (%s)", 115 poeAddr.String(), calculatedPoEAddr.String()) 116 } 117 118 // Approve the bridge and poe to spend 10000 matic tokens. 119 approvedAmount, _ := new(big.Int).SetString("10000000000000000000000", 10) //nolint:gomnd 120 _, err = maticContract.Approve(auth, bridgeAddr, approvedAmount) 121 if err != nil { 122 return nil, nil, common.Address{}, nil, nil, err 123 } 124 _, err = maticContract.Approve(auth, poeAddr, approvedAmount) 125 if err != nil { 126 return nil, nil, common.Address{}, nil, nil, err 127 } 128 _, err = poe.ActivateForceBatches(auth) 129 if err != nil { 130 return nil, nil, common.Address{}, nil, nil, err 131 } 132 133 client.Commit() 134 c := &Client{ 135 EthClient: client, 136 Supernets2: poe, 137 Matic: maticContract, 138 GlobalExitRootManager: globalExitRoot, 139 DataCommittee: da, 140 SCAddresses: []common.Address{poeAddr, exitManagerAddr, dataCommitteeAddr}, 141 auth: map[common.Address]bind.TransactOpts{}, 142 } 143 err = c.AddOrReplaceAuth(*auth) 144 if err != nil { 145 return nil, nil, common.Address{}, nil, nil, err 146 } 147 return c, client, maticAddr, br, da, nil 148 }