Skip to content

Commit

Permalink
fix: use pigeon gas estimate as gas limit
Browse files Browse the repository at this point in the history
  • Loading branch information
maharifu committed Oct 3, 2024
1 parent dbe9027 commit 85666b1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
13 changes: 11 additions & 2 deletions chain/evm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ type executeSmartContractIn struct {
method string
arguments []any
opts callOptions

gasEstimate *big.Int
}

func callSmartContract(
Expand Down Expand Up @@ -365,6 +367,11 @@ func callSmartContract(
// Once estimation is finished, we adjust the gas limit
// to be sure that the tx will be included in the next block.
txOpts.GasLimit = uint64(float64(gasLimit) * 1.5)
if args.gasEstimate != nil && args.gasEstimate.Uint64() > txOpts.GasLimit {
// If we have a gas estimate from pigeons, and it is greater than
// what we just got, we use it
txOpts.GasLimit = args.gasEstimate.Uint64()
}

if args.txType == 2 {
txOpts.GasFeeCap = gasPrice
Expand Down Expand Up @@ -664,6 +671,7 @@ func (c *Client) ExecuteSmartContract(
opts callOptions,
method string,
arguments []any,
gasEstimate *big.Int,
) (*etherumtypes.Transaction, error) {
var mevClient mevClient = nil
if opts.useMevRelay {
Expand All @@ -685,8 +693,9 @@ func (c *Client) ExecuteSmartContract(
keystore: c.keystore,
opts: opts,

method: method,
arguments: arguments,
method: method,
arguments: arguments,
gasEstimate: gasEstimate,
},
)
}
Expand Down
17 changes: 10 additions & 7 deletions chain/evm/compass.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var errValsetIDMismatch = errors.New("valset id mismatch")
//go:generate mockery --name=evmClienter --inpackage --testonly
type evmClienter interface {
FilterLogs(ctx context.Context, fq ethereum.FilterQuery, currBlockHeight *big.Int, fn func(logs []ethtypes.Log) bool) (bool, error)
ExecuteSmartContract(ctx context.Context, chainID *big.Int, contractAbi abi.ABI, addr common.Address, opts callOptions, method string, arguments []any) (*ethtypes.Transaction, error)
ExecuteSmartContract(ctx context.Context, chainID *big.Int, contractAbi abi.ABI, addr common.Address, opts callOptions, method string, arguments []any, gasEstimate *big.Int) (*ethtypes.Transaction, error)
DeployContract(ctx context.Context, chainID *big.Int, rawABI string, bytecode, constructorInput []byte) (contractAddr common.Address, tx *ethtypes.Transaction, err error)
TransactionByHash(ctx context.Context, txHash common.Hash) (*ethtypes.Transaction, bool, error)
TransactionReceipt(ctx context.Context, txHash common.Hash) (*ethtypes.Receipt, error)
Expand Down Expand Up @@ -181,12 +181,13 @@ func (t compass) updateValset(
// TODO: Use generated contract code directly
// compass 2.0.0
// def update_valset(consensus: Consensus, new_valset: Valset, relayer: address, gas_estimate: uint256)
tx, err := t.callCompass(ctx, opts, "update_valset", []any{
args := []any{
BuildCompassConsensus(currentValset, origMessage.Signatures),
TransformValsetToCompassValset(newValset),
ethSender,
estimate,
})
}
tx, err := t.callCompass(ctx, opts, "update_valset", args, estimate)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -268,7 +269,7 @@ func (t compass) submitLogicCall(
opts.useMevRelay = true
}
logger.WithField("consensus", con).WithField("args", args).Debug("submitting logic call")
tx, err := t.callCompass(ctx, opts, "submit_logic_call", args)
tx, err := t.callCompass(ctx, opts, "submit_logic_call", args, nil)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -334,7 +335,7 @@ func (t compass) compass_handover(
}

logger.WithField("consensus", con).WithField("args", args).Debug("compass handover")
tx, err := t.callCompass(ctx, opts, "compass_update_batch", args)
tx, err := t.callCompass(ctx, opts, "compass_update_batch", args, estimate)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -450,7 +451,7 @@ func (t compass) uploadUserSmartContract(

logger.WithField("consensus", con).WithField("args", args).
Debug("deploying user smart contract")
tx, err := t.callCompass(ctx, opts, "deploy_contract", args)
tx, err := t.callCompass(ctx, opts, "deploy_contract", args, nil)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -1417,11 +1418,12 @@ func (c compass) callCompass(
opts callOptions,
method string,
arguments []any,
gasEstimate *big.Int,
) (*ethtypes.Transaction, error) {
if c.compassAbi == nil {
return nil, ErrABINotInitialized
}
return c.evm.ExecuteSmartContract(ctx, c.chainID, *c.compassAbi, c.smartContractAddr, opts, method, arguments)
return c.evm.ExecuteSmartContract(ctx, c.chainID, *c.compassAbi, c.smartContractAddr, opts, method, arguments, gasEstimate)
}

func (t compass) skywayRelayBatches(ctx context.Context, batches []chain.SkywayBatchWithSignatures) error {
Expand Down Expand Up @@ -1573,6 +1575,7 @@ func (t compass) skywayRelayBatch(
ethSender,
estimate,
},
estimate,
)
if err != nil {
logger.WithError(err).Error("failed to relay batch")
Expand Down

0 comments on commit 85666b1

Please sign in to comment.