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.

760 lines
26 KiB

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