Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib/contracts): correct deployment of feeoraclev2 #2507

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/contracts/feeoraclev2/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ type DeploymentConfig struct {
Owner common.Address
Deployer common.Address
Manager common.Address // manager is the address that can set fee parameters (gas price, conversion rates)
BaseGasLimit uint32 // must fit in uint24 (max: 16,777,215)
BaseGasLimit *big.Int // must fit in uint24 (max: 16,777,215)
ProtocolFee *big.Int // must fit in uint72 (max: 4,722,366,482,869,645,213,695)
}

func isEmpty(addr common.Address) bool {
return addr == common.Address{}
}

var maxUint24 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 24), big.NewInt(1))
var maxUint72 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 72), big.NewInt(1))

func (cfg DeploymentConfig) Validate() error {
Expand All @@ -57,7 +58,7 @@ func (cfg DeploymentConfig) Validate() error {
if isEmpty(cfg.Manager) {
return errors.New("manager is zero")
}
if cfg.BaseGasLimit > (1<<24 - 1) {
if cfg.BaseGasLimit.Cmp(maxUint24) > 0 {
return errors.New("base gas limit too high")
}
if cfg.ProtocolFee.Cmp(maxUint72) > 0 {
Expand Down Expand Up @@ -125,7 +126,7 @@ func Deploy(ctx context.Context, network netconf.ID, chainID uint64, destChainID
Owner: eoa.MustAddress(network, eoa.RoleManager),
Deployer: eoa.MustAddress(network, eoa.RoleDeployer),
Manager: eoa.MustAddress(network, eoa.RoleMonitor), // NOTE: monitor is owner of fee oracle contracts, because monitor manages on chain gas prices / conversion rates
BaseGasLimit: 100_000,
BaseGasLimit: big.NewInt(100_000),
ProtocolFee: big.NewInt(0),
}

Expand Down
45 changes: 25 additions & 20 deletions lib/contracts/feeoraclev2/feeparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ func feeParams(ctx context.Context, srcChainID uint64, destChainIDs []uint64, ba
return nil, errors.New("meta by chain id", "dest_chain", destChain.Name)
}

ps, err := destFeeParams(ctx, srcChain, destChain, backends, pricer)
if err != nil {
return nil, err
}
ps := destFeeParams(ctx, srcChain, destChain, backends, pricer)

resp = append(resp, ps)
}
Expand All @@ -45,7 +42,7 @@ func feeParams(ctx context.Context, srcChainID uint64, destChainIDs []uint64, ba

// feeParams returns the fee parameters for the given source token and destination chains.
func destFeeParams(ctx context.Context, srcChain evmchain.Metadata, destChain evmchain.Metadata, backends ethbackend.Backends, pricer tokens.Pricer,
) (bindings.IFeeOracleV2FeeParams, error) {
) bindings.IFeeOracleV2FeeParams {
// conversion rate from "dest token" to "src token"
// ex if dest chain is ETH, and src chain is OMNI, we need to know the rate of ETH to OMNI.
toNativeRate, err := conversionRate(ctx, pricer, destChain.NativeToken, srcChain.NativeToken)
Expand All @@ -54,34 +51,42 @@ func destFeeParams(ctx context.Context, srcChain evmchain.Metadata, destChain ev
toNativeRate = 1
}

localBackend, err := backends.Backend(destChain.ChainID)
// Get execution gas price, defaulting to 1 Gwei if any error occurs.
var execBackend *ethbackend.Backend
var execGasPrice *big.Int
execBackend, err = backends.Backend(destChain.ChainID)
if err != nil {
return bindings.IFeeOracleV2FeeParams{}, errors.Wrap(err, "get local backend", "dest_chain", destChain.Name)
}

remoteBackend, err := backends.Backend(destChain.PostsTo)
if err != nil {
return bindings.IFeeOracleV2FeeParams{}, errors.Wrap(err, "get remote backend", "dest_chain", destChain.Name)
}

execGasPrice, err := localBackend.SuggestGasPrice(ctx)
if err != nil {
log.Warn(ctx, "Failed fetching exec gas price, using default 1 Gwei", err, "dest_chain", destChain.Name)
log.Warn(ctx, "Failed getting exec backend, using default 1 Gwei", err, "dest_chain", destChain.Name)
execGasPrice = big.NewInt(params.GWei)
} else {
execGasPrice, err = execBackend.SuggestGasPrice(ctx)
if err != nil {
log.Warn(ctx, "Failed fetching exec gas price, using default 1 Gwei", err, "dest_chain", destChain.Name)
execGasPrice = big.NewInt(params.GWei)
}
}

dataGasPrice, err := remoteBackend.SuggestGasPrice(ctx)
// Get data gas price, defaulting to 1 Gwei if any error occurs.
var dataBackend *ethbackend.Backend
var dataGasPrice *big.Int
dataBackend, err = backends.Backend(destChain.PostsTo)
if err != nil {
log.Warn(ctx, "Failed fetching data gas price, using default 1 Gwei", err, "dest_chain", destChain.Name)
log.Warn(ctx, "Failed getting data backend, using default 1 Gwei", err, "dest_chain", destChain.Name)
dataGasPrice = big.NewInt(params.GWei)
} else {
dataGasPrice, err = dataBackend.SuggestGasPrice(ctx)
if err != nil {
log.Warn(ctx, "Failed fetching data gas price, using default 1 Gwei", err, "dest_chain", destChain.Name)
dataGasPrice = big.NewInt(params.GWei)
}
}

return bindings.IFeeOracleV2FeeParams{
ChainId: destChain.ChainID,
ExecGasPrice: gasprice.Tier(execGasPrice.Uint64()),
DataGasPrice: gasprice.Tier(dataGasPrice.Uint64()),
ToNativeRate: rateToNumerator(toNativeRate),
}, nil
}
}

// conversionRate returns the conversion rate C from token F to token T, where C = price(F) / price(T).
Expand Down
Loading