Skip to content
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
27 changes: 26 additions & 1 deletion cmd/mito/internal/tx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/mitosis-org/chain/cmd/mito/internal/client"
Expand Down Expand Up @@ -86,7 +87,31 @@ func (b *Builder) CreateTransactionFromDataWithOptions(txData *TransactionData,
gasLimit = b.config.GasLimit
}
if gasLimit == 0 {
gasLimit = 500000 // Default
if b.ethClient != nil && !unsigned {
// Only estimate gas for signed transactions since we need a valid from address
fromAddr, err := b.GetSignerAddress()
if err != nil {
// Fallback to default if we can't get signer address
gasLimit = 200000
Copy link

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback gas limit of 200000 is significantly lower than the previous default of 500000. This could cause transaction failures for operations that require more gas. Consider using a more conservative fallback value or documenting why this reduction is safe.

Suggested change
gasLimit = 200000
gasLimit = 500000

Copilot uses AI. Check for mistakes.
} else {
msg := ethereum.CallMsg{
From: fromAddr,
To: &txData.To,
Value: txData.Value,
Data: txData.Data,
}

estimatedGas, err := b.ethClient.EstimateGas(context.Background(), msg)
if err != nil {
return nil, fmt.Errorf("gas estimation failed: %w", err)
} else {
// Add 20% buffer to estimated gas
gasLimit = estimatedGas + (estimatedGas / 5)
Copy link

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gas buffer calculation uses integer division which could result in precision loss for small gas estimates. Consider using gasLimit = (estimatedGas * 6) / 5 to avoid potential rounding issues.

Suggested change
gasLimit = estimatedGas + (estimatedGas / 5)
gasLimit = (estimatedGas * 6) / 5

Copilot uses AI. Check for mistakes.
}
}
} else {
gasLimit = 200000 // Conservative default when no RPC available or unsigned transaction
Copy link

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment describes this as 'Conservative default' but 200000 is actually less conservative than the previous 500000 default. This inconsistency between the comment and the actual behavior could be misleading.

Suggested change
gasLimit = 200000 // Conservative default when no RPC available or unsigned transaction
gasLimit = 500000 // Conservative default when no RPC available or unsigned transaction

Copilot uses AI. Check for mistakes.
}
}

// Determine gas price - use specified gas price or get from client
Expand Down
32 changes: 4 additions & 28 deletions cmd/mito/internal/tx/collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,12 @@ func (s *CollateralService) DepositCollateral(validatorAddr, amount string) (*ty
return nil, fmt.Errorf("failed to pack function call: %w", err)
}

// Set default gas limit if not provided
gasLimit := s.config.GasLimit
if gasLimit == 0 {
gasLimit = 500000
}

// Create transaction data
txData := &TransactionData{
To: common.HexToAddress(s.config.ValidatorManagerContractAddr),
Value: totalValue,
Data: data,
GasLimit: gasLimit,
GasLimit: 0, // Let builder handle gas estimation
}

// Create transaction
Expand Down Expand Up @@ -111,18 +105,12 @@ func (s *CollateralService) WithdrawCollateral(validatorAddr, amount, receiver s
return nil, fmt.Errorf("failed to pack function call: %w", err)
}

// Set default gas limit if not provided
gasLimit := s.config.GasLimit
if gasLimit == 0 {
gasLimit = 500000
}

// Create transaction data (withdraw only sends fee, not collateral)
txData := &TransactionData{
To: common.HexToAddress(s.config.ValidatorManagerContractAddr),
Value: s.config.ContractFee,
Data: data,
GasLimit: gasLimit,
GasLimit: 0, // Let builder handle gas estimation
}

// Create transaction
Expand Down Expand Up @@ -153,18 +141,12 @@ func (s *CollateralService) SetPermittedCollateralOwner(validatorAddr, collatera
return nil, fmt.Errorf("failed to pack function call: %w", err)
}

// Set default gas limit if not provided
gasLimit := s.config.GasLimit
if gasLimit == 0 {
gasLimit = 500000
}

// Create transaction data (no value needed for permission update)
txData := &TransactionData{
To: common.HexToAddress(s.config.ValidatorManagerContractAddr),
Value: big.NewInt(0),
Data: data,
GasLimit: gasLimit,
GasLimit: 0, // Let builder handle gas estimation
}

// Create transaction
Expand Down Expand Up @@ -195,18 +177,12 @@ func (s *CollateralService) TransferCollateralOwnership(validatorAddr, newOwner
return nil, fmt.Errorf("failed to pack function call: %w", err)
}

