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.

523 lines
19 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. "github.com/ethereum/go-ethereum/ethclient"
  7. HermezAuctionProtocol "github.com/hermeznetwork/hermez-node/eth/contracts/auction"
  8. )
  9. // AuctionConstants are the constants of the Rollup Smart Contract
  10. type AuctionConstants struct {
  11. // Blocks to wait before starting with the first slot
  12. DelayGenesis uint16
  13. // Blocks per slot
  14. BlocksPerSlot uint8
  15. // Minimum bid when no one has bid yet
  16. InitialMinimalBidding *big.Int
  17. // First block where the first slot begins
  18. GenesisBlockNum int64
  19. // Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee
  20. GovernanceAddress ethCommon.Address
  21. // ERC777 token with which the bids will be made
  22. TokenHEZ ethCommon.Address
  23. // HermezRollup smartcontract address
  24. HermezRollup ethCommon.Address
  25. }
  26. // SlotState is the state of a slot
  27. type SlotState struct {
  28. Forger ethCommon.Address
  29. BidAmount *big.Int
  30. ClosedMinBid *big.Int
  31. Fulfilled bool
  32. }
  33. // Coordinator is the details of the Coordinator identified by the forger address
  34. type Coordinator struct {
  35. WithdrawalAddress ethCommon.Address
  36. URL string
  37. }
  38. // AuctionVariables are the variables of the Auction Smart Contract
  39. type AuctionVariables struct {
  40. // Boot Coordinator Address
  41. DonationAddress ethCommon.Address
  42. // Boot Coordinator Address
  43. BootCoordinator ethCommon.Address
  44. // The minimum bid value in a series of 6 slots
  45. MinBidEpoch [6]*big.Int
  46. // Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
  47. ClosedAuctionSlots uint16
  48. // Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
  49. OpenAuctionSlots uint16
  50. // How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
  51. AllocationRatio [3]uint8
  52. // Minimum outbid (percentage) over the previous one to consider it valid
  53. Outbidding uint8
  54. // Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
  55. SlotDeadline uint8
  56. }
  57. // AuctionState represents the state of the Rollup in the Smart Contract
  58. type AuctionState struct {
  59. // Mapping to control slot state
  60. Slots map[int64]*SlotState
  61. // Mapping to control balances pending to claim
  62. PendingBalances map[ethCommon.Address]*big.Int
  63. // Mapping to register all the coordinators. The address used for the mapping is the forger address
  64. Coordinators map[ethCommon.Address]*Coordinator
  65. }
  66. // AuctionEventNewBid is an event of the Auction Smart Contract
  67. type AuctionEventNewBid struct {
  68. Slot int64
  69. BidAmount *big.Int
  70. CoordinatorForger ethCommon.Address
  71. }
  72. // AuctionEventNewSlotDeadline is an event of the Auction Smart Contract
  73. type AuctionEventNewSlotDeadline struct {
  74. NewSlotDeadline uint8
  75. }
  76. // AuctionEventNewClosedAuctionSlots is an event of the Auction Smart Contract
  77. type AuctionEventNewClosedAuctionSlots struct {
  78. NewClosedAuctionSlots uint16
  79. }
  80. // AuctionEventNewOutbidding is an event of the Auction Smart Contract
  81. type AuctionEventNewOutbidding struct {
  82. NewOutbidding uint8
  83. }
  84. // AuctionEventNewDonationAddress is an event of the Auction Smart Contract
  85. type AuctionEventNewDonationAddress struct {
  86. NewDonationAddress ethCommon.Address
  87. }
  88. // AuctionEventNewBootCoordinator is an event of the Auction Smart Contract
  89. type AuctionEventNewBootCoordinator struct {
  90. NewBootCoordinator ethCommon.Address
  91. }
  92. // AuctionEventNewOpenAuctionSlots is an event of the Auction Smart Contract
  93. type AuctionEventNewOpenAuctionSlots struct {
  94. NewOpenAuctionSlots uint16
  95. }
  96. // AuctionEventNewAllocationRatio is an event of the Auction Smart Contract
  97. type AuctionEventNewAllocationRatio struct {
  98. NewAllocationRatio [3]uint8
  99. }
  100. // AuctionEventNewCoordinator is an event of the Auction Smart Contract
  101. type AuctionEventNewCoordinator struct {
  102. ForgerAddress ethCommon.Address
  103. WithdrawalAddress ethCommon.Address
  104. URL string
  105. }
  106. // AuctionEventCoordinatorUpdated is an event of the Auction Smart Contract
  107. type AuctionEventCoordinatorUpdated struct {
  108. ForgerAddress ethCommon.Address
  109. WithdrawalAddress ethCommon.Address
  110. URL string
  111. }
  112. // AuctionEventNewForgeAllocated is an event of the Auction Smart Contract
  113. type AuctionEventNewForgeAllocated struct {
  114. Forger ethCommon.Address
  115. CurrentSlot int64
  116. BurnAmount *big.Int
  117. DonationAmount *big.Int
  118. GovernanceAmount *big.Int
  119. }
  120. // AuctionEventNewMinBidEpoch is an event of the Auction Smart Contract
  121. type AuctionEventNewMinBidEpoch struct {
  122. SlotEpoch int64
  123. NewInitialMinBid *big.Int
  124. }
  125. // AuctionEventNewForge is an event of the Auction Smart Contract
  126. type AuctionEventNewForge struct {
  127. Forger ethCommon.Address
  128. CurrentSlot int64
  129. }
  130. // AuctionEventHEZClaimed is an event of the Auction Smart Contract
  131. type AuctionEventHEZClaimed struct {
  132. Owner ethCommon.Address
  133. Amount *big.Int
  134. }
  135. // AuctionEvents is the list of events in a block of the Auction Smart Contract
  136. type AuctionEvents struct { //nolint:structcheck
  137. NewBid []AuctionEventNewBid
  138. NewSlotDeadline []AuctionEventNewSlotDeadline
  139. NewClosedAuctionSlots []AuctionEventNewClosedAuctionSlots
  140. NewOutbidding []AuctionEventNewOutbidding
  141. NewDonationAddress []AuctionEventNewDonationAddress
  142. NewBootCoordinator []AuctionEventNewBootCoordinator
  143. NewOpenAuctionSlots []AuctionEventNewOpenAuctionSlots
  144. NewAllocationRatio []AuctionEventNewAllocationRatio
  145. NewCoordinator []AuctionEventNewCoordinator
  146. CoordinatorUpdated []AuctionEventCoordinatorUpdated
  147. NewForgeAllocated []AuctionEventNewForgeAllocated
  148. NewMinBidEpoch []AuctionEventNewMinBidEpoch
  149. NewForge []AuctionEventNewForge
  150. HEZClaimed []AuctionEventHEZClaimed
  151. }
  152. // NewAuctionEvents creates an empty AuctionEvents with the slices initialized.
  153. func NewAuctionEvents() AuctionEvents {
  154. return AuctionEvents{
  155. NewBid: make([]AuctionEventNewBid, 0),
  156. NewSlotDeadline: make([]AuctionEventNewSlotDeadline, 0),
  157. NewClosedAuctionSlots: make([]AuctionEventNewClosedAuctionSlots, 0),
  158. NewOutbidding: make([]AuctionEventNewOutbidding, 0),
  159. NewDonationAddress: make([]AuctionEventNewDonationAddress, 0),
  160. NewBootCoordinator: make([]AuctionEventNewBootCoordinator, 0),
  161. NewOpenAuctionSlots: make([]AuctionEventNewOpenAuctionSlots, 0),
  162. NewAllocationRatio: make([]AuctionEventNewAllocationRatio, 0),
  163. NewCoordinator: make([]AuctionEventNewCoordinator, 0),
  164. CoordinatorUpdated: make([]AuctionEventCoordinatorUpdated, 0),
  165. NewForgeAllocated: make([]AuctionEventNewForgeAllocated, 0),
  166. NewMinBidEpoch: make([]AuctionEventNewMinBidEpoch, 0),
  167. NewForge: make([]AuctionEventNewForge, 0),
  168. HEZClaimed: make([]AuctionEventHEZClaimed, 0),
  169. }
  170. }
  171. // AuctionInterface is the inteface to to Auction Smart Contract
  172. type AuctionInterface interface {
  173. //
  174. // Smart Contract Methods
  175. //
  176. // Getter/Setter, where Setter is onlyOwner
  177. AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error)
  178. AuctionGetSlotDeadline() (uint8, error)
  179. AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error)
  180. AuctionGetOpenAuctionSlots() (uint16, error)
  181. AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error)
  182. AuctionGetClosedAuctionSlots() (uint16, error)
  183. AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error)
  184. AuctionGetOutbidding() (uint8, error)
  185. AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error)
  186. AuctionGetAllocationRatio() ([3]uint8, error)
  187. AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error)
  188. AuctionGetDonationAddress() (*ethCommon.Address, error)
  189. AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error)
  190. AuctionGetBootCoordinator() (*ethCommon.Address, error)
  191. AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error)
  192. // Coordinator Management
  193. AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error)
  194. AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error)
  195. AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error)
  196. // Slot Info
  197. AuctionGetCurrentSlotNumber() (int64, error)
  198. AuctionGetMinBidBySlot(slot int64) (*big.Int, error)
  199. AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error)
  200. // Bidding
  201. // AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int,
  202. // userData, operatorData []byte) error // Only called from another smart contract
  203. AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error)
  204. AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool,
  205. maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error)
  206. // Forge
  207. AuctionCanForge(forger ethCommon.Address) (bool, error)
  208. // AuctionForge(forger ethCommon.Address) (bool, error) // Only called from another smart contract
  209. // Fees
  210. AuctionClaimHEZ() (*types.Transaction, error)
  211. //
  212. // Smart Contract Status
  213. //
  214. AuctionConstants() (*AuctionConstants, error)
  215. AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error)
  216. }
  217. //
  218. // Implementation
  219. //
  220. // AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
  221. type AuctionClient struct {
  222. client *EthereumClient
  223. address ethCommon.Address
  224. }
  225. // NewAuctionClient creates a new AuctionClient
  226. func NewAuctionClient(client *EthereumClient, address ethCommon.Address) *AuctionClient {
  227. return &AuctionClient{
  228. client: client,
  229. address: address,
  230. }
  231. }
  232. // AuctionSetSlotDeadline is the interface to call the smart contract function
  233. func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
  234. return nil, errTODO
  235. }
  236. // AuctionGetSlotDeadline is the interface to call the smart contract function
  237. func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) {
  238. var slotDeadline uint8
  239. if err := c.client.Call(func(ec *ethclient.Client) error {
  240. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  241. if err != nil {
  242. return err
  243. }
  244. slotDeadline, err = auction.GetSlotDeadline(nil)
  245. return err
  246. }); err != nil {
  247. return 0, err
  248. }
  249. return slotDeadline, nil
  250. }
  251. // AuctionSetOpenAuctionSlots is the interface to call the smart contract function
  252. func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
  253. return nil, errTODO
  254. }
  255. // AuctionGetOpenAuctionSlots is the interface to call the smart contract function
  256. func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) {
  257. var openAuctionSlots uint16
  258. if err := c.client.Call(func(ec *ethclient.Client) error {
  259. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  260. if err != nil {
  261. return err
  262. }
  263. openAuctionSlots, err = auction.GetOpenAuctionSlots(nil)
  264. return err
  265. }); err != nil {
  266. return 0, err
  267. }
  268. return openAuctionSlots, nil
  269. }
  270. // AuctionSetClosedAuctionSlots is the interface to call the smart contract function
  271. func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
  272. return nil, errTODO
  273. }
  274. // AuctionGetClosedAuctionSlots is the interface to call the smart contract function
  275. func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) {
  276. var closedAuctionSlots uint16
  277. if err := c.client.Call(func(ec *ethclient.Client) error {
  278. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  279. if err != nil {
  280. return err
  281. }
  282. closedAuctionSlots, err = auction.GetClosedAuctionSlots(nil)
  283. return err
  284. }); err != nil {
  285. return 0, err
  286. }
  287. return closedAuctionSlots, nil
  288. }
  289. // AuctionSetOutbidding is the interface to call the smart contract function
  290. func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error) {
  291. return nil, errTODO
  292. }
  293. // AuctionGetOutbidding is the interface to call the smart contract function
  294. func (c *AuctionClient) AuctionGetOutbidding() (uint8, error) {
  295. var outbidding uint8
  296. if err := c.client.Call(func(ec *ethclient.Client) error {
  297. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  298. if err != nil {
  299. return err
  300. }
  301. outbidding, err = auction.GetOutbidding(nil)
  302. return err
  303. }); err != nil {
  304. return 0, err
  305. }
  306. return outbidding, nil
  307. }
  308. // AuctionSetAllocationRatio is the interface to call the smart contract function
  309. func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error) {
  310. return nil, errTODO
  311. }
  312. // AuctionGetAllocationRatio is the interface to call the smart contract function
  313. func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint8, error) {
  314. var allocationRation [3]uint8
  315. if err := c.client.Call(func(ec *ethclient.Client) error {
  316. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  317. if err != nil {
  318. return err
  319. }
  320. allocationRation, err = auction.GetAllocationRatio(nil)
  321. return err
  322. }); err != nil {
  323. return [3]uint8{}, err
  324. }
  325. return allocationRation, nil
  326. }
  327. // AuctionSetDonationAddress is the interface to call the smart contract function
  328. func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
  329. return nil, errTODO
  330. }
  331. // AuctionGetDonationAddress is the interface to call the smart contract function
  332. func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) {
  333. var donationAddress ethCommon.Address
  334. if err := c.client.Call(func(ec *ethclient.Client) error {
  335. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  336. if err != nil {
  337. return err
  338. }
  339. donationAddress, err = auction.GetDonationAddress(nil)
  340. return err
  341. }); err != nil {
  342. return nil, err
  343. }
  344. return &donationAddress, nil
  345. }
  346. // AuctionSetBootCoordinator is the interface to call the smart contract function
  347. func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
  348. return nil, errTODO
  349. }
  350. // AuctionGetBootCoordinator is the interface to call the smart contract function
  351. func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
  352. var bootCoordinator ethCommon.Address
  353. if err := c.client.Call(func(ec *ethclient.Client) error {
  354. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  355. if err != nil {
  356. return err
  357. }
  358. bootCoordinator, err = auction.GetBootCoordinator(nil)
  359. return err
  360. }); err != nil {
  361. return nil, err
  362. }
  363. return &bootCoordinator, nil
  364. }
  365. // AuctionChangeEpochMinBid is the interface to call the smart contract function
  366. func (c *AuctionClient) AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
  367. return nil, errTODO
  368. }
  369. // AuctionRegisterCoordinator is the interface to call the smart contract function
  370. func (c *AuctionClient) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) {
  371. return nil, errTODO
  372. }
  373. // AuctionIsRegisteredCoordinator is the interface to call the smart contract function
  374. func (c *AuctionClient) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
  375. return false, errTODO
  376. }
  377. // AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
  378. func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
  379. return nil, errTODO
  380. }
  381. // AuctionGetCurrentSlotNumber is the interface to call the smart contract function
  382. func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) {
  383. var _currentSlotNumber *big.Int
  384. if err := c.client.Call(func(ec *ethclient.Client) error {
  385. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  386. if err != nil {
  387. return err
  388. }
  389. _currentSlotNumber, err = auction.GetCurrentSlotNumber(nil)
  390. return err
  391. }); err != nil {
  392. return 0, err
  393. }
  394. currentSlotNumber := _currentSlotNumber.Int64()
  395. return currentSlotNumber, nil
  396. }
  397. // AuctionGetMinBidBySlot is the interface to call the smart contract function
  398. func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
  399. var minBid *big.Int
  400. if err := c.client.Call(func(ec *ethclient.Client) error {
  401. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  402. if err != nil {
  403. return err
  404. }
  405. slotToSend := big.NewInt(slot)
  406. minBid, err = auction.GetMinBidBySlot(nil, slotToSend)
  407. return err
  408. }); err != nil {
  409. return big.NewInt(0), err
  410. }
  411. return minBid, nil
  412. }
  413. // AuctionGetMinBidEpoch is the interface to call the smart contract function
  414. func (c *AuctionClient) AuctionGetMinBidEpoch(epoch uint8) (*big.Int, error) {
  415. var minBidEpoch *big.Int
  416. if err := c.client.Call(func(ec *ethclient.Client) error {
  417. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  418. if err != nil {
  419. return err
  420. }
  421. minBidEpoch, err = auction.GetMinBidEpoch(nil, epoch)
  422. return err
  423. }); err != nil {
  424. return big.NewInt(0), err
  425. }
  426. return minBidEpoch, nil
  427. }
  428. // AuctionTokensReceived is the interface to call the smart contract function
  429. // func (c *AuctionClient) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
  430. // return errTODO
  431. // }
  432. // AuctionBid is the interface to call the smart contract function
  433. func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
  434. return nil, errTODO
  435. }
  436. // AuctionMultiBid is the interface to call the smart contract function
  437. func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotEpoch [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
  438. return nil, errTODO
  439. }
  440. // AuctionCanForge is the interface to call the smart contract function
  441. func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address) (bool, error) {
  442. return false, errTODO
  443. }
  444. // AuctionForge is the interface to call the smart contract function
  445. // func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (bool, error) {
  446. // return false, errTODO
  447. // }
  448. // AuctionClaimHEZ is the interface to call the smart contract function
  449. func (c *AuctionClient) AuctionClaimHEZ() (*types.Transaction, error) {
  450. return nil, errTODO
  451. }
  452. // AuctionConstants returns the Constants of the Auction Smart Contract
  453. func (c *AuctionClient) AuctionConstants() (*AuctionConstants, error) {
  454. return nil, errTODO
  455. }
  456. // AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
  457. func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error) {
  458. return nil, nil, errTODO
  459. }