/
simulate.go
49 lines (41 loc) · 1014 Bytes
/
simulate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package tx
import (
"context"
"math"
"github.com/tessellated-io/pickaxe/grpc"
"github.com/cosmos/cosmos-sdk/client"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
)
type simulationResult struct {
GasRecommendation uint64
}
func simulateTx(
tx authsigning.Tx,
gasFactor float64,
nodeGrpcUri string,
txConfig client.TxConfig,
) (*simulationResult, error) {
// Connect to gRPC
conn, err := grpc.GetGrpcConnection(nodeGrpcUri)
if err != nil {
return nil, err
}
queryclient := txtypes.NewServiceClient(conn)
// Form a query
encoder := txConfig.TxEncoder()
txBytes, err := encoder(tx)
if err != nil {
return nil, err
}
query := &txtypes.SimulateRequest{
TxBytes: txBytes,
}
simulationResponse, err := queryclient.Simulate(context.Background(), query)
if err != nil {
return nil, err
}
return &simulationResult{
GasRecommendation: uint64(math.Ceil(float64(simulationResponse.GasInfo.GasUsed) * gasFactor)),
}, nil
}