Skip to content

Commit

Permalink
Merge pull request #9 from FastLane-Labs/atlas-multi-version
Browse files Browse the repository at this point in the history
feat: refactor to handle multiple atlas versions
  • Loading branch information
jj1980a authored Dec 6, 2024
2 parents 692a4ab + 432f4dc commit ce9b1fb
Show file tree
Hide file tree
Showing 49 changed files with 21,204 additions and 322 deletions.
24 changes: 20 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@ test:
$(GOTEST) --timeout 5m $(TESTALL)

contracts-bindings:
abigen --abi ./contract/atlas/abi.json --pkg atlas --type Atlas --out ./contract/atlas/atlas.go
abigen --abi ./contract/atlasverification/abi.json --pkg atlasverification --type AtlasVerification --out ./contract/atlasverification/atlasverification.go
abigen --abi ./contract/atlas/1.0/abi.json --pkg atlas_1_0 --type Atlas --out ./contract/atlas/1.0/atlas.go
abigen --abi ./contract/atlas/1.1/abi.json --pkg atlas_1_1 --type Atlas --out ./contract/atlas/1.1/atlas.go
abigen --abi ./contract/atlas/1.2/abi.json --pkg atlas_1_2 --type Atlas --out ./contract/atlas/1.2/atlas.go
abigen --abi ./contract/atlas/1.3/abi.json --pkg atlas_1_3 --type Atlas --out ./contract/atlas/1.3/atlas.go

abigen --abi ./contract/atlasverification/1.0/abi.json --pkg atlasverification_1_0 --type AtlasVerification --out ./contract/atlasverification/1.0/atlasverification.go
abigen --abi ./contract/atlasverification/1.1/abi.json --pkg atlasverification_1_1 --type AtlasVerification --out ./contract/atlasverification/1.1/atlasverification.go
abigen --abi ./contract/atlasverification/1.2/abi.json --pkg atlasverification_1_2 --type AtlasVerification --out ./contract/atlasverification/1.2/atlasverification.go
abigen --abi ./contract/atlasverification/1.3/abi.json --pkg atlasverification_1_3 --type AtlasVerification --out ./contract/atlasverification/1.3/atlasverification.go

abigen --abi ./contract/simulator/1.0/abi.json --pkg simulator_1_0 --type Simulator --out ./contract/simulator/1.0/simulator.go
abigen --abi ./contract/simulator/1.1/abi.json --pkg simulator_1_1 --type Simulator --out ./contract/simulator/1.1/simulator.go
abigen --abi ./contract/simulator/1.2/abi.json --pkg simulator_1_2 --type Simulator --out ./contract/simulator/1.2/simulator.go
abigen --abi ./contract/simulator/1.3/abi.json --pkg simulator_1_3 --type Simulator --out ./contract/simulator/1.3/simulator.go

abigen --abi ./contract/sorter/1.0/abi.json --pkg sorter_1_0 --type Sorter --out ./contract/sorter/1.0/sorter.go
abigen --abi ./contract/sorter/1.1/abi.json --pkg sorter_1_1 --type Sorter --out ./contract/sorter/1.1/sorter.go
abigen --abi ./contract/sorter/1.2/abi.json --pkg sorter_1_2 --type Sorter --out ./contract/sorter/1.2/sorter.go
abigen --abi ./contract/sorter/1.3/abi.json --pkg sorter_1_3 --type Sorter --out ./contract/sorter/1.3/sorter.go

abigen --abi ./contract/dappcontrol/abi.json --pkg dappcontrol --type DAppControl --out ./contract/dappcontrol/dappcontrol.go
abigen --abi ./contract/simulator/abi.json --pkg simulator --type Simulator --out ./contract/simulator/simulator.go
abigen --abi ./contract/sorter/abi.json --pkg sorter --type Sorter --out ./contract/sorter/sorter.go
71 changes: 52 additions & 19 deletions config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ type ChainConfig struct {
Eip712Domain *apitypes.TypedDataDomain `json:"eip712Domain"`
}

