arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/eth/auction.go
eth/auction.goRaw
  1package eth
  2
  3import (
  4	"fmt"
  5	"math/big"
  6
  7	"github.com/ethereum/go-ethereum/accounts/abi/bind"
  8	ethCommon "github.com/ethereum/go-ethereum/common"
  9	"github.com/ethereum/go-ethereum/core/types"
 10	"github.com/ethereum/go-ethereum/ethclient"
 11	HermezAuctionProtocol "github.com/hermeznetwork/hermez-node/eth/contracts/auction"
 12	"github.com/hermeznetwork/hermez-node/log"
 13)
 14
 15// AuctionConstants are the constants of the Rollup Smart Contract
 16type AuctionConstants struct {
 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	// Only for test
 25	GovernanceAddress ethCommon.Address
 26	// ERC777 token with which the bids will be made
 27	TokenHEZ ethCommon.Address
 28	// HermezRollup smartcontract address
 29	HermezRollup ethCommon.Address
 30}
 31
 32// SlotState is the state of a slot
 33type SlotState struct {
 34	Forger       ethCommon.Address
 35	BidAmount    *big.Int
 36	ClosedMinBid *big.Int
 37	Fulfilled    bool
 38}
 39
 40// NewSlotState returns an empty SlotState
 41func NewSlotState() *SlotState {
 42	return &SlotState{
 43		Forger:       ethCommon.Address{},
 44		BidAmount:    big.NewInt(0),
 45		ClosedMinBid: big.NewInt(0),
 46		Fulfilled:    false,
 47	}
 48}
 49
 50// Coordinator is the details of the Coordinator identified by the forger address
 51type Coordinator struct {
 52	WithdrawalAddress ethCommon.Address
 53	URL               string
 54}
 55
 56// AuctionVariables are the variables of the Auction Smart Contract
 57type AuctionVariables struct {
 58	// Boot Coordinator Address
 59	DonationAddress ethCommon.Address
 60	// Boot Coordinator Address
 61	BootCoordinator ethCommon.Address
 62	// The minimum bid value in a series of 6 slots
 63	DefaultSlotSetBid [6]*big.Int
 64	// Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
 65	ClosedAuctionSlots uint16
 66	// Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
 67	OpenAuctionSlots uint16
 68	// How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
 69	AllocationRatio [3]uint16
 70	// Minimum outbid (percentage) over the previous one to consider it valid
 71	Outbidding uint16
 72	// Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
 73	SlotDeadline uint8
 74}
 75
 76// AuctionState represents the state of the Rollup in the Smart Contract
 77type AuctionState struct {
 78	// Mapping to control slot state
 79	Slots map[int64]*SlotState
 80	// Mapping to control balances pending to claim
 81	PendingBalances map[ethCommon.Address]*big.Int
 82	// Mapping to register all the coordinators. The address used for the mapping is the forger address
 83	Coordinators map[ethCommon.Address]*Coordinator
 84}
 85
 86// AuctionEventNewBid is an event of the Auction Smart Contract
 87type AuctionEventNewBid struct {
 88	Slot              int64
 89	BidAmount         *big.Int
 90	CoordinatorForger ethCommon.Address
 91}
 92
 93// AuctionEventNewSlotDeadline is an event of the Auction Smart Contract
 94type AuctionEventNewSlotDeadline struct {
 95	NewSlotDeadline uint8
 96}
 97
 98// AuctionEventNewClosedAuctionSlots is an event of the Auction Smart Contract
 99type AuctionEventNewClosedAuctionSlots struct {
100	NewClosedAuctionSlots uint16
101}
102
103// AuctionEventNewOutbidding is an event of the Auction Smart Contract
104type AuctionEventNewOutbidding struct {
105	NewOutbidding uint16
106}
107
108// AuctionEventNewDonationAddress is an event of the Auction Smart Contract
109type AuctionEventNewDonationAddress struct {
110	NewDonationAddress ethCommon.Address
111}
112
113// AuctionEventNewBootCoordinator is an event of the Auction Smart Contract
114type AuctionEventNewBootCoordinator struct {
115	NewBootCoordinator ethCommon.Address
116}
117
118// AuctionEventNewOpenAuctionSlots is an event of the Auction Smart Contract
119type AuctionEventNewOpenAuctionSlots struct {
120	NewOpenAuctionSlots uint16
121}
122
123// AuctionEventNewAllocationRatio is an event of the Auction Smart Contract
124type AuctionEventNewAllocationRatio struct {
125	NewAllocationRatio [3]uint16
126}
127
128// AuctionEventNewCoordinator is an event of the Auction Smart Contract
129type AuctionEventNewCoordinator struct {
130	ForgerAddress     ethCommon.Address
131	WithdrawalAddress ethCommon.Address
132	URL               string
133}
134
135// AuctionEventCoordinatorUpdated is an event of the Auction Smart Contract
136type AuctionEventCoordinatorUpdated struct {
137	ForgerAddress     ethCommon.Address
138	WithdrawalAddress ethCommon.Address
139	URL               string
140}
141
142// AuctionEventNewForgeAllocated is an event of the Auction Smart Contract
143type AuctionEventNewForgeAllocated struct {
144	Forger           ethCommon.Address
145	CurrentSlot      int64
146	BurnAmount       *big.Int
147	DonationAmount   *big.Int
148	GovernanceAmount *big.Int
149}
150
151// AuctionEventNewDefaultSlotSetBid is an event of the Auction Smart Contract
152type AuctionEventNewDefaultSlotSetBid struct {
153	SlotSet          int64
154	NewInitialMinBid *big.Int
155}
156
157// AuctionEventNewForge is an event of the Auction Smart Contract
158type AuctionEventNewForge struct {
159	Forger      ethCommon.Address
160	CurrentSlot int64
161}
162
163// AuctionEventHEZClaimed is an event of the Auction Smart Contract
164type AuctionEventHEZClaimed struct {
165	Owner  ethCommon.Address
166	Amount *big.Int
167}
168
169// AuctionEvents is the list of events in a block of the Auction Smart Contract
170type AuctionEvents struct { //nolint:structcheck
171	NewBid                []AuctionEventNewBid
172	NewSlotDeadline       []AuctionEventNewSlotDeadline
173	NewClosedAuctionSlots []AuctionEventNewClosedAuctionSlots
174	NewOutbidding         []AuctionEventNewOutbidding
175	NewDonationAddress    []AuctionEventNewDonationAddress
176	NewBootCoordinator    []AuctionEventNewBootCoordinator
177	NewOpenAuctionSlots   []AuctionEventNewOpenAuctionSlots
178	NewAllocationRatio    []AuctionEventNewAllocationRatio
179	NewCoordinator        []AuctionEventNewCoordinator
180	CoordinatorUpdated    []AuctionEventCoordinatorUpdated
181	NewForgeAllocated     []AuctionEventNewForgeAllocated
182	NewDefaultSlotSetBid  []AuctionEventNewDefaultSlotSetBid
183	NewForge              []AuctionEventNewForge
184	HEZClaimed            []AuctionEventHEZClaimed
185}
186
187// NewAuctionEvents creates an empty AuctionEvents with the slices initialized.
188func NewAuctionEvents() AuctionEvents {
189	return AuctionEvents{
190		NewBid:                make([]AuctionEventNewBid, 0),
191		NewSlotDeadline:       make([]AuctionEventNewSlotDeadline, 0),
192		NewClosedAuctionSlots: make([]AuctionEventNewClosedAuctionSlots, 0),
193		NewOutbidding:         make([]AuctionEventNewOutbidding, 0),
194		NewDonationAddress:    make([]AuctionEventNewDonationAddress, 0),
195		NewBootCoordinator:    make([]AuctionEventNewBootCoordinator, 0),
196		NewOpenAuctionSlots:   make([]AuctionEventNewOpenAuctionSlots, 0),
197		NewAllocationRatio:    make([]AuctionEventNewAllocationRatio, 0),
198		NewCoordinator:        make([]AuctionEventNewCoordinator, 0),
199		CoordinatorUpdated:    make([]AuctionEventCoordinatorUpdated, 0),
200		NewForgeAllocated:     make([]AuctionEventNewForgeAllocated, 0),
201		NewDefaultSlotSetBid:  make([]AuctionEventNewDefaultSlotSetBid, 0),
202		NewForge:              make([]AuctionEventNewForge, 0),
203		HEZClaimed:            make([]AuctionEventHEZClaimed, 0),
204	}
205}
206
207// AuctionInterface is the inteface to to Auction Smart Contract
208type AuctionInterface interface {
209	//
210	// Smart Contract Methods
211	//
212
213	// Getter/Setter, where Setter is onlyOwner
214	AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error)
215	AuctionGetSlotDeadline() (uint8, error)
216	AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error)
217	AuctionGetOpenAuctionSlots() (uint16, error)
218	AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error)
219	AuctionGetClosedAuctionSlots() (uint16, error)
220	AuctionSetOutbidding(newOutbidding uint16) (*types.Transaction, error)
221	AuctionGetOutbidding() (uint16, error)
222	AuctionSetAllocationRatio(newAllocationRatio [3]uint16) (*types.Transaction, error)
223	AuctionGetAllocationRatio() ([3]uint16, error)
224	AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error)
225	AuctionGetDonationAddress() (*ethCommon.Address, error)
226	AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error)
227	AuctionGetBootCoordinator() (*ethCommon.Address, error)
228	AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (*types.Transaction, error)
229
230	// Coordinator Management
231	AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error)
232	AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error)
233	AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error)
234
235	// Slot Info
236	AuctionGetCurrentSlotNumber() (int64, error)
237	AuctionGetMinBidBySlot(slot int64) (*big.Int, error)
238	AuctionGetDefaultSlotSetBid(slotSet uint8) (*big.Int, error)
239
240	// Bidding
241	// AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int,
242	// 	userData, operatorData []byte) error // Only called from another smart contract
243	AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error)
244	AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool,
245		maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error)
246
247	// Forge
248	AuctionCanForge(forger ethCommon.Address, blockNum int64) (bool, error)
249	// AuctionForge(forger ethCommon.Address) (bool, error) // Only called from another smart contract
250
251	// Fees
252	AuctionClaimHEZ(claimAddress ethCommon.Address) (*types.Transaction, error)
253
254	//
255	// Smart Contract Status
256	//
257
258	AuctionConstants() (*AuctionConstants, error)
259	AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error)
260}
261
262//
263// Implementation
264//
265
266// AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
267type AuctionClient struct {
268	client   *EthereumClient
269	address  ethCommon.Address
270	gasLimit uint64
271}
272
273// NewAuctionClient creates a new AuctionClient
274func NewAuctionClient(client *EthereumClient, address ethCommon.Address) *AuctionClient {
275	return &AuctionClient{
276		client:   client,
277		address:  address,
278		gasLimit: 1000000, //nolint:gomnd
279	}
280}
281
282// AuctionSetSlotDeadline is the interface to call the smart contract function
283func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
284	var tx *types.Transaction
285	var err error
286	if tx, err = c.client.CallAuth(
287		c.gasLimit,
288		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
289			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
290			if err != nil {
291				return nil, err
292			}
293			return auction.SetSlotDeadline(auth, newDeadline)
294		},
295	); err != nil {
296		return nil, fmt.Errorf("Failed setting slotDeadline: %w", err)
297	}
298	return tx, nil
299}
300
301// AuctionGetSlotDeadline is the interface to call the smart contract function
302func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) {
303	var slotDeadline uint8
304	if err := c.client.Call(func(ec *ethclient.Client) error {
305		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
306		if err != nil {
307			return err
308		}
309		slotDeadline, err = auction.GetSlotDeadline(nil)
310		return err
311	}); err != nil {
312		return 0, err
313	}
314	return slotDeadline, nil
315}
316
317// AuctionSetOpenAuctionSlots is the interface to call the smart contract function
318func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
319	var tx *types.Transaction
320	var err error
321	if tx, err = c.client.CallAuth(
322		c.gasLimit,
323		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
324			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
325			if err != nil {
326				return nil, err
327			}
328			return auction.SetOpenAuctionSlots(auth, newOpenAuctionSlots)
329		},
330	); err != nil {
331		return nil, fmt.Errorf("Failed setting openAuctionSlots: %w", err)
332	}
333	return tx, nil
334}
335
336// AuctionGetOpenAuctionSlots is the interface to call the smart contract function
337func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) {
338	var openAuctionSlots uint16
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		openAuctionSlots, err = auction.GetOpenAuctionSlots(nil)
345		return err
346	}); err != nil {
347		return 0, err
348	}
349	return openAuctionSlots, nil
350}
351
352// AuctionSetClosedAuctionSlots is the interface to call the smart contract function
353func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
354	var tx *types.Transaction
355	var err error
356	if tx, err = c.client.CallAuth(
357		c.gasLimit,
358		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
359			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
360			if err != nil {
361				return nil, err
362			}
363			return auction.SetClosedAuctionSlots(auth, newClosedAuctionSlots)
364		},
365	); err != nil {
366		return nil, fmt.Errorf("Failed setting closedAuctionSlots: %w", err)
367	}
368	return tx, nil
369}
370
371// AuctionGetClosedAuctionSlots is the interface to call the smart contract function
372func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) {
373	var closedAuctionSlots uint16
374	if err := c.client.Call(func(ec *ethclient.Client) error {
375		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
376		if err != nil {
377			return err
378		}
379		closedAuctionSlots, err = auction.GetClosedAuctionSlots(nil)
380		return err
381	}); err != nil {
382		return 0, err
383	}
384	return closedAuctionSlots, nil
385}
386
387// AuctionSetOutbidding is the interface to call the smart contract function
388func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint16) (*types.Transaction, error) {
389	var tx *types.Transaction
390	var err error
391	if tx, err = c.client.CallAuth(
392		12500000, //nolint:gomnd
393		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
394			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
395			if err != nil {
396				return nil, err
397			}
398			return auction.SetOutbidding(auth, newOutbidding)
399		},
400	); err != nil {
401		return nil, fmt.Errorf("Failed setting setOutbidding: %w", err)
402	}
403	return tx, nil
404}
405
406// AuctionGetOutbidding is the interface to call the smart contract function
407func (c *AuctionClient) AuctionGetOutbidding() (uint16, error) {
408	var outbidding uint16
409	if err := c.client.Call(func(ec *ethclient.Client) error {
410		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
411		if err != nil {
412			return err
413		}
414		outbidding, err = auction.GetOutbidding(nil)
415		return err
416	}); err != nil {
417		return 0, err
418	}
419	return outbidding, nil
420}
421
422// AuctionSetAllocationRatio is the interface to call the smart contract function
423func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint16) (*types.Transaction, error) {
424	var tx *types.Transaction
425	var err error
426	if tx, err = c.client.CallAuth(
427		c.gasLimit,
428		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
429			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
430			if err != nil {
431				return nil, err
432			}
433			return auction.SetAllocationRatio(auth, newAllocationRatio)
434		},
435	); err != nil {
436		return nil, fmt.Errorf("Failed setting allocationRatio: %w", err)
437	}
438	return tx, nil
439}
440
441// AuctionGetAllocationRatio is the interface to call the smart contract function
442func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint16, error) {
443	var allocationRation [3]uint16
444	if err := c.client.Call(func(ec *ethclient.Client) error {
445		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
446		if err != nil {
447			return err
448		}
449		allocationRation, err = auction.GetAllocationRatio(nil)
450		return err
451	}); err != nil {
452		return [3]uint16{}, err
453	}
454	return allocationRation, nil
455}
456
457// AuctionSetDonationAddress is the interface to call the smart contract function
458func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
459	var tx *types.Transaction
460	var err error
461	if tx, err = c.client.CallAuth(
462		c.gasLimit,
463		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
464			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
465			if err != nil {
466				return nil, err
467			}
468			return auction.SetDonationAddress(auth, newDonationAddress)
469		},
470	); err != nil {
471		return nil, fmt.Errorf("Failed setting donationAddress: %w", err)
472	}
473	return tx, nil
474}
475
476// AuctionGetDonationAddress is the interface to call the smart contract function
477func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) {
478	var donationAddress ethCommon.Address
479	if err := c.client.Call(func(ec *ethclient.Client) error {
480		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
481		if err != nil {
482			return err
483		}
484		donationAddress, err = auction.GetDonationAddress(nil)
485		return err
486	}); err != nil {
487		return nil, err
488	}
489	return &donationAddress, nil
490}
491
492// AuctionSetBootCoordinator is the interface to call the smart contract function
493func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
494	var tx *types.Transaction
495	var err error
496	if tx, err = c.client.CallAuth(
497		c.gasLimit,
498		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
499			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
500			if err != nil {
501				return nil, err
502			}
503			return auction.SetBootCoordinator(auth, newBootCoordinator)
504		},
505	); err != nil {
506		return nil, fmt.Errorf("Failed setting bootCoordinator: %w", err)
507	}
508	return tx, nil
509}
510
511// AuctionGetBootCoordinator is the interface to call the smart contract function
512func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
513	var bootCoordinator ethCommon.Address
514	if err := c.client.Call(func(ec *ethclient.Client) error {
515		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
516		if err != nil {
517			return err
518		}
519		bootCoordinator, err = auction.GetBootCoordinator(nil)
520		return err
521	}); err != nil {
522		return nil, err
523	}
524	return &bootCoordinator, nil
525}
526
527// AuctionChangeDefaultSlotSetBid is the interface to call the smart contract function
528func (c *AuctionClient) AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
529	var tx *types.Transaction
530	var err error
531	if tx, err = c.client.CallAuth(
532		c.gasLimit,
533		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
534			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
535			if err != nil {
536				return nil, err
537			}
538			slotSetToSend := big.NewInt(slotSet)
539			return auction.ChangeDefaultSlotSetBid(auth, slotSetToSend, newInitialMinBid)
540		},
541	); err != nil {
542		return nil, fmt.Errorf("Failed changing epoch minBid: %w", err)
543	}
544	return tx, nil
545}
546
547// AuctionRegisterCoordinator is the interface to call the smart contract function
548func (c *AuctionClient) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) {
549	var tx *types.Transaction
550	var err error
551	if tx, err = c.client.CallAuth(
552		c.gasLimit,
553		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
554			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
555			if err != nil {
556				return nil, err
557			}
558			return auction.RegisterCoordinator(auth, forgerAddress, URL)
559		},
560	); err != nil {
561		return nil, fmt.Errorf("Failed register coordinator: %w", err)
562	}
563	return tx, nil
564}
565
566// AuctionIsRegisteredCoordinator is the interface to call the smart contract function
567func (c *AuctionClient) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
568	var registered bool
569	if err := c.client.Call(func(ec *ethclient.Client) error {
570		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
571		if err != nil {
572			return err
573		}
574		registered, err = auction.IsRegisteredCoordinator(nil, forgerAddress)
575		return err
576	}); err != nil {
577		return false, err
578	}
579	return registered, nil
580}
581
582// AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
583func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
584	var tx *types.Transaction
585	var err error
586	if tx, err = c.client.CallAuth(
587		c.gasLimit,
588		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
589			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
590			if err != nil {
591				return nil, err
592			}
593			return auction.UpdateCoordinatorInfo(auth, forgerAddress, newWithdrawAddress, newURL)
594		},
595	); err != nil {
596		return nil, fmt.Errorf("Failed update coordinator info: %w", err)
597	}
598	return tx, nil
599}
600
601// AuctionGetCurrentSlotNumber is the interface to call the smart contract function
602func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) {
603	var _currentSlotNumber *big.Int
604	if err := c.client.Call(func(ec *ethclient.Client) error {
605		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
606		if err != nil {
607			return err
608		}
609		_currentSlotNumber, err = auction.GetCurrentSlotNumber(nil)
610		return err
611	}); err != nil {
612		return 0, err
613	}
614	currentSlotNumber := _currentSlotNumber.Int64()
615	return currentSlotNumber, nil
616}
617
618// AuctionGetMinBidBySlot is the interface to call the smart contract function
619func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
620	var minBid *big.Int
621	if err := c.client.Call(func(ec *ethclient.Client) error {
622		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
623		if err != nil {
624			return err
625		}
626		slotToSend := big.NewInt(slot)
627		minBid, err = auction.GetMinBidBySlot(nil, slotToSend)
628		return err
629	}); err != nil {
630		return big.NewInt(0), err
631	}
632	return minBid, nil
633}
634
635// AuctionGetDefaultSlotSetBid is the interface to call the smart contract function
636func (c *AuctionClient) AuctionGetDefaultSlotSetBid(slotSet uint8) (*big.Int, error) {
637	var minBidSlotSet *big.Int
638	if err := c.client.Call(func(ec *ethclient.Client) error {
639		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
640		if err != nil {
641			return err
642		}
643		minBidSlotSet, err = auction.GetDefaultSlotSetBid(nil, slotSet)
644		return err
645	}); err != nil {
646		return big.NewInt(0), err
647	}
648	return minBidSlotSet, nil
649}
650
651// AuctionTokensReceived is the interface to call the smart contract function
652// func (c *AuctionClient) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
653// 	return errTODO
654// }
655
656// AuctionBid is the interface to call the smart contract function
657func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
658	log.Error("TODO")
659	return nil, errTODO
660}
661
662// AuctionMultiBid is the interface to call the smart contract function
663func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
664	log.Error("TODO")
665	return nil, errTODO
666}
667
668// AuctionCanForge is the interface to call the smart contract function
669func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address, blockNum int64) (bool, error) {
670	var canForge bool
671	if err := c.client.Call(func(ec *ethclient.Client) error {
672		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
673		if err != nil {
674			return err
675		}
676		canForge, err = auction.CanForge(nil, forger, big.NewInt(blockNum))
677		return err
678	}); err != nil {
679		return false, err
680	}
681	return canForge, nil
682}
683
684// AuctionForge is the interface to call the smart contract function
685// func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (bool, error) {
686// 	return false, errTODO
687// }
688
689// AuctionClaimHEZ is the interface to call the smart contract function
690func (c *AuctionClient) AuctionClaimHEZ(claimAddress ethCommon.Address) (*types.Transaction, error) {
691	var tx *types.Transaction
692	var err error
693	if tx, err = c.client.CallAuth(
694		c.gasLimit,
695		func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
696			auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
697			if err != nil {
698				return nil, err
699			}
700			return auction.ClaimHEZ(auth, claimAddress)
701		},
702	); err != nil {
703		return nil, fmt.Errorf("Failed claim HEZ: %w", err)
704	}
705	return tx, nil
706}
707
708// AuctionConstants returns the Constants of the Auction Smart Contract
709func (c *AuctionClient) AuctionConstants() (*AuctionConstants, error) {
710	auctionConstants := new(AuctionConstants)
711	if err := c.client.Call(func(ec *ethclient.Client) error {
712		auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
713		if err != nil {
714			return err
715		}
716		auctionConstants.BlocksPerSlot, err = auction.BLOCKSPERSLOT(nil)
717		if err != nil {
718			return err
719		}
720		genesisBlock, err := auction.GenesisBlock(nil)
721		if err != nil {
722			return err
723		}
724		auctionConstants.GenesisBlockNum = genesisBlock.Int64()
725		auctionConstants.HermezRollup, err = auction.HermezRollup(nil)
726		if err != nil {
727			return err
728		}
729		auctionConstants.InitialMinimalBidding, err = auction.INITIALMINIMALBIDDING(nil)
730		if err != nil {
731			return err
732		}
733		auctionConstants.TokenHEZ, err = auction.TokenHEZ(nil)
734		return err
735	}); err != nil {
736		return nil, err
737	}
738	return auctionConstants, nil
739}
740
741// AuctionVariables returns the variables of the Auction Smart Contract
742func (c *AuctionClient) AuctionVariables() (*AuctionVariables, error) {
743	auctionVariables := new(AuctionVariables)
744	if err := c.client.Call(func(ec *ethclient.Client) error {
745		var err error
746		auctionVariables.AllocationRatio, err = c.AuctionGetAllocationRatio()
747		if err != nil {
748			return err
749		}
750		bootCoordinator, err := c.AuctionGetBootCoordinator()
751		if err != nil {
752			return err
753		}
754		auctionVariables.BootCoordinator = *bootCoordinator
755		auctionVariables.ClosedAuctionSlots, err = c.AuctionGetClosedAuctionSlots()
756		if err != nil {
757			return err
758		}
759		var defaultSlotSetBid [6]*big.Int
760		for i := uint8(0); i < 6; i++ {
761			bid, err := c.AuctionGetDefaultSlotSetBid(i)
762			if err != nil {
763				return err
764			}
765			defaultSlotSetBid[i] = bid
766		}
767		auctionVariables.DefaultSlotSetBid = defaultSlotSetBid
768		donationAddress, err := c.AuctionGetDonationAddress()
769		if err != nil {
770			return err
771		}
772		auctionVariables.DonationAddress = *donationAddress
773		auctionVariables.OpenAuctionSlots, err = c.AuctionGetOpenAuctionSlots()
774		if err != nil {
775			return err
776		}
777		auctionVariables.Outbidding, err = c.AuctionGetOutbidding()
778		if err != nil {
779			return err
780		}
781		auctionVariables.SlotDeadline, err = c.AuctionGetSlotDeadline()
782		return err
783	}); err != nil {
784		return nil, err
785	}
786	return auctionVariables, nil
787}
788
789// AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
790func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error) {
791	log.Error("TODO")
792	return nil, nil, errTODO
793}