Skip to content

Commit 77db5d4

Browse files
committed
Add gas estimation to client
1 parent 1f69059 commit 77db5d4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

internal/client/client.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55

66
"github.com/gnolang/gno/gno.land/pkg/gnoland"
77
"github.com/gnolang/gno/tm2/pkg/amino"
8+
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
89
"github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
910
core_types "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
1011
"github.com/gnolang/gno/tm2/pkg/std"
1112
"github.com/gnolang/supernova/internal/common"
1213
)
1314

15+
const simulatePath = ".app/simulate"
16+
1417
type Client struct {
1518
conn *client.RPCClient
1619
}
@@ -133,3 +136,37 @@ func (h *Client) GetBlockGasLimit(height int64) (int64, error) {
133136

134137
return consensusParams.ConsensusParams.Block.MaxGas, nil
135138
}
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

Comments
 (0)