/
stafi_stake.go
113 lines (96 loc) · 2.88 KB
/
stafi_stake.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
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
package task
import (
"encoding/hex"
"fmt"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"github.com/sirupsen/logrus"
"github.com/stafiprotocol/eth-ssv-client/pkg/credential"
"github.com/stafiprotocol/eth-ssv-client/pkg/utils"
)
func (task *Task) checkAndStake() error {
if !task.offchainStateIsLatest() {
return nil
}
poolBalance, err := task.getUsablePoolBalance()
if err != nil {
return err
}
poolBalanceDeci := decimal.NewFromBigInt(poolBalance, 0)
logrus.WithFields(logrus.Fields{
"balance": poolBalanceDeci.String(),
}).Debug("stake-poolBalance")
if poolBalanceDeci.LessThan(minAmountNeedStake) {
return nil
}
// select validators need stake
validatorsNeedStake := make([]*Validator, 0)
for i := 0; i < len(task.validatorsByKeyIndex); i++ {
val := task.validatorsByKeyIndex[i]
if val.statusOnStafi != valStatusMatch {
continue
}
if poolBalanceDeci.LessThan(minAmountNeedStake) {
break
}
validatorsNeedStake = append(validatorsNeedStake, val)
poolBalanceDeci = poolBalanceDeci.Sub(superNodeStakeAmount)
}
lengthOfValidatorsNeedStake := len(validatorsNeedStake)
logrus.WithFields(logrus.Fields{
"stakeLen": lengthOfValidatorsNeedStake,
}).Debug("stake-info")
if lengthOfValidatorsNeedStake == 0 {
return nil
}
// build payload
validatorPubkeys := make([][]byte, lengthOfValidatorsNeedStake)
sigs := make([][]byte, lengthOfValidatorsNeedStake)
dataRoots := make([][32]byte, lengthOfValidatorsNeedStake)
for i, val := range validatorsNeedStake {
credential, err := credential.NewCredential(task.seed, val.keyIndex, superNodeStakeAmount.Div(utils.GweiDeci).BigInt(),
task.chain, task.eth1WithdrawalAdress)
if err != nil {
return err
}
validatorPubkeys[i] = credential.SigningSk.PublicKey().Marshal()
depositData, err := credential.SigningDepositData()
if err != nil {
return err
}
sigBts, err := hex.DecodeString(depositData.Signature)
if err != nil {
return err
}
sigs[i] = sigBts
dataRootBts, err := hex.DecodeString(depositData.DepositDataRoot)
if err != nil {
return err
}
if len(dataRootBts) != 32 {
return fmt.Errorf("dataRoot length %d not match", len(dataRootBts))
}
dataRoots[i] = [32]byte(dataRootBts)
}
// send tx
err = task.connectionOfSuperNodeAccount.LockAndUpdateTxOpts()
if err != nil {
return fmt.Errorf("LockAndUpdateTxOpts err: %s", err)
}
defer task.connectionOfSuperNodeAccount.UnlockTxOpts()
stakeTx, err := task.superNodeContract.Stake(task.connectionOfSuperNodeAccount.TxOpts(), validatorPubkeys, sigs, dataRoots)
if err != nil {
return errors.Wrap(err, "superNodeContract.Stake failed")
}
logrus.WithFields(logrus.Fields{
"txHash": stakeTx.Hash(),
"validatorPubkeys": validatorPubkeys,
"sigs": sigs,
"dataRoots": dataRoots,
}).Info("stake-tx")
err = task.waitTxOk(stakeTx.Hash())
if err != nil {
return err
}
return nil
}