forked from tranvictor/walletarmy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
201 lines (172 loc) · 4.9 KB
/
Copy pathrequest.go
File metadata and controls
201 lines (172 loc) · 4.9 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package walletarmy
import (
"math/big"
"time"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/tranvictor/jarvis/networks"
)
// TxRequest represents a transaction request with builder pattern
type TxRequest struct {
wm *WalletManager
numRetries int
sleepDuration time.Duration
txCheckInterval time.Duration
// Transaction parameters
txType uint8
from, to common.Address
value *big.Int
gasLimit uint64
extraGasLimit uint64
gasPrice float64
extraGasPrice float64
tipCapGwei float64
extraTipCapGwei float64
maxGasPrice float64
maxTipCap float64
data []byte
network networks.Network
// Hooks
beforeSignAndBroadcastHook Hook
afterSignAndBroadcastHook Hook
gasEstimationFailedHook GasEstimationFailedHook
abis []abi.ABI
}
// R creates a new transaction request (similar to go-resty's R() method)
func (wm *WalletManager) R() *TxRequest {
return &TxRequest{
wm: wm,
value: big.NewInt(0), // default value
network: networks.EthereumMainnet, // default network is Ethereum Mainnet
}
}
// SetNumRetries sets the number of retries
func (r *TxRequest) SetNumRetries(numRetries int) *TxRequest {
r.numRetries = numRetries
return r
}
// SetSleepDuration sets the sleep duration
func (r *TxRequest) SetSleepDuration(sleepDuration time.Duration) *TxRequest {
r.sleepDuration = sleepDuration
return r
}
// SetTxCheckInterval sets the transaction check interval
func (r *TxRequest) SetTxCheckInterval(txCheckInterval time.Duration) *TxRequest {
r.txCheckInterval = txCheckInterval
return r
}
// SetTxType sets the transaction type
func (r *TxRequest) SetTxType(txType uint8) *TxRequest {
r.txType = txType
return r
}
// SetFrom sets the from address
func (r *TxRequest) SetFrom(from common.Address) *TxRequest {
r.from = from
return r
}
// SetTo sets the to address
func (r *TxRequest) SetTo(to common.Address) *TxRequest {
r.to = to
return r
}
// SetValue sets the transaction value
func (r *TxRequest) SetValue(value *big.Int) *TxRequest {
if value != nil {
r.value = value
}
return r
}
// SetGasLimit sets the gas limit
func (r *TxRequest) SetGasLimit(gasLimit uint64) *TxRequest {
r.gasLimit = gasLimit
return r
}
// SetExtraGasLimit sets the extra gas limit
func (r *TxRequest) SetExtraGasLimit(extraGasLimit uint64) *TxRequest {
r.extraGasLimit = extraGasLimit
return r
}
// SetGasPrice sets the gas price
func (r *TxRequest) SetGasPrice(gasPrice float64) *TxRequest {
r.gasPrice = gasPrice
return r
}
// SetExtraGasPrice sets the extra gas price
func (r *TxRequest) SetExtraGasPrice(extraGasPrice float64) *TxRequest {
r.extraGasPrice = extraGasPrice
return r
}
// SetTipCapGwei sets the tip cap in gwei
func (r *TxRequest) SetTipCapGwei(tipCapGwei float64) *TxRequest {
r.tipCapGwei = tipCapGwei
return r
}
// SetExtraTipCapGwei sets the extra tip cap in gwei
func (r *TxRequest) SetExtraTipCapGwei(extraTipCapGwei float64) *TxRequest {
r.extraTipCapGwei = extraTipCapGwei
return r
}
// SetMaxGasPrice sets the maximum gas price limit
func (r *TxRequest) SetMaxGasPrice(maxGasPrice float64) *TxRequest {
r.maxGasPrice = maxGasPrice
return r
}
// SetMaxTipCap sets the maximum tip cap limit
func (r *TxRequest) SetMaxTipCap(maxTipCap float64) *TxRequest {
r.maxTipCap = maxTipCap
return r
}
// SetData sets the transaction data
func (r *TxRequest) SetData(data []byte) *TxRequest {
r.data = data
return r
}
// SetNetwork sets the network
func (r *TxRequest) SetNetwork(network networks.Network) *TxRequest {
r.network = network
return r
}
// SetAbis sets the abis
func (r *TxRequest) SetAbis(abis ...abi.ABI) *TxRequest {
r.abis = abis
return r
}
// SetBeforeSignAndBroadcastHook sets the hook to be called before signing and broadcasting
func (r *TxRequest) SetBeforeSignAndBroadcastHook(hook Hook) *TxRequest {
r.beforeSignAndBroadcastHook = hook
return r
}
// SetAfterSignAndBroadcastHook sets the hook to be called after signing and broadcasting
func (r *TxRequest) SetAfterSignAndBroadcastHook(hook Hook) *TxRequest {
r.afterSignAndBroadcastHook = hook
return r
}
// SetGasEstimationFailedHook sets the hook to be called when gas estimation fails
func (r *TxRequest) SetGasEstimationFailedHook(hook GasEstimationFailedHook) *TxRequest {
r.gasEstimationFailedHook = hook
return r
}
// Execute executes the transaction request
func (r *TxRequest) Execute() (*types.Transaction, *types.Receipt, error) {
return r.wm.EnsureTxWithHooks(
r.numRetries,
r.sleepDuration,
r.txCheckInterval,
r.txType,
r.from,
r.to,
r.value,
r.gasLimit, r.extraGasLimit,
r.gasPrice, r.extraGasPrice,
r.tipCapGwei, r.extraTipCapGwei,
r.maxGasPrice, r.maxTipCap,
r.data,
r.network,
r.beforeSignAndBroadcastHook,
r.afterSignAndBroadcastHook,
r.abis,
r.gasEstimationFailedHook,
)
}