Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
palango committed Nov 28, 2024
1 parent cb9442b commit 3be79f3
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 19 deletions.
12 changes: 7 additions & 5 deletions contracts/addresses/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
30 changes: 23 additions & 7 deletions contracts/fee_currencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
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)
exchangeRates, err := contracts.GetExchangeRates(&backend, chain.chainConfig.ChainID)
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)
feeCurrencyContext, err := contracts.GetFeeCurrencyContext(caller, config.ChainID)
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)
feeCurrencyContext, err := contracts.GetFeeCurrencyContext(caller, config.ChainID)
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)
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)
}
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)
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)
}
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)
er, err := contracts.GetExchangeRates(cb, cb.ChainConfig.ChainID)
if err != nil {
return nil, fmt.Errorf("retrieve exchange rates from current state: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/celoapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 3be79f3

Please sign in to comment.