You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

399 lines
15 KiB

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