8
8
9
9
"github.com/ethereum/go-ethereum/common"
10
10
"github.com/ethereum/go-ethereum/core/types"
11
+ "github.com/rs/zerolog"
11
12
"github.com/rs/zerolog/log"
12
13
13
14
"github.com/sygmaprotocol/sygma-core/chains/evm/client"
@@ -19,6 +20,10 @@ type GasPricer interface {
19
20
GasPrice (priority * uint8 ) ([]* big.Int , error )
20
21
}
21
22
23
+ type GasTracker interface {
24
+ TrackGasUsage (domainID uint8 , gasUsed uint64 , gasPrice * big.Int )
25
+ }
26
+
22
27
type RawTx struct {
23
28
nonce uint64
24
29
to * common.Address
@@ -30,9 +35,26 @@ type RawTx struct {
30
35
creationTime time.Time
31
36
}
32
37
38
+ // GasPrice returns transaction gas price in gwei
39
+ //
40
+ // It returns base fee for both London and legacy transactions.
41
+ func (tx * RawTx ) GasPrice () * big.Int {
42
+ var gasPrice * big.Int
43
+ if len (tx .gasPrice ) == 1 {
44
+ gasPrice = tx .gasPrice [0 ]
45
+ } else {
46
+ gasPrice = tx .gasPrice [1 ]
47
+ }
48
+ return new (big.Int ).Div (gasPrice , big .NewInt (1e9 ))
49
+ }
50
+
33
51
type MonitoredTransactor struct {
52
+ domainID uint8
53
+ log zerolog.Logger
54
+
34
55
txFabric transaction.TxFabric
35
56
gasPriceClient GasPricer
57
+ gasTracker GasTracker
36
58
client client.Client
37
59
38
60
maxGasPrice * big.Int
@@ -49,15 +71,20 @@ type MonitoredTransactor struct {
49
71
// Gas price is increased by increasePercentage param which
50
72
// is a percentage value with which old gas price should be increased (e.g 15)
51
73
func NewMonitoredTransactor (
74
+ domainID uint8 ,
52
75
txFabric transaction.TxFabric ,
53
76
gasPriceClient GasPricer ,
77
+ gasTracker GasTracker ,
54
78
client client.Client ,
55
79
maxGasPrice * big.Int ,
56
80
increasePercentage * big.Int ,
57
81
) * MonitoredTransactor {
58
82
return & MonitoredTransactor {
83
+ domainID : domainID ,
84
+ log : log .With ().Uint8 ("domainID" , domainID ).Logger (),
59
85
client : client ,
60
86
gasPriceClient : gasPriceClient ,
87
+ gasTracker : gasTracker ,
61
88
txFabric : txFabric ,
62
89
pendingTxns : make (map [common.Hash ]RawTx ),
63
90
maxGasPrice : maxGasPrice ,
@@ -143,18 +170,20 @@ func (t *MonitoredTransactor) Monitor(
143
170
for oldHash , tx := range pendingTxCopy {
144
171
receipt , err := t .client .TransactionReceipt (context .Background (), oldHash )
145
172
if err == nil {
173
+ t .gasTracker .TrackGasUsage (t .domainID , receipt .GasUsed , tx .GasPrice ())
174
+
146
175
if receipt .Status == types .ReceiptStatusSuccessful {
147
- log .Info ().Uint64 ("nonce" , tx .nonce ).Msgf ("Executed transaction %s with nonce %d" , oldHash , tx .nonce )
176
+ t . log .Info ().Uint64 ("nonce" , tx .nonce ).Msgf ("Executed transaction %s with nonce %d" , oldHash , tx .nonce )
148
177
} else {
149
- log .Error ().Uint64 ("nonce" , tx .nonce ).Msgf ("Transaction %s failed on chain" , oldHash )
178
+ t . log .Error ().Uint64 ("nonce" , tx .nonce ).Msgf ("Transaction %s failed on chain" , oldHash )
150
179
}
151
180
152
181
delete (t .pendingTxns , oldHash )
153
182
continue
154
183
}
155
184
156
185
if time .Since (tx .creationTime ) > txTimeout {
157
- log .Error ().Uint64 ("nonce" , tx .nonce ).Msgf ("Transaction %s has timed out" , oldHash )
186
+ t . log .Error ().Uint64 ("nonce" , tx .nonce ).Msgf ("Transaction %s has timed out" , oldHash )
158
187
delete (t .pendingTxns , oldHash )
159
188
continue
160
189
}
@@ -164,7 +193,7 @@ func (t *MonitoredTransactor) Monitor(
164
193
165
194
hash , err := t .resendTransaction (& tx )
166
195
if err != nil {
167
- log .Warn ().Uint64 ("nonce" , tx .nonce ).Err (err ).Msgf ("Failed resending transaction %s" , hash )
196
+ t . log .Warn ().Uint64 ("nonce" , tx .nonce ).Err (err ).Msgf ("Failed resending transaction %s" , hash )
168
197
continue
169
198
}
170
199
@@ -188,7 +217,7 @@ func (t *MonitoredTransactor) resendTransaction(tx *RawTx) (common.Hash, error)
188
217
return common.Hash {}, err
189
218
}
190
219
191
- log .Debug ().Uint64 ("nonce" , tx .nonce ).Msgf ("Resent transaction with hash %s" , hash )
220
+ t . log .Debug ().Uint64 ("nonce" , tx .nonce ).Msgf ("Resent transaction with hash %s" , hash )
192
221
193
222
return hash , nil
194
223
}
0 commit comments