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)
  9
 10// AuctionConstants are the constants of the Rollup Smart Contract
 11type AuctionConstants struct {
 12	// Blocks to wait before starting with the first slot
 13	DelayGenesis uint16
 14	// Blocks per slot
 15	BlocksPerSlot uint8
 16	// Minimum bid when no one has bid yet
 17	InitialMinimalBidding *big.Int
 18	// First block where the first slot begins
 19	GenesisBlockNum int64
 20	// Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee
 21	GovernanceAddress ethCommon.Address
 22	// ERC777 token with which the bids will be made
 23	TokenHEZ ethCommon.Address
 24	// HermezRollup smartcontract address
 25	HermezRollup ethCommon.Address
 26}
 27
 28// SlotState is the state of a slot
 29type SlotState struct {
 30	Forger       ethCommon.Address
 31	BidAmount    *big.Int
 32	ClosedMinBid *big.Int
 33	Fulfilled    bool
 34}
 35
 36// Coordinator is the details of the Coordinator identified by the forger address
 37type Coordinator struct {
 38	WithdrawalAddress ethCommon.Address
 39	URL               string
 40}
 41
 42// AuctionVariables are the variables of the Auction Smart Contract
 43type AuctionVariables struct {
 44	// Boot Coordinator Address
 45	DonationAddress ethCommon.Address
 46	// Boot Coordinator Address
 47	BootCoordinator ethCommon.Address
 48	// The minimum bid value in a series of 6 slots
 49	MinBidEpoch [6]*big.Int
 50	// Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
 51	ClosedAuctionSlots uint16
 52	// Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
 53	OpenAuctionSlots uint16
 54	// How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
 55	AllocationRatio [3]uint8
 56	// Minimum outbid (percentage) over the previous one to consider it valid
 57	Outbidding uint8
 58	// Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
 59	SlotDeadline uint8
 60}
 61
 62// AuctionState represents the state of the Rollup in the Smart Contract
 63type AuctionState struct {
 64	// Mapping to control slot state
 65	Slots map[int64]SlotState
 66	// Mapping to control balances pending to claim
 67	PendingBalances map[ethCommon.Address]*big.Int
 68
 69	// Mapping to register all the coordinators. The address used for the mapping is the forger address
 70	Coordinators map[ethCommon.Address]Coordinator
 71}
 72
 73// AuctionEventNewBid is an event of the Auction Smart Contract
 74type AuctionEventNewBid struct {
 75	Slot              int64
 76	BidAmount         *big.Int
 77	CoordinatorForger ethCommon.Address
 78}
 79
 80// AuctionEventNewSlotDeadline is an event of the Auction Smart Contract
 81type AuctionEventNewSlotDeadline struct {
 82	NewSlotDeadline uint8
 83}
 84
 85// AuctionEventNewClosedAuctionSlots is an event of the Auction Smart Contract
 86type AuctionEventNewClosedAuctionSlots struct {
 87	NewClosedAuctionSlots uint16
 88}
 89
 90// AuctionEventNewOutbidding is an event of the Auction Smart Contract
 91type AuctionEventNewOutbidding struct {
 92	NewOutbidding uint8
 93}
 94
 95// AuctionEventNewDonationAddress is an event of the Auction Smart Contract
 96type AuctionEventNewDonationAddress struct {
 97	NewDonationAddress ethCommon.Address
 98}
 99
