You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, gas estimation (if not already set) is called automatically internally inside the func (c *BoundContract) createLegacyTx or func (c *BoundContract) createDynamicTx functions.
This does not give the user the possibility to manually adjust this value. For example, a smart contract function can have a lot of conditionals (ifs), which can drastically change the gas needed if the transaction enters a block before or after some other transaction. Our solution usually was to multiply the base estimation by some arbitrary percentage value (170%) to ensure that the transaction execution will pass in any case.
What are the use-cases?
Sometimes, a user wants to manually adjust the returned gas estimation value or call it for multiple different possibilities. It would be nice if the structures generated by abigen provided that capability in an easy way.
Implementation
Do you have ideas regarding the implementation of this feature?
BoundContract can implement public method EstimateGas like this:
// EstimateGas estimates gas for method and params. GasPrice or GasFeeCap/GasTipCap should be set before calling this method
func (c *BoundContract) EstimateGas(opts *TransactOpts, method string, params ...interface{}) (uint64, error) {
input, err := c.abi.Pack(method, params...)
if err != nil {
return uint64(0), err
}
value := opts.Value
if value == nil {
value = new(big.Int)
}
contract := &c.address
if opts.GasPrice != nil {
return c.estimateGasLimit(opts, contract, input, opts.GasPrice, nil, nil, value)
}
return c.estimateGasLimit(opts, contract, input, nil, opts.GasTipCap, opts.GasFeeCap, value)
}
igorcrevar
changed the title
It would be nice if abigen can provide easy way to manually call estimateGas
It would be helpful if abigen provided an easy way to manually call estimateGas
May 20, 2024
ABIGEN v2 (#26782) will solve this. It's not done yet, but the new package will allow encoding calls and then you can submit them to EstimateGas yourself.
Rationale
Why should this feature exist?
Currently, gas estimation (if not already set) is called automatically internally inside the
func (c *BoundContract) createLegacyTx
orfunc (c *BoundContract) createDynamicTx
functions.This does not give the user the possibility to manually adjust this value. For example, a smart contract function can have a lot of conditionals (ifs), which can drastically change the gas needed if the transaction enters a block before or after some other transaction. Our solution usually was to multiply the base estimation by some arbitrary percentage value (170%) to ensure that the transaction execution will pass in any case.
What are the use-cases?
Sometimes, a user wants to manually adjust the returned gas estimation value or call it for multiple different possibilities. It would be nice if the structures generated by abigen provided that capability in an easy way.
Implementation
Do you have ideas regarding the implementation of this feature?
BoundContract
can implement public methodEstimateGas
like this:Generated contract code can expose
BoundCountract
After that, user can easily use estimation with something like this:
Are you willing to implement this feature?
yes
The text was updated successfully, but these errors were encountered: