diff --git a/contracts/addresses/addresses.go b/contracts/addresses/addresses.go index 500054bc70..e56dee0ac8 100644 --- a/contracts/addresses/addresses.go +++ b/contracts/addresses/addresses.go @@ -5,11 +5,13 @@ import "github.com/ethereum/go-ethereum/common" var ( CeloTokenAddress = common.HexToAddress("0x471ece3750da237f93b8e339c536989b8978a438") FeeHandlerAddress = common.HexToAddress("0xcd437749e43a154c07f3553504c68fbfd56b8778") - FeeCurrencyDirectoryAddress = common.HexToAddress("0x9212Fb72ae65367A7c887eC4Ad9bE310BAC611BF") + FeeCurrencyDirectoryAddress = common.HexToAddress("0x9212Fb72ae65367A7c887eC4Ad9bE310BAC611BF") // TODO - CeloTokenAlfajoresAddress = common.HexToAddress("0xF194afDf50B03e69Bd7D057c1Aa9e10c9954E4C9") - FeeHandlerAlfajoresAddress = common.HexToAddress("0xEAaFf71AB67B5d0eF34ba62Ea06Ac3d3E2dAAA38") + CeloTokenAlfajoresAddress = common.HexToAddress("0xF194afDf50B03e69Bd7D057c1Aa9e10c9954E4C9") + FeeHandlerAlfajoresAddress = common.HexToAddress("0xEAaFf71AB67B5d0eF34ba62Ea06Ac3d3E2dAAA38") + FeeCurrencyDirectoryAlfajoresAddress = common.HexToAddress("0x9212Fb72ae65367A7c887eC4Ad9bE310BAC611BF") - CeloTokenBaklavaAddress = common.HexToAddress("0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8") - FeeHandlerBaklavaAddress = common.HexToAddress("0xeed0A69c51079114C280f7b936C79e24bD94013e") + CeloTokenBaklavaAddress = common.HexToAddress("0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8") + FeeHandlerBaklavaAddress = common.HexToAddress("0xeed0A69c51079114C280f7b936C79e24bD94013e") + FeeCurrencyDirectoryBaklavaAddress = common.HexToAddress("0x9212Fb72ae65367A7c887eC4Ad9bE310BAC611BF") // TODO ) diff --git a/contracts/fee_currencies.go b/contracts/fee_currencies.go index 23c1b3e19e..6a277c06fe 100644 --- a/contracts/fee_currencies.go +++ b/contracts/fee_currencies.go @@ -15,6 +15,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/params" ) var feeCurrencyABI *abi.ABI @@ -182,17 +183,32 @@ func CreditFees( func GetRegisteredCurrencies(caller *abigen.FeeCurrencyDirectoryCaller) ([]common.Address, error) { currencies, err := caller.GetCurrencies(&bind.CallOpts{}) if err != nil { - return currencies, fmt.Errorf("Failed to get registered tokens: %w", err) + return currencies, fmt.Errorf("failed to get registered tokens: %w", err) } return currencies, nil } +func getDirectoryAddress(chainId *big.Int) common.Address { + if chainId == nil { + return addresses.FeeCurrencyDirectoryAddress + } + + switch chainId.Uint64() { + case params.CeloAlfajoresChainID: + return addresses.FeeCurrencyDirectoryAlfajoresAddress + case params.CeloBaklavaChainID: + return addresses.FeeCurrencyDirectoryBaklavaAddress + default: + return addresses.FeeCurrencyDirectoryAddress + } +} + // GetExchangeRates returns the exchange rates for the provided gas currencies -func GetExchangeRates(caller bind.ContractCaller) (common.ExchangeRates, error) { +func GetExchangeRates(caller bind.ContractCaller, chainId *big.Int) (common.ExchangeRates, error) { exchangeRates := map[common.Address]*big.Rat{} - directory, err := abigen.NewFeeCurrencyDirectoryCaller(addresses.FeeCurrencyDirectoryAddress, caller) + directory, err := abigen.NewFeeCurrencyDirectoryCaller(getDirectoryAddress(chainId), caller) if err != nil { - return exchangeRates, fmt.Errorf("Failed to access FeeCurrencyDirectory: %w", err) + return exchangeRates, fmt.Errorf("failed to access FeeCurrencyDirectory: %w", err) } currencies, err := GetRegisteredCurrencies(directory) if err != nil { @@ -202,11 +218,11 @@ func GetExchangeRates(caller bind.ContractCaller) (common.ExchangeRates, error) } // GetFeeCurrencyContext returns the fee currency block context for all registered gas currencies from CELO -func GetFeeCurrencyContext(caller bind.ContractCaller) (common.FeeCurrencyContext, error) { +func GetFeeCurrencyContext(caller bind.ContractCaller, chainId *big.Int) (common.FeeCurrencyContext, error) { var feeContext common.FeeCurrencyContext - directory, err := abigen.NewFeeCurrencyDirectoryCaller(addresses.FeeCurrencyDirectoryAddress, caller) + directory, err := abigen.NewFeeCurrencyDirectoryCaller(getDirectoryAddress(chainId), caller) if err != nil { - return feeContext, fmt.Errorf("Failed to access FeeCurrencyDirectory: %w", err) + return feeContext, fmt.Errorf("failed to access FeeCurrencyDirectory: %w", err) } currencies, err := GetRegisteredCurrencies(directory) diff --git a/core/blockchain_celo_test.go b/core/blockchain_celo_test.go index ec5c8eadac..5cef08a308 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) + exchangeRates, err := contracts.GetExchangeRates(&backend, chain.chainConfig.ChainID) if err != nil { t.Fatal("could not get exchange rates") } diff --git a/core/celo_evm.go b/core/celo_evm.go index 3858f30aee..1f772a1abd 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) + feeCurrencyContext, err := contracts.GetFeeCurrencyContext(caller, config.ChainID) 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) + feeCurrencyContext, err := contracts.GetFeeCurrencyContext(caller, config.ChainID) 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 7b453a3ca4..021515a686 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) + currencyContext, err := contracts.GetFeeCurrencyContext(pool.celoBackend, pool.chain.Config().ChainID) 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 a6f4da7a2b..37b79a4cf7 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) + feeCurrencyContext, err := contracts.GetFeeCurrencyContext(pool.celoBackend, pool.chainconfig.ChainID) 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 73279b98bf..52ef604a95 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) + er, err := contracts.GetExchangeRates(cb, cb.ChainConfig.ChainID) 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 85e1d3f105..c37aecdf6f 100644 --- a/internal/celoapi/backend.go +++ b/internal/celoapi/backend.go @@ -53,7 +53,7 @@ func (b *CeloAPIBackend) GetExchangeRates(ctx context.Context, blockNumOrHash rp if err != nil { return nil, err } - er, err := contracts.GetExchangeRates(contractBackend) + er, err := contracts.GetExchangeRates(contractBackend, b.ChainConfig().ChainID) if err != nil { return nil, err }