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 // Only for test
29 GovernanceAddress ethCommon.Address `json:"governanceAddress"`
30}
31
32// SlotNum returns the slot number of a block number
33func (c *AuctionConstants) SlotNum(blockNum int64) int64 {
34 if blockNum >= c.GenesisBlockNum {
35 return (blockNum - c.GenesisBlockNum) / int64(c.BlocksPerSlot)
36 }
37 return -1
38}
39
40// SlotBlocks returns the first and the last block numbers included in that slot
41func (c *AuctionConstants) SlotBlocks(slotNum int64) (int64, int64) {
42 startBlock := c.GenesisBlockNum + slotNum*int64(c.BlocksPerSlot)
43 endBlock := startBlock + int64(c.BlocksPerSlot) - 1
44 return startBlock, endBlock
45}
46
47// RelativeBlock returns the relative block number within the slot where the
48// block number belongs
49func (c *AuctionConstants) RelativeBlock(blockNum int64) int64 {
50 slotNum := c.SlotNum(blockNum)
51 return blockNum - (c.GenesisBlockNum + (slotNum * int64(c.BlocksPerSlot)))
52}
53
54// AuctionVariables are the variables of the Auction Smart Contract
55type AuctionVariables struct {
56 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
57 // Boot Coordinator Address
58 DonationAddress ethCommon.Address `json:"donationAddress" meddler:"donation_address" validate:"required"`
59 // Boot Coordinator Address
60 BootCoordinator ethCommon.Address `json:"bootCoordinator" meddler:"boot_coordinator" validate:"required"`
61 // Boot Coordinator URL
62 BootCoordinatorURL string `json:"bootCoordinatorUrl" meddler:"boot_coordinator_url" validate:"required"`
63 // The minimum bid value in a series of 6 slots
64 DefaultSlotSetBid [6]*big.Int `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
65 // SlotNum at which the new default_slot_set_bid applies
66 DefaultSlotSetBidSlotNum int64 `json:"-" meddler:"default_slot_set_bid_slot_num"`
67 // Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
68 ClosedAuctionSlots uint16 `json:"closedAuctionSlots" meddler:"closed_auction_slots" validate:"required"`
69 // Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
70 OpenAuctionSlots uint16 `json:"openAuctionSlots" meddler:"open_auction_slots" validate:"required"`
71 // How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
72 AllocationRatio [3]uint16 `json:"allocationRatio" meddler:"allocation_ratio,json" validate:"required"`
73 // Minimum outbid (percentage) over the previous one to consider it valid
74 Outbidding uint16 `json:"outbidding" meddler:"outbidding" validate:"required"`
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 `json:"slotDeadline" meddler:"slot_deadline" validate:"required"`
77}
78
79// Copy returns a deep copy of the Variables
80func (v *AuctionVariables) Copy() *AuctionVariables {
81 vCpy := *v
82 for i := range v.DefaultSlotSetBid {
83 vCpy.DefaultSlotSetBid[i] = CopyBigInt(v.DefaultSlotSetBid[i])
84 }
85 return &vCpy
86}