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