const (
AtlasV_1_0 = "1.0"
AtlasV_1_1 = "1.1"
AtlasV_1_2 = "1.2"
AtlasV_1_3 = "1.3"
AtlasVLatest = AtlasV_1_3
)

var (
DEFAULT_MULTICALL3 = common.HexToAddress("0xcA11bde05977b3631167028862bE2a173976CA11")
chainConfig = map[uint64]*ChainConfig{}
chainConfig = map[uint64]map[string]*ChainConfig{} // Indexed by [chainId][atlasVersion]
initOnce sync.Once
mu sync.RWMutex

allVersions = []string{AtlasV_1_0, AtlasV_1_1, AtlasV_1_2, AtlasV_1_3}
)

func initChainConfig() error {
Expand Down Expand Up @@ -60,22 +70,39 @@ func initChainConfig() error {
return initErr
}

func GetChainConfig(chainId uint64) (*ChainConfig, error) {
func GetAllVersions() []string {
v := make([]string, len(allVersions))
copy(v, allVersions)
return v
}

func GetVersion(version *string) string {
if version == nil {
return AtlasVLatest
}

return *version
}

func GetChainConfig(chainId uint64, version *string) (*ChainConfig, error) {
if err := initChainConfig(); err != nil {
return nil, err
}

v := GetVersion(version)

mu.RLock()
defer mu.RUnlock()

if chainConfig[chainId] == nil {
return nil, fmt.Errorf("chain config not found for chain id %d", chainId)
_chainConfig, ok := chainConfig[chainId][v]
if !ok {
return nil, fmt.Errorf("chain config not found for chain id %d and version %s", chainId, v)
}

return chainConfig[chainId], nil
return _chainConfig, nil
}

func OverrideChainConfig(chainId uint64, config *ChainConfig) error {
func OverrideChainConfig(chainId uint64, version *string, config *ChainConfig) error {
if config.Contract == nil {
return errors.New("contract config is required")
}
Expand Down Expand Up @@ -120,60 +147,66 @@ func OverrideChainConfig(chainId uint64, config *ChainConfig) error {
return errors.New("eip712 domain verifying contract is invalid")
}

v := GetVersion(version)

mu.Lock()
defer mu.Unlock()

chainConfig[chainId] = config
if chainConfig[chainId] == nil {
chainConfig[chainId] = make(map[string]*ChainConfig)
}

chainConfig[chainId][v] = config
return nil
}

func GetAtlasAddress(chainId uint64) (common.Address, error) {
chainConf, err := GetChainConfig(chainId)
func GetAtlasAddress(chainId uint64, version *string) (common.Address, error) {
chainConf, err := GetChainConfig(chainId, version)
if err != nil {
return common.Address{}, err
}

return chainConf.Contract.Atlas, nil
}

func GetAtlasVerificationAddress(chainId uint64) (common.Address, error) {
chainConf, err := GetChainConfig(chainId)
func GetAtlasVerificationAddress(chainId uint64, version *string) (common.Address, error) {
chainConf, err := GetChainConfig(chainId, version)
if err != nil {
return common.Address{}, err
}

return chainConf.Contract.AtlasVerification, nil
}

func GetSorterAddress(chainId uint64) (common.Address, error) {
chainConf, err := GetChainConfig(chainId)
func GetSorterAddress(chainId uint64, version *string) (common.Address, error) {
chainConf, err := GetChainConfig(chainId, version)
if err != nil {
return common.Address{}, err
}

return chainConf.Contract.Sorter, nil
}

func GetSimulatorAddress(chainId uint64) (common.Address, error) {
chainConf, err := GetChainConfig(chainId)
func GetSimulatorAddress(chainId uint64, version *string) (common.Address, error) {
chainConf, err := GetChainConfig(chainId, version)
if err != nil {
return common.Address{}, err
}

return chainConf.Contract.Simulator, nil
}

func GetMulticall3Address(chainId uint64) (common.Address, error) {
chainConf, err := GetChainConfig(chainId)
func GetMulticall3Address(chainId uint64, version *string) (common.Address, error) {
chainConf, err := GetChainConfig(chainId, version)
if err != nil {
return common.Address{}, err
}

return chainConf.Contract.Multicall3, nil
}

func GetEip712Domain(chainId uint64) (*apitypes.TypedDataDomain, error) {
chainConf, err := GetChainConfig(chainId)
func GetEip712Domain(chainId uint64, version *string) (*apitypes.TypedDataDomain, error) {
chainConf, err := GetChainConfig(chainId, version)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions config/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

const (
ChainConfigUrl = "https://raw.githubusercontent.com/FastLane-Labs/atlas-config/refs/heads/main/configs/chain-config.json"
ChainConfigUrl = "https://raw.githubusercontent.com/FastLane-Labs/atlas-config/refs/heads/main/configs/chain-configs-multi-version.json"
)

func downloadChainConfig() (map[uint64]*ChainConfig, error) {
func downloadChainConfig() (map[uint64]map[string]*ChainConfig, error) {
resp, err := http.Get(ChainConfigUrl)
if err != nil {
return nil, err
Expand All @@ -23,7 +23,7 @@ func downloadChainConfig() (map[uint64]*ChainConfig, error) {
return nil, err
}

remoteConfig := make(map[uint64]*ChainConfig)
remoteConfig := make(map[uint64]map[string]*ChainConfig)

err = json.Unmarshal(body, &remoteConfig)
if err != nil {
Expand Down
119 changes: 110 additions & 9 deletions contract/abi.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,118 @@
package contract

import (
"github.com/FastLane-Labs/atlas-sdk-go/contract/atlas"
"github.com/FastLane-Labs/atlas-sdk-go/contract/atlasverification"
"errors"

"github.com/FastLane-Labs/atlas-sdk-go/config"
"github.com/ethereum/go-ethereum/accounts/abi"

"github.com/FastLane-Labs/atlas-sdk-go/contract/atlas/1.0"
"github.com/FastLane-Labs/atlas-sdk-go/contract/atlas/1.1"
"github.com/FastLane-Labs/atlas-sdk-go/contract/atlas/1.2"
"github.com/FastLane-Labs/atlas-sdk-go/contract/atlas/1.3"

"github.com/FastLane-Labs/atlas-sdk-go/contract/atlasverification/1.0"
"github.com/FastLane-Labs/atlas-sdk-go/contract/atlasverification/1.1"
"github.com/FastLane-Labs/atlas-sdk-go/contract/atlasverification/1.2"
"github.com/FastLane-Labs/atlas-sdk-go/contract/atlasverification/1.3"

"github.com/FastLane-Labs/atlas-sdk-go/contract/simulator/1.0"
"github.com/FastLane-Labs/atlas-sdk-go/contract/simulator/1.1"
"github.com/FastLane-Labs/atlas-sdk-go/contract/simulator/1.2"
"github.com/FastLane-Labs/atlas-sdk-go/contract/simulator/1.3"

"github.com/FastLane-Labs/atlas-sdk-go/contract/sorter/1.0"
"github.com/FastLane-Labs/atlas-sdk-go/contract/sorter/1.1"
"github.com/FastLane-Labs/atlas-sdk-go/contract/sorter/1.2"
"github.com/FastLane-Labs/atlas-sdk-go/contract/sorter/1.3"

"github.com/FastLane-Labs/atlas-sdk-go/contract/dappcontrol"
"github.com/FastLane-Labs/atlas-sdk-go/contract/simulator"
"github.com/FastLane-Labs/atlas-sdk-go/contract/sorter"
)

var (
AtlasAbi, _ = atlas.AtlasMetaData.GetAbi()
AtlasVerificationAbi, _ = atlasverification.AtlasVerificationMetaData.GetAbi()
SimulatorAbi, _ = simulator.SimulatorMetaData.GetAbi()
SorterAbi, _ = sorter.SorterMetaData.GetAbi()
DAppControlAbi, _ = dappcontrol.DAppControlMetaData.GetAbi()
atlas_1_0_Abi, _ = atlas_1_0.AtlasMetaData.GetAbi()
atlas_1_1_Abi, _ = atlas_1_1.AtlasMetaData.GetAbi()
atlas_1_2_Abi, _ = atlas_1_2.AtlasMetaData.GetAbi()
atlas_1_3_Abi, _ = atlas_1_3.AtlasMetaData.GetAbi()

atlasverification_1_0_Abi, _ = atlasverification_1_0.AtlasVerificationMetaData.GetAbi()
atlasverification_1_1_Abi, _ = atlasverification_1_1.AtlasVerificationMetaData.GetAbi()
atlasverification_1_2_Abi, _ = atlasverification_1_2.AtlasVerificationMetaData.GetAbi()
atlasverification_1_3_Abi, _ = atlasverification_1_3.AtlasVerificationMetaData.GetAbi()

simulator_1_0_Abi, _ = simulator_1_0.SimulatorMetaData.GetAbi()
simulator_1_1_Abi, _ = simulator_1_1.SimulatorMetaData.GetAbi()
simulator_1_2_Abi, _ = simulator_1_2.SimulatorMetaData.GetAbi()
simulator_1_3_Abi, _ = simulator_1_3.SimulatorMetaData.GetAbi()

sorter_1_0_Abi, _ = sorter_1_0.SorterMetaData.GetAbi()
sorter_1_1_Abi, _ = sorter_1_1.SorterMetaData.GetAbi()
sorter_1_2_Abi, _ = sorter_1_2.SorterMetaData.GetAbi()
sorter_1_3_Abi, _ = sorter_1_3.SorterMetaData.GetAbi()

dappcontrol_Abi, _ = dappcontrol.DAppControlMetaData.GetAbi()
)

var (
errInvalidAtlasVersion = errors.New("invalid atlas version")
)

func GetAtlasAbi(version *string) (*abi.ABI, error) {
switch config.GetVersion(version) {
case config.AtlasV_1_0:
return atlas_1_0_Abi, nil
case config.AtlasV_1_1:
return atlas_1_1_Abi, nil
case config.AtlasV_1_2:
return atlas_1_2_Abi, nil
case config.AtlasV_1_3:
return atlas_1_3_Abi, nil
}
return nil, errInvalidAtlasVersion
}

func GetAtlasVerificationAbi(version *string) (*abi.ABI, error) {
switch config.GetVersion(version) {
case config.AtlasV_1_0:
return atlasverification_1_0_Abi, nil
case config.AtlasV_1_1:
return atlasverification_1_1_Abi, nil
case config.AtlasV_1_2:
return atlasverification_1_2_Abi, nil
case config.AtlasV_1_3:
return atlasverification_1_3_Abi, nil
}
return nil, errInvalidAtlasVersion
}

func GetSimulatorAbi(version *string) (*abi.ABI, error) {
switch config.GetVersion(version) {
case config.AtlasV_1_0:
return simulator_1_0_Abi, nil
case config.AtlasV_1_1:
return simulator_1_1_Abi, nil
case config.AtlasV_1_2:
return simulator_1_2_Abi, nil
case config.AtlasV_1_3:
return simulator_1_3_Abi, nil
}
return nil, errInvalidAtlasVersion
}

func GetSorterAbi(version *string) (*abi.ABI, error) {
switch config.GetVersion(version) {
case config.AtlasV_1_0:
return sorter_1_0_Abi, nil
case config.AtlasV_1_1:
return sorter_1_1_Abi, nil
case config.AtlasV_1_2:
return sorter_1_2_Abi, nil
case config.AtlasV_1_3:
return sorter_1_3_Abi, nil
}
return nil, errInvalidAtlasVersion
}

func GetDAppControlAbi() *abi.ABI {
return dappcontrol_Abi
}
2 changes: 1 addition & 1 deletion contract/atlas/abi.json → contract/atlas/1.0/abi.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions contract/atlas/atlas.go → contract/atlas/1.0/atlas.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions contract/atlas/1.1/abi.json

Large diffs are not rendered by default.

Loading

0 comments on commit ce9b1fb

Please sign in to comment.