From f21eab2167827c3341001de5edf6fcdf81c620a2 Mon Sep 17 00:00:00 2001 From: nagdahimanshu Date: Thu, 18 Jan 2024 12:25:13 +0100 Subject: [PATCH] :art: Code cleanup --- pkg/chain/chain_constants.go | 26 ++++++++++++------------- pkg/chain/contracts.go | 37 +++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/pkg/chain/chain_constants.go b/pkg/chain/chain_constants.go index 21f9535..213e012 100644 --- a/pkg/chain/chain_constants.go +++ b/pkg/chain/chain_constants.go @@ -15,7 +15,7 @@ type L2ChainIDs struct { BASE_MAINNET uint64 } -var L2_CHAIN_ID = L2ChainIDs{ +var L2_CHAIN_IDS = L2ChainIDs{ OPTIMISM: 10, OPTIMISM_GOERLI: 420, OPTIMISM_SEPOLIA: 11155420, @@ -36,9 +36,9 @@ type L1Contracts struct { l2OutputOracle string } -func GetContractAddresses(chainID uint64) map[string]L1Contracts { +func GetContractAddressesByChainID(chainID uint64) map[string]L1Contracts { CONTRACT_ADDRESSES := map[uint64]map[string]L1Contracts{ - L2_CHAIN_ID.OPTIMISM: { + L2_CHAIN_IDS.OPTIMISM: { "l1": { l1CrossDomainMessenger: "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318", stateCommitmentChain: "0xBe5dAb4A2e9cd0F27300dB4aB94BeE3A233AEB19", @@ -46,7 +46,7 @@ func GetContractAddresses(chainID uint64) map[string]L1Contracts { l2OutputOracle: "0xdfe97868233d1aa22e815a266982f2cf17685a27", }, }, - L2_CHAIN_ID.OPTIMISM_GOERLI: { + L2_CHAIN_IDS.OPTIMISM_GOERLI: { "l1": { l1CrossDomainMessenger: "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318", stateCommitmentChain: "0x9c945aC97Baf48cB784AbBB61399beB71aF7A378", @@ -54,7 +54,7 @@ func GetContractAddresses(chainID uint64) map[string]L1Contracts { l2OutputOracle: "0xE6Dfba0953616Bacab0c9A8ecb3a9BBa77FC15c0", }, }, - L2_CHAIN_ID.OPTIMISM_SEPOLIA: { + L2_CHAIN_IDS.OPTIMISM_SEPOLIA: { "l1": { l1CrossDomainMessenger: "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318", stateCommitmentChain: "0x0000000000000000000000000000000000000000", @@ -62,7 +62,7 @@ func GetContractAddresses(chainID uint64) map[string]L1Contracts { l2OutputOracle: "0x90E9c4f8a994a250F6aEfd61CAFb4F2e895D458F", }, }, - L2_CHAIN_ID.OPTIMISM_HARDHAT_LOCAL: { + L2_CHAIN_IDS.OPTIMISM_HARDHAT_LOCAL: { "l1": { l1CrossDomainMessenger: "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318", stateCommitmentChain: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9", @@ -70,7 +70,7 @@ func GetContractAddresses(chainID uint64) map[string]L1Contracts { l2OutputOracle: "0x0000000000000000000000000000000000000000", }, }, - L2_CHAIN_ID.OPTIMISM_HARDHAT_DEVNET: { + L2_CHAIN_IDS.OPTIMISM_HARDHAT_DEVNET: { "l1": { l1CrossDomainMessenger: "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318", stateCommitmentChain: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9", @@ -78,7 +78,7 @@ func GetContractAddresses(chainID uint64) map[string]L1Contracts { l2OutputOracle: "0x0000000000000000000000000000000000000000", }, }, - L2_CHAIN_ID.OPTIMISM_BEDROCK_ALPHA_TESTNET: { + L2_CHAIN_IDS.OPTIMISM_BEDROCK_ALPHA_TESTNET: { "l1": { l1CrossDomainMessenger: "0x838a6DC4E37CA45D4Ef05bb776bf05eEf50798De", stateCommitmentChain: "0x0000000000000000000000000000000000000000", @@ -86,7 +86,7 @@ func GetContractAddresses(chainID uint64) map[string]L1Contracts { l2OutputOracle: "0x3A234299a14De50027eA65dCdf1c0DaC729e04A6", }, }, - L2_CHAIN_ID.BASE_GOERLI: { + L2_CHAIN_IDS.BASE_GOERLI: { "l1": { l1CrossDomainMessenger: "0x8e5693140eA606bcEB98761d9beB1BC87383706D", stateCommitmentChain: "0x0000000000000000000000000000000000000000", @@ -94,7 +94,7 @@ func GetContractAddresses(chainID uint64) map[string]L1Contracts { l2OutputOracle: "0x2A35891ff30313CcFa6CE88dcf3858bb075A2298", }, }, - L2_CHAIN_ID.BASE_SEPOLIA: { + L2_CHAIN_IDS.BASE_SEPOLIA: { "l1": { l1CrossDomainMessenger: "0xC34855F4De64F1840e5686e64278da901e261f20", stateCommitmentChain: "0x0000000000000000000000000000000000000000", @@ -102,7 +102,7 @@ func GetContractAddresses(chainID uint64) map[string]L1Contracts { l2OutputOracle: "0x84457ca9D0163FbC4bbfe4Dfbb20ba46e48DF254", }, }, - L2_CHAIN_ID.BASE_MAINNET: { + L2_CHAIN_IDS.BASE_MAINNET: { "l1": { l1CrossDomainMessenger: "0x866E82a600A1414e583f7F13623F1aC5d58b0Afa", stateCommitmentChain: "0x0000000000000000000000000000000000000000", @@ -110,7 +110,7 @@ func GetContractAddresses(chainID uint64) map[string]L1Contracts { l2OutputOracle: "0x56315b90c40730925ec5485cf004d835058518A0", }, }, - L2_CHAIN_ID.ZORA_GOERLI: { + L2_CHAIN_IDS.ZORA_GOERLI: { "l1": { l1CrossDomainMessenger: "0xD87342e16352D33170557A7dA1e5fB966a60FafC", stateCommitmentChain: "0x0000000000000000000000000000000000000000", @@ -118,7 +118,7 @@ func GetContractAddresses(chainID uint64) map[string]L1Contracts { l2OutputOracle: "0xdD292C9eEd00f6A32Ff5245d0BCd7f2a15f24e00", }, }, - L2_CHAIN_ID.ZORA_MAINNET: { + L2_CHAIN_IDS.ZORA_MAINNET: { "l1": { l1CrossDomainMessenger: "0xdC40a14d9abd6F410226f1E6de71aE03441ca506", stateCommitmentChain: "0x0000000000000000000000000000000000000000", diff --git a/pkg/chain/contracts.go b/pkg/chain/contracts.go index d312135..c1dbd8e 100644 --- a/pkg/chain/contracts.go +++ b/pkg/chain/contracts.go @@ -11,13 +11,24 @@ import ( "github.com/ethereum-optimism/optimism/op-bindings/bindings" ) +type OracleContract struct { + contractInstance *bindings.L2OutputOracle + log log.Logger +} + +func SetContractInstance(contractInstance *bindings.L2OutputOracle, log log.Logger) (*OracleContract, error) { + return &OracleContract{ + contractInstance: contractInstance, + log: log, + }, nil +} + func GetL1OracleContractAddressByChainID(chainID uint64) string { - ContractAddresses := GetContractAddresses(chainID) + ContractAddresses := GetContractAddressesByChainID(chainID) address := ContractAddresses["l1"].l2OutputOracle return address } -// TODO: Create oracle Struct with required functions func OracleContractInstance(client *ethclient.Client, chainID uint64, log log.Logger) (*bindings.L2OutputOracle, error) { oracleContractAddress := GetL1OracleContractAddressByChainID(chainID) @@ -30,38 +41,38 @@ func OracleContractInstance(client *ethclient.Client, chainID uint64, log log.Lo return contract, nil } -// TODO: Use EthClientInterface -func CreateContractInstance(url string, chainID uint64, logger log.Logger) *bindings.L2OutputOracle { +// CreateContractInstance return [OracleContract] with contract instance. +func CreateContractInstance(url string, chainID uint64, logger log.Logger) (*OracleContract, error) { client, err := ethclient.Dial(url) if err != nil { logger.Errorf("Error occurred while connecting %w", err) } - contract, err := OracleContractInstance(client, chainID, logger) + contractInstance, err := OracleContractInstance(client, chainID, logger) if err != nil { - logger.Errorf("Error occurred while creating contract instance %w", err) + return nil, err } - return contract + return SetContractInstance(contractInstance, logger) } -func GetNextOutputIndex(contractInstance *bindings.L2OutputOracle, log log.Logger) *big.Int { - nextOutputIndex, err := contractInstance.NextOutputIndex(&bind.CallOpts{}) +func (oc *OracleContract) GetNextOutputIndex() *big.Int { + nextOutputIndex, err := oc.contractInstance.NextOutputIndex(&bind.CallOpts{}) if err != nil { - log.Errorf("Error occurred while retrieving next output index %w", err) + oc.log.Errorf("Error occurred while retrieving next output index %w", err) } return nextOutputIndex } -func GetL2Output(contractInstance *bindings.L2OutputOracle, index *big.Int, log log.Logger) bindings.TypesOutputProposal { - l2Output, err := contractInstance.GetL2Output(&bind.CallOpts{}, index) +func (oc *OracleContract) GetL2Output(index *big.Int) bindings.TypesOutputProposal { + l2Output, err := oc.contractInstance.GetL2Output(&bind.CallOpts{}, index) if err != nil { - log.Errorf("Error occurred while retrieving L2 outout %w", err) + oc.log.Errorf("Error occurred while retrieving L2 outout %w", err) } return l2Output