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