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
71 // Mapping to register all the coordinators. The address used for the mapping is the forger address
72 Coordinators map[ethCommon.Address]Coordinator
73}
74
75// AuctionEventNewBid is an event of the Auction Smart Contract
76type AuctionEventNewBid struct {
77 Slot int64
78 BidAmount *big.Int
79 CoordinatorForger ethCommon.Address
80}
81
82// AuctionEventNewSlotDeadline is an event of the Auction Smart Contract
83type AuctionEventNewSlotDeadline struct {
84 NewSlotDeadline uint8
85}
86
87// AuctionEventNewClosedAuctionSlots is an event of the Auction Smart Contract
88type AuctionEventNewClosedAuctionSlots struct {
89 NewClosedAuctionSlots uint16
90}
91
92// AuctionEventNewOutbidding is an event of the Auction Smart Contract
93type AuctionEventNewOutbidding struct {
94 NewOutbidding uint8
95}
96
97// AuctionEventNewDonationAddress is an event of the Auction Smart Contract
98type AuctionEventNewDonationAddress struct {
99 NewDonationAddress ethCommon.Address
100}
101
102// AuctionEventNewBootCoordinator is an event of the Auction Smart Contract
103type AuctionEventNewBootCoordinator struct {
104 NewBootCoordinator ethCommon.Address
105}
106
107// AuctionEventNewOpenAuctionSlots is an event of the Auction Smart Contract
108type AuctionEventNewOpenAuctionSlots struct {
109 NewOpenAuctionSlots uint16
110}
111
112// AuctionEventNewAllocationRatio is an event of the Auction Smart Contract
113type AuctionEventNewAllocationRatio struct {
114 NewAllocationRatio [3]uint8
115}
116
117// AuctionEventNewCoordinator is an event of the Auction Smart Contract
118type AuctionEventNewCoordinator struct {
119 ForgerAddress ethCommon.Address
120 WithdrawalAddress ethCommon.Address
121 URL string
122}
123
124// AuctionEventCoordinatorUpdated is an event of the Auction Smart Contract
125type AuctionEventCoordinatorUpdated struct {
126 ForgerAddress ethCommon.Address
127 WithdrawalAddress ethCommon.Address
128 URL string
129}
130
131// AuctionEventNewForgeAllocated is an event of the Auction Smart Contract
132type AuctionEventNewForgeAllocated struct {
133 Forger ethCommon.Address
134 CurrentSlot int64
135 BurnAmount *big.Int
136 DonationAmount *big.Int
137 GovernanceAmount *big.Int
138}
139
140// AuctionEventNewMinBidEpoch is an event of the Auction Smart Contract
141type AuctionEventNewMinBidEpoch struct {
142 SlotEpoch int64
143 NewInitialMinBid *big.Int
144}
145
146// AuctionEventNewForge is an event of the Auction Smart Contract
147type AuctionEventNewForge struct {
148 Forger ethCommon.Address
149 CurrentSlot int64
150}
151
152// AuctionEventHEZClaimed is an event of the Auction Smart Contract
153type AuctionEventHEZClaimed struct {
154 Owner ethCommon.Address
155 Amount *big.Int
156}
157
158// AuctionEvents is the list of events in a block of the Auction Smart Contract
159type AuctionEvents struct { //nolint:structcheck
160 NewBid []AuctionEventNewBid
161 NewSlotDeadline []AuctionEventNewSlotDeadline
162 NewClosedAuctionSlots []AuctionEventNewClosedAuctionSlots
163 NewOutbidding []AuctionEventNewOutbidding
164 NewDonationAddress []AuctionEventNewDonationAddress
165 NewBootCoordinator []AuctionEventNewBootCoordinator
166 NewOpenAuctionSlots []AuctionEventNewOpenAuctionSlots
167 NewAllocationRatio []AuctionEventNewAllocationRatio
168 NewCoordinator []AuctionEventNewCoordinator
169 CoordinatorUpdated []AuctionEventCoordinatorUpdated
170 NewForgeAllocated []AuctionEventNewForgeAllocated
171 NewMinBidEpoch []AuctionEventNewMinBidEpoch
172 NewForge []AuctionEventNewForge
173 HEZClaimed []AuctionEventHEZClaimed
174}
175
176// NewAuctionEvents creates an empty AuctionEvents with the slices initialized.
177func NewAuctionEvents() AuctionEvents {
178 return AuctionEvents{
179 NewBid: make([]AuctionEventNewBid, 0),
180 NewSlotDeadline: make([]AuctionEventNewSlotDeadline, 0),
181 NewClosedAuctionSlots: make([]AuctionEventNewClosedAuctionSlots, 0),
182 NewOutbidding: make([]AuctionEventNewOutbidding, 0),
183 NewDonationAddress: make([]AuctionEventNewDonationAddress, 0),
184 NewBootCoordinator: make([]AuctionEventNewBootCoordinator, 0),
185 NewOpenAuctionSlots: make([]AuctionEventNewOpenAuctionSlots, 0),
186 NewAllocationRatio: make([]AuctionEventNewAllocationRatio, 0),
187 NewCoordinator: make([]AuctionEventNewCoordinator, 0),
188 CoordinatorUpdated: make([]AuctionEventCoordinatorUpdated, 0),
189 NewForgeAllocated: make([]AuctionEventNewForgeAllocated, 0),
190 NewMinBidEpoch: make([]AuctionEventNewMinBidEpoch, 0),
191 NewForge: make([]AuctionEventNewForge, 0),
192 HEZClaimed: make([]AuctionEventHEZClaimed, 0),
193 }
194}
195
196// AuctionInterface is the inteface to to Auction Smart Contract
197type AuctionInterface interface {
198 //
199 // Smart Contract Methods
200 //
201
202 // Getter/Setter, where Setter is onlyOwner
203 AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error)
204 AuctionGetSlotDeadline() (uint8, error)
205 AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error)
206 AuctionGetOpenAuctionSlots() (uint16, error)
207 AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error)
208 AuctionGetClosedAuctionSlots() (uint16, error)
209 AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error)
210 AuctionGetOutbidding() (uint8, error)
211 AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error)
212 AuctionGetAllocationRatio() ([3]uint8, error)
213 AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error)
214 AuctionGetDonationAddress() (*ethCommon.Address, error)
215 AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error)
216 AuctionGetBootCoordinator() (*ethCommon.Address, error)
217 AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error)
218
219 // Coordinator Management
220 AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error)
221 AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error)
222 AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error)
223
224 // Slot Info
225 AuctionGetCurrentSlotNumber() (int64, error)
226 AuctionGetMinBidBySlot(slot int64) (*big.Int, error)
227 AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error)
228
229 // Bidding
230 // AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int,
231 // userData, operatorData []byte) error // Only called from another smart contract
232 AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error)
233 AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool,
234 maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error)
235
236 // Forge
237 AuctionCanForge(forger ethCommon.Address) (bool, error)
238 // AuctionForge(forger ethCommon.Address) (bool, error) // Only called from another smart contract
239
240 // Fees
241 AuctionClaimHEZ() (*types.Transaction, error)
242
243 //
244 // Smart Contract Status
245 //
246
247 AuctionConstants() (*AuctionConstants, error)
248 AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error)
249}
250
251//
252// Implementation
253//
254
255// AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
256type AuctionClient struct {
257 client *EthereumClient
258 address ethCommon.Address
259}
260
261// NewAuctionClient creates a new AuctionClient
262func NewAuctionClient(client *EthereumClient, address ethCommon.Address) *AuctionClient {
263 return &AuctionClient{
264 client: client,
265 address: address,
266 }
267}
268
269// AuctionSetSlotDeadline is the interface to call the smart contract function
270func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
271 return nil, errTODO
272}
273
274// AuctionGetSlotDeadline is the interface to call the smart contract function
275func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) {
276 var slotDeadline uint8
277 if err := c.client.Call(func(ec *ethclient.Client) error {
278 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
279 if err != nil {
280 return err
281 }
282 slotDeadline, err = auction.GetSlotDeadline(nil)
283 return err
284 }); err != nil {
285 return 0, err
286 }
287 return slotDeadline, nil
288}
289
290// AuctionSetOpenAuctionSlots is the interface to call the smart contract function
291func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
292 return nil, errTODO
293}
294
295// AuctionGetOpenAuctionSlots is the interface to call the smart contract function
296func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) {
297 var openAuctionSlots uint16
298 if err := c.client.Call(func(ec *ethclient.Client) error {
299 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
300 if err != nil {
301 return err
302 }
303 openAuctionSlots, err = auction.GetOpenAuctionSlots(nil)
304 return err
305 }); err != nil {
306 return 0, err
307 }
308 return openAuctionSlots, nil
309}
310
311// AuctionSetClosedAuctionSlots is the interface to call the smart contract function
312func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
313 return nil, errTODO
314}
315
316// AuctionGetClosedAuctionSlots is the interface to call the smart contract function
317func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) {
318 var closedAuctionSlots uint16
319 if err := c.client.Call(func(ec *ethclient.Client) error {
320 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
321 if err != nil {
322 return err
323 }
324 closedAuctionSlots, err = auction.GetClosedAuctionSlots(nil)
325 return err
326 }); err != nil {
327 return 0, err
328 }
329 return closedAuctionSlots, nil
330}
331
332// AuctionSetOutbidding is the interface to call the smart contract function
333func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error) {
334 return nil, errTODO
335}
336
337// AuctionGetOutbidding is the interface to call the smart contract function
338func (c *AuctionClient) AuctionGetOutbidding() (uint8, error) {
339 var outbidding uint8
340 if err := c.client.Call(func(ec *ethclient.Client) error {
341 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
342 if err != nil {
343 return err
344 }
345 outbidding, err = auction.GetOutbidding(nil)
346 return err
347 }); err != nil {
348 return 0, err
349 }
350 return outbidding, nil
351}
352
353// AuctionSetAllocationRatio is the interface to call the smart contract function
354func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error) {
355 return nil, errTODO
356}
357
358// AuctionGetAllocationRatio is the interface to call the smart contract function
359func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint8, error) {
360 var allocationRation [3]uint8
361 if err := c.client.Call(func(ec *ethclient.Client) error {
362 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
363 if err != nil {
364 return err
365 }
366 allocationRation, err = auction.GetAllocationRatio(nil)
367 return err
368 }); err != nil {
369 return [3]uint8{}, err
370 }
371 return allocationRation, nil
372}
373
374// AuctionSetDonationAddress is the interface to call the smart contract function
375func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
376 return nil, errTODO
377}
378
379// AuctionGetDonationAddress is the interface to call the smart contract function
380func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) {
381 var donationAddress ethCommon.Address
382 if err := c.client.Call(func(ec *ethclient.Client) error {
383 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
384 if err != nil {
385 return err
386 }
387 donationAddress, err = auction.GetDonationAddress(nil)
388 return err
389 }); err != nil {
390 return nil, err
391 }
392 return &donationAddress, nil
393}
394
395// AuctionSetBootCoordinator is the interface to call the smart contract function
396func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
397 return nil, errTODO
398}
399
400// AuctionGetBootCoordinator is the interface to call the smart contract function
401func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
402 var bootCoordinator ethCommon.Address
403 if err := c.client.Call(func(ec *ethclient.Client) error {
404 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
405 if err != nil {
406 return err
407 }
408 bootCoordinator, err = auction.GetBootCoordinator(nil)
409 return err
410 }); err != nil {
411 return nil, err
412 }
413 return &bootCoordinator, nil
414}
415
416// AuctionChangeEpochMinBid is the interface to call the smart contract function
417func (c *AuctionClient) AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
418 return nil, errTODO
419}
420
421// AuctionRegisterCoordinator is the interface to call the smart contract function
422func (c *AuctionClient) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) {
423 return nil, errTODO
424}
425
426// AuctionIsRegisteredCoordinator is the interface to call the smart contract function
427func (c *AuctionClient) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
428 return false, errTODO
429}
430
431// AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
432func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
433 return nil, errTODO
434}
435
436// AuctionGetCurrentSlotNumber is the interface to call the smart contract function
437func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) {
438 var _currentSlotNumber *big.Int
439 if err := c.client.Call(func(ec *ethclient.Client) error {
440 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
441 if err != nil {
442 return err
443 }
444 _currentSlotNumber, err = auction.GetCurrentSlotNumber(nil)
445 return err
446 }); err != nil {
447 return 0, err
448 }
449 currentSlotNumber := _currentSlotNumber.Int64()
450 return currentSlotNumber, nil
451}
452
453// AuctionGetMinBidBySlot is the interface to call the smart contract function
454func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
455 var minBid *big.Int
456 if err := c.client.Call(func(ec *ethclient.Client) error {
457 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
458 if err != nil {
459 return err
460 }
461 slotToSend := big.NewInt(slot)
462 minBid, err = auction.GetMinBidBySlot(nil, slotToSend)
463 return err
464 }); err != nil {
465 return big.NewInt(0), err
466 }
467 return minBid, nil
468}
469
470// AuctionGetMinBidEpoch is the interface to call the smart contract function
471func (c *AuctionClient) AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error) {
472 var minBidEpoch *big.Int
473 if err := c.client.Call(func(ec *ethclient.Client) error {
474 auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
475 if err != nil {
476 return err
477 }
478 minBidEpoch, err = auction.GetMinBidEpoch(nil, epoch)
479 return err
480 }); err != nil {
481 return big.NewInt(0), err
482 }
483 return minBidEpoch, nil
484}
485
486// AuctionTokensReceived is the interface to call the smart contract function
487// func (c *AuctionClient) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
488// return errTODO
489// }
490
491// AuctionBid is the interface to call the smart contract function
492func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
493 return nil, errTODO
494}
495
496// AuctionMultiBid is the interface to call the smart contract function
497func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
498 return nil, errTODO
499}
500
501// AuctionCanForge is the interface to call the smart contract function
502func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address) (bool, error) {
503 return false, errTODO
504}
505
506// AuctionForge is the interface to call the smart contract function
507// func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (bool, error) {
508// return false, errTODO
509// }
510
511// AuctionClaimHEZ is the interface to call the smart contract function
512func (c *AuctionClient) AuctionClaimHEZ() (*types.Transaction, error) {
513 return nil, errTODO
514}
515
516// AuctionConstants returns the Constants of the Auction Smart Contract
517func (c *AuctionClient) AuctionConstants() (*AuctionConstants, error) {
518 return nil, errTODO
519}
520
521// AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
522func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error) {
523 return nil, nil, errTODO
524}