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