/
utils.go
266 lines (217 loc) · 5.93 KB
/
utils.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package auction
import (
"context"
"math/big"
"github.com/Seb369888/poolsea-go/auction"
"github.com/Seb369888/poolsea-go/network"
"github.com/Seb369888/poolsea-go/rocketpool"
"github.com/Seb369888/poolsea-go/settings/protocol"
"github.com/Seb369888/poolsea-go/utils/eth"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/sync/errgroup"
"github.com/Seb369888/smartnode/shared/types/api"
)
// Settings
const LotCountDetailsBatchSize = 10
const LotDetailsBatchSize = 10
// Lot count details
type lotCountDetails struct {
AddressHasBid bool
Cleared bool
HasRemainingRpl bool
RplRecovered bool
}
// Check if bidding has ended for a lot
func getLotBiddingEnded(rp *rocketpool.RocketPool, lotIndex uint64) (bool, error) {
// Data
var wg errgroup.Group
var currentBlock uint64
var lotEndBlock uint64
// Get current block
wg.Go(func() error {
header, err := rp.Client.HeaderByNumber(context.Background(), nil)
if err == nil {
currentBlock = header.Number.Uint64()
}
return err
})
// Get lot end block
wg.Go(func() error {
var err error
lotEndBlock, err = auction.GetLotEndBlock(rp, lotIndex, nil)
return err
})
// Wait for data
if err := wg.Wait(); err != nil {
return false, err
}
// Return
return (currentBlock >= lotEndBlock), nil
}
// Check whether sufficient remaining RPL is available to create a lot
func getSufficientRemainingRPLForLot(rp *rocketpool.RocketPool) (bool, error) {
// Data
var wg errgroup.Group
var remainingRplBalance *big.Int
var lotMinimumEthValue *big.Int
var rplPrice *big.Int
// Get data
wg.Go(func() error {
var err error
remainingRplBalance, err = auction.GetRemainingRPLBalance(rp, nil)
return err
})
wg.Go(func() error {
var err error
lotMinimumEthValue, err = protocol.GetLotMinimumEthValue(rp, nil)
return err
})
wg.Go(func() error {
var err error
rplPrice, err = network.GetRPLPrice(rp, nil)
return err
})
// Wait for data
if err := wg.Wait(); err != nil {
return false, err
}
// Calculate lot minimum RPL amount
var tmp big.Int
var lotMinimumRplAmount big.Int
tmp.Mul(lotMinimumEthValue, eth.EthToWei(1))
lotMinimumRplAmount.Quo(&tmp, rplPrice)
// Return
return (remainingRplBalance.Cmp(&lotMinimumRplAmount) >= 0), nil
}
// Get all lot count details
func getAllLotCountDetails(rp *rocketpool.RocketPool, bidderAddress common.Address) ([]lotCountDetails, error) {
// Get lot count
lotCount, err := auction.GetLotCount(rp, nil)
if err != nil {
return []lotCountDetails{}, err
}
// Load details in batches
details := make([]lotCountDetails, lotCount)
for bsi := uint64(0); bsi < lotCount; bsi += LotCountDetailsBatchSize {
// Get batch start & end index
lsi := bsi
lei := bsi + LotCountDetailsBatchSize
if lei > lotCount {
lei = lotCount
}
// Load details
var wg errgroup.Group
for li := lsi; li < lei; li++ {
li := li
wg.Go(func() error {
lotDetails, err := getLotCountDetails(rp, bidderAddress, li)
if err == nil {
details[li] = lotDetails
}
return err
})
}
if err := wg.Wait(); err != nil {
return []lotCountDetails{}, err
}
}
// Return
return details, nil
}
// Get a lot's count details
func getLotCountDetails(rp *rocketpool.RocketPool, bidderAddress common.Address, lotIndex uint64) (lotCountDetails, error) {
// Data
var wg errgroup.Group
var addressBidAmount *big.Int
var cleared bool
var remainingRpl *big.Int
var rplRecovered bool
// Get address bid amount
wg.Go(func() error {
var err error
addressBidAmount, err = auction.GetLotAddressBidAmount(rp, lotIndex, bidderAddress, nil)
return err
})
// Get lot cleared status
wg.Go(func() error {
var err error
cleared, err = auction.GetLotIsCleared(rp, lotIndex, nil)
return err
})
// Get lot remaining RPL amount
wg.Go(func() error {
var err error
remainingRpl, err = auction.GetLotRemainingRPLAmount(rp, lotIndex, nil)
return err
})
// Get lot RPL recovered status
wg.Go(func() error {
var err error
rplRecovered, err = auction.GetLotRPLRecovered(rp, lotIndex, nil)
return err
})
// Wait for data
if err := wg.Wait(); err != nil {
return lotCountDetails{}, err
}
// Return
return lotCountDetails{
AddressHasBid: (addressBidAmount.Cmp(big.NewInt(0)) > 0),
Cleared: cleared,
HasRemainingRpl: (remainingRpl.Cmp(big.NewInt(0)) > 0),
RplRecovered: rplRecovered,
}, nil
}
// Get all lot details
func getAllLotDetails(rp *rocketpool.RocketPool, bidderAddress common.Address) ([]api.LotDetails, error) {
// Get lot count
lotCount, err := auction.GetLotCount(rp, nil)
if err != nil {
return []api.LotDetails{}, err
}
// Load details in batches
details := make([]api.LotDetails, lotCount)
for bsi := uint64(0); bsi < lotCount; bsi += LotDetailsBatchSize {
// Get batch start & end index
lsi := bsi
lei := bsi + LotDetailsBatchSize
if lei > lotCount {
lei = lotCount
}
// Load details
var wg errgroup.Group
for li := lsi; li < lei; li++ {
li := li
wg.Go(func() error {
lotDetails, err := getLotDetails(rp, bidderAddress, li)
if err == nil {
details[li] = lotDetails
}
return err
})
}
if err := wg.Wait(); err != nil {
return []api.LotDetails{}, err
}
}
// Return
return details, nil
}
// Get a lot's details
func getLotDetails(rp *rocketpool.RocketPool, bidderAddress common.Address, lotIndex uint64) (api.LotDetails, error) {
// Get lot details
details, err := auction.GetLotDetailsWithBids(rp, lotIndex, bidderAddress, nil)
if err != nil {
return api.LotDetails{}, err
}
// Check lot conditions
addressHasBid := (details.AddressBidAmount.Cmp(big.NewInt(0)) > 0)
hasRemainingRpl := (details.RemainingRPLAmount.Cmp(big.NewInt(0)) > 0)
// Return
return api.LotDetails{
Details: details,
ClaimAvailable: (addressHasBid && details.Cleared),
BiddingAvailable: (!details.Cleared && hasRemainingRpl),
RPLRecoveryAvailable: (details.Cleared && hasRemainingRpl && !details.RPLRecovered),
}, nil
}