@@ -5,12 +5,15 @@ import (
5
5
6
6
"github.com/gnolang/gno/gno.land/pkg/gnoland"
7
7
"github.com/gnolang/gno/tm2/pkg/amino"
8
+ abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
8
9
"github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
9
10
core_types "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
10
11
"github.com/gnolang/gno/tm2/pkg/std"
11
12
"github.com/gnolang/supernova/internal/common"
12
13
)
13
14
15
+ const simulatePath = ".app/simulate"
16
+
14
17
type Client struct {
15
18
conn * client.RPCClient
16
19
}
@@ -133,3 +136,37 @@ func (h *Client) GetBlockGasLimit(height int64) (int64, error) {
133
136
134
137
return consensusParams .ConsensusParams .Block .MaxGas , nil
135
138
}
139
+
140
+ func (h * Client ) EstimateGas (tx * std.Tx ) (int64 , error ) {
141
+ // Prepare the transaction.
142
+ // The transaction needs to be amino-binary encoded
143
+ // in order to be estimated
144
+ encodedTx , err := amino .Marshal (tx )
145
+ if err != nil {
146
+ return 0 , fmt .Errorf ("unable to marshal tx: %w" , err )
147
+ }
148
+
149
+ // Perform the simulation query
150
+ resp , err := h .conn .ABCIQuery (simulatePath , encodedTx )
151
+ if err != nil {
152
+ return 0 , fmt .Errorf ("unable to perform ABCI query: %w" , err )
153
+ }
154
+
155
+ // Extract the query response
156
+ if err = resp .Response .Error ; err != nil {
157
+ return 0 , fmt .Errorf ("error encountered during ABCI query: %w" , err )
158
+ }
159
+
160
+ var deliverTx abci.ResponseDeliverTx
161
+ if err = amino .Unmarshal (resp .Response .Value , & deliverTx ); err != nil {
162
+ return 0 , fmt .Errorf ("unable to unmarshal gas estimation response: %w" , err )
163
+ }
164
+
165
+ if err = deliverTx .Error ; err != nil {
166
+ return 0 , fmt .Errorf ("error encountered during gas estimation: %w" , err )
167
+ }
168
+
169
+ // Return the actual value returned by the node
170
+ // for executing the transaction
171
+ return deliverTx .GasUsed , nil
172
+ }
0 commit comments