-
Notifications
You must be signed in to change notification settings - Fork 3
/
submit-tx.go
82 lines (70 loc) · 1.89 KB
/
submit-tx.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
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
package tx
import (
"errors"
"fmt"
_ "time/tzdata"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/gorilla/mux"
"github.com/nodeset-org/hyperdrive/hyperdrive-daemon/server/utils"
"github.com/nodeset-org/hyperdrive/shared/types/api"
)
// ===============
// === Factory ===
// ===============
type txSubmitTxContextFactory struct {
handler *TxHandler
}
func (f *txSubmitTxContextFactory) Create(body api.SubmitTxBody) (*txSubmitTxContext, error) {
c := &txSubmitTxContext{
handler: f.handler,
body: body,
}
// Validate the submission
if body.Submission.TxInfo == nil {
return nil, fmt.Errorf("submission TX info must be set")
}
if body.Submission.GasLimit == 0 {
return nil, fmt.Errorf("submission gas limit must be set")
}
if body.MaxFee == nil {
return nil, fmt.Errorf("submission max fee must be set")
}
if body.MaxPriorityFee == nil {
return nil, fmt.Errorf("submission max priority fee must be set")
}
return c, nil
}
func (f *txSubmitTxContextFactory) RegisterRoute(router *mux.Router) {
utils.RegisterQuerylessPost[*txSubmitTxContext, api.SubmitTxBody, api.TxData](
router, "submit-tx", f, f.handler.serviceProvider,
)
}
// ===============
// === Context ===
// ===============
type txSubmitTxContext struct {
handler *TxHandler
body api.SubmitTxBody
}
func (c *txSubmitTxContext) PrepareData(data *api.TxData, opts *bind.TransactOpts) error {
sp := c.handler.serviceProvider
txMgr := sp.GetTransactionManager()
err := errors.Join(
sp.RequireWalletReady(),
)
if err != nil {
return err
}
if c.body.Nonce != nil {
opts.Nonce = c.body.Nonce
}
opts.GasLimit = c.body.Submission.GasLimit
opts.GasFeeCap = c.body.MaxFee
opts.GasTipCap = c.body.MaxPriorityFee
tx, err := txMgr.ExecuteTransaction(c.body.Submission.TxInfo, opts)
if err != nil {
return fmt.Errorf("error submitting transaction: %w", err)
}
data.TxHash = tx.Hash()
return nil
}