Skip to content

Commit

Permalink
Refactor code tomminimize diff
Browse files Browse the repository at this point in the history
  • Loading branch information
palango committed Nov 29, 2024
1 parent c480dd9 commit 9e320b6
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 12 deletions.
4 changes: 4 additions & 0 deletions contracts/celo_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ func (b *CeloBackend) NewEVM(feeCurrencyContext *common.FeeCurrencyContext) *vm.
vmConfig := vm.Config{}
return vm.NewEVM(blockCtx, txCtx, b.State, b.ChainConfig, vmConfig)
}

func (b *CeloBackend) Config() *params.ChainConfig {
return b.ChainConfig
}
14 changes: 10 additions & 4 deletions contracts/fee_currencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ var feeCurrencyABI *abi.ABI

var ErrFeeCurrencyEVMCall = errors.New("fee-currency contract error during internal EVM call")

type CeloContractCaller interface {
bind.ContractCaller

Config() *params.ChainConfig
}

func init() {
var err error
feeCurrencyABI, err = abigen.FeeCurrencyMetaData.GetAbi()
Expand Down Expand Up @@ -204,8 +210,8 @@ func GetRegisteredCurrencies(caller *abigen.FeeCurrencyDirectoryCaller) ([]commo
}

// GetExchangeRates returns the exchange rates for the provided gas currencies
func GetExchangeRates(caller bind.ContractCaller, chainId *big.Int) (common.ExchangeRates, error) {
directory, err := abigen.NewFeeCurrencyDirectoryCaller(getDirectoryAddress(chainId), caller)
func GetExchangeRates(caller CeloContractCaller) (common.ExchangeRates, error) {
directory, err := abigen.NewFeeCurrencyDirectoryCaller(getDirectoryAddress(caller.Config().ChainID), caller)
if err != nil {
return common.ExchangeRates{}, fmt.Errorf("failed to access FeeCurrencyDirectory: %w", err)
}
Expand All @@ -217,9 +223,9 @@ func GetExchangeRates(caller bind.ContractCaller, chainId *big.Int) (common.Exch
}

// GetFeeCurrencyContext returns the fee currency block context for all registered gas currencies from CELO
func GetFeeCurrencyContext(caller bind.ContractCaller, chainId *big.Int) (common.FeeCurrencyContext, error) {
func GetFeeCurrencyContext(caller CeloContractCaller) (common.FeeCurrencyContext, error) {
var feeContext common.FeeCurrencyContext
directory, err := abigen.NewFeeCurrencyDirectoryCaller(getDirectoryAddress(chainId), caller)
directory, err := abigen.NewFeeCurrencyDirectoryCaller(getDirectoryAddress(caller.Config().ChainID), caller)
if err != nil {
return feeContext, fmt.Errorf("failed to access FeeCurrencyDirectory: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_celo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func testNativeTransferWithFeeCurrency(t *testing.T, scheme string, feeCurrencyA
ChainConfig: chain.chainConfig,
State: state,
}
exchangeRates, err := contracts.GetExchangeRates(&backend, chain.chainConfig.ChainID)
exchangeRates, err := contracts.GetExchangeRates(&backend)
if err != nil {
t.Fatal("could not get exchange rates")
}
Expand Down
4 changes: 2 additions & 2 deletions core/celo_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GetFeeCurrencyContext(header *types.Header, config *params.ChainConfig, sta

caller := &contracts.CeloBackend{ChainConfig: config, State: statedb}

feeCurrencyContext, err := contracts.GetFeeCurrencyContext(caller, config.ChainID)
feeCurrencyContext, err := contracts.GetFeeCurrencyContext(caller)
if err != nil {
log.Error("Error fetching exchange rates!", "err", err)
}
Expand All @@ -30,7 +30,7 @@ func GetExchangeRates(header *types.Header, config *params.ChainConfig, statedb

caller := &contracts.CeloBackend{ChainConfig: config, State: statedb}

feeCurrencyContext, err := contracts.GetFeeCurrencyContext(caller, config.ChainID)
feeCurrencyContext, err := contracts.GetFeeCurrencyContext(caller)
if err != nil {
log.Error("Error fetching exchange rates!", "err", err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/txpool/blobpool/celo_blobpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (pool *BlobPool) recreateCeloProperties() {
ChainConfig: pool.chain.Config(),
State: pool.state,
}
currencyContext, err := contracts.GetFeeCurrencyContext(pool.celoBackend, pool.chain.Config().ChainID)
currencyContext, err := contracts.GetFeeCurrencyContext(pool.celoBackend)
if err != nil {
log.Error("Error trying to get fee currency context in txpool.", "cause", err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/txpool/legacypool/celo_legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (pool *LegacyPool) recreateCeloProperties() {
ChainConfig: pool.chainconfig,
State: pool.currentState,
}
feeCurrencyContext, err := contracts.GetFeeCurrencyContext(pool.celoBackend, pool.chainconfig.ChainID)
feeCurrencyContext, err := contracts.GetFeeCurrencyContext(pool.celoBackend)
if err != nil {
log.Error("Error trying to get fee currency context in txpool.", "cause", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/celoapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *CeloAPI) convertCeloToCurrency(nativePrice *big.Int, feeCurrency *commo
if err != nil {
return nil, err
}
er, err := contracts.GetExchangeRates(cb, cb.ChainConfig.ChainID)
er, err := contracts.GetExchangeRates(cb)
if err != nil {
return nil, fmt.Errorf("retrieve exchange rates from current state: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/celoapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func (b *CeloAPIBackend) GetExchangeRates(ctx context.Context, blockNumOrHash rp
if err != nil {
return nil, err
}
er, err := contracts.GetExchangeRates(contractBackend, b.ChainConfig().ChainID)
exchangeRates, err := contracts.GetExchangeRates(contractBackend)
if err != nil {
return nil, err
}
return er, nil
return exchangeRates, nil
}

func (b *CeloAPIBackend) ConvertToCurrency(ctx context.Context, blockNumOrHash rpc.BlockNumberOrHash, celoAmount *big.Int, toFeeCurrency *common.Address) (*big.Int, error) {
Expand Down

0 comments on commit 9e320b6

Please sign in to comment.