Skip to content

Commit

Permalink
👌 Applied suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Jan 22, 2024
1 parent 1e645ca commit 3020d21
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 95 deletions.
156 changes: 77 additions & 79 deletions pkg/chain/chain_constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package chain

import "fmt"

// L2ChainIDs manages L2 network chainIDs.
type L2ChainIDs struct {
optimism uint64
Expand Down Expand Up @@ -48,87 +46,87 @@ type Contracts struct {
networkType NetworkType
}

// GetContractAddressesByChainID returns contract addresses by chainID.
func GetContractAddressesByChainID(chainID uint64) (Contracts, error) {
contractAddresses := map[uint64]Contracts{
l2NetworkChainIDs.optimism: {
stateCommitmentChain: "0xBe5dAb4A2e9cd0F27300dB4aB94BeE3A233AEB19",
optimismPortal: "0xbEb5Fc579115071764c7423A4f12eDde41f106Ed",
l2OutputOracle: "0xdfe97868233d1aa22e815a266982f2cf17685a27",
networkType: L1,
},
l2NetworkChainIDs.optimismGoerli: {
stateCommitmentChain: "0x9c945aC97Baf48cB784AbBB61399beB71aF7A378",
optimismPortal: "0x5b47E1A08Ea6d985D6649300584e6722Ec4B1383",
l2OutputOracle: "0xE6Dfba0953616Bacab0c9A8ecb3a9BBa77FC15c0",
networkType: L1,
},
l2NetworkChainIDs.optimismSepolia: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x16Fc5058F25648194471939df75CF27A2fdC48BC",
l2OutputOracle: "0x90E9c4f8a994a250F6aEfd61CAFb4F2e895D458F",
networkType: L1,
},
l2NetworkChainIDs.optimismHardhatLocal: {
stateCommitmentChain: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
optimismPortal: "0x0000000000000000000000000000000000000000",
l2OutputOracle: "0x0000000000000000000000000000000000000000",
networkType: L1,
},
l2NetworkChainIDs.optimismHardhatDevnet: {
stateCommitmentChain: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
optimismPortal: "0x0000000000000000000000000000000000000000",
l2OutputOracle: "0x0000000000000000000000000000000000000000",
networkType: L1,
},
l2NetworkChainIDs.optimismBedrockAlphaTestnet: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0xA581Ca3353DB73115C4625FFC7aDF5dB379434A8",
l2OutputOracle: "0x3A234299a14De50027eA65dCdf1c0DaC729e04A6",
networkType: L1,
},
l2NetworkChainIDs.baseGoerli: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0xe93c8cD0D409341205A592f8c4Ac1A5fe5585cfA",
l2OutputOracle: "0x2A35891ff30313CcFa6CE88dcf3858bb075A2298",
networkType: L1,
},
l2NetworkChainIDs.baseSepolia: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x49f53e41452C74589E85cA1677426Ba426459e85",
l2OutputOracle: "0x84457ca9D0163FbC4bbfe4Dfbb20ba46e48DF254",
networkType: L1,
},
l2NetworkChainIDs.baseMainnet: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e",
l2OutputOracle: "0x56315b90c40730925ec5485cf004d835058518A0",
networkType: L1,
},
l2NetworkChainIDs.zoraGoerli: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0xDb9F51790365e7dc196e7D072728df39Be958ACe",
l2OutputOracle: "0xdD292C9eEd00f6A32Ff5245d0BCd7f2a15f24e00",
networkType: L1,
},
l2NetworkChainIDs.zoraMainnet: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x1a0ad011913A150f69f6A19DF447A0CfD9551054",
l2OutputOracle: "0x9E6204F750cD866b299594e2aC9eA824E2e5f95c",
networkType: L1,
},
l2NetworkChainIDs.liskSepolia: {
optimismPortal: "0xe3d90F21490686Ec7eF37BE788E02dfC12787264",
l2OutputOracle: "0xA0E35F56C318DE1bD5D9ca6A94Fe7e37C5663348",
networkType: L1,
},
}
var contractAddresses = map[uint64]Contracts{
l2NetworkChainIDs.optimism: {
stateCommitmentChain: "0xBe5dAb4A2e9cd0F27300dB4aB94BeE3A233AEB19",
optimismPortal: "0xbEb5Fc579115071764c7423A4f12eDde41f106Ed",
l2OutputOracle: "0xdfe97868233d1aa22e815a266982f2cf17685a27",
networkType: L1,
},
l2NetworkChainIDs.optimismGoerli: {
stateCommitmentChain: "0x9c945aC97Baf48cB784AbBB61399beB71aF7A378",
optimismPortal: "0x5b47E1A08Ea6d985D6649300584e6722Ec4B1383",
l2OutputOracle: "0xE6Dfba0953616Bacab0c9A8ecb3a9BBa77FC15c0",
networkType: L1,
},
l2NetworkChainIDs.optimismSepolia: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x16Fc5058F25648194471939df75CF27A2fdC48BC",
l2OutputOracle: "0x90E9c4f8a994a250F6aEfd61CAFb4F2e895D458F",
networkType: L1,
},
l2NetworkChainIDs.optimismHardhatLocal: {
stateCommitmentChain: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
optimismPortal: "0x0000000000000000000000000000000000000000",
l2OutputOracle: "0x0000000000000000000000000000000000000000",
networkType: L1,
},
l2NetworkChainIDs.optimismHardhatDevnet: {
stateCommitmentChain: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
optimismPortal: "0x0000000000000000000000000000000000000000",
l2OutputOracle: "0x0000000000000000000000000000000000000000",
networkType: L1,
},
l2NetworkChainIDs.optimismBedrockAlphaTestnet: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0xA581Ca3353DB73115C4625FFC7aDF5dB379434A8",
l2OutputOracle: "0x3A234299a14De50027eA65dCdf1c0DaC729e04A6",
networkType: L1,
},
l2NetworkChainIDs.baseGoerli: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0xe93c8cD0D409341205A592f8c4Ac1A5fe5585cfA",
l2OutputOracle: "0x2A35891ff30313CcFa6CE88dcf3858bb075A2298",
networkType: L1,
},
l2NetworkChainIDs.baseSepolia: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x49f53e41452C74589E85cA1677426Ba426459e85",
l2OutputOracle: "0x84457ca9D0163FbC4bbfe4Dfbb20ba46e48DF254",
networkType: L1,
},
l2NetworkChainIDs.baseMainnet: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e",
l2OutputOracle: "0x56315b90c40730925ec5485cf004d835058518A0",
networkType: L1,
},
l2NetworkChainIDs.zoraGoerli: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0xDb9F51790365e7dc196e7D072728df39Be958ACe",
l2OutputOracle: "0xdD292C9eEd00f6A32Ff5245d0BCd7f2a15f24e00",
networkType: L1,
},
l2NetworkChainIDs.zoraMainnet: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x1a0ad011913A150f69f6A19DF447A0CfD9551054",
l2OutputOracle: "0x9E6204F750cD866b299594e2aC9eA824E2e5f95c",
networkType: L1,
},
l2NetworkChainIDs.liskSepolia: {
optimismPortal: "0xe3d90F21490686Ec7eF37BE788E02dfC12787264",
l2OutputOracle: "0xA0E35F56C318DE1bD5D9ca6A94Fe7e37C5663348",
networkType: L1,
},
}

