@@ -5,10 +5,13 @@ import (
55
66 "github.com/gnolang/gno/tm2/pkg/crypto"
77 "github.com/gnolang/gno/tm2/pkg/std"
8+ "github.com/gnolang/supernova/internal/common"
89 "github.com/gnolang/supernova/internal/signer"
910 "github.com/schollz/progressbar/v3"
1011)
1112
13+ const gasBuffer = 10_000 // 10k gas
14+
1215// msgFn defines the transaction message constructor
1316type msgFn func (creator std.Account , index int ) std.Msg
1417
@@ -20,6 +23,7 @@ func constructTransactions(
2023 transactions uint64 ,
2124 chainID string ,
2225 getMsg msgFn ,
26+ estimateFn EstimateGasFn ,
2327) ([]* std.Tx , error ) {
2428 var (
2529 txs = make ([]* std.Tx , transactions )
@@ -30,6 +34,53 @@ func constructTransactions(
3034 nonceMap = make (map [uint64 ]uint64 ) // accountNumber -> nonce
3135 )
3236
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 )
3384 fmt .Printf ("\n 🔨 Constructing Transactions 🔨\n \n " )
3485
3586 bar := progressbar .Default (int64 (transactions ), "constructing txs" )
@@ -44,7 +95,7 @@ func constructTransactions(
4495
4596 tx := & std.Tx {
4697 Msgs : []std.Msg {getMsg (creator , i )},
47- Fee : defaultDeployTxFee ,
98+ Fee : txFee ,
4899 }
49100
50101 // Fetch the next account nonce
0 commit comments