// Set default gas limit if not provided
gasLimit := s.config.GasLimit
if gasLimit == 0 {
gasLimit = 500000
}

// Create transaction data
txData := &TransactionData{
To: common.HexToAddress(s.config.ValidatorManagerContractAddr),
Value: s.config.ContractFee,
Data: data,
GasLimit: gasLimit,
GasLimit: 0, // Let builder handle gas estimation
}

// Create transaction
Expand Down
48 changes: 6 additions & 42 deletions cmd/mito/internal/tx/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,12 @@ func (s *ValidatorService) CreateValidatorWithOptions(req *CreateValidatorReques
return nil, fmt.Errorf("failed to pack function call: %w", err)
}

// Set default gas limit if not provided
gasLimit := s.config.GasLimit
if gasLimit == 0 {
gasLimit = 500000 // Default gas limit
}

// Create transaction data
txData := &TransactionData{
To: common.HexToAddress(s.config.ValidatorManagerContractAddr),
Value: totalValue,
Data: data,
GasLimit: gasLimit,
GasLimit: 0, // Let builder handle gas estimation
}

// Create transaction
Expand Down Expand Up @@ -142,18 +136,12 @@ func (s *ValidatorService) UpdateMetadataWithOptions(validatorAddr, metadata str
return nil, fmt.Errorf("failed to pack function call: %w", err)
}

// Set default gas limit if not provided
gasLimit := s.config.GasLimit
if gasLimit == 0 {
gasLimit = 500000
}

// Create transaction data (no value needed for metadata update)
txData := &TransactionData{
To: common.HexToAddress(s.config.ValidatorManagerContractAddr),
Value: big.NewInt(0),
Data: data,
GasLimit: gasLimit,
GasLimit: 0, // Let builder handle gas estimation
}

// Create transaction
Expand Down Expand Up @@ -189,18 +177,12 @@ func (s *ValidatorService) UpdateOperatorWithOptions(validatorAddr, newOperator
return nil, fmt.Errorf("failed to pack function call: %w", err)
}

// Set default gas limit if not provided
gasLimit := s.config.GasLimit
if gasLimit == 0 {
gasLimit = 500000
}

// Create transaction data (no value needed for operator update)
txData := &TransactionData{
To: common.HexToAddress(s.config.ValidatorManagerContractAddr),
Value: big.NewInt(0),
Data: data,
GasLimit: gasLimit,
GasLimit: 0, // Let builder handle gas estimation
}

// Create transaction
Expand Down Expand Up @@ -242,18 +224,12 @@ func (s *ValidatorService) UpdateRewardConfigWithOptions(validatorAddr, commissi
return nil, fmt.Errorf("failed to pack function call: %w", err)
}

// Set default gas limit if not provided
gasLimit := s.config.GasLimit
if gasLimit == 0 {
gasLimit = 500000
}

// Create transaction data (no value needed for reward config update)
txData := &TransactionData{
To: common.HexToAddress(s.config.ValidatorManagerContractAddr),
Value: big.NewInt(0),
Data: data,
GasLimit: gasLimit,
GasLimit: 0, // Let builder handle gas estimation
}

// Create transaction
Expand Down Expand Up @@ -289,18 +265,12 @@ func (s *ValidatorService) UpdateRewardManagerWithOptions(validatorAddr, rewardM
return nil, fmt.Errorf("failed to pack function call: %w", err)
}

// Set default gas limit if not provided
gasLimit := s.config.GasLimit
if gasLimit == 0 {
gasLimit = 500000
}

// Create transaction data (no value needed for reward manager update)
txData := &TransactionData{
To: common.HexToAddress(s.config.ValidatorManagerContractAddr),
Value: big.NewInt(0),
Data: data,
GasLimit: gasLimit,
GasLimit: 0, // Let builder handle gas estimation
}

// Create transaction
Expand Down Expand Up @@ -331,18 +301,12 @@ func (s *ValidatorService) UnjailValidatorWithOptions(validatorAddr string, unsi
return nil, fmt.Errorf("failed to pack function call: %w", err)
}

// Set default gas limit if not provided
gasLimit := s.config.GasLimit
if gasLimit == 0 {
gasLimit = 500000
}

// Create transaction data
txData := &TransactionData{
To: common.HexToAddress(s.config.ValidatorManagerContractAddr),
Value: s.config.ContractFee,
Data: data,
GasLimit: gasLimit,
GasLimit: 0, // Let builder handle gas estimation
}

// Create transaction
Expand Down
Loading