100// AuctionEventNewBootCoordinator is an event of the Auction Smart Contract
101type AuctionEventNewBootCoordinator struct {
102	NewBootCoordinator ethCommon.Address
103}
104
105// AuctionEventNewOpenAuctionSlots is an event of the Auction Smart Contract
106type AuctionEventNewOpenAuctionSlots struct {
107	NewOpenAuctionSlots uint16
108}
109
110// AuctionEventNewAllocationRatio is an event of the Auction Smart Contract
111type AuctionEventNewAllocationRatio struct {
112	NewAllocationRatio [3]uint8
113}
114
115// AuctionEventNewCoordinator is an event of the Auction Smart Contract
116type AuctionEventNewCoordinator struct {
117	ForgerAddress     ethCommon.Address
118	WithdrawalAddress ethCommon.Address
119	URL               string
120}
121
122// AuctionEventCoordinatorUpdated is an event of the Auction Smart Contract
123type AuctionEventCoordinatorUpdated struct {
124	ForgerAddress     ethCommon.Address
125	WithdrawalAddress ethCommon.Address
126	URL               string
127}
128
129// AuctionEventNewForgeAllocated is an event of the Auction Smart Contract
130type AuctionEventNewForgeAllocated struct {
131	Forger           ethCommon.Address
132	CurrentSlot      int64
133	BurnAmount       *big.Int
134	DonationAmount   *big.Int
135	GovernanceAmount *big.Int
136}
137
138// AuctionEventNewMinBidEpoch is an event of the Auction Smart Contract
139type AuctionEventNewMinBidEpoch struct {
140	SlotEpoch        int64
141	NewInitialMinBid *big.Int
142}
143
144// AuctionEventNewForge is an event of the Auction Smart Contract
145type AuctionEventNewForge struct {
146	Forger      ethCommon.Address
147	CurrentSlot int64
148}
149
150// AuctionEventHEZClaimed is an event of the Auction Smart Contract
151type AuctionEventHEZClaimed struct {
152	Owner  ethCommon.Address
153	Amount *big.Int
154}
155
156// AuctionEvents is the list of events in a block of the Auction Smart Contract
157type AuctionEvents struct { //nolint:structcheck
158	NewBid                []AuctionEventNewBid
159	NewSlotDeadline       []AuctionEventNewSlotDeadline
160	NewClosedAuctionSlots []AuctionEventNewClosedAuctionSlots
161	NewOutbidding         []AuctionEventNewOutbidding
162	NewDonationAddress    []AuctionEventNewDonationAddress
163	NewBootCoordinator    []AuctionEventNewBootCoordinator
164	NewOpenAuctionSlots   []AuctionEventNewOpenAuctionSlots
165	NewAllocationRatio    []AuctionEventNewAllocationRatio
166	NewCoordinator        []AuctionEventNewCoordinator
167	CoordinatorUpdated    []AuctionEventCoordinatorUpdated
168	NewForgeAllocated     []AuctionEventNewForgeAllocated
169	NewMinBidEpoch        []AuctionEventNewMinBidEpoch
170	NewForge              []AuctionEventNewForge
171	HEZClaimed            []AuctionEventHEZClaimed
172}
173
174// NewAuctionEvents creates an empty AuctionEvents with the slices initialized.
175func NewAuctionEvents() AuctionEvents {
176	return AuctionEvents{
177		NewBid:                make([]AuctionEventNewBid, 0),
178		NewSlotDeadline:       make([]AuctionEventNewSlotDeadline, 0),
179		NewClosedAuctionSlots: make([]AuctionEventNewClosedAuctionSlots, 0),
180		NewOutbidding:         make([]AuctionEventNewOutbidding, 0),
181		NewDonationAddress:    make([]AuctionEventNewDonationAddress, 0),
182		NewBootCoordinator:    make([]AuctionEventNewBootCoordinator, 0),
183		NewOpenAuctionSlots:   make([]AuctionEventNewOpenAuctionSlots, 0),
184		NewAllocationRatio:    make([]AuctionEventNewAllocationRatio, 0),
185		NewCoordinator:        make([]AuctionEventNewCoordinator, 0),
186		CoordinatorUpdated:    make([]AuctionEventCoordinatorUpdated, 0),
187		NewForgeAllocated:     make([]AuctionEventNewForgeAllocated, 0),
188		NewMinBidEpoch:        make([]AuctionEventNewMinBidEpoch, 0),
189		NewForge:              make([]AuctionEventNewForge, 0),
190		HEZClaimed:            make([]AuctionEventHEZClaimed, 0),
191	}
192}
193
194// AuctionInterface is the inteface to to Auction Smart Contract
195type AuctionInterface interface {
196	//
197	// Smart Contract Methods
198	//
199
200	// Getter/Setter, where Setter is onlyOwner
201	AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error)
202	AuctionGetSlotDeadline() (uint8, error)
203	AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error)
204	AuctionGetOpenAuctionSlots() (uint16, error)
205	AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error)
206	AuctionGetClosedAuctionSlots() (uint16, error)
207	AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error)
208	AuctionGetOutbidding() (uint8, error)
209	AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error)
210	AuctionGetAllocationRatio() ([3]uint8, error)
211	AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error)
212	AuctionGetDonationAddress() (*ethCommon.Address, error)
213	AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error)
214	AuctionGetBootCoordinator() (*ethCommon.Address, error)
215	AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error)
216
217	// Coordinator Management
218	AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error)
219	AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error)
220	AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error)
221
222	// Slot Info
223	AuctionGetCurrentSlotNumber() (int64, error)
224	AuctionGetMinBidBySlot(slot int64) (*big.Int, error)
225	AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error)
226
227	// Bidding
228	// AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int,
229	// 	userData, operatorData []byte) error // Only called from another smart contract
230	AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error)
231	AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool,
232		maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error)
233
234	// Forge
235	AuctionCanForge(forger ethCommon.Address) (bool, error)
236	// AuctionForge(forger ethCommon.Address) (bool, error) // Only called from another smart contract
237
238	// Fees
239	AuctionClaimHEZ() (*types.Transaction, error)
240
241	//
242	// Smart Contract Status
243	//
244
245	AuctionConstants() (*AuctionConstants, error)
246	AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error)
247}
248
249//
250// Implementation
251//
252
253// AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
254type AuctionClient struct {
255}
256
257// AuctionSetSlotDeadline is the interface to call the smart contract function
258func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
259	return nil, errTODO
260}
261
262// AuctionGetSlotDeadline is the interface to call the smart contract function
263func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) {
264	return 0, errTODO
265}
266
267// AuctionSetOpenAuctionSlots is the interface to call the smart contract function
268func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
269	return nil, errTODO
270}
271
272// AuctionGetOpenAuctionSlots is the interface to call the smart contract function
273func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) {
274	return 0, errTODO
275}
276
277// AuctionSetClosedAuctionSlots is the interface to call the smart contract function
278func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
279	return nil, errTODO
280}
281
282// AuctionGetClosedAuctionSlots is the interface to call the smart contract function
283func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) {
284	return 0, errTODO
285}
286
287// AuctionSetOutbidding is the interface to call the smart contract function
288func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error) {
289	return nil, errTODO
290}
291
292// AuctionGetOutbidding is the interface to call the smart contract function
293func (c *AuctionClient) AuctionGetOutbidding() (uint8, error) {
294	return 0, errTODO
295}
296
297// AuctionSetAllocationRatio is the interface to call the smart contract function
298func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error) {
299	return nil, errTODO
300}
301
302// AuctionGetAllocationRatio is the interface to call the smart contract function
303func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint8, error) {
304	return [3]uint8{}, errTODO
305}
306
307// AuctionSetDonationAddress is the interface to call the smart contract function
308func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
309	return nil, errTODO
310}
311
312// AuctionGetDonationAddress is the interface to call the smart contract function
313func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) {
314	return nil, errTODO
315}
316
317// AuctionSetBootCoordinator is the interface to call the smart contract function
318func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
319	return nil, errTODO
320}
321
322// AuctionGetBootCoordinator is the interface to call the smart contract function
323func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
324	return nil, errTODO
325}
326
327// AuctionChangeEpochMinBid is the interface to call the smart contract function
328func (c *AuctionClient) AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
329	return nil, errTODO
330}
331
332// AuctionRegisterCoordinator is the interface to call the smart contract function
333func (c *AuctionClient) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) {
334	return nil, errTODO
335}
336
337// AuctionIsRegisteredCoordinator is the interface to call the smart contract function
338func (c *AuctionClient) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
339	return false, errTODO
340}
341
342// AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
343func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
344	return nil, errTODO
345}
346
347// AuctionGetCurrentSlotNumber is the interface to call the smart contract function
348func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) {
349	return 0, errTODO
350}
351
352// AuctionGetMinBidBySlot is the interface to call the smart contract function
353func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
354	return nil, errTODO
355}
356
357// AuctionGetMinBidEpoch is the interface to call the smart contract function
358func (c *AuctionClient) AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error) {
359	return nil, errTODO
360}
361
362// AuctionTokensReceived is the interface to call the smart contract function
363// func (c *AuctionClient) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
364// 	return errTODO
365// }
366
367// AuctionBid is the interface to call the smart contract function
368func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
369	return nil, errTODO
370}
371
372// AuctionMultiBid is the interface to call the smart contract function
373func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
374	return nil, errTODO
375}
376
377// AuctionCanForge is the interface to call the smart contract function
378func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address) (bool, error) {
379	return false, errTODO
380}
381
382// AuctionForge is the interface to call the smart contract function
383// func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (bool, error) {
384// 	return false, errTODO
385// }
386
387// AuctionClaimHEZ is the interface to call the smart contract function
388func (c *AuctionClient) AuctionClaimHEZ() (*types.Transaction, error) {
389	return nil, errTODO
390}
391
392// AuctionConstants returns the Constants of the Auction Smart Contract
393func (c *AuctionClient) AuctionConstants() (*AuctionConstants, error) {
394	return nil, errTODO
395}
396
397// AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
398func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error) {
399	return nil, nil, errTODO
400}