-
Notifications
You must be signed in to change notification settings - Fork 0
/
neth_large_request.go
189 lines (162 loc) · 6.49 KB
/
neth_large_request.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
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
// description:
// @author renshiwei
// Date: 2023/5/24
package exitscan
import (
"context"
"github.com/NodeDAO/operator-sdk/common/logger"
"github.com/NodeDAO/operator-sdk/contracts"
"github.com/NodeDAO/operator-sdk/contracts/withdrawalRequest"
"github.com/NodeDAO/operator-sdk/eth1"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"math/big"
"strings"
)
// NETHExitScan nETH exit scan
// Only largeRequest nETH needs to be scanned to exit
type NETHExitScan struct {
// param
network string
// init
elClient *eth1.EthClient
// contracts
withdrawalRequestContract *contracts.WithdrawalRequestContract
vnftContract *contracts.VnftContract
liqContract *contracts.LiqContract
}
// NewNETHExitScan new nETH exit scan
func NewNETHExitScan(ctx context.Context, network, elAddr string) (*NETHExitScan, error) {
var err error
elClient, err := eth1.NewEthClient(ctx, elAddr)
if err != nil {
return nil, errors.Wrapf(err, "Fail to new eth client. network:%s", network)
}
withdrawalRequestContract, err := contracts.NewWithdrawalRequestContract(network, elClient.Client)
if err != nil {
return nil, errors.Wrapf(err, "Fail to new withdrawalRequestContract. network:%s", network)
}
vnftContract, err := contracts.NewVnftContract(network, elClient.Client)
if err != nil {
return nil, errors.Wrapf(err, "Fail to new vnftContract. network:%s", network)
}
liqContract, err := contracts.NewLiqContract(network, elClient.Client)
if err != nil {
return nil, errors.Wrapf(err, "Fail to new liqContract. network:%s", network)
}
return &NETHExitScan{
network: network,
elClient: elClient,
withdrawalRequestContract: withdrawalRequestContract,
vnftContract: vnftContract,
liqContract: liqContract,
}, nil
}
// WithdrawalRequestScan Scanning the smart contract requires processing the exiting WithdrawalRequest.
// !!! Handling exits is delayed, and additional operations are required to mark and filter the WithdrawalRequest,
// the simplest way is to use db, see example for this part.
func (s *NETHExitScan) WithdrawalRequestScan(operatorId *big.Int) ([]*WithdrawalRequest, error) {
withdrawalInfos := make([]*WithdrawalRequest, 0)
// Iterate through the withdrawal Request in the contract that is not handled by the current operator.
// The current withdrawalRequestContract method 'getWithdrawalOfOperator' does not return a requestId and cannot be used at the moment.
i := 0
for {
opId, withdrawHeight, withdrawNethAmount, withdrawExchange, claimEthAmount, owner, isClaim, err := s.withdrawalRequestContract.Contract.GetWithdrawalOfRequestId(nil, big.NewInt(int64(i)))
if err != nil {
if strings.Contains(err.Error(), eth1.CONTRACT_REVENT) {
break
} else {
return nil, errors.Wrapf(err, "Fail to get withdrawalRequestContract GetWithdrawalOfRequestId. network:%s", s.network)
}
}
// If the operatorId is the same as the current operatorId, and the withdrawal has not been claimed, it needs to be exited\
if operatorId.Cmp(opId) == 0 && !isClaim {
withdrawalInfos = append(withdrawalInfos, &WithdrawalRequest{
ID: big.NewInt(int64(i)),
WithdrawalRequestInfo: &withdrawalRequest.WithdrawalRequestWithdrawalInfo{
OperatorId: opId,
WithdrawHeight: withdrawHeight,
WithdrawNethAmount: withdrawNethAmount,
WithdrawExchange: withdrawExchange,
ClaimEthAmount: claimEthAmount,
Owner: owner,
IsClaim: isClaim,
},
})
}
i++
}
return withdrawalInfos, nil
}
// ExitScan Filter for exits
// !!! Use the filtered []WithdrawalRequest to operator
// @param operatorId operator id
func (s *NETHExitScan) ExitScan(operatorId *big.Int) ([]*VnftRecord, error) {
vnfts := make([]*VnftRecord, 0)
// Get the number of active vnft of the operator.
vnftActiveCount, err := s.vnftContract.Contract.GetUserActiveNftCountsOfOperator(nil, operatorId)
if err != nil {
return nil, errors.Wrapf(err, "Fail to get vnftContract GetUserActiveNftCountsOfOperator. network:%s", s.network)
}
if vnftActiveCount.Cmp(big.NewInt(0)) == 0 {
return vnfts, nil
}
// Query the tokenId of all vnfts owned by the LiquidStaking pool
stakingPoolTokenIds, err := s.vnftContract.Contract.ActiveNftsOfStakingPool(nil)
if err != nil {
return nil, errors.Wrapf(err, "Fail to get vnftContract ActiveNftsOfStakingPool. network:%s", s.network)
}
for _, tokenId := range stakingPoolTokenIds {
// Filter based on operatorId
operatorOf, err := s.vnftContract.Contract.OperatorOf(nil, tokenId)
if err != nil {
return nil, errors.Wrapf(err, "Fail to get vnftContract OperatorOf. network:%s", s.network)
}
// If the operatorId is the same as the current operatorId, and the withdrawal has not been claimed, it needs to be exited
if operatorOf.Cmp(operatorId) == 0 {
pubkeyBytes, err := s.vnftContract.Contract.ValidatorOf(nil, tokenId)
if err != nil {
return nil, errors.Wrapf(err, "Fail to get vnftContract ValidatorOf. network:%s", s.network)
}
pubkey := hexutil.Encode(pubkeyBytes)
vnfts = append(vnfts, &VnftRecord{
Network: s.network,
OperatorId: operatorId,
TokenId: tokenId,
Pubkey: pubkey,
Type: NETH,
})
}
if vnftActiveCount.Cmp(big.NewInt(int64(len(vnfts)))) == 0 {
break
}
}
logger.Infof("nETH ExitScan success by contract. stakingPoolTokenIds:%+v", stakingPoolTokenIds)
return vnfts, nil
}
// ExitCounter Calculate the number of validators that need to be exited by a Withdrawal Request
// @param filterWithdrawalRequests A list of offline filtered Withdrawal Requests
// --------------------------------------------------------
// if sumETHAmount = 64 ether, need to exit 2 validator
// if sumETHAmount = 66 ether, need to exit 3 validator
func (s *NETHExitScan) ExitCounter(filterWithdrawalRequests []*WithdrawalRequest) (uint32, error) {
if len(filterWithdrawalRequests) == 0 {
return 0, nil
}
sumETHAmount := big.NewInt(0)
for _, request := range filterWithdrawalRequests {
sumETHAmount = new(big.Int).Add(sumETHAmount, request.WithdrawalRequestInfo.ClaimEthAmount)
}
if sumETHAmount.Cmp(big.NewInt(0)) == 0 {
return 0, nil
}
// Calculate the number of withdrawals The part greater than 32eth needs to withdraw one more
vnftScanCount := 0
div, mod := new(big.Int).DivMod(sumETHAmount, eth1.ETH32(), new(big.Int))
if mod.Cmp(big.NewInt(0)) == 1 {
vnftScanCount = int(div.Uint64()) + 1
} else {
vnftScanCount = int(div.Uint64())
}
return uint32(vnftScanCount), nil
}