// GetContractAddressesByChainID returns contract addresses by network chainID.
func GetContractAddressesByChainID(chainID uint64) (Contracts, bool) {
filteredContracts := contractAddresses[chainID]

if len(filteredContracts.l2OutputOracle) == 0 {
return filteredContracts, fmt.Errorf("contract information is unavailable for the chain %v", chainID)
return filteredContracts, false
}

return filteredContracts, nil
return filteredContracts, true
}
8 changes: 4 additions & 4 deletions pkg/chain/chain_constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ func TestGetContractAddressesByChainID(t *testing.T) {
assert := assert.New(t)

const availableChainID uint64 = 10
contractAddresses, err := GetContractAddressesByChainID(availableChainID)
contractAddresses, areAddressesAvailable := GetContractAddressesByChainID(availableChainID)
contractAddressesExpected := Contracts{
stateCommitmentChain: "0xBe5dAb4A2e9cd0F27300dB4aB94BeE3A233AEB19",
optimismPortal: "0xbEb5Fc579115071764c7423A4f12eDde41f106Ed",
l2OutputOracle: "0xdfe97868233d1aa22e815a266982f2cf17685a27",
networkType: "L1",
}
assert.NoError(err)
assert.Equal(true, areAddressesAvailable)
assert.Equal(contractAddressesExpected, contractAddresses)

const unavailableChainID uint64 = 5
contractAddresses, err = GetContractAddressesByChainID(unavailableChainID)
assert.Error(err)
contractAddresses, areAddressesAvailable = GetContractAddressesByChainID(unavailableChainID)
assert.Equal(false, areAddressesAvailable)
assert.Equal(0, len(contractAddresses.l2OutputOracle))
}
16 changes: 8 additions & 8 deletions pkg/chain/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ type ConfigOptions struct {
L2OutputOracleContractAddress string
}

func getL1OracleContractAddressByChainID(chainID uint64) (string, error) {
cAddr, err := GetContractAddressesByChainID(chainID)
if err != nil {
return "", err
func getL1OracleContractAddressByChainID(chainID uint64) (string, bool) {
cAddr, areAddressesAvailable := GetContractAddressesByChainID(chainID)
if !areAddressesAvailable {
return "", false
}

return cAddr.l2OutputOracle, nil
return cAddr.l2OutputOracle, true
}

// NewOracleContract returns [OracleAccessor] with contract instance.
func NewOracleContract(ctx context.Context, opts ConfigOptions) (*OracleAccessor, error) {
func NewOracleContract(ctx context.Context, opts *ConfigOptions) (*OracleAccessor, error) {
client, err := ethclient.DialContext(ctx, opts.L1RPCEndpoint)
if err != nil {
return nil, err
}

oracleContractAddress, err := getL1OracleContractAddressByChainID(opts.ChainID)
oracleContractAddress, isAddressExists := getL1OracleContractAddressByChainID(opts.ChainID)

// Verify if oracle contract address is available in the chain constants
// If not available, use l2OutputContractAddress from the config options
if err != nil {
if !isAddressExists {
if len(opts.L2OutputOracleContractAddress) > 0 {
oracleContractAddress = opts.L2OutputOracleContractAddress
} else {
Expand Down
8 changes: 4 additions & 4 deletions pkg/chain/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ func TestGetL1OracleContractAddressByChainID(t *testing.T) {

const availableChainID uint64 = 10
oracleContractAddressExpected := "0xdfe97868233d1aa22e815a266982f2cf17685a27"
contractAddress, err := getL1OracleContractAddressByChainID(availableChainID)
assert.NoError(err)
contractAddress, isAddressExists := getL1OracleContractAddressByChainID(availableChainID)
assert.Equal(true, isAddressExists)
assert.Equal(oracleContractAddressExpected, contractAddress)

const unavailableChainID uint64 = 5
contractAddress, err = getL1OracleContractAddressByChainID(unavailableChainID)
assert.Error(err)
contractAddress, isAddressExists = getL1OracleContractAddressByChainID(unavailableChainID)
assert.Equal(false, isAddressExists)
assert.Equal(0, len(contractAddress))
}

0 comments on commit 3020d21

Please sign in to comment.