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
31 changes: 31 additions & 0 deletions pkg/chain/ethereum/ethutil/ethutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,37 @@ func CallAtBlock(
return nil
}

// EstimateGas tries to estimate the gas needed to execute a specific transaction based on
// the current pending state of the backend blockchain. There is no guarantee that this is
// the true gas limit requirement as other transactions may be added or removed by miners,
// but it should provide a basis for setting a reasonable default.
func EstimateGas(
from common.Address,
to common.Address,
method string,
contractABI *abi.ABI,
transactor bind.ContractTransactor,
parameters ...interface{},
) (uint64, error) {
input, err := contractABI.Pack(method, parameters...)
if err != nil {
return 0, err
}

msg := ethereum.CallMsg{
From: from,
To: &to,
Data: input,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We are not passing Value to CallMsg so the estimation may not be correct for payable functions. I think we need to generate a variation of estimate function just for payable functions.

It's not a top priority because the problem we are having in threshold-network/keep-core#1479 is solved with the code here. I'll open a separate issue to address payable functions.

Copy link
Copy Markdown
Member

@pdyraga pdyraga Apr 10, 2020

Choose a reason for hiding this comment

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

Captured this issue in #26

}

gas, err := transactor.EstimateGas(context.TODO(), msg)
if err != nil {
return 0, err
}

return gas, nil
}

type loggingWrapper struct {
bind.ContractBackend

Expand Down
16 changes: 9 additions & 7 deletions tools/generators/ethereum/contract.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import (
var {{.ShortVar}}Logger = log.Logger("keep-contract-{{.Class}}")

type {{.Class}} struct {
contract *abi.{{.AbiClass}}
contractAddress common.Address
contractABI *ethereumabi.ABI
caller bind.ContractCaller
callerOptions *bind.CallOpts
transactorOptions *bind.TransactOpts
errorResolver *ethutil.ErrorResolver
contract *abi.{{.AbiClass}}
contractAddress common.Address
contractABI *ethereumabi.ABI
caller bind.ContractCaller
transactor bind.ContractTransactor
callerOptions *bind.CallOpts
transactorOptions *bind.TransactOpts
errorResolver *ethutil.ErrorResolver

transactionMutex *sync.Mutex
}
Expand Down Expand Up @@ -72,6 +73,7 @@ func New{{.Class}}(
contractAddress: contractAddress,
contractABI: &contractABI,
caller: backend,
transactor: backend,
callerOptions: callerOptions,
transactorOptions: transactorOptions,
errorResolver: ethutil.NewErrorResolver(backend, &contractABI, &contractAddress),
Expand Down
17 changes: 17 additions & 0 deletions tools/generators/ethereum/contract_non_const_methods.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,21 @@ func ({{$contract.ShortVar}} *{{$contract.Class}}) Call{{$method.CapsName}}(
return {{$returnVar}}err
}

func ({{$contract.ShortVar}} *{{$contract.Class}}) {{$method.CapsName}}GasEstimate(
{{$method.ParamDeclarations -}}
) (uint64, error) {
var result uint64

result, err := ethutil.EstimateGas(
{{$contract.ShortVar}}.callerOptions.From,
{{$contract.ShortVar}}.contractAddress,
"{{$method.LowerName}}",
{{$contract.ShortVar}}.contractABI,
{{$contract.ShortVar}}.transactor,
{{$method.Params}}
)

return result, err
}

{{- end -}}
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,22 @@ func ({{$contract.ShortVar}} *{{$contract.Class}}) Call{{$method.CapsName}}(
return {{$returnVar}}err
}

func ({{$contract.ShortVar}} *{{$contract.Class}}) {{$method.CapsName}}GasEstimate(
{{$method.ParamDeclarations -}}
) (uint64, error) {
var result uint64

result, err := ethutil.EstimateGas(
{{$contract.ShortVar}}.callerOptions.From,
{{$contract.ShortVar}}.contractAddress,
"{{$method.LowerName}}",
{{$contract.ShortVar}}.contractABI,
{{$contract.ShortVar}}.transactor,
{{$method.Params}}
)

return result, err
}

{{- end -}}
`
16 changes: 9 additions & 7 deletions tools/generators/ethereum/contract_template_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ import (
var {{.ShortVar}}Logger = log.Logger("keep-contract-{{.Class}}")

type {{.Class}} struct {
contract *abi.{{.AbiClass}}
contractAddress common.Address
contractABI *ethereumabi.ABI
caller bind.ContractCaller
callerOptions *bind.CallOpts
transactorOptions *bind.TransactOpts
errorResolver *ethutil.ErrorResolver
contract *abi.{{.AbiClass}}
contractAddress common.Address
contractABI *ethereumabi.ABI
caller bind.ContractCaller
transactor bind.ContractTransactor
callerOptions *bind.CallOpts
transactorOptions *bind.TransactOpts
errorResolver *ethutil.ErrorResolver

transactionMutex *sync.Mutex
}
Expand Down Expand Up @@ -75,6 +76,7 @@ func New{{.Class}}(
contractAddress: contractAddress,
contractABI: &contractABI,
caller: backend,
transactor: backend,
callerOptions: callerOptions,
transactorOptions: transactorOptions,
errorResolver: ethutil.NewErrorResolver(backend, &contractABI, &contractAddress),
Expand Down