@@ -5,10 +5,13 @@ import (
5
5
6
6
"github.com/gnolang/gno/tm2/pkg/crypto"
7
7
"github.com/gnolang/gno/tm2/pkg/std"
8
+ "github.com/gnolang/supernova/internal/common"
8
9
"github.com/gnolang/supernova/internal/signer"
9
10
"github.com/schollz/progressbar/v3"
10
11
)
11
12
13
+ const gasBuffer = 10_000 // 10k gas
14
+
12
15
// msgFn defines the transaction message constructor
13
16
type msgFn func (creator std.Account , index int ) std.Msg
14
17
@@ -20,6 +23,7 @@ func constructTransactions(
20
23
transactions uint64 ,
21
24
chainID string ,
22
25
getMsg msgFn ,
26
+ estimateFn EstimateGasFn ,
23
27
) ([]* std.Tx , error ) {
24
28
var (
25
29
txs = make ([]* std.Tx , transactions )
@@ -30,6 +34,53 @@ func constructTransactions(
30
34
nonceMap = make (map [uint64 ]uint64 ) // accountNumber -> nonce
31
35
)
32
36
37
+ fmt .Printf ("\n ⏳ Estimating Gas ⏳\n " )
38
+
39
+ // Estimate the fee for the transaction batch
40
+ txFee := common .CalculateFeeInRatio (
41
+ 1_000_000 ,
42
+ common .DefaultGasPrice ,
43
+ )
44
+
45
+ // Construct the first tx
46
+ var (
47
+ creator = accounts [0 ]
48
+ creatorKey = keys [0 ]
49
+ )
50
+
51
+ tx := & std.Tx {
52
+ Msgs : []std.Msg {getMsg (creator , 0 )},
53
+ Fee : txFee ,
54
+ }
55
+
56
+ // Sign the transaction
57
+ cfg := signer.SignCfg {
58
+ ChainID : chainID ,
59
+ AccountNumber : creator .GetAccountNumber (),
60
+ Sequence : creator .GetSequence (),
61
+ }
62
+
63
+ if err := signer .SignTx (tx , creatorKey , cfg ); err != nil {
64
+ return nil , fmt .Errorf ("unable to sign transaction, %w" , err )
65
+ }
66
+
67
+ gasWanted , err := estimateFn (tx )
68
+ if err != nil {
69
+ return nil , fmt .Errorf ("unable to estimate gas, %w" , err )
70
+ }
71
+
72
+ // Clear the old signatures, because they need
73
+ // to be regenerated
74
+ clear (tx .Signatures )
75
+
76
+ // Use the estimated gas limit
77
+ txFee = common .CalculateFeeInRatio (gasWanted + gasBuffer , common .DefaultGasPrice ) // 10k gas buffer
78
+
79
+ if err = signer .SignTx (tx , creatorKey , cfg ); err != nil {
80
+ return nil , fmt .Errorf ("unable to sign transaction, %w" , err )
81
+ }
82
+
83
+ fmt .Printf ("\n Estimated Gas for 1 run tx: %d \n " , tx .Fee .GasWanted )
33
84
fmt .Printf ("\n 🔨 Constructing Transactions 🔨\n \n " )
34
85
35
86
bar := progressbar .Default (int64 (transactions ), "constructing txs" )
@@ -44,7 +95,7 @@ func constructTransactions(
44
95
45
96
tx := & std.Tx {
46
97
Msgs : []std.Msg {getMsg (creator , i )},
47
- Fee : defaultDeployTxFee ,
98
+ Fee : txFee ,
48
99
}
49
100
50
101
// Fetch the next account nonce
0 commit comments