/
begin-reduce-bond.go
70 lines (59 loc) · 2.35 KB
/
begin-reduce-bond.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
package minipool
import (
"errors"
"fmt"
"math/big"
"net/url"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/gorilla/mux"
"github.com/rocket-pool/node-manager-core/api/server"
"github.com/rocket-pool/node-manager-core/api/types"
"github.com/rocket-pool/node-manager-core/eth"
"github.com/rocket-pool/node-manager-core/utils/input"
"github.com/rocket-pool/rocketpool-go/v2/minipool"
)
// ===============
// === Factory ===
// ===============
type minipoolBeginReduceBondContextFactory struct {
handler *MinipoolHandler
}
func (f *minipoolBeginReduceBondContextFactory) Create(args url.Values) (*minipoolBeginReduceBondContext, error) {
c := &minipoolBeginReduceBondContext{
handler: f.handler,
}
inputErrs := []error{
server.ValidateArg("new-bond-amount", args, input.ValidateBigInt, &c.newBondAmountWei),
server.ValidateArgBatch("addresses", args, minipoolAddressBatchSize, input.ValidateAddress, &c.minipoolAddresses),
}
return c, errors.Join(inputErrs...)
}
func (f *minipoolBeginReduceBondContextFactory) RegisterRoute(router *mux.Router) {
server.RegisterQuerylessGet[*minipoolBeginReduceBondContext, types.BatchTxInfoData](
router, "begin-reduce-bond", f, f.handler.logger.Logger, f.handler.serviceProvider.ServiceProvider,
)
}
// ===============
// === Context ===
// ===============
type minipoolBeginReduceBondContext struct {
handler *MinipoolHandler
newBondAmountWei *big.Int
minipoolAddresses []common.Address
}
func (c *minipoolBeginReduceBondContext) PrepareData(data *types.BatchTxInfoData, opts *bind.TransactOpts) (types.ResponseStatus, error) {
return prepareMinipoolBatchTxData(c.handler.ctx, c.handler.serviceProvider, c.minipoolAddresses, data, c.CreateTx, "begin-bond-reduce")
}
func (c *minipoolBeginReduceBondContext) CreateTx(mp minipool.IMinipool, opts *bind.TransactOpts) (types.ResponseStatus, *eth.TransactionInfo, error) {
mpv3, success := minipool.GetMinipoolAsV3(mp)
if !success {
mpCommon := mp.Common()
return types.ResponseStatus_InvalidChainState, nil, fmt.Errorf("cannot create v3 binding for minipool %s, version %d", mpCommon.Address.Hex(), mpCommon.Version)
}
txInfo, err := mpv3.BeginReduceBondAmount(c.newBondAmountWei, opts)
if err != nil {
return types.ResponseStatus_Error, nil, err
}
return types.ResponseStatus_Success, txInfo, nil
}