diff --git a/contracts/celo_backend.go b/contracts/celo_backend.go index 86c8cf78ee..cba65666cc 100644 --- a/contracts/celo_backend.go +++ b/contracts/celo_backend.go @@ -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 +} diff --git a/contracts/fee_currencies.go b/contracts/fee_currencies.go index 55d2661f1a..b0e7c14813 100644 --- a/contracts/fee_currencies.go +++ b/contracts/fee_currencies.go @@ -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() @@ -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) } @@ -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) } diff --git a/core/blockchain_celo_test.go b/core/blockchain_celo_test.go index 5cef08a308..ec5c8eadac 100644 --- a/core/blockchain_celo_test.go +++ b/core/blockchain_celo_test.go @@ -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") } diff --git a/core/celo_evm.go b/core/celo_evm.go index 1f772a1abd..3858f30aee 100644 --- a/core/celo_evm.go +++ b/core/celo_evm.go @@ -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) } @@ -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) } diff --git a/core/txpool/blobpool/celo_blobpool.go b/core/txpool/blobpool/celo_blobpool.go index 021515a686..7b453a3ca4 100644 --- a/core/txpool/blobpool/celo_blobpool.go +++ b/core/txpool/blobpool/celo_blobpool.go @@ -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) } diff --git a/core/txpool/legacypool/celo_legacypool.go b/core/txpool/legacypool/celo_legacypool.go index 37b79a4cf7..a6f4da7a2b 100644 --- a/core/txpool/legacypool/celo_legacypool.go +++ b/core/txpool/legacypool/celo_legacypool.go @@ -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) } diff --git a/internal/celoapi/api.go b/internal/celoapi/api.go index 52ef604a95..73279b98bf 100644 --- a/internal/celoapi/api.go +++ b/internal/celoapi/api.go @@ -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) } diff --git a/internal/celoapi/backend.go b/internal/celoapi/backend.go index c37aecdf6f..6125df6865 100644 --- a/internal/celoapi/backend.go +++ b/internal/celoapi/backend.go @@ -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) {