Skip to content

Commit

Permalink
Chain dependent FeeCurrencyDirectory address (#284)
Browse files Browse the repository at this point in the history
* Select fee currency directory based on chainid

* Drive by changes

- Fix some capitalized error messages
- Remove variable
- Rename a variable

* Refactor code to minimize diff

* Review changes

* Improve naming
  • Loading branch information
palango authored Nov 29, 2024
1 parent cb9442b commit a102e49
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 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("0xD59E1599F45e42Eb356202B2C714D6C7b734C034")
)
33 changes: 25 additions & 8 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 All @@ -29,6 +30,23 @@ func init() {
}
}

// Returns the address of the FeeCurrencyDirectory contract for the given chainId
func getFeeCurrencyDirectoryAddress(chainId *big.Int) common.Address {
// ChainId can be uninitialized in some tests
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
}
}

// Returns nil if debit is possible, used in tx pool validation
func TryDebitFees(tx *types.Transaction, from common.Address, backend *CeloBackend, feeContext common.FeeCurrencyContext) error {
amount := new(big.Int).SetUint64(tx.Gas())
Expand Down Expand Up @@ -182,17 +200,16 @@ 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
}

// GetExchangeRates returns the exchange rates for the provided gas currencies
func GetExchangeRates(caller bind.ContractCaller) (common.ExchangeRates, error) {
exchangeRates := map[common.Address]*big.Rat{}
directory, err := abigen.NewFeeCurrencyDirectoryCaller(addresses.FeeCurrencyDirectoryAddress, caller)
func GetExchangeRates(caller *CeloBackend) (common.ExchangeRates, error) {
directory, err := abigen.NewFeeCurrencyDirectoryCaller(getFeeCurrencyDirectoryAddress(caller.ChainConfig.ChainID), caller)
if err != nil {
return exchangeRates, fmt.Errorf("Failed to access FeeCurrencyDirectory: %w", err)
return common.ExchangeRates{}, fmt.Errorf("failed to access FeeCurrencyDirectory: %w", err)
}
currencies, err := GetRegisteredCurrencies(directory)
if err != nil {
Expand All @@ -202,11 +219,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 *CeloBackend) (common.FeeCurrencyContext, error) {
var feeContext common.FeeCurrencyContext
directory, err := abigen.NewFeeCurrencyDirectoryCaller(addresses.FeeCurrencyDirectoryAddress, caller)
directory, err := abigen.NewFeeCurrencyDirectoryCaller(getFeeCurrencyDirectoryAddress(caller.ChainConfig.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
4 changes: 2 additions & 2 deletions internal/celoapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func (c *CeloAPI) convertCeloToCurrency(nativePrice *big.Int, feeCurrency *commo
if err != nil {
return nil, err
}
er, err := contracts.GetExchangeRates(cb)
exchangeRates, err := contracts.GetExchangeRates(cb)
if err != nil {
return nil, fmt.Errorf("retrieve exchange rates from current state: %w", err)
}
return exchange.ConvertCeloToCurrency(er, feeCurrency, nativePrice)
return exchange.ConvertCeloToCurrency(exchangeRates, feeCurrency, nativePrice)
}

// GasPrice wraps the original JSON RPC `eth_gasPrice` and adds an additional
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)
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 a102e49

Please sign in to comment.