Skip to content

Commit 4125811

Browse files
committed
Adding gas estimate function
- Added *GasEstimate template. - Added EstimateGas() in ethutil.go
1 parent 7228425 commit 4125811

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
lines changed

pkg/chain/ethereum/ethutil/ethutil.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,37 @@ func CallAtBlock(
136136
return nil
137137
}
138138

139+
// EstimateGas tries to estimate the gas needed to execute a specific transaction based on
140+
// the current pending state of the backend blockchain. There is no guarantee that this is
141+
// the true gas limit requirement as other transactions may be added or removed by miners,
142+
// but it should provide a basis for setting a reasonable default.
143+
func EstimateGas(
144+
from common.Address,
145+
to common.Address,
146+
method string,
147+
contractABI *abi.ABI,
148+
transactor bind.ContractTransactor,
149+
parameters ...interface{},
150+
) (uint64, error) {
151+
input, err := contractABI.Pack(method, parameters...)
152+
if err != nil {
153+
return 0, err
154+
}
155+
156+
msg := ethereum.CallMsg{
157+
From: from,
158+
To: &to,
159+
Data: input,
160+
}
161+
162+
gas, err := transactor.EstimateGas(context.TODO(), msg)
163+
if err != nil {
164+
return 0, err
165+
}
166+
167+
return gas, nil
168+
}
169+
139170
type loggingWrapper struct {
140171
bind.ContractBackend
141172

tools/generators/ethereum/contract.go.tmpl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ import (
2626
var {{.ShortVar}}Logger = log.Logger("keep-contract-{{.Class}}")
2727

2828
type {{.Class}} struct {
29-
contract *abi.{{.AbiClass}}
30-
contractAddress common.Address
31-
contractABI *ethereumabi.ABI
32-
caller bind.ContractCaller
33-
callerOptions *bind.CallOpts
34-
transactorOptions *bind.TransactOpts
35-
errorResolver *ethutil.ErrorResolver
29+
contract *abi.{{.AbiClass}}
30+
contractAddress common.Address
31+
contractABI *ethereumabi.ABI
32+
caller bind.ContractCaller
33+
transactor bind.ContractTransactor
34+
callerOptions *bind.CallOpts
35+
transactorOptions *bind.TransactOpts
36+
errorResolver *ethutil.ErrorResolver
3637

3738
transactionMutex *sync.Mutex
3839
}
@@ -72,6 +73,7 @@ func New{{.Class}}(
7273
contractAddress: contractAddress,
7374
contractABI: &contractABI,
7475
caller: backend,
76+
transactor: backend,
7577
callerOptions: callerOptions,
7678
transactorOptions: transactorOptions,
7779
errorResolver: ethutil.NewErrorResolver(backend, &contractABI, &contractAddress),

tools/generators/ethereum/contract_non_const_methods.go.tmpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,21 @@ func ({{$contract.ShortVar}} *{{$contract.Class}}) Call{{$method.CapsName}}(
108108
return {{$returnVar}}err
109109
}
110110

111+
func ({{$contract.ShortVar}} *{{$contract.Class}}) {{$method.CapsName}}GasEstimate(
112+
{{$method.ParamDeclarations -}}
113+
) (uint64, error) {
114+
var result uint64
115+
116+
result, err := ethutil.EstimateGas(
117+
{{$contract.ShortVar}}.callerOptions.From,
118+
{{$contract.ShortVar}}.contractAddress,
119+
"{{$method.LowerName}}",
120+
{{$contract.ShortVar}}.contractABI,
121+
{{$contract.ShortVar}}.transactor,
122+
{{$method.Params}}
123+
)
124+
125+
return result, err
126+
}
127+
111128
{{- end -}}

tools/generators/ethereum/contract_non_const_methods_template_content.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,22 @@ func ({{$contract.ShortVar}} *{{$contract.Class}}) Call{{$method.CapsName}}(
111111
return {{$returnVar}}err
112112
}
113113
114+
func ({{$contract.ShortVar}} *{{$contract.Class}}) {{$method.CapsName}}GasEstimate(
115+
{{$method.ParamDeclarations -}}
116+
) (uint64, error) {
117+
var result uint64
118+
119+
result, err := ethutil.EstimateGas(
120+
{{$contract.ShortVar}}.callerOptions.From,
121+
{{$contract.ShortVar}}.contractAddress,
122+
"{{$method.LowerName}}",
123+
{{$contract.ShortVar}}.contractABI,
124+
{{$contract.ShortVar}}.transactor,
125+
{{$method.Params}}
126+
)
127+
128+
return result, err
129+
}
130+
114131
{{- end -}}
115132
`

tools/generators/ethereum/contract_template_content.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ import (
2929
var {{.ShortVar}}Logger = log.Logger("keep-contract-{{.Class}}")
3030
3131
type {{.Class}} struct {
32-
contract *abi.{{.AbiClass}}
33-
contractAddress common.Address
34-
contractABI *ethereumabi.ABI
35-
caller bind.ContractCaller
36-
callerOptions *bind.CallOpts
37-
transactorOptions *bind.TransactOpts
38-
errorResolver *ethutil.ErrorResolver
32+
contract *abi.{{.AbiClass}}
33+
contractAddress common.Address
34+
contractABI *ethereumabi.ABI
35+
caller bind.ContractCaller
36+
transactor bind.ContractTransactor
37+
callerOptions *bind.CallOpts
38+
transactorOptions *bind.TransactOpts
39+
errorResolver *ethutil.ErrorResolver
3940
4041
transactionMutex *sync.Mutex
4142
}
@@ -75,6 +76,7 @@ func New{{.Class}}(
7576
contractAddress: contractAddress,
7677
contractABI: &contractABI,
7778
caller: backend,
79+
transactor: backend,
7880
callerOptions: callerOptions,
7981
transactorOptions: transactorOptions,
8082
errorResolver: ethutil.NewErrorResolver(backend, &contractABI, &contractAddress),

0 commit comments

Comments
 (0)