Skip to content

Commit

Permalink
Refactor address config (#294)
Browse files Browse the repository at this point in the history
* Refactor address config

Move common logic into the `address` package

* Style fixes
  • Loading branch information
palango authored Dec 10, 2024
1 parent e649949 commit d3cec77
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 50 deletions.
54 changes: 44 additions & 10 deletions contracts/addresses/addresses.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
package addresses

import "github.com/ethereum/go-ethereum/common"
import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
)

type CeloAddresses struct {
CeloToken common.Address
FeeHandler common.Address
FeeCurrencyDirectory common.Address
}

var (
CeloTokenAddress = common.HexToAddress("0x471ece3750da237f93b8e339c536989b8978a438")
FeeHandlerAddress = common.HexToAddress("0xcd437749e43a154c07f3553504c68fbfd56b8778")
FeeCurrencyDirectoryAddress = common.HexToAddress("0x9212Fb72ae65367A7c887eC4Ad9bE310BAC611BF") // TODO
MainnetAddresses = &CeloAddresses{
CeloToken: common.HexToAddress("0x471ece3750da237f93b8e339c536989b8978a438"),
FeeHandler: common.HexToAddress("0xcd437749e43a154c07f3553504c68fbfd56b8778"),
FeeCurrencyDirectory: common.HexToAddress("0x9212Fb72ae65367A7c887eC4Ad9bE310BAC611BF"), // TODO
}

CeloTokenAlfajoresAddress = common.HexToAddress("0xF194afDf50B03e69Bd7D057c1Aa9e10c9954E4C9")
FeeHandlerAlfajoresAddress = common.HexToAddress("0xEAaFf71AB67B5d0eF34ba62Ea06Ac3d3E2dAAA38")
FeeCurrencyDirectoryAlfajoresAddress = common.HexToAddress("0x9212Fb72ae65367A7c887eC4Ad9bE310BAC611BF")
AlfajoresAddresses = &CeloAddresses{
CeloToken: common.HexToAddress("0xF194afDf50B03e69Bd7D057c1Aa9e10c9954E4C9"),
FeeHandler: common.HexToAddress("0xEAaFf71AB67B5d0eF34ba62Ea06Ac3d3E2dAAA38"),
FeeCurrencyDirectory: common.HexToAddress("0x9212Fb72ae65367A7c887eC4Ad9bE310BAC611BF"),
}

CeloTokenBaklavaAddress = common.HexToAddress("0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8")
FeeHandlerBaklavaAddress = common.HexToAddress("0xeed0A69c51079114C280f7b936C79e24bD94013e")
FeeCurrencyDirectoryBaklavaAddress = common.HexToAddress("0xD59E1599F45e42Eb356202B2C714D6C7b734C034")
BaklavaAddresses = &CeloAddresses{
CeloToken: common.HexToAddress("0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8"),
FeeHandler: common.HexToAddress("0xeed0A69c51079114C280f7b936C79e24bD94013e"),
FeeCurrencyDirectory: common.HexToAddress("0xD59E1599F45e42Eb356202B2C714D6C7b734C034"),
}
)

