arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/ethauction.go
common/ethauction.goRaw
 1package common
 2
 3import (
 4	"math/big"
 5
 6	ethCommon "github.com/ethereum/go-ethereum/common"
 7)
 8
 9// AuctionConstants are the constants of the Rollup Smart Contract
10type AuctionConstants struct {
11	// Blocks per slot
12	BlocksPerSlot uint8 `json:"blocksPerSlot"`
13	// Minimum bid when no one has bid yet
14	InitialMinimalBidding *big.Int `json:"initialMinimalBidding"`
15	// First block where the first slot begins
16	GenesisBlockNum int64 `json:"genesisBlockNum"`
17	// ERC777 token with which the bids will be made
18	TokenHEZ ethCommon.Address `json:"tokenHEZ"`
19	// HermezRollup smartcontract address
20	HermezRollup ethCommon.Address `json:"hermezRollup"`
21	// Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee
22	// Only for test
23	GovernanceAddress ethCommon.Address `json:"governanceAddress"`
24}
25
26// SlotNum returns the slot number of a block number
27func (c *AuctionConstants) SlotNum(blockNum int64) int64 {
28	if blockNum >= c.GenesisBlockNum {
29		return (blockNum - c.GenesisBlockNum) / int64(c.BlocksPerSlot)
30	}
31	return -1
32}
33
34// SlotBlocks returns the first and the last block numbers included in that slot
35func (c *AuctionConstants) SlotBlocks(slotNum int64) (int64, int64) {
36	startBlock := c.GenesisBlockNum + slotNum*int64(c.BlocksPerSlot)
37	endBlock := startBlock + int64(c.BlocksPerSlot) - 1
38	return startBlock, endBlock
39}
40
41// RelativeBlock returns the relative block number within the slot where the
42// block number belongs
43func (c *AuctionConstants) RelativeBlock(blockNum int64) int64 {
44	slotNum := c.SlotNum(blockNum)
45	return blockNum - (c.GenesisBlockNum + (slotNum * int64(c.BlocksPerSlot)))
46}
47
48// AuctionVariables are the variables of the Auction Smart Contract
49type AuctionVariables struct {
50	EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
51	// Boot Coordinator Address
52	DonationAddress ethCommon.Address `json:"donationAddress" meddler:"donation_address" validate:"required"`
53	// Boot Coordinator Address
54	BootCoordinator ethCommon.Address `json:"bootCoordinator" meddler:"boot_coordinator" validate:"required"`
55	// The minimum bid value in a series of 6 slots
56	DefaultSlotSetBid [6]*big.Int `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
57	// SlotNum at which the new default_slot_set_bid applies
58	DefaultSlotSetBidSlotNum int64 `json:"-" meddler:"default_slot_set_bid_slot_num"`
59	// Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
60	ClosedAuctionSlots uint16 `json:"closedAuctionSlots" meddler:"closed_auction_slots" validate:"required"`
61	// Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
62	OpenAuctionSlots uint16 `json:"openAuctionSlots" meddler:"open_auction_slots" validate:"required"`
63	// How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
64	AllocationRatio [3]uint16 `json:"allocationRatio" meddler:"allocation_ratio,json" validate:"required"`
65	// Minimum outbid (percentage) over the previous one to consider it valid
66	Outbidding uint16 `json:"outbidding" meddler:"outbidding" validate:"required"`
67	// Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
68	SlotDeadline uint8 `json:"slotDeadline" meddler:"slot_deadline" validate:"required"`
69}
70
71// Copy returns a deep copy of the Variables
72func (v *AuctionVariables) Copy() *AuctionVariables {
73	vCpy := *v
74	for i := range v.DefaultSlotSetBid {
75		vCpy.DefaultSlotSetBid[i] = CopyBigInt(v.DefaultSlotSetBid[i])
76	}
77	return &vCpy
78}