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.

380 lines
14 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. // AuctionInterface is the inteface to to Auction Smart Contract
  151. type AuctionInterface interface {
  152. //
  153. // Smart Contract Methods
  154. //
  155. // Getter/Setter, where Setter is onlyOwner
  156. AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error)
  157. AuctionGetSlotDeadline() (uint8, error)
  158. AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error)
  159. AuctionGetOpenAuctionSlots() (uint16, error)
  160. AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error)
  161. AuctionGetClosedAuctionSlots() (uint16, error)
  162. AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error)
  163. AuctionGetOutbidding() (uint8, error)
  164. AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error)
  165. AuctionGetAllocationRatio() ([3]uint8, error)
  166. AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error)
  167. AuctionGetDonationAddress() (*ethCommon.Address, error)
  168. AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error)
  169. AuctionGetBootCoordinator() (*ethCommon.Address, error)
  170. AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error)
  171. // Coordinator Management
  172. AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error)
  173. AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error)
  174. AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error)
  175. // Slot Info
  176. AuctionGetCurrentSlotNumber() (int64, error)
  177. AuctionGetMinBidBySlot(slot int64) (*big.Int, error)
  178. AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error)
  179. // Bidding
  180. // AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int,
  181. // userData, operatorData []byte) error // Only called from another smart contract
  182. AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error)
  183. AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool,
  184. maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error)
  185. // Forge
  186. AuctionCanForge(forger ethCommon.Address) (bool, error)
  187. // AuctionForge(forger ethCommon.Address) (bool, error) // Only called from another smart contract
  188. // Fees
  189. AuctionClaimHEZ() (*types.Transaction, error)
  190. //
  191. // Smart Contract Status
  192. //
  193. AuctionConstants() (*AuctionConstants, error)
  194. AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error)
  195. }
  196. //
  197. // Implementation
  198. //
  199. // AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
  200. type AuctionClient struct {
  201. }
  202. // AuctionSetSlotDeadline is the interface to call the smart contract function
  203. func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
  204. return nil, errTODO
  205. }
  206. // AuctionGetSlotDeadline is the interface to call the smart contract function
  207. func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) {
  208. return 0, errTODO
  209. }
  210. // AuctionSetOpenAuctionSlots is the interface to call the smart contract function
  211. func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
  212. return nil, errTODO
  213. }
  214. // AuctionGetOpenAuctionSlots is the interface to call the smart contract function
  215. func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) {
  216. return 0, errTODO
  217. }
  218. // AuctionSetClosedAuctionSlots is the interface to call the smart contract function
  219. func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
  220. return nil, errTODO
  221. }
  222. // AuctionGetClosedAuctionSlots is the interface to call the smart contract function
  223. func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) {
  224. return 0, errTODO
  225. }
  226. // AuctionSetOutbidding is the interface to call the smart contract function
  227. func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error) {
  228. return nil, errTODO
  229. }
  230. // AuctionGetOutbidding is the interface to call the smart contract function
  231. func (c *AuctionClient) AuctionGetOutbidding() (uint8, error) {
  232. return 0, errTODO
  233. }
  234. // AuctionSetAllocationRatio is the interface to call the smart contract function
  235. func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error) {
  236. return nil, errTODO
  237. }
  238. // AuctionGetAllocationRatio is the interface to call the smart contract function
  239. func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint8, error) {
  240. return [3]uint8{}, errTODO
  241. }
  242. // AuctionSetDonationAddress is the interface to call the smart contract function
  243. func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
  244. return nil, errTODO
  245. }
  246. // AuctionGetDonationAddress is the interface to call the smart contract function
  247. func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) {
  248. return nil, errTODO
  249. }
  250. // AuctionSetBootCoordinator is the interface to call the smart contract function
  251. func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
  252. return nil, errTODO
  253. }
  254. // AuctionGetBootCoordinator is the interface to call the smart contract function
  255. func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
  256. return nil, errTODO
  257. }
  258. // AuctionChangeEpochMinBid is the interface to call the smart contract function
  259. func (c *AuctionClient) AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
  260. return nil, errTODO
  261. }
  262. // AuctionRegisterCoordinator is the interface to call the smart contract function
  263. func (c *AuctionClient) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) {
  264. return nil, errTODO
  265. }
  266. // AuctionIsRegisteredCoordinator is the interface to call the smart contract function
  267. func (c *AuctionClient) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
  268. return false, errTODO
  269. }
  270. // AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
  271. func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
  272. return nil, errTODO
  273. }
  274. // AuctionGetCurrentSlotNumber is the interface to call the smart contract function
  275. func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) {
  276. return 0, errTODO
  277. }
  278. // AuctionGetMinBidBySlot is the interface to call the smart contract function
  279. func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
  280. return nil, errTODO
  281. }
  282. // AuctionGetMinBidEpoch is the interface to call the smart contract function
  283. func (c *AuctionClient) AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error) {
  284. return nil, errTODO
  285. }
  286. // AuctionTokensReceived is the interface to call the smart contract function
  287. // func (c *AuctionClient) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
  288. // return errTODO
  289. // }
  290. // AuctionBid is the interface to call the smart contract function
  291. func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
  292. return nil, errTODO
  293. }
  294. // AuctionMultiBid is the interface to call the smart contract function
  295. func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
  296. return nil, errTODO
  297. }
  298. // AuctionCanForge is the interface to call the smart contract function
  299. func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address) (bool, error) {
  300. return false, errTODO
  301. }
  302. // AuctionForge is the interface to call the smart contract function
  303. // func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (bool, error) {
  304. // return false, errTODO
  305. // }
  306. // AuctionClaimHEZ is the interface to call the smart contract function
  307. func (c *AuctionClient) AuctionClaimHEZ() (*types.Transaction, error) {
  308. return nil, errTODO
  309. }
  310. // AuctionConstants returns the Constants of the Auction Smart Contract
  311. func (c *AuctionClient) AuctionConstants() (*AuctionConstants, error) {
  312. return nil, errTODO
  313. }
  314. // AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
  315. func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error) {
  316. return nil, nil, errTODO
  317. }