hermez-node/eth/auction.go
eth/auction.goRaw
1package eth
2
3import (
4 "math/big"
5
6 ethCommon "github.com/ethereum/go-ethereum/common"
7 "github.com/ethereum/go-ethereum/core/types"
8 "github.com/ethereum/go-ethereum/ethclient"
9 HermezAuctionProtocol "github.com/hermeznetwork/hermez-node/eth/contracts/auction"
10)
11
12// AuctionConstants are the constants of the Rollup Smart Contract
13type AuctionConstants struct {
14 // Blocks to wait before starting with the first slot
15 DelayGenesis uint16
16 // Blocks per slot
17 BlocksPerSlot uint8
18 // Minimum bid when no one has bid yet
19 InitialMinimalBidding *big.Int
20 // First block where the first slot begins
21 GenesisBlockNum int64
22 // Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee
23 GovernanceAddress ethCommon.Address
24 // ERC777 token with which the bids will be made
25 TokenHEZ ethCommon.Address
26 // HermezRollup smartcontract address
27 HermezRollup ethCommon.Address
28}
29
30// SlotState is the state of a slot
31type SlotState struct {
32 Forger ethCommon.Address
33 BidAmount *big.Int
34 ClosedMinBid *big.Int
35 Fulfilled bool
36}
37
38// Coordinator is the details of the Coordinator identified by the forger address
39type Coordinator struct {
40 WithdrawalAddress ethCommon.Address
41 URL string
42}
43
44// AuctionVariables are the variables of the Auction Smart Contract
45type AuctionVariables struct {
46 // Boot Coordinator Address
47 DonationAddress ethCommon.Address
48 // Boot Coordinator Address
49 BootCoordinator ethCommon.Address
50 // The minimum bid value in a series of 6 slots
51 MinBidEpoch [6]*big.Int
52 // Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
53 ClosedAuctionSlots uint16
54 // Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
55 OpenAuctionSlots uint16
56 // How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
57 AllocationRatio [3]uint8
58 // Minimum outbid (percentage) over the previous one to consider it valid
59 Outbidding uint8
60 // Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
61 SlotDeadline uint8
62}
63
64// AuctionState represents the state of the Rollup in the Smart Contract
65type AuctionState struct {
66 // Mapping to control slot state
67 Slots map[int64]*SlotState
68 // Mapping to control balances pending to claim
69 PendingBalances map[ethCommon.Address]*big.Int
70 // Mapping to register all the coordinators. The address used for the mapping is the forger address
71 Coordinators map[ethCommon.Address]*Coordinator
72}
73
74// AuctionEventNewBid is an event of the Auction Smart Contract
75type AuctionEventNewBid struct {
76 Slot int64
77 BidAmount *big.Int
78 CoordinatorForger ethCommon.Address
79}
80
81// AuctionEventNewSlotDeadline is an event of the Auction Smart Contract
82type AuctionEventNewSlotDeadline struct {
83 NewSlotDeadline uint8
84}
85
86// AuctionEventNewClosedAuctionSlots is an event of the Auction Smart Contract
87type AuctionEventNewClosedAuctionSlots struct {
88 NewClosedAuctionSlots uint16
89}
90
91// AuctionEventNewOutbidding is an event of the Auction Smart Contract
92type AuctionEventNewOutbidding struct {
93 NewOutbidding uint8
94}
95
96// AuctionEventNewDonationAddress is an event of the Auction Smart Contract
97type AuctionEventNewDonationAddress struct {
98 NewDonationAddress ethCommon.Address
99}
100
101// AuctionEventNewBootCoordinator is an event of the Auction Smart Contract
102type AuctionEventNewBootCoordinator struct {
103 NewBootCoordinator ethCommon.Address
104}
105
106// AuctionEventNewOpenAuctionSlots is an event of the Auction Smart Contract
107type AuctionEventNewOpenAuctionSlots struct {
108 NewOpenAuctionSlots uint16
109}
110
111// AuctionEventNewAllocationRatio is an event of the Auction Smart Contract
112type AuctionEventNewAllocationRatio struct {
113 NewAllocationRatio [3]uint8
114}
115
116// AuctionEventNewCoordinator is an event of the Auction Smart Contract
117type AuctionEventNewCoordinator struct {
118 ForgerAddress ethCommon.Address
119 WithdrawalAddress ethCommon.Address
120 URL string
121}
122
123// AuctionEventCoordinatorUpdated is an event of the Auction Smart Contract
124type AuctionEventCoordinatorUpdated struct {
125 ForgerAddress ethCommon.Address
126 WithdrawalAddress ethCommon.Address
127 URL string
128}
129
130// AuctionEventNewForgeAllocated is an event of the Auction Smart Contract
131type AuctionEventNewForgeAllocated struct {
132 Forger ethCommon.Address
133 CurrentSlot int64
134 BurnAmount *big.Int
135 DonationAmount *big.Int
136 GovernanceAmount *big.Int
137}
138
139// AuctionEventNewMinBidEpoch is an event of the Auction Smart Contract
140type AuctionEventNewMinBidEpoch struct {
141 SlotEpoch int64
142 NewInitialMinBid *big.Int
143}
144
145// AuctionEventNewForge is an event of the Auction Smart Contract
146type AuctionEventNewForge struct {
147 Forger ethCommon.Address
148 CurrentSlot int64
149}
150
151// AuctionEventHEZClaimed is an event of the Auction Smart Contract
152type AuctionEventHEZClaimed struct {
153 Owner ethCommon.Address
154 Amount *big.Int
155}
156
157// AuctionEvents is the list of events in a block of the Auction Smart Contract
158type AuctionEvents struct { //nolint:structcheck
159 NewBid []AuctionEventNewBid
160 NewSlotDeadline []AuctionEventNewSlotDeadline
161 NewClosedAuctionSlots []AuctionEventNewClosedAuctionSlots
162 NewOutbidding []AuctionEventNewOutbidding
163 NewDonationAddress []AuctionEventNewDonationAddress
164 NewBootCoordinator []AuctionEventNewBootCoordinator
165 NewOpenAuctionSlots []AuctionEventNewOpenAuctionSlots
166 NewAllocationRatio []AuctionEventNewAllocationRatio
167 NewCoordinator []AuctionEventNewCoordinator
168 CoordinatorUpdated []AuctionEventCoordinatorUpdated
169 NewForgeAllocated []AuctionEventNewForgeAllocated
170 NewMinBidEpoch []AuctionEventNewMinBidEpoch
171 NewForge []AuctionEventNewForge
172 HEZClaimed []AuctionEventHEZClaimed
173}
174
175// NewAuctionEvents creates an empty AuctionEvents with the slices initialized.
176func NewAuctionEvents() AuctionEvents {
177 return AuctionEvents{
178 NewBid: make([]AuctionEventNewBid, 0),
179 NewSlotDeadline: make([]AuctionEventNewSlotDeadline, 0),
180 NewClosedAuctionSlots: make([]AuctionEventNewClosedAuctionSlots, 0),
181 NewOutbidding: make([]AuctionEventNewOutbidding, 0),
182 NewDonationAddress: make([]AuctionEventNewDonationAddress, 0),
183 NewBootCoordinator: make([]AuctionEventNewBootCoordinator, 0),
184 NewOpenAuctionSlots: make([]AuctionEventNewOpenAuctionSlots, 0),
185 NewAllocationRatio: make([]AuctionEventNewAllocationRatio, 0),
186 NewCoordinator: make([]AuctionEventNewCoordinator, 0),
187 CoordinatorUpdated: make([]AuctionEventCoordinatorUpdated, 0),
188 NewForgeAllocated: make([]AuctionEventNewForgeAllocated, 0),
189 NewMinBidEpoch: make([]AuctionEventNewMinBidEpoch, 0),
190 NewForge: make([]AuctionEventNewForge, 0),
191 HEZClaimed: make([]AuctionEventHEZClaimed, 0),
192 }
193}
194
195// AuctionInterface is the inteface to to Auction Smart Contract
196type AuctionInterface interface {
197 //
198 // Smart Contract Methods
199 //
200
201 // Getter/Setter, where Setter is onlyOwner
202 AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error)
203 AuctionGetSlotDeadline() (uint8, error)
204 AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error)
205 AuctionGetOpenAuctionSlots() (uint16, error)
206 AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error)
207 AuctionGetClosedAuctionSlots() (uint16, error)
208 AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error)
209 AuctionGetOutbidding() (uint8, error)
210 AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error)
211 AuctionGetAllocationRatio() ([3]uint8, error)
212 AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error)
213 AuctionGetDonationAddress() (*ethCommon.Address, error)
214 AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error)
215 AuctionGetBootCoordinator() (*ethCommon.Address, error)
216 AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error)
217
218 // Coordinator Management
219 AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error)
220 AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error)
221 AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error)
222
223 // Slot Info
224 AuctionGetCurrentSlotNumber() (int64, error)
225 AuctionGetMinBidBySlot(slot int64) (*big.Int, error)
226 AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error)
227
228 // Bidding
229 // AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int,
230 // userData, operatorData []byte) error // Only called from another smart contract
231 AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error)
232 AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool,
233 maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error)
234
235 // Forge
236 AuctionCanForge(forger ethCommon.Address) (bool, error)
237 // AuctionForge(forger ethCommon.Address) (bool, error) // Only called from another smart contract
238
239 // Fees
240 AuctionClaimHEZ() (*types.Transaction, error)
241
242 //
243 // Smart Contract Status
244 //
245
246 AuctionConstants() (*AuctionConstants, error)
247 AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error)
248}
249
250//
251// Implementation
252//
253
254// AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
255type AuctionClient struct {
256 client *EthereumClient
257 address ethCommon.Address
258}
259
260// NewAuctionClient creates a new AuctionClient
261func NewAuctionClient(client *EthereumClient, address ethCommon.Address) *AuctionClient {
262 return &AuctionClient{
263 client: client,
264 address: address,
265 }
266}
267
268// AuctionSetSlotDeadline is the interface to call the smart contract function
269func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
270 return nil, errTODO
271}
272
273// AuctionGetSlotDeadline is the interface to call the smart contract function
274func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) {
275 var slotDeadline uint8
276 if err := c.client.Call(func(ec *ethclient.Client) error {
277 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
278 if err != nil {
279 return err
280 }
281 slotDeadline, err = auction.GetSlotDeadline(nil)
282 return err
283 }); err != nil {
284 return 0, err
285 }
286 return slotDeadline, nil
287}
288
289// AuctionSetOpenAuctionSlots is the interface to call the smart contract function
290func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
291 return nil, errTODO
292}
293
294// AuctionGetOpenAuctionSlots is the interface to call the smart contract function
295func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) {
296 var openAuctionSlots uint16
297 if err := c.client.Call(func(ec *ethclient.Client) error {
298 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
299 if err != nil {
300 return err
301 }
302 openAuctionSlots, err = auction.GetOpenAuctionSlots(nil)
303 return err
304 }); err != nil {
305 return 0, err
306 }
307 return openAuctionSlots, nil
308}
309
310// AuctionSetClosedAuctionSlots is the interface to call the smart contract function
311func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
312 return nil, errTODO
313}
314
315// AuctionGetClosedAuctionSlots is the interface to call the smart contract function
316func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) {
317 var closedAuctionSlots uint16
318 if err := c.client.Call(func(ec *ethclient.Client) error {
319 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
320 if err != nil {
321 return err
322 }
323 closedAuctionSlots, err = auction.GetClosedAuctionSlots(nil)
324 return err
325 }); err != nil {
326 return 0, err
327 }
328 return closedAuctionSlots, nil
329}
330
331// AuctionSetOutbidding is the interface to call the smart contract function
332func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error) {
333 return nil, errTODO
334}
335
336// AuctionGetOutbidding is the interface to call the smart contract function
337func (c *AuctionClient) AuctionGetOutbidding() (uint8, error) {
338 var outbidding uint8
339 if err := c.client.Call(func(ec *ethclient.Client) error {
340 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
341 if err != nil {
342 return err
343 }
344 outbidding, err = auction.GetOutbidding(nil)
345 return err
346 }); err != nil {
347 return 0, err
348 }
349 return outbidding, nil
350}
351
352// AuctionSetAllocationRatio is the interface to call the smart contract function
353func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error) {
354 return nil, errTODO
355}
356
357// AuctionGetAllocationRatio is the interface to call the smart contract function
358func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint8, error) {
359 var allocationRation [3]uint8
360 if err := c.client.Call(func(ec *ethclient.Client) error {
361 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
362 if err != nil {
363 return err
364 }
365 allocationRation, err = auction.GetAllocationRatio(nil)
366 return err
367 }); err != nil {
368 return [3]uint8{}, err
369 }
370 return allocationRation, nil
371}
372
373// AuctionSetDonationAddress is the interface to call the smart contract function
374func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
375 return nil, errTODO
376}
377
378// AuctionGetDonationAddress is the interface to call the smart contract function
379func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) {
380 var donationAddress ethCommon.Address
381 if err := c.client.Call(func(ec *ethclient.Client) error {
382 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
383 if err != nil {
384 return err
385 }
386 donationAddress, err = auction.GetDonationAddress(nil)
387 return err
388 }); err != nil {
389 return nil, err
390 }
391 return &donationAddress, nil
392}
393
394// AuctionSetBootCoordinator is the interface to call the smart contract function
395func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
396 return nil, errTODO
397}
398
399// AuctionGetBootCoordinator is the interface to call the smart contract function
400func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
401 var bootCoordinator ethCommon.Address
402 if err := c.client.Call(func(ec *ethclient.Client) error {
403 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
404 if err != nil {
405 return err
406 }
407 bootCoordinator, err = auction.GetBootCoordinator(nil)
408 return err
409 }); err != nil {
410 return nil, err
411 }
412 return &bootCoordinator, nil
413}
414
415// AuctionChangeEpochMinBid is the interface to call the smart contract function
416func (c *AuctionClient) AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
417 return nil, errTODO
418}
419
420// AuctionRegisterCoordinator is the interface to call the smart contract function
421func (c *AuctionClient) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) {
422 return nil, errTODO
423}
424
425// AuctionIsRegisteredCoordinator is the interface to call the smart contract function
426func (c *AuctionClient) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
427 return false, errTODO
428}
429
430// AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
431func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
432 return nil, errTODO
433}
434
435// AuctionGetCurrentSlotNumber is the interface to call the smart contract function
436func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) {
437 var _currentSlotNumber *big.Int
438 if err := c.client.Call(func(ec *ethclient.Client) error {
439 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
440 if err != nil {
441 return err
442 }
443 _currentSlotNumber, err = auction.GetCurrentSlotNumber(nil)
444 return err
445 }); err != nil {
446 return 0, err
447 }
448 currentSlotNumber := _currentSlotNumber.Int64()
449 return currentSlotNumber, nil
450}
451
452// AuctionGetMinBidBySlot is the interface to call the smart contract function
453func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
454 var minBid *big.Int
455 if err := c.client.Call(func(ec *ethclient.Client) error {
456 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
457 if err != nil {
458 return err
459 }
460 slotToSend := big.NewInt(slot)
461 minBid, err = auction.GetMinBidBySlot(nil, slotToSend)
462 return err
463 }); err != nil {
464 return big.NewInt(0), err
465 }
466 return minBid, nil
467}
468
469// AuctionGetMinBidEpoch is the interface to call the smart contract function
470func (c *AuctionClient) AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error) {
471 var minBidEpoch *big.Int
472 if err := c.client.Call(func(ec *ethclient.Client) error {
473 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
474 if err != nil {
475 return err
476 }
477 minBidEpoch, err = auction.GetMinBidEpoch(nil, epoch)
478 return err
479 }); err != nil {
480 return big.NewInt(0), err
481 }
482 return minBidEpoch, nil
483}
484
485// AuctionTokensReceived is the interface to call the smart contract function
486// func (c *AuctionClient) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
487// return errTODO
488// }
489
490// AuctionBid is the interface to call the smart contract function
491func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
492 return nil, errTODO
493}
494
495// AuctionMultiBid is the interface to call the smart contract function
496func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
497 return nil, errTODO
498}
499
500// AuctionCanForge is the interface to call the smart contract function
501func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address) (bool, error) {
502 return false, errTODO
503}
504
505// AuctionForge is the interface to call the smart contract function
506// func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (bool, error) {
507// return false, errTODO
508// }
509
510// AuctionClaimHEZ is the interface to call the smart contract function
511func (c *AuctionClient) AuctionClaimHEZ() (*types.Transaction, error) {
512 return nil, errTODO
513}
514
515// AuctionConstants returns the Constants of the Auction Smart Contract
516func (c *AuctionClient) AuctionConstants() (*AuctionConstants, error) {
517 return nil, errTODO
518}
519
520// AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
521func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error) {
522 return nil, nil, errTODO
523}