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.

793 lines
27 KiB

  1. package eth
  2. import (
  3. "fmt"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/core/types"
  8. "github.com/ethereum/go-ethereum/ethclient"
  9. HermezAuctionProtocol "github.com/hermeznetwork/hermez-node/eth/contracts/auction"
  10. "github.com/hermeznetwork/hermez-node/log"
  11. )
  12. // AuctionConstants are the constants of the Rollup Smart Contract
  13. type AuctionConstants struct {
  14. // Blocks per slot
  15. BlocksPerSlot uint8
  16. // Minimum bid when no one has bid yet
  17. InitialMinimalBidding *big.Int
  18. // First block where the first slot begins
  19. GenesisBlockNum int64
  20. // Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee
  21. // Only for test
  22. GovernanceAddress ethCommon.Address
  23. // ERC777 token with which the bids will be made
  24. TokenHEZ ethCommon.Address
  25. // HermezRollup smartcontract address
  26. HermezRollup ethCommon.Address
  27. }
  28. // SlotState is the state of a slot
  29. type SlotState struct {
  30. Forger ethCommon.Address
  31. BidAmount *big.Int
  32. ClosedMinBid *big.Int
  33. Fulfilled bool
  34. }
  35. // NewSlotState returns an empty SlotState
  36. func NewSlotState() *SlotState {
  37. return &SlotState{
  38. Forger: ethCommon.Address{},
  39. BidAmount: big.NewInt(0),
  40. ClosedMinBid: big.NewInt(0),
  41. Fulfilled: false,
  42. }
  43. }
  44. // Coordinator is the details of the Coordinator identified by the forger address
  45. type Coordinator struct {
  46. WithdrawalAddress ethCommon.Address
  47. URL string
  48. }
  49. // AuctionVariables are the variables of the Auction Smart Contract
  50. type AuctionVariables struct {
  51. // Boot Coordinator Address
  52. DonationAddress ethCommon.Address
  53. // Boot Coordinator Address
  54. BootCoordinator ethCommon.Address
  55. // The minimum bid value in a series of 6 slots
  56. DefaultSlotSetBid [6]*big.Int
  57. // Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
  58. ClosedAuctionSlots uint16
  59. // Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
  60. OpenAuctionSlots uint16
  61. // How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
  62. AllocationRatio [3]uint16
  63. // Minimum outbid (percentage) over the previous one to consider it valid
  64. Outbidding uint16
  65. // Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
  66. SlotDeadline uint8
  67. }
  68. // AuctionState represents the state of the Rollup in the Smart Contract
  69. type AuctionState struct {
  70. // Mapping to control slot state
  71. Slots map[int64]*SlotState
  72. // Mapping to control balances pending to claim
  73. PendingBalances map[ethCommon.Address]*big.Int
  74. // Mapping to register all the coordinators. The address used for the mapping is the forger address
  75. Coordinators map[ethCommon.Address]*Coordinator
  76. }
  77. // AuctionEventNewBid is an event of the Auction Smart Contract
  78. type AuctionEventNewBid struct {
  79. Slot int64
  80. BidAmount *big.Int
  81. CoordinatorForger ethCommon.Address
  82. }
  83. // AuctionEventNewSlotDeadline is an event of the Auction Smart Contract
  84. type AuctionEventNewSlotDeadline struct {
  85. NewSlotDeadline uint8
  86. }
  87. // AuctionEventNewClosedAuctionSlots is an event of the Auction Smart Contract
  88. type AuctionEventNewClosedAuctionSlots struct {
  89. NewClosedAuctionSlots uint16
  90. }
  91. // AuctionEventNewOutbidding is an event of the Auction Smart Contract
  92. type AuctionEventNewOutbidding struct {
  93. NewOutbidding uint16
  94. }
  95. // AuctionEventNewDonationAddress is an event of the Auction Smart Contract
  96. type AuctionEventNewDonationAddress struct {
  97. NewDonationAddress ethCommon.Address
  98. }
  99. // AuctionEventNewBootCoordinator is an event of the Auction Smart Contract
  100. type AuctionEventNewBootCoordinator struct {
  101. NewBootCoordinator ethCommon.Address
  102. }
  103. // AuctionEventNewOpenAuctionSlots is an event of the Auction Smart Contract
  104. type AuctionEventNewOpenAuctionSlots struct {
  105. NewOpenAuctionSlots uint16
  106. }
  107. // AuctionEventNewAllocationRatio is an event of the Auction Smart Contract
  108. type AuctionEventNewAllocationRatio struct {
  109. NewAllocationRatio [3]uint16
  110. }
  111. // AuctionEventNewCoordinator is an event of the Auction Smart Contract
  112. type AuctionEventNewCoordinator struct {
  113. ForgerAddress ethCommon.Address
  114. WithdrawalAddress ethCommon.Address
  115. URL string
  116. }
  117. // AuctionEventCoordinatorUpdated is an event of the Auction Smart Contract
  118. type AuctionEventCoordinatorUpdated struct {
  119. ForgerAddress ethCommon.Address
  120. WithdrawalAddress ethCommon.Address
  121. URL string
  122. }
  123. // AuctionEventNewForgeAllocated is an event of the Auction Smart Contract
  124. type AuctionEventNewForgeAllocated struct {
  125. Forger ethCommon.Address
  126. CurrentSlot int64
  127. BurnAmount *big.Int
  128. DonationAmount *big.Int
  129. GovernanceAmount *big.Int
  130. }
  131. // AuctionEventNewDefaultSlotSetBid is an event of the Auction Smart Contract
  132. type AuctionEventNewDefaultSlotSetBid struct {
  133. SlotSet int64
  134. NewInitialMinBid *big.Int
  135. }
  136. // AuctionEventNewForge is an event of the Auction Smart Contract
  137. type AuctionEventNewForge struct {
  138. Forger ethCommon.Address
  139. CurrentSlot int64
  140. }
  141. // AuctionEventHEZClaimed is an event of the Auction Smart Contract
  142. type AuctionEventHEZClaimed struct {
  143. Owner ethCommon.Address
  144. Amount *big.Int
  145. }
  146. // AuctionEvents is the list of events in a block of the Auction Smart Contract
  147. type AuctionEvents struct { //nolint:structcheck
  148. NewBid []AuctionEventNewBid
  149. NewSlotDeadline []AuctionEventNewSlotDeadline
  150. NewClosedAuctionSlots []AuctionEventNewClosedAuctionSlots
  151. NewOutbidding []AuctionEventNewOutbidding
  152. NewDonationAddress []AuctionEventNewDonationAddress
  153. NewBootCoordinator []AuctionEventNewBootCoordinator
  154. NewOpenAuctionSlots []AuctionEventNewOpenAuctionSlots
  155. NewAllocationRatio []AuctionEventNewAllocationRatio
  156. NewCoordinator []AuctionEventNewCoordinator
  157. CoordinatorUpdated []AuctionEventCoordinatorUpdated
  158. NewForgeAllocated []AuctionEventNewForgeAllocated
  159. NewDefaultSlotSetBid []AuctionEventNewDefaultSlotSetBid
  160. NewForge []AuctionEventNewForge
  161. HEZClaimed []AuctionEventHEZClaimed
  162. }
  163. // NewAuctionEvents creates an empty AuctionEvents with the slices initialized.
  164. func NewAuctionEvents() AuctionEvents {
  165. return AuctionEvents{
  166. NewBid: make([]AuctionEventNewBid, 0),
  167. NewSlotDeadline: make([]AuctionEventNewSlotDeadline, 0),
  168. NewClosedAuctionSlots: make([]AuctionEventNewClosedAuctionSlots, 0),
  169. NewOutbidding: make([]AuctionEventNewOutbidding, 0),
  170. NewDonationAddress: make([]AuctionEventNewDonationAddress, 0),
  171. NewBootCoordinator: make([]AuctionEventNewBootCoordinator, 0),
  172. NewOpenAuctionSlots: make([]AuctionEventNewOpenAuctionSlots, 0),
  173. NewAllocationRatio: make([]AuctionEventNewAllocationRatio, 0),
  174. NewCoordinator: make([]AuctionEventNewCoordinator, 0),
  175. CoordinatorUpdated: make([]AuctionEventCoordinatorUpdated, 0),
  176. NewForgeAllocated: make([]AuctionEventNewForgeAllocated, 0),
  177. NewDefaultSlotSetBid: make([]AuctionEventNewDefaultSlotSetBid, 0),
  178. NewForge: make([]AuctionEventNewForge, 0),
  179. HEZClaimed: make([]AuctionEventHEZClaimed, 0),
  180. }
  181. }
  182. // AuctionInterface is the inteface to to Auction Smart Contract
  183. type AuctionInterface interface {
  184. //
  185. // Smart Contract Methods
  186. //
  187. // Getter/Setter, where Setter is onlyOwner
  188. AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error)
  189. AuctionGetSlotDeadline() (uint8, error)
  190. AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error)
  191. AuctionGetOpenAuctionSlots() (uint16, error)
  192. AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error)
  193. AuctionGetClosedAuctionSlots() (uint16, error)
  194. AuctionSetOutbidding(newOutbidding uint16) (*types.Transaction, error)
  195. AuctionGetOutbidding() (uint16, error)
  196. AuctionSetAllocationRatio(newAllocationRatio [3]uint16) (*types.Transaction, error)
  197. AuctionGetAllocationRatio() ([3]uint16, error)
  198. AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error)
  199. AuctionGetDonationAddress() (*ethCommon.Address, error)
  200. AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error)
  201. AuctionGetBootCoordinator() (*ethCommon.Address, error)
  202. AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (*types.Transaction, error)
  203. // Coordinator Management
  204. AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error)
  205. AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error)
  206. AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error)
  207. // Slot Info
  208. AuctionGetCurrentSlotNumber() (int64, error)
  209. AuctionGetMinBidBySlot(slot int64) (*big.Int, error)
  210. AuctionGetDefaultSlotSetBid(slotSet uint8) (*big.Int, error)
  211. // Bidding
  212. // AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int,
  213. // userData, operatorData []byte) error // Only called from another smart contract
  214. AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error)
  215. AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool,
  216. maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error)
  217. // Forge
  218. AuctionCanForge(forger ethCommon.Address, blockNum int64) (bool, error)
  219. // AuctionForge(forger ethCommon.Address) (bool, error) // Only called from another smart contract
  220. // Fees
  221. AuctionClaimHEZ(claimAddress ethCommon.Address) (*types.Transaction, error)
  222. //
  223. // Smart Contract Status
  224. //
  225. AuctionConstants() (*AuctionConstants, error)
  226. AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error)
  227. }
  228. //
  229. // Implementation
  230. //
  231. // AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
  232. type AuctionClient struct {
  233. client *EthereumClient
  234. address ethCommon.Address
  235. gasLimit uint64
  236. }
  237. // NewAuctionClient creates a new AuctionClient
  238. func NewAuctionClient(client *EthereumClient, address ethCommon.Address) *AuctionClient {
  239. return &AuctionClient{
  240. client: client,
  241. address: address,
  242. gasLimit: 1000000, //nolint:gomnd
  243. }
  244. }
  245. // AuctionSetSlotDeadline is the interface to call the smart contract function
  246. func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
  247. var tx *types.Transaction
  248. var err error
  249. if tx, err = c.client.CallAuth(
  250. c.gasLimit,
  251. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  252. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  253. if err != nil {
  254. return nil, err
  255. }
  256. return auction.SetSlotDeadline(auth, newDeadline)
  257. },
  258. ); err != nil {
  259. return nil, fmt.Errorf("Failed setting slotDeadline: %w", err)
  260. }
  261. return tx, nil
  262. }
  263. // AuctionGetSlotDeadline is the interface to call the smart contract function
  264. func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) {
  265. var slotDeadline uint8
  266. if err := c.client.Call(func(ec *ethclient.Client) error {
  267. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  268. if err != nil {
  269. return err
  270. }
  271. slotDeadline, err = auction.GetSlotDeadline(nil)
  272. return err
  273. }); err != nil {
  274. return 0, err
  275. }
  276. return slotDeadline, nil
  277. }
  278. // AuctionSetOpenAuctionSlots is the interface to call the smart contract function
  279. func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
  280. var tx *types.Transaction
  281. var err error
  282. if tx, err = c.client.CallAuth(
  283. c.gasLimit,
  284. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  285. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  286. if err != nil {
  287. return nil, err
  288. }
  289. return auction.SetOpenAuctionSlots(auth, newOpenAuctionSlots)
  290. },
  291. ); err != nil {
  292. return nil, fmt.Errorf("Failed setting openAuctionSlots: %w", err)
  293. }
  294. return tx, nil
  295. }
  296. // AuctionGetOpenAuctionSlots is the interface to call the smart contract function
  297. func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) {
  298. var openAuctionSlots uint16
  299. if err := c.client.Call(func(ec *ethclient.Client) error {
  300. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  301. if err != nil {
  302. return err
  303. }
  304. openAuctionSlots, err = auction.GetOpenAuctionSlots(nil)
  305. return err
  306. }); err != nil {
  307. return 0, err
  308. }
  309. return openAuctionSlots, nil
  310. }
  311. // AuctionSetClosedAuctionSlots is the interface to call the smart contract function
  312. func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
  313. var tx *types.Transaction
  314. var err error
  315. if tx, err = c.client.CallAuth(
  316. c.gasLimit,
  317. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  318. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  319. if err != nil {
  320. return nil, err
  321. }
  322. return auction.SetClosedAuctionSlots(auth, newClosedAuctionSlots)
  323. },
  324. ); err != nil {
  325. return nil, fmt.Errorf("Failed setting closedAuctionSlots: %w", err)
  326. }
  327. return tx, nil
  328. }
  329. // AuctionGetClosedAuctionSlots is the interface to call the smart contract function
  330. func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) {
  331. var closedAuctionSlots uint16
  332. if err := c.client.Call(func(ec *ethclient.Client) error {
  333. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  334. if err != nil {
  335. return err
  336. }
  337. closedAuctionSlots, err = auction.GetClosedAuctionSlots(nil)
  338. return err
  339. }); err != nil {
  340. return 0, err
  341. }
  342. return closedAuctionSlots, nil
  343. }
  344. // AuctionSetOutbidding is the interface to call the smart contract function
  345. func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint16) (*types.Transaction, error) {
  346. var tx *types.Transaction
  347. var err error
  348. if tx, err = c.client.CallAuth(
  349. 12500000, //nolint:gomnd
  350. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  351. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  352. if err != nil {
  353. return nil, err
  354. }
  355. return auction.SetOutbidding(auth, newOutbidding)
  356. },
  357. ); err != nil {
  358. return nil, fmt.Errorf("Failed setting setOutbidding: %w", err)
  359. }
  360. return tx, nil
  361. }
  362. // AuctionGetOutbidding is the interface to call the smart contract function
  363. func (c *AuctionClient) AuctionGetOutbidding() (uint16, error) {
  364. var outbidding uint16
  365. if err := c.client.Call(func(ec *ethclient.Client) error {
  366. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  367. if err != nil {
  368. return err
  369. }
  370. outbidding, err = auction.GetOutbidding(nil)
  371. return err
  372. }); err != nil {
  373. return 0, err
  374. }
  375. return outbidding, nil
  376. }
  377. // AuctionSetAllocationRatio is the interface to call the smart contract function
  378. func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint16) (*types.Transaction, error) {
  379. var tx *types.Transaction
  380. var err error
  381. if tx, err = c.client.CallAuth(
  382. c.gasLimit,
  383. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  384. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  385. if err != nil {
  386. return nil, err
  387. }
  388. return auction.SetAllocationRatio(auth, newAllocationRatio)
  389. },
  390. ); err != nil {
  391. return nil, fmt.Errorf("Failed setting allocationRatio: %w", err)
  392. }
  393. return tx, nil
  394. }
  395. // AuctionGetAllocationRatio is the interface to call the smart contract function
  396. func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint16, error) {
  397. var allocationRation [3]uint16
  398. if err := c.client.Call(func(ec *ethclient.Client) error {
  399. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  400. if err != nil {
  401. return err
  402. }
  403. allocationRation, err = auction.GetAllocationRatio(nil)
  404. return err
  405. }); err != nil {
  406. return [3]uint16{}, err
  407. }
  408. return allocationRation, nil
  409. }
  410. // AuctionSetDonationAddress is the interface to call the smart contract function
  411. func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
  412. var tx *types.Transaction
  413. var err error
  414. if tx, err = c.client.CallAuth(
  415. c.gasLimit,
  416. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  417. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  418. if err != nil {
  419. return nil, err
  420. }
  421. return auction.SetDonationAddress(auth, newDonationAddress)
  422. },
  423. ); err != nil {
  424. return nil, fmt.Errorf("Failed setting donationAddress: %w", err)
  425. }
  426. return tx, nil
  427. }
  428. // AuctionGetDonationAddress is the interface to call the smart contract function
  429. func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) {
  430. var donationAddress ethCommon.Address
  431. if err := c.client.Call(func(ec *ethclient.Client) error {
  432. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  433. if err != nil {
  434. return err
  435. }
  436. donationAddress, err = auction.GetDonationAddress(nil)
  437. return err
  438. }); err != nil {
  439. return nil, err
  440. }
  441. return &donationAddress, nil
  442. }
  443. // AuctionSetBootCoordinator is the interface to call the smart contract function
  444. func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
  445. var tx *types.Transaction
  446. var err error
  447. if tx, err = c.client.CallAuth(
  448. c.gasLimit,
  449. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  450. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  451. if err != nil {
  452. return nil, err
  453. }
  454. return auction.SetBootCoordinator(auth, newBootCoordinator)
  455. },
  456. ); err != nil {
  457. return nil, fmt.Errorf("Failed setting bootCoordinator: %w", err)
  458. }
  459. return tx, nil
  460. }
  461. // AuctionGetBootCoordinator is the interface to call the smart contract function
  462. func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
  463. var bootCoordinator ethCommon.Address
  464. if err := c.client.Call(func(ec *ethclient.Client) error {
  465. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  466. if err != nil {
  467. return err
  468. }
  469. bootCoordinator, err = auction.GetBootCoordinator(nil)
  470. return err
  471. }); err != nil {
  472. return nil, err
  473. }
  474. return &bootCoordinator, nil
  475. }
  476. // AuctionChangeDefaultSlotSetBid is the interface to call the smart contract function
  477. func (c *AuctionClient) AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
  478. var tx *types.Transaction
  479. var err error
  480. if tx, err = c.client.CallAuth(
  481. c.gasLimit,
  482. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  483. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  484. if err != nil {
  485. return nil, err
  486. }
  487. slotSetToSend := big.NewInt(slotSet)
  488. return auction.ChangeDefaultSlotSetBid(auth, slotSetToSend, newInitialMinBid)
  489. },
  490. ); err != nil {
  491. return nil, fmt.Errorf("Failed changing epoch minBid: %w", err)
  492. }
  493. return tx, nil
  494. }
  495. // AuctionRegisterCoordinator is the interface to call the smart contract function
  496. func (c *AuctionClient) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) {
  497. var tx *types.Transaction
  498. var err error
  499. if tx, err = c.client.CallAuth(
  500. c.gasLimit,
  501. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  502. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  503. if err != nil {
  504. return nil, err
  505. }
  506. return auction.RegisterCoordinator(auth, forgerAddress, URL)
  507. },
  508. ); err != nil {
  509. return nil, fmt.Errorf("Failed register coordinator: %w", err)
  510. }
  511. return tx, nil
  512. }
  513. // AuctionIsRegisteredCoordinator is the interface to call the smart contract function
  514. func (c *AuctionClient) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
  515. var registered bool
  516. if err := c.client.Call(func(ec *ethclient.Client) error {
  517. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  518. if err != nil {
  519. return err
  520. }
  521. registered, err = auction.IsRegisteredCoordinator(nil, forgerAddress)
  522. return err
  523. }); err != nil {
  524. return false, err
  525. }
  526. return registered, nil
  527. }
  528. // AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
  529. func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
  530. var tx *types.Transaction
  531. var err error
  532. if tx, err = c.client.CallAuth(
  533. c.gasLimit,
  534. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  535. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  536. if err != nil {
  537. return nil, err
  538. }
  539. return auction.UpdateCoordinatorInfo(auth, forgerAddress, newWithdrawAddress, newURL)
  540. },
  541. ); err != nil {
  542. return nil, fmt.Errorf("Failed update coordinator info: %w", err)
  543. }
  544. return tx, nil
  545. }
  546. // AuctionGetCurrentSlotNumber is the interface to call the smart contract function
  547. func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) {
  548. var _currentSlotNumber *big.Int
  549. if err := c.client.Call(func(ec *ethclient.Client) error {
  550. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  551. if err != nil {
  552. return err
  553. }
  554. _currentSlotNumber, err = auction.GetCurrentSlotNumber(nil)
  555. return err
  556. }); err != nil {
  557. return 0, err
  558. }
  559. currentSlotNumber := _currentSlotNumber.Int64()
  560. return currentSlotNumber, nil
  561. }
  562. // AuctionGetMinBidBySlot is the interface to call the smart contract function
  563. func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
  564. var minBid *big.Int
  565. if err := c.client.Call(func(ec *ethclient.Client) error {
  566. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  567. if err != nil {
  568. return err
  569. }
  570. slotToSend := big.NewInt(slot)
  571. minBid, err = auction.GetMinBidBySlot(nil, slotToSend)
  572. return err
  573. }); err != nil {
  574. return big.NewInt(0), err
  575. }
  576. return minBid, nil
  577. }
  578. // AuctionGetDefaultSlotSetBid is the interface to call the smart contract function
  579. func (c *AuctionClient) AuctionGetDefaultSlotSetBid(slotSet uint8) (*big.Int, error) {
  580. var minBidSlotSet *big.Int
  581. if err := c.client.Call(func(ec *ethclient.Client) error {
  582. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  583. if err != nil {
  584. return err
  585. }
  586. minBidSlotSet, err = auction.GetDefaultSlotSetBid(nil, slotSet)
  587. return err
  588. }); err != nil {
  589. return big.NewInt(0), err
  590. }
  591. return minBidSlotSet, nil
  592. }
  593. // AuctionTokensReceived is the interface to call the smart contract function
  594. // func (c *AuctionClient) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
  595. // return errTODO
  596. // }
  597. // AuctionBid is the interface to call the smart contract function
  598. func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
  599. log.Error("TODO")
  600. return nil, errTODO
  601. }
  602. // AuctionMultiBid is the interface to call the smart contract function
  603. func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
  604. log.Error("TODO")
  605. return nil, errTODO
  606. }
  607. // AuctionCanForge is the interface to call the smart contract function
  608. func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address, blockNum int64) (bool, error) {
  609. var canForge bool
  610. if err := c.client.Call(func(ec *ethclient.Client) error {
  611. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  612. if err != nil {
  613. return err
  614. }
  615. canForge, err = auction.CanForge(nil, forger, big.NewInt(blockNum))
  616. return err
  617. }); err != nil {
  618. return false, err
  619. }
  620. return canForge, nil
  621. }
  622. // AuctionForge is the interface to call the smart contract function
  623. // func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (bool, error) {
  624. // return false, errTODO
  625. // }
  626. // AuctionClaimHEZ is the interface to call the smart contract function
  627. func (c *AuctionClient) AuctionClaimHEZ(claimAddress ethCommon.Address) (*types.Transaction, error) {
  628. var tx *types.Transaction
  629. var err error
  630. if tx, err = c.client.CallAuth(
  631. c.gasLimit,
  632. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  633. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  634. if err != nil {
  635. return nil, err
  636. }
  637. return auction.ClaimHEZ(auth, claimAddress)
  638. },
  639. ); err != nil {
  640. return nil, fmt.Errorf("Failed claim HEZ: %w", err)
  641. }
  642. return tx, nil
  643. }
  644. // AuctionConstants returns the Constants of the Auction Smart Contract
  645. func (c *AuctionClient) AuctionConstants() (*AuctionConstants, error) {
  646. auctionConstants := new(AuctionConstants)
  647. if err := c.client.Call(func(ec *ethclient.Client) error {
  648. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  649. if err != nil {
  650. return err
  651. }
  652. auctionConstants.BlocksPerSlot, err = auction.BLOCKSPERSLOT(nil)
  653. if err != nil {
  654. return err
  655. }
  656. genesisBlock, err := auction.GenesisBlock(nil)
  657. if err != nil {
  658. return err
  659. }
  660. auctionConstants.GenesisBlockNum = genesisBlock.Int64()
  661. auctionConstants.HermezRollup, err = auction.HermezRollup(nil)
  662. if err != nil {
  663. return err
  664. }
  665. auctionConstants.InitialMinimalBidding, err = auction.INITIALMINIMALBIDDING(nil)
  666. if err != nil {
  667. return err
  668. }
  669. auctionConstants.TokenHEZ, err = auction.TokenHEZ(nil)
  670. return err
  671. }); err != nil {
  672. return nil, err
  673. }
  674. return auctionConstants, nil
  675. }
  676. // AuctionVariables returns the variables of the Auction Smart Contract
  677. func (c *AuctionClient) AuctionVariables() (*AuctionVariables, error) {
  678. auctionVariables := new(AuctionVariables)
  679. if err := c.client.Call(func(ec *ethclient.Client) error {
  680. var err error
  681. auctionVariables.AllocationRatio, err = c.AuctionGetAllocationRatio()
  682. if err != nil {
  683. return err
  684. }
  685. bootCoordinator, err := c.AuctionGetBootCoordinator()
  686. if err != nil {
  687. return err
  688. }
  689. auctionVariables.BootCoordinator = *bootCoordinator
  690. auctionVariables.ClosedAuctionSlots, err = c.AuctionGetClosedAuctionSlots()
  691. if err != nil {
  692. return err
  693. }
  694. var defaultSlotSetBid [6]*big.Int
  695. for i := uint8(0); i < 6; i++ {
  696. bid, err := c.AuctionGetDefaultSlotSetBid(i)
  697. if err != nil {
  698. return err
  699. }
  700. defaultSlotSetBid[i] = bid
  701. }
  702. auctionVariables.DefaultSlotSetBid = defaultSlotSetBid
  703. donationAddress, err := c.AuctionGetDonationAddress()
  704. if err != nil {
  705. return err
  706. }
  707. auctionVariables.DonationAddress = *donationAddress
  708. auctionVariables.OpenAuctionSlots, err = c.AuctionGetOpenAuctionSlots()
  709. if err != nil {
  710. return err
  711. }
  712. auctionVariables.Outbidding, err = c.AuctionGetOutbidding()
  713. if err != nil {
  714. return err
  715. }
  716. auctionVariables.SlotDeadline, err = c.AuctionGetSlotDeadline()
  717. return err
  718. }); err != nil {
  719. return nil, err
  720. }
  721. return auctionVariables, nil
  722. }
  723. // AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
  724. func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error) {
  725. log.Error("TODO")
  726. return nil, nil, errTODO
  727. }