// GetAddresses returns the addresses for the given chainID.
func GetAddresses(chainID *big.Int) *CeloAddresses {
// ChainID can be uninitialized in some tests
if chainID == nil {
return MainnetAddresses
}

switch chainID.Uint64() {
case params.CeloAlfajoresChainID:
return AlfajoresAddresses
case params.CeloBaklavaChainID:
return BaklavaAddresses
default:
return MainnetAddresses
}
}
22 changes: 2 additions & 20 deletions contracts/fee_currencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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 @@ -30,23 +29,6 @@ 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 @@ -207,7 +189,7 @@ func GetRegisteredCurrencies(caller *abigen.FeeCurrencyDirectoryCaller) ([]commo

// GetExchangeRates returns the exchange rates for the provided gas currencies
func GetExchangeRates(caller *CeloBackend) (common.ExchangeRates, error) {
directory, err := abigen.NewFeeCurrencyDirectoryCaller(getFeeCurrencyDirectoryAddress(caller.ChainConfig.ChainID), caller)
directory, err := abigen.NewFeeCurrencyDirectoryCaller(addresses.GetAddresses(caller.ChainConfig.ChainID).FeeCurrencyDirectory, caller)
if err != nil {
return common.ExchangeRates{}, fmt.Errorf("failed to access FeeCurrencyDirectory: %w", err)
}
Expand All @@ -221,7 +203,7 @@ func GetExchangeRates(caller *CeloBackend) (common.ExchangeRates, error) {
// GetFeeCurrencyContext returns the fee currency block context for all registered gas currencies from CELO
func GetFeeCurrencyContext(caller *CeloBackend) (common.FeeCurrencyContext, error) {
var feeContext common.FeeCurrencyContext
directory, err := abigen.NewFeeCurrencyDirectoryCaller(getFeeCurrencyDirectoryAddress(caller.ChainConfig.ChainID), caller)
directory, err := abigen.NewFeeCurrencyDirectoryCaller(addresses.GetAddresses(caller.ChainConfig.ChainID).FeeCurrencyDirectory, 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 @@ -138,7 +138,7 @@ func testNativeTransferWithFeeCurrency(t *testing.T, scheme string, feeCurrencyA
}

// 5: Check that base fee has been moved to the fee handler.
actual, _ = contracts.GetBalanceERC20(&backend, addresses.FeeHandlerAddress, feeCurrencyAddr)
actual, _ = contracts.GetBalanceERC20(&backend, addresses.MainnetAddresses.FeeHandler, feeCurrencyAddr)
expected = new(big.Int).SetUint64(block.GasUsed() * baseFeeInFeeCurrency.Uint64())
if actual.Cmp(expected) != 0 {
t.Fatalf("fee handler balance incorrect: expected %d, got %d", expected, actual)
Expand Down
4 changes: 2 additions & 2 deletions core/celo_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func celoGenesisAccounts(fundedAddr common.Address) GenesisAlloc {
panic("Couldn not set faucet balance!")
}
genesisAccounts := map[common.Address]GenesisAccount{
addresses.CeloTokenAddress: {
addresses.MainnetAddresses.CeloToken: {
Code: celoTokenBytecode,
Balance: big.NewInt(0),
},
Expand Down Expand Up @@ -149,7 +149,7 @@ func celoGenesisAccounts(fundedAddr common.Address) GenesisAlloc {
// add entries to currencyConfig mapping
addFeeCurrencyToStorage(DevFeeCurrencyAddr, mockOracleAddr, feeCurrencyDirectoryStorage)
addFeeCurrencyToStorage(DevFeeCurrencyAddr2, mockOracleAddr2, feeCurrencyDirectoryStorage)
genesisAccounts[addresses.FeeCurrencyDirectoryAddress] = GenesisAccount{
genesisAccounts[addresses.MainnetAddresses.FeeCurrencyDirectory] = GenesisAccount{
Code: feeCurrencyDirectoryBytecode,
Balance: big.NewInt(0),
Storage: feeCurrencyDirectoryStorage,
Expand Down
10 changes: 2 additions & 8 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,15 +765,9 @@ func (st *StateTransition) distributeTxFees() error {
tipTxFee := new(big.Int).Sub(totalTxFee, baseTxFee)

feeCurrency := st.msg.FeeCurrency
feeHandlerAddress := addresses.FeeHandlerAddress
if st.evm.ChainConfig().ChainID != nil && st.evm.ChainConfig().ChainID.Uint64() == params.CeloAlfajoresChainID {
feeHandlerAddress = addresses.FeeHandlerAlfajoresAddress
}
if st.evm.ChainConfig().ChainID != nil && st.evm.ChainConfig().ChainID.Uint64() == params.CeloBaklavaChainID {
feeHandlerAddress = addresses.FeeHandlerBaklavaAddress
}
feeHandlerAddress := addresses.GetAddresses(st.evm.ChainConfig().ChainID).FeeHandler

log.Trace("distributeTxFees", "from", from, "refund", refund, "feeCurrency", st.msg.FeeCurrency,
log.Trace("distributeTxFees", "from", from, "refund", refund, "feeCurrency", feeCurrency,
"coinbaseFeeRecipient", st.evm.Context.Coinbase, "coinbaseFee", tipTxFee,
"feeHandler", feeHandlerAddress, "communityFundFee", baseTxFee)

Expand Down
8 changes: 1 addition & 7 deletions core/vm/celo_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ func celoPrecompileAddress(index byte) common.Address {
}

func (ctx *celoPrecompileContext) IsCallerCeloToken() (bool, error) {
tokenAddress := addresses.CeloTokenAddress
if ctx.evm.ChainConfig().ChainID != nil && ctx.evm.ChainConfig().ChainID.Uint64() == params.CeloAlfajoresChainID {
tokenAddress = addresses.CeloTokenAlfajoresAddress
}
if ctx.evm.ChainConfig().ChainID != nil && ctx.evm.ChainConfig().ChainID.Uint64() == params.CeloBaklavaChainID {
tokenAddress = addresses.CeloTokenBaklavaAddress
}
tokenAddress := addresses.GetAddresses(ctx.evm.ChainConfig().ChainID).CeloToken

return tokenAddress == ctx.caller, nil
}
Expand Down
4 changes: 2 additions & 2 deletions core/vm/celo_contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestPrecompileTransfer(t *testing.T) {
name: "Test transfer with short input",
args: args{
input: []byte("0000"),
ctx: NewContext(addresses.CeloTokenAddress, mockEVM),
ctx: NewContext(addresses.MainnetAddresses.CeloToken, mockEVM),
},
wantErr: true,
expectedErr: "invalid input length",
Expand All @@ -98,7 +98,7 @@ func TestPrecompileTransfer(t *testing.T) {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
},
ctx: NewContext(addresses.CeloTokenAddress, mockEVM),
ctx: NewContext(addresses.MainnetAddresses.CeloToken, mockEVM),
},
wantErr: true,
expectedErr: "insufficient balance for transfer",
Expand Down

0 comments on commit d3cec77

Please sign in to comment.